|
|
|
package com.supervision.controller;
|
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
import cn.hutool.jwt.JWT;
|
|
|
|
import com.supervision.config.UserSingleLoginConfig;
|
|
|
|
import com.supervision.domain.UserInfo;
|
|
|
|
import com.supervision.exception.BusinessException;
|
|
|
|
import com.supervision.model.User;
|
|
|
|
import com.supervision.pojo.vo.LoginReqVO;
|
|
|
|
import com.supervision.service.UserService;
|
|
|
|
import com.supervision.util.TokenUtil;
|
|
|
|
import com.supervision.util.UserUtil;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
@Api(tags = "用户管理")
|
|
|
|
@RestController
|
|
|
|
@RequestMapping("user")
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
public class UserController {
|
|
|
|
|
|
|
|
private final UserService userService;
|
|
|
|
|
|
|
|
@ApiOperation("登录")
|
|
|
|
@PostMapping("login")
|
|
|
|
public String login(@RequestBody LoginReqVO reqVO) {
|
|
|
|
if (!StrUtil.isAllNotBlank(reqVO.getUserAccount(), reqVO.getPassword())) {
|
|
|
|
throw new BusinessException("用户名不能为空");
|
|
|
|
}
|
|
|
|
Optional<User> user = userService.lambdaQuery().eq(User::getAccount, reqVO.getUserAccount()).last("limit 1").oneOpt();
|
|
|
|
if (!user.isPresent()) {
|
|
|
|
throw new BusinessException("未找到用户");
|
|
|
|
}
|
|
|
|
if (!user.get().getPassword().equals(reqVO.getPassword())) {
|
|
|
|
throw new BusinessException("密码错误");
|
|
|
|
}
|
|
|
|
String token = TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
|
|
|
// 将用户的token保存起来
|
|
|
|
UserSingleLoginConfig.loginOrRefreshUser(user.get().getId(), JWT.create().parse(token));
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
|
|
|
@ApiOperation("token心跳")
|
|
|
|
@PostMapping("keepaliveToken")
|
|
|
|
public void keepaliveToken() {
|
|
|
|
String token = UserUtil.getUserToken();
|
|
|
|
UserSingleLoginConfig.loginOrRefreshUser(UserUtil.getUser().getId(), JWT.create().parse(token));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|