mirror of
https://github.com/suyiiyii/SIMS.git
synced 2025-06-03 12:56:10 +08:00
改一下
This commit is contained in:
parent
038b7ebe8d
commit
55d2072fa0
4
pom.xml
4
pom.xml
@ -45,6 +45,10 @@
|
|||||||
<artifactId>mybatis-plus-ext-spring-boot3-starter</artifactId>
|
<artifactId>mybatis-plus-ext-spring-boot3-starter</artifactId>
|
||||||
<version>3.5.7-EXT691</version>
|
<version>3.5.7-EXT691</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.session</groupId>
|
||||||
|
<artifactId>spring-session-core</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.mysql</groupId>
|
<groupId>com.mysql</groupId>
|
||||||
<artifactId>mysql-connector-j</artifactId>
|
<artifactId>mysql-connector-j</artifactId>
|
||||||
|
@ -4,7 +4,7 @@ import com.tangzc.autotable.springboot.EnableAutoTable;
|
|||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
@EnableAutoTable
|
/*@EnableAutoTable*/
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class SimsApplication {
|
public class SimsApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
package top.suyiiyii.sims.VO;
|
|
||||||
|
|
||||||
import lombok.Data;
|
|
||||||
import top.suyiiyii.sims.entity.Role;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @Author tortoise
|
|
||||||
* @Date 2024/8/15 16:04
|
|
||||||
* @PackageName:top.suyiiyii.sims.VO
|
|
||||||
* @ClassName: UserVO
|
|
||||||
* @Description: TODO
|
|
||||||
* @Version 1.0
|
|
||||||
*/
|
|
||||||
@Data
|
|
||||||
public class UserVO {
|
|
||||||
private Integer userId;
|
|
||||||
private String username;
|
|
||||||
private String grade;
|
|
||||||
private String group;
|
|
||||||
private List<String> roles; // 角色名称列表
|
|
||||||
|
|
||||||
}
|
|
@ -1,18 +1,25 @@
|
|||||||
package top.suyiiyii.sims.controller;
|
package top.suyiiyii.sims.controller;
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpSession;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.session.Session;
|
||||||
|
import org.springframework.session.SessionRepository;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import top.suyiiyii.sims.common.AuthAccess;
|
import top.suyiiyii.sims.common.AuthAccess;
|
||||||
import top.suyiiyii.sims.common.Result;
|
import top.suyiiyii.sims.common.Result;
|
||||||
import top.suyiiyii.sims.dto.CommonResponse;
|
import top.suyiiyii.sims.dto.CommonResponse;
|
||||||
|
import top.suyiiyii.sims.dto.UserDto;
|
||||||
import top.suyiiyii.sims.entity.User;
|
import top.suyiiyii.sims.entity.User;
|
||||||
import top.suyiiyii.sims.exception.ServiceException;
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
import top.suyiiyii.sims.service.RoleService;
|
import top.suyiiyii.sims.service.RoleService;
|
||||||
import top.suyiiyii.sims.service.UserService;
|
import top.suyiiyii.sims.service.UserService;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Author tortoise
|
* @Author tortoise
|
||||||
@ -32,6 +39,9 @@ public class UserController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
RoleService roleService;
|
RoleService roleService;
|
||||||
|
|
||||||
|
private SessionRepository sessionRepository;
|
||||||
|
|
||||||
|
|
||||||
@AuthAccess
|
@AuthAccess
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public Result hello() {
|
public Result hello() {
|
||||||
@ -41,17 +51,20 @@ public class UserController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/user/login")
|
@PostMapping("/user/login")
|
||||||
public Result<LoginResponse> login(@RequestBody LoginRequest request) {
|
public Result<LoginResponse> login(@RequestBody LoginRequest request, HttpServletRequest httpServletRequest) {
|
||||||
log.info("login request:{}", request);
|
log.info("login request:{}", request);
|
||||||
|
|
||||||
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
if (StrUtil.isBlank(request.getUsername()) || StrUtil.isBlank(request.getPassword())) {
|
||||||
|
|
||||||
return Result.error("用户名或密码不能为空");
|
return Result.error("用户名或密码不能为空");
|
||||||
}
|
}
|
||||||
|
String token = userService.login(request.getUsername(), request.getPassword());
|
||||||
User user = userService.login(request.getUsername(), request.getPassword());
|
if (token == null) {
|
||||||
|
return Result.error("用户名或密码错误");
|
||||||
return Result.success(new LoginResponse());
|
}
|
||||||
|
LoginResponse response = new LoginResponse();
|
||||||
|
response.setToken(token);
|
||||||
|
return Result.success(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/user/register")
|
@PostMapping("/user/register")
|
||||||
@ -64,8 +77,13 @@ public class UserController {
|
|||||||
if (request.getPassword() == null || request.getPassword().length() < 3) {
|
if (request.getPassword() == null || request.getPassword().length() < 3) {
|
||||||
throw new ServiceException("密码长度不能小于3位");
|
throw new ServiceException("密码长度不能小于3位");
|
||||||
}
|
}
|
||||||
|
User user = new User();
|
||||||
userService.register(new User());
|
user.setUsername(request.getUsername());
|
||||||
|
user.setPassword(request.getPassword());
|
||||||
|
user.setEmail(request.getEmail());
|
||||||
|
user.setGrade(request.getGrade());
|
||||||
|
user.setGroup(request.getGroup());
|
||||||
|
userService.register(user);
|
||||||
|
|
||||||
return Result.success(CommonResponse.factory("注册成功"));
|
return Result.success(CommonResponse.factory("注册成功"));
|
||||||
}
|
}
|
||||||
@ -73,19 +91,23 @@ public class UserController {
|
|||||||
@DeleteMapping("/admin/user/{id}")
|
@DeleteMapping("/admin/user/{id}")
|
||||||
public Result<CommonResponse> adminDelete(@PathVariable Integer id) {
|
public Result<CommonResponse> adminDelete(@PathVariable Integer id) {
|
||||||
log.info("delete request:{}", id);
|
log.info("delete request:{}", id);
|
||||||
// userService.deleteUser(user.getId());
|
userService.deleteUser(id);
|
||||||
return Result.success(CommonResponse.factory("删除成功"));
|
return Result.success(CommonResponse.factory("删除成功"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/admin/user/{id}")
|
@GetMapping("/admin/user")
|
||||||
public Result<User> adminGetById(@PathVariable Integer id) {
|
public Result<List<UserDto>> adminGetById() {
|
||||||
|
List<UserDto> allUsers = userService.findAllUsers();
|
||||||
|
return Result.success(allUsers);
|
||||||
|
}
|
||||||
|
@GetMapping("/user/{id}")
|
||||||
|
public Result<UserDto> GetById(@PathVariable Integer id) {
|
||||||
log.info("selectById request:{}", id);
|
log.info("selectById request:{}", id);
|
||||||
User user = userService.selectById(id);
|
UserDto user = userService.findUser(id);
|
||||||
return Result.success(user);
|
return Result.success(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public static class RegisterRequest {
|
public static class RegisterRequest {
|
||||||
private String username;
|
private String username;
|
||||||
|
@ -14,8 +14,9 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
public class UserDto {
|
public class UserDto {
|
||||||
private Long userId;
|
private Integer userId;
|
||||||
private String username;
|
private String username;
|
||||||
|
private String grade;
|
||||||
|
private String group;
|
||||||
private List<String> roles; // 角色名称列表
|
private List<String> roles; // 角色名称列表
|
||||||
private List<String> permissions; // 权限列表
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,12 @@ package top.suyiiyii.sims.service;
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import top.suyiiyii.sims.VO.UserVO;
|
|
||||||
|
import top.suyiiyii.sims.dto.UserDto;
|
||||||
import top.suyiiyii.sims.entity.*;
|
import top.suyiiyii.sims.entity.*;
|
||||||
import top.suyiiyii.sims.exception.ServiceException;
|
import top.suyiiyii.sims.exception.ServiceException;
|
||||||
import top.suyiiyii.sims.mapper.PermissionsMapper;
|
import top.suyiiyii.sims.mapper.PermissionsMapper;
|
||||||
@ -38,10 +40,6 @@ public class UserService {
|
|||||||
userMapper.addUser(user);
|
userMapper.addUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User selectById(int id) {
|
|
||||||
return userMapper.selectById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateUser(User user) {
|
public void updateUser(User user) {
|
||||||
userMapper.updateUser(user);
|
userMapper.updateUser(user);
|
||||||
}
|
}
|
||||||
@ -54,7 +52,8 @@ public class UserService {
|
|||||||
return userMapper.selectAll();
|
return userMapper.selectAll();
|
||||||
}
|
}
|
||||||
//TODO:返回一个DTO,用户基本信息
|
//TODO:返回一个DTO,用户基本信息
|
||||||
public User login(String username, String password) {
|
public String login(String username, String password) {
|
||||||
|
|
||||||
User dbUser = userMapper.selectByUserName(username);
|
User dbUser = userMapper.selectByUserName(username);
|
||||||
if (dbUser == null) {
|
if (dbUser == null) {
|
||||||
throw new ServiceException("账号不存在");
|
throw new ServiceException("账号不存在");
|
||||||
@ -78,10 +77,13 @@ public class UserService {
|
|||||||
dbUser.setPermissions(permissionsSet);
|
dbUser.setPermissions(permissionsSet);
|
||||||
|
|
||||||
String token = JwtUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
|
String token = JwtUtils.createToken(dbUser.getId().toString(), dbUser.getPassword());
|
||||||
dbUser.setToken(token);
|
|
||||||
return dbUser;
|
|
||||||
|
return token;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public User register(User user) {
|
public User register(User user) {
|
||||||
|
|
||||||
User dbUser = userMapper.selectByUserId(user.getStudentId());
|
User dbUser = userMapper.selectByUserId(user.getStudentId());
|
||||||
@ -114,17 +116,17 @@ public class UserService {
|
|||||||
public void updatePassword(User user) {
|
public void updatePassword(User user) {
|
||||||
userMapper.updatePassword(user);
|
userMapper.updatePassword(user);
|
||||||
}
|
}
|
||||||
public List<UserVO> findAllUsers(){
|
public List<UserDto> findAllUsers(){
|
||||||
List<User> users = userMapper.selectAll();
|
List<User> users = userMapper.selectAll();
|
||||||
List<UserVO> userVOS = new ArrayList<>();
|
List<UserDto> UserDtos = new ArrayList<>();
|
||||||
|
|
||||||
for (User user : users) {
|
for (User user : users) {
|
||||||
UserVO userVO = new UserVO();
|
UserDto UserDto = new UserDto();
|
||||||
userVO.setUserId(user.getId());
|
UserDto.setUserId(user.getId());
|
||||||
userVO.setUsername(user.getUsername());
|
UserDto.setUsername(user.getUsername());
|
||||||
userVO.setGrade(user.getGrade());
|
UserDto.setGrade(user.getGrade());
|
||||||
userVO.setGroup(user.getGroup());
|
UserDto.setGroup(user.getGroup());
|
||||||
userVO.setRoles(new ArrayList<>());
|
UserDto.setRoles(new ArrayList<>());
|
||||||
Integer id = user.getId();
|
Integer id = user.getId();
|
||||||
List<UserRole> userRoles = roleMapper.selectRolesById(id);
|
List<UserRole> userRoles = roleMapper.selectRolesById(id);
|
||||||
for (UserRole userRole : userRoles) {
|
for (UserRole userRole : userRoles) {
|
||||||
@ -132,11 +134,31 @@ public class UserService {
|
|||||||
// 获取一个角色的名称列表
|
// 获取一个角色的名称列表
|
||||||
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
||||||
// 累加角色名称到用户的角色列表中
|
// 累加角色名称到用户的角色列表中
|
||||||
userVO.getRoles().addAll(roleNameList);
|
UserDto.getRoles().addAll(roleNameList);
|
||||||
}
|
}
|
||||||
userVOS.add(userVO);
|
UserDtos.add(UserDto);
|
||||||
}
|
}
|
||||||
return userVOS;
|
return UserDtos;
|
||||||
}
|
}
|
||||||
|
public UserDto findUser(Integer id) {
|
||||||
|
|
||||||
|
UserDto UserDto = new UserDto();
|
||||||
|
User user = userMapper.selectById(id);
|
||||||
|
UserDto.setUserId(user.getId());
|
||||||
|
UserDto.setUsername(user.getUsername());
|
||||||
|
UserDto.setGrade(user.getGrade());
|
||||||
|
UserDto.setGroup(user.getGroup());
|
||||||
|
UserDto.setRoles(new ArrayList<>());
|
||||||
|
List<UserRole> userRoles = roleMapper.selectRolesById(id);
|
||||||
|
for (UserRole userRole : userRoles) {
|
||||||
|
Integer roleId = userRole.getRoleId();
|
||||||
|
// 获取一个角色的名称列表
|
||||||
|
List<String> roleNameList = roleMapper.selectRoleNamesByRoleId(roleId);
|
||||||
|
// 累加角色名称到用户的角色列表中
|
||||||
|
UserDto.getRoles().addAll(roleNameList);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return UserDto;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user