|
|
@ -1,11 +1,13 @@
|
|
|
|
package com.supervision.controller;
|
|
|
|
package com.supervision.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
import com.supervision.constant.UserTokenConstant;
|
|
|
|
import com.supervision.constant.UserTokenConstant;
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
import com.supervision.model.User;
|
|
|
|
import com.supervision.model.User;
|
|
|
|
import com.supervision.pojo.vo.LoginReqVO;
|
|
|
|
import com.supervision.pojo.vo.LoginReqVO;
|
|
|
|
|
|
|
|
import com.supervision.pojo.vo.LoginResVO;
|
|
|
|
import com.supervision.service.UserService;
|
|
|
|
import com.supervision.service.UserService;
|
|
|
|
import com.supervision.util.TokenUtil;
|
|
|
|
import com.supervision.util.TokenUtil;
|
|
|
|
import com.supervision.util.UserUtil;
|
|
|
|
import com.supervision.util.UserUtil;
|
|
|
@ -31,21 +33,21 @@ public class UserController {
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation("登录")
|
|
|
|
@ApiOperation("登录")
|
|
|
|
@PostMapping("login")
|
|
|
|
@PostMapping("login")
|
|
|
|
public String login(@RequestBody LoginReqVO reqVO) {
|
|
|
|
public LoginResVO login(@RequestBody LoginReqVO reqVO) {
|
|
|
|
if (!StrUtil.isAllNotBlank(reqVO.getUserAccount(), reqVO.getPassword())) {
|
|
|
|
if (!StrUtil.isAllNotBlank(reqVO.getUserAccount(), reqVO.getPassword())) {
|
|
|
|
throw new BusinessException("用户名不能为空");
|
|
|
|
throw new BusinessException("用户名不能为空");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Optional<User> user = userService.lambdaQuery().eq(User::getAccount, reqVO.getUserAccount()).last("limit 1").oneOpt();
|
|
|
|
Optional<User> user = userService.lambdaQuery().eq(User::getAccount, reqVO.getUserAccount()).last("limit 1").oneOpt();
|
|
|
|
if (!user.isPresent()) {
|
|
|
|
if (!user.isPresent() || !user.get().getPassword().equals(reqVO.getPassword())) {
|
|
|
|
throw new BusinessException("未找到用户");
|
|
|
|
throw new BusinessException("用户名或密码有误!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!user.get().getPassword().equals(reqVO.getPassword())) {
|
|
|
|
|
|
|
|
throw new BusinessException("密码错误");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String token = TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
|
|
|
String token = TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
|
|
|
// 将用户的token保存起来,超时时间为5分钟
|
|
|
|
// 将用户的token保存起来,超时时间为5分钟
|
|
|
|
redisTemplate.opsForValue().set(UserTokenConstant.TOKEN_CACHE + user.get().getId(), String.valueOf(System.currentTimeMillis()), 1000 * 5L, TimeUnit.MILLISECONDS);
|
|
|
|
redisTemplate.opsForValue().set(UserTokenConstant.TOKEN_CACHE + user.get().getId(), String.valueOf(System.currentTimeMillis()), 1000 * 5L, TimeUnit.MILLISECONDS);
|
|
|
|
return token;
|
|
|
|
|
|
|
|
|
|
|
|
LoginResVO loginResVO = BeanUtil.toBean(user.get(), LoginResVO.class);
|
|
|
|
|
|
|
|
loginResVO.setToken(token);
|
|
|
|
|
|
|
|
return loginResVO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ApiOperation("token心跳")
|
|
|
|
@ApiOperation("token心跳")
|
|
|
|