- 加载业务层Service的接口
- 编写API接口,使用@PostMapping,@GetMapping,@PutMapping,@DeleteMapping ```java package com.tj.reggie.controller;
import com.alibaba.druid.sql.visitor.functions.Hex; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.tj.reggie.controller.utils.R; import com.tj.reggie.entity.Users; import com.tj.reggie.service.UsersService; import com.tj.reggie.utils.Sha256Util; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import java.security.MessageDigest;
@Slf4j //日志 @RestController //RESTful风格写法 @RequestMapping(“/users”) //定义API接口 public class UsersController {
@Autowired //自动加载业务层接口
private UsersService usersService;
/**
* 用户登录
*
* @param request
* @param users
* @return
*/
@PostMapping("/login")
public R<Users> login(HttpServletRequest request, @RequestBody Users users) {
//1.将页面提交的密码password进行md5加密处理
String password = users.getPassword();
password = Sha256Util.sha256(password);//sha256加密
// password = DigestUtils.md5DigestAsHex(password.getBytes());//md5加密
//2.根据页面提交的用户名userName查询数据库
LambdaQueryWrapper<Users> lqw = new LambdaQueryWrapper<>(); //条件对象
lqw.eq(Users::getUserId, users.getUserId()); //判断数据库里是否有相同的用户名
Users res = usersService.getOne(lqw); //查询一条记录
//3.如果没有查询到则返回登录失败结果
if (res == null) {
return R.error("用户名不存在");
}
//4.密码比对,如果不一致则返回登录失败结构
if (!res.getPassword().equals(password)) {
return R.error("密码不正确");
}
//5.查看员工状态,如果为已禁用,则返回用户禁用信息
if (res.getIsuse() == 1) {
return R.error("账号已经被禁用");
}
//6.登录成功,将员工id存入Session并返回登录成功结果
request.getSession().setAttribute("users", res.getId());
return R.success(res);
}
}
```