You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.8 KiB
Java
82 lines
2.8 KiB
Java
package com.supervision.controller;
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
import cn.hutool.core.util.ObjectUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.json.JSONUtil;
|
|
import com.supervision.exception.BusinessException;
|
|
import com.supervision.model.User;
|
|
import com.supervision.pojo.vo.LoginReqVO;
|
|
import com.supervision.pojo.vo.LoginResVO;
|
|
import com.supervision.pojo.vo.UserInfoReqVo;
|
|
import com.supervision.pojo.vo.UserInfoResVo;
|
|
import com.supervision.service.UserManageService;
|
|
import com.supervision.service.UserService;
|
|
import com.supervision.util.TokenUtil;
|
|
import com.supervision.util.UserUtil;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.time.LocalDateTime;
|
|
import java.util.Optional;
|
|
|
|
@Tag(name = "用户管理")
|
|
@RestController
|
|
@RequestMapping("user")
|
|
@RequiredArgsConstructor
|
|
public class UserController {
|
|
|
|
private final UserService userService;
|
|
|
|
private final RedisTemplate<String, String> redisTemplate;
|
|
|
|
private final UserManageService userManageService;
|
|
|
|
|
|
@Operation(summary = "登录")
|
|
@PostMapping("login")
|
|
public LoginResVO 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() || !user.get().getPassword().equals(reqVO.getPassword())) {
|
|
throw new BusinessException("用户名或密码有误!");
|
|
}
|
|
if (ObjectUtil.isNotEmpty(user.get().getStatus()) && user.get().getStatus() != 0){
|
|
throw new BusinessException("用户已被禁用!");
|
|
}
|
|
// 更新用户最近的登录时间
|
|
userService.lambdaUpdate().set(User::getRecentLoginTime,LocalDateTime.now()).eq(User::getId, user.get().getId()).update();
|
|
String token = TokenUtil.creatToken(JSONUtil.toJsonStr(user.get()));
|
|
|
|
LoginResVO loginResVO = BeanUtil.toBean(user.get(), LoginResVO.class);
|
|
loginResVO.setToken(token);
|
|
return loginResVO;
|
|
}
|
|
|
|
|
|
@Operation(summary = "修改用户信息")
|
|
@PutMapping("updateUserInfo")
|
|
public boolean updateUserInfo(@RequestBody UserInfoReqVo userInfo) {
|
|
|
|
return userManageService.updateUserInfo(userInfo);
|
|
}
|
|
|
|
@Operation(summary = "查看账号信息")
|
|
@GetMapping("getUserAccountInfo")
|
|
public UserInfoResVo getUserAccountInfo() {
|
|
User user = UserUtil.getUser();
|
|
return userManageService.getUserAccountInfo(user.getId());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|