mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-03 12:56:10 +08:00
feat(user): 完成User的接口
This commit is contained in:
parent
2cf298759c
commit
1c59d33932
@ -1,12 +1,13 @@
|
||||
package top.suyiiyii.sims.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.validation.constraints.Null;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.suyiiyii.sims.common.AuthAccess;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.dto.CommonResponse;
|
||||
import top.suyiiyii.sims.entity.User;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.service.RoleService;
|
||||
@ -21,8 +22,10 @@ import top.suyiiyii.sims.service.UserService;
|
||||
* @Description: TODO
|
||||
* @Version 1.0
|
||||
*/
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
//@RequestMapping("/user")
|
||||
public class UserController {
|
||||
@Autowired
|
||||
UserService userService;
|
||||
@ -37,8 +40,9 @@ public class UserController {
|
||||
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
@PostMapping("/user/login")
|
||||
public Result<LoginResponse> login(@RequestBody LoginRequest request) {
|
||||
log.info("login request:{}", request);
|
||||
|
||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||
|
||||
@ -50,8 +54,9 @@ public class UserController {
|
||||
return Result.success(new LoginResponse());
|
||||
}
|
||||
|
||||
@PostMapping("/register")
|
||||
public Result<Null> register(@RequestBody RegisterRequest request) {
|
||||
@PostMapping("/user/register")
|
||||
public Result<CommonResponse> register(@RequestBody RegisterRequest request) {
|
||||
log.info("register request:{}", request);
|
||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||
|
||||
return Result.error("用户名或密码不能为空");
|
||||
@ -62,80 +67,24 @@ public class UserController {
|
||||
|
||||
userService.register(new User());
|
||||
|
||||
return Result.success();
|
||||
return Result.success(CommonResponse.factory("注册成功"));
|
||||
}
|
||||
|
||||
@PostMapping("/add")
|
||||
public Result add(@RequestBody User user) {
|
||||
userService.addUser(user);
|
||||
return Result.success();
|
||||
@DeleteMapping("/admin/user/{id}")
|
||||
public Result<CommonResponse> adminDelete(@PathVariable Integer id) {
|
||||
log.info("delete request:{}", id);
|
||||
// userService.deleteUser(user.getId());
|
||||
return Result.success(CommonResponse.factory("删除成功"));
|
||||
}
|
||||
|
||||
@PostMapping("/delete")
|
||||
public Result delete(@RequestBody User user) {
|
||||
userService.deleteUser(user.getId());
|
||||
return Result.success("删除成功");
|
||||
@GetMapping("/admin/user/{id}")
|
||||
public Result<User> adminGetById(@PathVariable Integer id) {
|
||||
log.info("selectById request:{}", id);
|
||||
User user = userService.selectById(id);
|
||||
return Result.success(user);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:34
|
||||
* @Description: 更新用户信息, 自己改的(不包括密码)
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
public Result update(@RequestBody User user) {
|
||||
userService.updateUser(user);
|
||||
return Result.success("更新成功");
|
||||
}
|
||||
|
||||
@PostMapping("/select")
|
||||
public Result select(@RequestBody User user) {
|
||||
return Result.success(userService.selectById(user.getId()));
|
||||
}
|
||||
|
||||
@PostMapping("/selectByUsername")
|
||||
public Result selectByUsername(@RequestBody User user) {
|
||||
return Result.success(userService.selectByUsername(user.getUsername()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:48
|
||||
* @Description: TODO 用户更新密码
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/updatePassword")
|
||||
public Result updatePassword(@RequestBody User user) {
|
||||
if (StrUtil.isBlank(user.getPassword())) {
|
||||
return Result.error("密码不能为空");
|
||||
}
|
||||
User user1 = userService.selectByUsername(user.getUsername());
|
||||
//验证原密码是否正确
|
||||
if (!user.getPassword().equals(user1.getPassword())) {
|
||||
return Result.error("原密码错误");
|
||||
}
|
||||
userService.updatePassword(user);
|
||||
return Result.success("更新成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user
|
||||
* @author: tortoise
|
||||
* @date: 2024/8/14 13:48
|
||||
* @Description: TODO 管理员修改密码
|
||||
* @return: top.suyiiyii.sims.common.Result
|
||||
*/
|
||||
@PostMapping("/updatePasswordByAdmin")
|
||||
public Result updatePasswordByAdmin(@RequestBody User user) {
|
||||
if (StrUtil.isBlank(user.getPassword())) {
|
||||
return Result.error("密码不能为空");
|
||||
}
|
||||
userService.updatePassword(user);
|
||||
return Result.success("更新成功");
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class RegisterRequest {
|
||||
|
16
src/main/java/top/suyiiyii/sims/dto/CommonResponse.java
Normal file
16
src/main/java/top/suyiiyii/sims/dto/CommonResponse.java
Normal file
@ -0,0 +1,16 @@
|
||||
package top.suyiiyii.sims.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
public class CommonResponse {
|
||||
String message;
|
||||
|
||||
public static CommonResponse factory(String message) {
|
||||
return new CommonResponse(message);
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
package top.suyiiyii.sims.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.tangzc.mpe.autotable.annotation.Table;
|
||||
@ -25,7 +24,7 @@ import java.util.Set;
|
||||
public class User {
|
||||
@TableId("id")
|
||||
private Integer id;
|
||||
private Integer userId;
|
||||
private Integer studentId;
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
|
@ -6,8 +6,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import top.suyiiyii.sims.VO.UserVO;
|
||||
import top.suyiiyii.sims.common.Result;
|
||||
import top.suyiiyii.sims.dto.UserDTO;
|
||||
import top.suyiiyii.sims.entity.*;
|
||||
import top.suyiiyii.sims.exception.ServiceException;
|
||||
import top.suyiiyii.sims.mapper.PermissionsMapper;
|
||||
@ -16,10 +14,8 @@ import top.suyiiyii.sims.mapper.UserMapper;
|
||||
import top.suyiiyii.sims.utils.JwtUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.function.ToDoubleBiFunction;
|
||||
|
||||
/**
|
||||
* @Author tortoise
|
||||
@ -88,7 +84,7 @@ public class UserService {
|
||||
|
||||
public User register(User user) {
|
||||
|
||||
User dbUser = userMapper.selectByUserId(user.getUserId());
|
||||
User dbUser = userMapper.selectByUserId(user.getStudentId());
|
||||
|
||||
if (user.getUsername() == null || user.getUsername().equals("")) {
|
||||
throw new ServiceException("用户名不能为空");
|
||||
@ -96,7 +92,7 @@ public class UserService {
|
||||
if (dbUser != null) {
|
||||
throw new ServiceException("账号已经存在");
|
||||
}
|
||||
if (user.getUserId() == null || user.getUserId().equals("")) {
|
||||
if (user.getStudentId() == null || user.getStudentId().equals("")) {
|
||||
throw new ServiceException("用户id不能为空");
|
||||
}
|
||||
if( user.getPassword() == null || user.getPassword().equals("")) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user