1. 添加用户管理相关接口开发
parent
afc05b833e
commit
81d91229a3
@ -0,0 +1,90 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.supervision.utils.TokenUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
public class JwtInterceptor implements HandlerInterceptor {
|
||||
|
||||
public JwtInterceptor() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
//请求消息头获取用户ID
|
||||
String token = request.getHeader("token");
|
||||
if (StrUtil.isBlank(token)) {
|
||||
// 如果是swagger来的接口,说明这里是测试的,会伪造一个用户
|
||||
String referer = request.getHeader("Referer");
|
||||
if (StrUtil.isNotBlank(referer) && StrUtil.contains(referer, "swagger-ui")) {
|
||||
cacheAuth(JWTUtil.parseToken(devActiveUser()));
|
||||
return true;
|
||||
} else {
|
||||
throw new BusinessException("当前用户未登录", HttpStatus.UNAUTHORIZED.value());
|
||||
}
|
||||
}
|
||||
JWT jwt = JWTUtil.parseToken(token);
|
||||
// 校验token是否过期,如果过期了,需要提示过期重新登录
|
||||
checkTokenExpire(jwt);
|
||||
cacheAuth(jwt);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void afterCompletion(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler,
|
||||
@Nullable Exception ex) throws Exception {
|
||||
// 请求结束,将用户信息从thread中移除
|
||||
clearAuth();
|
||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
||||
}
|
||||
|
||||
|
||||
private void checkTokenExpire(JWT jwt) {
|
||||
Object expireTime = jwt.getPayload("expireTime");
|
||||
long l = Long.parseLong(String.valueOf(expireTime));
|
||||
// 校验是否比当前时间大
|
||||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (currentTimeMillis > l) {
|
||||
throw new BusinessException("用户登录已过期,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void cacheAuth(JWT jwt) {
|
||||
try {
|
||||
JSONObject claimsJson = jwt.getPayload().getClaimsJson();
|
||||
ThreadCache.USER.set(claimsJson.toString());
|
||||
} catch (Exception e) {
|
||||
log.error("用户信息异常", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String devActiveUser() {
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("id", "1");
|
||||
map.put("account", "test");
|
||||
map.put("name", "测试");
|
||||
return TokenUtil.creatToken(JSONUtil.toJsonStr(map));
|
||||
}
|
||||
|
||||
private void clearAuth() {
|
||||
ThreadCache.USER.remove();
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* 文 件 名: ThreadCache
|
||||
* 版 权:
|
||||
* 描 述: <描述>
|
||||
* 修 改 人: RedName
|
||||
* 修改时间: 2023/9/4
|
||||
* 跟踪单号: <跟踪单号>
|
||||
* 修改单号: <修改单号>
|
||||
* 修改内容: <修改内容>
|
||||
*/
|
||||
package com.supervision.config;
|
||||
|
||||
|
||||
/**
|
||||
* <功能详细描述>
|
||||
*
|
||||
* @author ljt
|
||||
* @version [版本号, 2023/9/4]
|
||||
* @see [相关类/方法]
|
||||
* @since [产品/模块版本]
|
||||
*/
|
||||
public class ThreadCache {
|
||||
|
||||
/**
|
||||
* 用户ID
|
||||
*/
|
||||
public static final ThreadLocal<String> USER = new ThreadLocal<>();
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,56 @@
|
||||
package com.supervision.config;
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
|
||||
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 添加权限拦截器
|
||||
registry.addInterceptor(new JwtInterceptor())
|
||||
.addPathPatterns("/**")
|
||||
.excludePathPatterns(ignorePathPatterns());
|
||||
}
|
||||
|
||||
public List<String> ignorePathPatterns() {
|
||||
List<String> paths = new ArrayList<>();
|
||||
paths.add("/swagger-resources/**");
|
||||
paths.add("/webjars/**");
|
||||
paths.add("/v3/**");
|
||||
paths.add("/swagger-ui.html/**");
|
||||
paths.add("/swagger-ui/**");
|
||||
paths.add("/webjars/");
|
||||
paths.add("/doc.html/**");
|
||||
paths.add("/error");
|
||||
paths.add("/favicon.ico");
|
||||
paths.add("/user/login");
|
||||
paths.add("/user/register");
|
||||
paths.add("/user/changePassWord");
|
||||
paths.add("/user/checkAccount");
|
||||
paths.add("/webSocket/**");
|
||||
paths.add("/ask/downloadTalkVideo");
|
||||
paths.add("/fileManage/downloadFile");
|
||||
paths.add("/aqLibrary/downloadQuestionLibraryTemplate");
|
||||
paths.add("/medicalRecManage/downloadMedicalAnswerTemplate");
|
||||
paths.add("/qaKnowledge/**");
|
||||
// 开发环境,放开不校验token.每次修改这里需要重启(热部署不行)
|
||||
// paths.add("/**");
|
||||
return paths;
|
||||
}
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package com.supervision.police.controller;
|
||||
|
||||
import com.supervision.police.dto.MenuDTO;
|
||||
import com.supervision.police.service.SystemMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Tag(name = "菜单管理")
|
||||
@RestController
|
||||
@Slf4j
|
||||
@RequestMapping("/menu")
|
||||
@RequiredArgsConstructor
|
||||
public class MenuController {
|
||||
|
||||
private final SystemMenuService menuService;
|
||||
@Operation(summary = "查看用户信息列表")
|
||||
@GetMapping("/listMenu")
|
||||
public List<MenuDTO> listMenu() {
|
||||
return menuService.listMenu();
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.supervision.police.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.supervision.police.dto.RoleDTO;
|
||||
import com.supervision.police.service.SystemRoleService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name = "用户角色管理")
|
||||
@RestController
|
||||
@RequestMapping("/roleManage")
|
||||
@RequiredArgsConstructor
|
||||
public class RoleController {
|
||||
|
||||
private final SystemRoleService roleService;
|
||||
@Operation(summary = "新增角色")
|
||||
@PostMapping("/save")
|
||||
public String saveRole(@RequestBody RoleDTO roleDTO) {
|
||||
|
||||
return roleService.saveRole(roleDTO);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改角色")
|
||||
@PostMapping("/update")
|
||||
public void updateRole(@RequestBody RoleDTO roleDTO) {
|
||||
roleService.updateRole(roleDTO);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除角色")
|
||||
@DeleteMapping("/delete")
|
||||
public Boolean deleteRole(@Parameter(name = "id") @RequestParam(name="id") String id) {
|
||||
return roleService.deleteRole(id);
|
||||
}
|
||||
@Operation(summary = "查看角色列表")
|
||||
@GetMapping("/list")
|
||||
public IPage<RoleDTO> list(@Parameter(name = "roleName",description = "角色名称") @RequestParam(required = false) String roleName,
|
||||
@Parameter(name = "pageNum",description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@Parameter(name = "pageSize",description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
|
||||
return roleService.list(roleName, pageNum, pageSize);
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.supervision.police.controller;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.supervision.police.dto.user.*;
|
||||
import com.supervision.police.service.SystemUserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@Tag(name = "用户管理")
|
||||
@RestController
|
||||
@RequestMapping("/user")
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final SystemUserService userService;
|
||||
|
||||
|
||||
@Operation(summary = "登录")
|
||||
@PostMapping("/login")
|
||||
public LoginResVO login(@RequestBody LoginReqVO reqVO) {
|
||||
|
||||
return userService.login(reqVO);
|
||||
}
|
||||
|
||||
|
||||
@Operation(summary = "新增用户信息")
|
||||
@PostMapping("/saveUserInfo")
|
||||
public String saveUserInfo(@RequestBody UserInfoReqVo userInfoReqVo) {
|
||||
|
||||
return userService.saveUserInfo(userInfoReqVo);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改用户信息")
|
||||
@PostMapping("/update")
|
||||
public void updateUser(@RequestBody UserInfoReqVo userInfoReqVo) {
|
||||
userService.updateUserInfo(userInfoReqVo);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改用户状态")
|
||||
@PostMapping("/updateUserStatus")
|
||||
public void updateUserStatus(@RequestBody UserStatusReqVo userStatusReqVo) {
|
||||
userService.updateUserStatus(userStatusReqVo);
|
||||
}
|
||||
|
||||
@Operation(summary = "删除用户信息")
|
||||
@DeleteMapping("/delete")
|
||||
public Boolean deleteUser(@Parameter(name = "id") @RequestParam(name="id") String id) {
|
||||
return userService.deleteUser(id);
|
||||
}
|
||||
@Operation(summary = "查看用户信息列表")
|
||||
@GetMapping("/list")
|
||||
public IPage<UserInfoDTO> list(@Parameter(name = "userName",description = "用户名") @RequestParam(required = false) String userName,
|
||||
@Parameter(name = "roleId",description = "角色id") @RequestParam(required = false) String roleId,
|
||||
@Parameter(name = "roleName",description = "角色名") @RequestParam(required = false) String roleName,
|
||||
@Parameter(name = "pageNum",description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@Parameter(name = "pageSize",description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
|
||||
return userService.list(userName, roleId, roleName, pageNum, pageSize);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package com.supervision.police.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 角色菜单关联表
|
||||
* @TableName system_role_menu_relation
|
||||
*/
|
||||
@TableName(value ="system_role_menu_relation")
|
||||
@Data
|
||||
public class SystemRoleMenuRelation implements Serializable {
|
||||
/**
|
||||
* 主键id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 角色id
|
||||
*/
|
||||
private String roleId;
|
||||
|
||||
/**
|
||||
* 菜单id
|
||||
*/
|
||||
private String menuId;
|
||||
|
||||
/**
|
||||
* 创建人ID
|
||||
*/
|
||||
private String createUserId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新人
|
||||
*/
|
||||
private String updateUserId;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
@TableField(fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public SystemRoleMenuRelation() {
|
||||
}
|
||||
|
||||
public SystemRoleMenuRelation(String roleId, String menuId) {
|
||||
this.roleId = roleId;
|
||||
this.menuId = menuId;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package com.supervision.police.dto;
|
||||
|
||||
import com.supervision.police.domain.SystemMenu;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Data
|
||||
public class MenuDTO {
|
||||
|
||||
@Schema(description = "菜单id")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "标签(菜单名或按钮名)")
|
||||
private String label;
|
||||
|
||||
@Schema(description = "标签码 前端权限使用")
|
||||
private String labelCode;
|
||||
|
||||
@Schema(description = "标签类型 0: 菜单 1:标签 默认菜单")
|
||||
private Integer labelType;
|
||||
|
||||
@Schema(description = "父级id")
|
||||
private String parentId;
|
||||
|
||||
@Schema(description = "子菜单")
|
||||
private List<MenuDTO> children;
|
||||
|
||||
public MenuDTO() {
|
||||
}
|
||||
|
||||
public MenuDTO(SystemMenu systemMenu) {
|
||||
if (Objects.isNull(systemMenu)){
|
||||
return;
|
||||
}
|
||||
this.id = systemMenu.getId();
|
||||
this.label = systemMenu.getLabel();
|
||||
this.labelCode = systemMenu.getLabelCode();
|
||||
this.labelType = systemMenu.getLabelType();
|
||||
this.parentId = systemMenu.getParentId();
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
package com.supervision.police.dto;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.supervision.police.domain.SystemRole;
|
||||
import com.supervision.police.domain.SystemRoleMenuRelation;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@Data
|
||||
public class RoleDTO {
|
||||
|
||||
@Schema(description = "角色id")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "角色名称")
|
||||
private String roleName;
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "菜单id列表")
|
||||
private List<String> menuIdList;
|
||||
|
||||
@Schema(description = "关联用户数量")
|
||||
private int userCount;
|
||||
|
||||
@Schema(description = "更新时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
public RoleDTO() {
|
||||
}
|
||||
|
||||
public RoleDTO(SystemRole role, Long userCount, List<SystemRoleMenuRelation> roleMenuRelationList) {
|
||||
if (Objects.isNull(role)){
|
||||
return;
|
||||
}
|
||||
this.id = role.getId();
|
||||
this.roleName = role.getRoleName();
|
||||
this.remark = role.getRemark();
|
||||
this.updateTime = role.getUpdateTime();
|
||||
this.userCount = userCount.intValue();
|
||||
if (CollUtil.isNotEmpty(roleMenuRelationList)){
|
||||
this.menuIdList = roleMenuRelationList.stream().map(SystemRoleMenuRelation::getMenuId).toList();
|
||||
}
|
||||
}
|
||||
public SystemRole toSystemRole() {
|
||||
SystemRole systemRole = new SystemRole();
|
||||
systemRole.setId(this.id);
|
||||
systemRole.setRoleName(this.roleName);
|
||||
systemRole.setRemark(this.remark);
|
||||
return systemRole;
|
||||
}
|
||||
|
||||
public List<SystemRoleMenuRelation> menuIdListToRoleMenu() {
|
||||
if (CollUtil.isEmpty(this.menuIdList)){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return this.menuIdList.stream().map(menuId -> new SystemRoleMenuRelation(this.id,menuId)).toList();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginReqVO {
|
||||
|
||||
private String userAccount;
|
||||
|
||||
private String password;
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.supervision.police.domain.SystemUser;
|
||||
import com.supervision.utils.TokenUtil;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
@Data
|
||||
public class LoginResVO {
|
||||
|
||||
@Schema(description = "主键")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "用户账户")
|
||||
private String account;
|
||||
|
||||
@Schema(description = "用户名称")
|
||||
private String userName;
|
||||
|
||||
@Schema(description = "用户角色编码")
|
||||
private String roleCode;
|
||||
|
||||
@Schema(description = "菜单权限标识")
|
||||
private List<String> permission = new ArrayList<>();
|
||||
|
||||
@Schema(description = "token")
|
||||
private String token;
|
||||
|
||||
public LoginResVO() {
|
||||
}
|
||||
|
||||
public LoginResVO(SystemUser systemUser) {
|
||||
if (Objects.isNull(systemUser)){
|
||||
return;
|
||||
}
|
||||
this.id = systemUser.getId();
|
||||
this.account = systemUser.getAccount();
|
||||
this.userName = systemUser.getUserName();
|
||||
}
|
||||
|
||||
public void setToken(UserInfoDTO userInfoDTO) {
|
||||
if (Objects.isNull(userInfoDTO)){
|
||||
return;
|
||||
}
|
||||
this.token = TokenUtil.creatToken(JSONUtil.toJsonStr(userInfoDTO));
|
||||
}
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
public void setPermission(List<String> permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public void roleMenuDTOSetPermission(List<RoleMenuDTO> roleMenuDTOS) {
|
||||
if (CollUtil.isEmpty(roleMenuDTOS)){
|
||||
return;
|
||||
}
|
||||
this.permission = roleMenuDTOS.stream().map(RoleMenuDTO::getLabelCode).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class RoleMenuDTO {
|
||||
|
||||
@Schema(description = "角色id")
|
||||
private String roleId;
|
||||
|
||||
@Schema(description = "用户id")
|
||||
private String userId;
|
||||
|
||||
@Schema(description = "菜单id")
|
||||
private String menuId;
|
||||
|
||||
@Schema(description = "菜单名称")
|
||||
private String label;
|
||||
|
||||
@Schema(description = "菜单编码")
|
||||
private String labelCode;
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.supervision.police.domain.SystemUser;
|
||||
import lombok.Data;
|
||||
import org.springframework.scheduling.support.SimpleTriggerContext;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
|
||||
@Data
|
||||
public class UserInfoDTO {
|
||||
|
||||
|
||||
/**
|
||||
* 主键
|
||||
*/
|
||||
@TableId
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 头像id
|
||||
*/
|
||||
private String headPicId;
|
||||
|
||||
/**
|
||||
* 账户名
|
||||
*/
|
||||
private String account;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 手机号码
|
||||
*/
|
||||
private String phoneNum;
|
||||
|
||||
/**
|
||||
* 账号状态 0正常 1停用
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
|
||||
private List<UserRoleDTO> userRoleList;
|
||||
/**
|
||||
* 最近登录时间
|
||||
*/
|
||||
private LocalDateTime recentLoginTime;
|
||||
|
||||
public UserInfoDTO() {
|
||||
}
|
||||
|
||||
public UserInfoDTO(SystemUser systemUser) {
|
||||
systemUserSetter(systemUser);
|
||||
}
|
||||
|
||||
public UserInfoDTO(SystemUser systemUser, Map<String, List<UserRoleDTO>> userRoleMap) {
|
||||
systemUserSetter(systemUser);
|
||||
if (StrUtil.isNotEmpty(this.id)){
|
||||
this.setUserRoleList(userRoleMap.getOrDefault(this.id, new ArrayList<>(1)));
|
||||
}
|
||||
}
|
||||
|
||||
private void systemUserSetter(SystemUser systemUser){
|
||||
if (Objects.isNull(systemUser)){
|
||||
return;
|
||||
}
|
||||
this.id = systemUser.getId();
|
||||
this.headPicId = systemUser.getHeadPicId();
|
||||
this.account = systemUser.getAccount();
|
||||
this.userName = systemUser.getUserName();
|
||||
this.phoneNum = systemUser.getPhoneNum();
|
||||
this.status = systemUser.getStatus();
|
||||
this.recentLoginTime = systemUser.getRecentLoginTime();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import com.supervision.police.domain.SystemUser;
|
||||
import com.supervision.utils.UserUtil;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class UserInfoReqVo {
|
||||
|
||||
@Schema(description = "用户id")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "头像ID")
|
||||
private String headPicId;
|
||||
|
||||
@Schema(description = "账号")
|
||||
private String account;
|
||||
|
||||
@Schema(description = "用户名")
|
||||
private String userName;
|
||||
|
||||
@Schema(description = "密码")
|
||||
private String password;
|
||||
|
||||
@Schema(description = "手机号")
|
||||
private String phoneNum;
|
||||
|
||||
@Schema(description = "账号状态 0正常 1停用")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "角色ID列表")
|
||||
private List<String> roleIdList;
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
public SystemUser toSystemUser() {
|
||||
SystemUser systemUser = new SystemUser();
|
||||
systemUser.setId(id);
|
||||
systemUser.setHeadPicId(headPicId);
|
||||
systemUser.setAccount(account);
|
||||
systemUser.setUserName(userName);
|
||||
systemUser.setUserPd(password);
|
||||
systemUser.setRemark(remark);
|
||||
systemUser.setStatus(status);
|
||||
systemUser.setUserPd(UserUtil.signPassword(this.getPassword()));
|
||||
return systemUser;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserRoleDTO {
|
||||
|
||||
private String userId;
|
||||
|
||||
private String roleId;
|
||||
|
||||
private String roleName;
|
||||
|
||||
private Integer roleType;
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.supervision.police.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UserStatusReqVo {
|
||||
|
||||
@Schema(description = "用户ID")
|
||||
private String id;
|
||||
|
||||
@Schema(description = "用户状态 0正常 1停用")
|
||||
private String status;
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.supervision.police.mapper;
|
||||
|
||||
import com.supervision.police.domain.SystemRoleMenuRelation;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【system_role_menu_relation(角色菜单关联表)】的数据库操作Mapper
|
||||
* @createDate 2024-07-31 15:02:36
|
||||
* @Entity com.supervision.police.domain.SystemRoleMenuRelation
|
||||
*/
|
||||
public interface SystemRoleMenuRelationMapper extends BaseMapper<SystemRoleMenuRelation> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,22 @@
|
||||
package com.supervision.police.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.supervision.police.domain.SystemRoleMenuRelation;
|
||||
import com.supervision.police.domain.SystemUserRoleRelation;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【system_role_menu_relation(角色菜单关联表)】的数据库操作Service
|
||||
* @createDate 2024-07-31 15:02:36
|
||||
*/
|
||||
public interface SystemRoleMenuRelationService extends IService<SystemRoleMenuRelation> {
|
||||
|
||||
|
||||
void updateRoleMenu(String roleId, List<String> menuIds);
|
||||
|
||||
void deleteRoleMenu(String roleId);
|
||||
|
||||
List<SystemRoleMenuRelation> listRoleMenuByRoleIdList(List<String> roleIdList);
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.supervision.police.service.impl;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.supervision.police.domain.SystemRoleMenuRelation;
|
||||
import com.supervision.police.domain.SystemUserRoleRelation;
|
||||
import com.supervision.police.service.SystemRoleMenuRelationService;
|
||||
import com.supervision.police.mapper.SystemRoleMenuRelationMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Administrator
|
||||
* @description 针对表【system_role_menu_relation(角色菜单关联表)】的数据库操作Service实现
|
||||
* @createDate 2024-07-31 15:02:36
|
||||
*/
|
||||
@Service
|
||||
public class SystemRoleMenuRelationServiceImpl extends ServiceImpl<SystemRoleMenuRelationMapper, SystemRoleMenuRelation>
|
||||
implements SystemRoleMenuRelationService {
|
||||
|
||||
@Override
|
||||
@Transactional(transactionManager = "dataSourceTransactionManager",rollbackFor = Exception.class)
|
||||
public void updateRoleMenu(String roleId, List<String> menuIds) {
|
||||
Assert.notEmpty(roleId, "角色id不能为空");
|
||||
|
||||
if (CollUtil.isNotEmpty(menuIds)){
|
||||
List<SystemRoleMenuRelation> list = menuIds.stream().map(menuId -> new SystemRoleMenuRelation(roleId, menuId)).toList();
|
||||
list.forEach(super::save);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteRoleMenu(String roleId) {
|
||||
super.remove(lambdaQuery().eq(SystemRoleMenuRelation::getRoleId, roleId));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SystemRoleMenuRelation> listRoleMenuByRoleIdList(List<String> roleIdList) {
|
||||
if(CollUtil.isEmpty(roleIdList)){
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return super.lambdaQuery().in(SystemRoleMenuRelation::getRoleId, roleIdList).list();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.supervision.utils;
|
||||
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import cn.hutool.jwt.signers.JWTSigner;
|
||||
import cn.hutool.jwt.signers.JWTSignerUtil;
|
||||
|
||||
public class TokenUtil {
|
||||
|
||||
public static String creatToken(String userInfo){
|
||||
final JWTSigner signer = JWTSignerUtil.hs256("~~||DDEdfdfee33s6$$".getBytes());
|
||||
JSONObject info = JSONUtil.parseObj(userInfo);
|
||||
// 过期时间一天,同时这个字段也作为单点登录使用
|
||||
info.putOnce("expireTime",System.currentTimeMillis() + 1000 * 60 * 60 * 24);
|
||||
info.putOnce("issueTime",System.currentTimeMillis());
|
||||
return JWTUtil.createToken(info, signer);
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.supervision.utils;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.jwt.JWT;
|
||||
import cn.hutool.jwt.JWTUtil;
|
||||
import com.supervision.police.dto.user.UserInfoDTO;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
public class UserUtil {
|
||||
|
||||
public static UserInfoDTO getUser() {
|
||||
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
Assert.notNull(requestAttributes, "未获取到用户信息");
|
||||
HttpServletRequest request = requestAttributes.getRequest();
|
||||
|
||||
String token = request.getHeader("token");
|
||||
if (StrUtil.isBlank(token)) {
|
||||
token = (String) request.getAttribute("token");
|
||||
|
||||
}
|
||||
Assert.notEmpty(token, "未获取到用户token信息");
|
||||
JWT jwt = JWTUtil.parseToken(token);
|
||||
JSONObject claimsJson = jwt.getPayload().getClaimsJson();
|
||||
UserInfoDTO bean = JSONUtil.toBean(claimsJson.toString(), UserInfoDTO.class);
|
||||
Assert.notNull(bean, "未获取到用户信息");
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证密码
|
||||
* @param password 明文密码
|
||||
* @param userPd 密文签名
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkUserPassword(String password, String userPd) {
|
||||
return SM2Util.verify(password, userPd);
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码加密签名
|
||||
* @param password 密码
|
||||
* @return
|
||||
*/
|
||||
public static String signPassword(String password){
|
||||
return SM2Util.sign(password);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.supervision.police.mapper.SystemRoleMenuRelationMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.supervision.police.domain.SystemRoleMenuRelation">
|
||||
<result property="id" column="id" jdbcType="VARCHAR"/>
|
||||
<result property="roleId" column="role_id" jdbcType="VARCHAR"/>
|
||||
<result property="menuId" column="menu_id" jdbcType="VARCHAR"/>
|
||||
<result property="createUserId" column="create_user_id" jdbcType="VARCHAR"/>
|
||||
<result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateUserId" column="update_user_id" jdbcType="VARCHAR"/>
|
||||
<result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,role_id,menu_id,
|
||||
create_user_id,create_time,update_user_id,
|
||||
update_time
|
||||
</sql>
|
||||
</mapper>
|
@ -0,0 +1 @@
|
||||
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgIc/Neoxk2aVlkKS4WyHJQ6NTWRMcaSh7dqAeH7gHKyegCgYIKoEcz1UBgi2hRANCAASSC2D8SWIwi6S2QmAzuiJXwtcTHcPsY/Epd4y6VOy0x5OZ4+hXdAq9KrXtRXJlbho1x8+0Rifr396CChxfYcAN
|
@ -0,0 +1 @@
|
||||
MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAEkgtg/EliMIuktkJgM7oiV8LXEx3D7GPxKXeMulTstMeTmePoV3QKvSq17UVyZW4aNcfPtEYn69/eggocX2HADQ==
|
Loading…
Reference in New Issue