旗开得胜
This commit is contained in:
parent
1bad62e831
commit
8c557aa0fd
|
@ -1,18 +0,0 @@
|
|||
package com.greenorange.promotion.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 权限校验
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface AuthCheck {
|
||||
/**
|
||||
* 必须有某个角色
|
||||
*/
|
||||
String mustRole() default "";
|
||||
}
|
|
@ -10,6 +10,8 @@ import java.lang.annotation.*;
|
|||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface RequiresPermission {
|
||||
String roles() default " ";
|
||||
String permissions() default " ";
|
||||
/**
|
||||
* 接口调用权限
|
||||
*/
|
||||
String mustRole() default " ";
|
||||
}
|
|
@ -1,77 +0,0 @@
|
|||
package com.greenorange.promotion.aop;
|
||||
|
||||
|
||||
import com.greenorange.promotion.annotation.AuthCheck;
|
||||
import com.greenorange.promotion.common.ErrorCode;
|
||||
import com.greenorange.promotion.constant.UserConstant;
|
||||
import com.greenorange.promotion.exception.BusinessException;
|
||||
import com.greenorange.promotion.model.entity.User;
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import com.greenorange.promotion.service.user.UserService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestAttributes;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
/**
|
||||
* 权限校验AOP
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
public class AuthInterceptor {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 执行拦截
|
||||
*/
|
||||
@Around("@annotation(authCheck)")
|
||||
public Object doInterceptor(ProceedingJoinPoint joinPoint, AuthCheck authCheck) throws Throwable {
|
||||
// 接口的权限
|
||||
String mustRole = authCheck.mustRole();
|
||||
RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
|
||||
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
|
||||
//当前登录用户
|
||||
User loginUser = userService.getLoginUser(request);
|
||||
//必须有该权限才通过
|
||||
if (StringUtils.isNotBlank(mustRole)) {
|
||||
//mustUserRoleEnum是接口权限
|
||||
UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValues(mustRole);
|
||||
if(mustUserRoleEnum == null) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
//用户权限
|
||||
String userRole = loginUser.getUserRole();
|
||||
//根据用户角色获取封装后的枚举类对象
|
||||
UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole);
|
||||
|
||||
//如果被封号,直接拒绝
|
||||
if (UserRoleEnum.BAN.equals(userRoleEnum)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
|
||||
//如果接口需要Boss权限,则需要判断用户是否是boss管理员
|
||||
if (UserRoleEnum.BOSS.equals(mustUserRoleEnum)) {
|
||||
if (!mustRole.equals(userRole)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
}
|
||||
//如果接口需要管理员权限,则需要判断用户是否是boss或者admin管理员
|
||||
if (UserRoleEnum.ADMIN.equals(mustUserRoleEnum)) {
|
||||
if (!mustRole.equals(userRole) && !userRole.equals(UserConstant.BOSS_ROLE)) {
|
||||
throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
//通过权限校验,放行
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,34 +1,25 @@
|
|||
package com.greenorange.promotion.aop;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import com.auth0.jwt.JWT;
|
||||
import com.auth0.jwt.exceptions.JWTDecodeException;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.greenorange.promotion.annotation.RequiresPermission;
|
||||
import com.greenorange.promotion.model.entity.User;
|
||||
import com.greenorange.promotion.service.user.UserService;
|
||||
import com.wechat.pay.java.core.exception.ServiceException;
|
||||
import com.greenorange.promotion.common.ErrorCode;
|
||||
import com.greenorange.promotion.exception.ThrowUtils;
|
||||
import com.greenorange.promotion.model.entity.UserInfo;
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import com.greenorange.promotion.service.user.UserInfoService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.Signature;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -42,91 +33,44 @@ public class PermissionCheck {
|
|||
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
private UserInfoService userInfoService;
|
||||
|
||||
|
||||
|
||||
/***
|
||||
* @MethodName: permissionCheckPointCut
|
||||
* @description: 定义一个切点
|
||||
* @Author: LiuTao
|
||||
* @UpdateTime: 2023/6/20 19:34
|
||||
* 执行拦截
|
||||
**/
|
||||
@Pointcut("@annotation(com.greenorange.promotion.annotation.RequiresPermission)")
|
||||
public void permissionCheckPointCut() {
|
||||
|
||||
}
|
||||
|
||||
/***
|
||||
* @MethodName: check
|
||||
* @description: 环绕通知
|
||||
* @Author: LiuTao
|
||||
* @Param: [pjp]
|
||||
* @UpdateTime: 2023/6/20 19:34
|
||||
* @Return: java.lang.Object
|
||||
* @Throw: Throwable
|
||||
**/
|
||||
@Around("permissionCheckPointCut()")
|
||||
public Object check(ProceedingJoinPoint pjp) throws Throwable {
|
||||
@Around("@annotation(requiresPermission)")
|
||||
public Object check(ProceedingJoinPoint joinPoint, RequiresPermission requiresPermission) throws Throwable {
|
||||
// 获取请求对象
|
||||
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
||||
// 记录日志
|
||||
log.info("===============系统操作日志===============");
|
||||
Signature signature = pjp.getSignature();
|
||||
// 请求的类
|
||||
String className = pjp.getTarget().getClass().getName();
|
||||
String methodName = signature.getName();
|
||||
log.info("请求类:{}", className);
|
||||
log.info("请求方法:{}", methodName);
|
||||
log.info("请求方式:{}", request.getMethod());
|
||||
log.info("请求ip:{}", request.getRemoteAddr());
|
||||
log.info("请求类方法:{}", signature);
|
||||
log.info("请求参数:{}", Arrays.toString(pjp.getArgs()));
|
||||
// 权限注解校验
|
||||
MethodSignature handlerMethod = (MethodSignature) signature;
|
||||
Method method = handlerMethod.getMethod();
|
||||
System.out.println("method:" + method);
|
||||
System.out.println("-------------------------------------------");
|
||||
// 判断当前方法上有没有注解
|
||||
System.out.println(method.isAnnotationPresent(RequiresPermission.class));
|
||||
System.out.println("-------------------------------------------");
|
||||
|
||||
if (method.isAnnotationPresent(RequiresPermission.class)) {
|
||||
RequiresPermission auth = method.getAnnotation(RequiresPermission.class);
|
||||
System.out.println("++++++++++++++++++++++++++++auth:" + auth);
|
||||
String roles = auth.roles();
|
||||
String permissions = auth.permissions();
|
||||
|
||||
String token = request.getHeader("token");
|
||||
// 认证
|
||||
if (StrUtil.isBlank(token)) {
|
||||
// throw new ServiceException(Constants.CODE_401, "请登录!!!");
|
||||
}
|
||||
String id = null;
|
||||
// try {
|
||||
// id = JWT.decode(token).getAudience().get(0);
|
||||
// } catch (JWTDecodeException jwtDecodeException) {
|
||||
//// throw new ServiceException(Constants.CODE_401, "token验证失败,请重新登录");
|
||||
// }
|
||||
// User user = userService.getById(id);
|
||||
// 校验角色
|
||||
// if (StrUtil.isNotBlank(roles)) {
|
||||
// if (!Arrays.asList(roles.split(",")).contains(user.getRole())) {
|
||||
//// throw new ServiceException(Constants.CODE_403, "当前角色权限不足");
|
||||
// }
|
||||
// }
|
||||
// 校验权限
|
||||
// if (StrUtil.isNotBlank(permissions)) {
|
||||
// List<String> userPermissions = menuUtil
|
||||
// .getPermissions(user.getRole())
|
||||
// .stream()
|
||||
// .map(BtnVo::getPermission)
|
||||
// .collect(Collectors.toList());
|
||||
// if (!new HashSet<>(userPermissions).containsAll(Arrays.asList(permissions.split(",")))) {
|
||||
// throw new ServiceException(Constants.CODE_401, "无权限访问资源");
|
||||
// }
|
||||
// }
|
||||
// 接口的权限
|
||||
String mustRole = requiresPermission.mustRole();
|
||||
// 获取接口权限的枚举类
|
||||
UserRoleEnum mustUserRoleEnum = UserRoleEnum.getEnumByValues(mustRole);
|
||||
ThrowUtils.throwIf(mustUserRoleEnum == null, ErrorCode.NO_AUTH_ERROR);
|
||||
// 获取用户权限
|
||||
String token = request.getHeader("token");
|
||||
ThrowUtils.throwIf(StringUtils.isBlank(token), ErrorCode.NOT_LOGIN_ERROR);
|
||||
String id = null;
|
||||
try {
|
||||
id = JWT.decode(token).getAudience().get(0);
|
||||
} catch (JWTDecodeException jwtDecodeException) {
|
||||
log.info("JWT已失效");
|
||||
}
|
||||
return pjp.proceed();
|
||||
UserInfo userInfo = userInfoService.getById(id);
|
||||
ThrowUtils.throwIf(userInfo == null, ErrorCode.OPERATION_ERROR);
|
||||
// 获取用户权限的枚举类
|
||||
String userRole = userInfo.getUserRole();
|
||||
UserRoleEnum userRoleEnum = UserRoleEnum.getEnumByValues(userRole);
|
||||
|
||||
// 接口权限只能是 ADMIN 或者 BOSS,用户权限是 ADMIN 或者 BOSS,USER,BAN
|
||||
// 校验角色
|
||||
ThrowUtils.throwIf(UserRoleEnum.USER.equals(userRoleEnum), ErrorCode.NO_AUTH_ERROR);
|
||||
ThrowUtils.throwIf(UserRoleEnum.BAN.equals(userRoleEnum), ErrorCode.NO_AUTH_ERROR, "用户已被封禁");
|
||||
ThrowUtils.throwIf(UserRoleEnum.ADMIN.equals(userRoleEnum) && UserRoleEnum.BOSS.equals(mustUserRoleEnum), ErrorCode.NO_AUTH_ERROR);
|
||||
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,115 +0,0 @@
|
|||
package com.greenorange.promotion.controller.user;
|
||||
|
||||
import com.greenorange.promotion.annotation.AuthCheck;
|
||||
import com.greenorange.promotion.annotation.RequiresPermission;
|
||||
import com.greenorange.promotion.common.BaseResponse;
|
||||
import com.greenorange.promotion.common.ResultUtils;
|
||||
import com.greenorange.promotion.constant.UserConstant;
|
||||
import com.greenorange.promotion.model.dto.CommonBatchRequest;
|
||||
import com.greenorange.promotion.model.dto.CommonRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserAddRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserQueryRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserUpdateRequest;
|
||||
import com.greenorange.promotion.model.vo.user.UserVO;
|
||||
import com.greenorange.promotion.service.user.UserService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 用户表 控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("user")
|
||||
@Slf4j
|
||||
@Tag(name = "用户表管理")
|
||||
public class UserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员添加用户
|
||||
* @param userAddRequest 用户添加请求体
|
||||
* @return 是否添加成功
|
||||
*/
|
||||
@PostMapping("add")
|
||||
@Operation(summary = "web端管理员添加用户", description = "参数:用户表添加请求体,权限:管理员(boss, admin),方法名:addUser")
|
||||
@RequiresPermission(roles = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Long> addUser(@RequestBody UserAddRequest userAddRequest) {
|
||||
return ResultUtils.success(userService.addUser(userAddRequest));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员更新用户表
|
||||
* @param userUpdateRequest 用户更新请求体
|
||||
* @return 是否更新成功
|
||||
*/
|
||||
@PostMapping("update")
|
||||
@Operation(summary = "web端管理员更新用户", description = "参数:用户更新请求体,权限:管理员(boss, admin),方法名:updateUser")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest) {
|
||||
return ResultUtils.success(userService.updateUser(userUpdateRequest));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员删除用户
|
||||
* @param commonRequest 用户删除请求体
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
@PostMapping("delete")
|
||||
@Operation(summary = "web端管理员删除用户", description = "参数:用户删除请求体,权限:管理员(boss, admin),方法名:deleteUser")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> deleteUser(@RequestBody CommonRequest commonRequest) {
|
||||
return ResultUtils.success(userService.deleteUser(commonRequest));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Web端管理员分页查看用户表
|
||||
* @param userQueryRequest 用户表查询请求体
|
||||
* @return 用户表列表
|
||||
*/
|
||||
@PostMapping("page")
|
||||
@Operation(summary = "Web端管理员分页查看用户表", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:listUserByPage")
|
||||
@RequiresPermission(roles = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> listUserByPage(@RequestBody UserQueryRequest userQueryRequest) {
|
||||
return ResultUtils.success(true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员根据id查询用户表
|
||||
* @param commonRequest 用户表查询请求体
|
||||
* @return 用户表信息
|
||||
*/
|
||||
@PostMapping("queryById")
|
||||
@Operation(summary = "web端管理员根据id查询用户表", description = "参数:用户表查询请求体,权限:管理员(boss, admin),方法名:queryUserById")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<UserVO> queryUserById(@RequestBody CommonRequest commonRequest) {
|
||||
return ResultUtils.success(userService.queryUserById(commonRequest));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* web端管理员批量删除用户
|
||||
* @param commonBatchRequest id列表
|
||||
* @return 是否删除成功
|
||||
*/
|
||||
@PostMapping("delBatch")
|
||||
@Operation(summary = "web端管理员批量删除用户", description = "参数:id列表,权限:管理员(boss, admin),方法名:delBatchUser")
|
||||
@AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
|
||||
public BaseResponse<Boolean> delBatchUser(@RequestBody CommonBatchRequest commonBatchRequest) {
|
||||
return ResultUtils.success(userService.delBatchUser(commonBatchRequest));
|
||||
}
|
||||
|
||||
}
|
|
@ -15,9 +15,9 @@ import java.util.*;
|
|||
public class Generator {
|
||||
|
||||
// 数据源配置
|
||||
private static final String DATASOURCE_URL = "jdbc:mysql://8.130.119.119:3306/qingcheng?serverTimezone=Asia/Shanghai";
|
||||
private static final String DATASOURCE_URL = "jdbc:mysql://1.94.237.210:3306/qingcheng?serverTimezone=Asia/Shanghai";
|
||||
private static final String USERNAME = "qingcheng";
|
||||
private static final String PASSWORD = "qingcheng";
|
||||
private static final String PASSWORD = "Qc@123456";
|
||||
|
||||
// 输出路径
|
||||
private static final String OUTPUT_PATH = System.getProperty("user.dir");
|
||||
|
@ -50,9 +50,9 @@ public class Generator {
|
|||
// 表注释
|
||||
private static final String TABLE_COMMENT = "用户表";
|
||||
// 实体类名
|
||||
private static final String ENTITY_NAME = "User";
|
||||
private static final String ENTITY_NAME = "UserInfo";
|
||||
// 表名
|
||||
private static final String TABLE_NAME = "user";
|
||||
private static final String TABLE_NAME = "user_info";
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
package com.greenorange.promotion.mapper;
|
||||
|
||||
import com.greenorange.promotion.model.entity.User;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
|
||||
/**
|
||||
* @author 35880
|
||||
* @description 针对表【user(用户表)】的数据库操作Mapper
|
||||
* @createDate 2025-03-30 23:03:14
|
||||
* @Entity com.greenorange.promotion.model.entity.User
|
||||
*/
|
||||
public interface UserMapper extends BaseMapper<User> {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
package com.greenorange.promotion.model.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户表添加请求体
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表添加请求体", requiredProperties =
|
||||
{"userAccount", "userPassword", "miniOpenId", "userName", "userAvatar", "points", "userRole"})
|
||||
public class UserAddRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@Schema(description = "账号", example = "qingcheng")
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@Schema(description = "密码", example = "123456")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 小程序openId
|
||||
*/
|
||||
@Schema(description = "小程序openId", example = "324324")
|
||||
private String miniOpenId;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
@Schema(description = "用户昵称", example = "Jack")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
@Schema(description = "用户头像", example = "https://www.com")
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 积分
|
||||
*/
|
||||
@Schema(description = "积分", example = "1200")
|
||||
private Integer points;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "user")
|
||||
private String userRole;
|
||||
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
package com.greenorange.promotion.model.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import com.greenorange.promotion.common.PageRequest;
|
||||
|
||||
/**
|
||||
* 用户表查询请求体,继承自分页请求 PageRequest
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表查询请求体", requiredProperties = {"current", "pageSize"})
|
||||
public class UserQueryRequest extends PageRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@Schema(description = "用户id", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
@Schema(description = "用户昵称", example = "Jack")
|
||||
private String userName;
|
||||
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "user")
|
||||
private String userRole;
|
||||
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -1,68 +0,0 @@
|
|||
package com.greenorange.promotion.model.dto.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户表更新请求体
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表更新请求体", requiredProperties =
|
||||
{"id", "userAccount", "userPassword", "miniOpenId", "userName", "userAvatar", "points", "userRole"})
|
||||
public class UserUpdateRequest implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@Schema(description = "用户id", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@Schema(description = "账号", example = "qingcheng")
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@Schema(description = "密码", example = "123456")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 小程序openId
|
||||
*/
|
||||
@Schema(description = "小程序openId", example = "fdsafdfasd")
|
||||
private String miniOpenId;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
@Schema(description = "用户昵称", example = "Jack")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
@Schema(description = "用户头像", example = "https://www.com")
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 积分
|
||||
*/
|
||||
@Schema(description = "积分", example = "12000")
|
||||
private Integer points;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "admin")
|
||||
private String userRole;
|
||||
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -1,76 +0,0 @@
|
|||
package com.greenorange.promotion.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户表
|
||||
* @TableName user
|
||||
*/
|
||||
@TableName(value ="user")
|
||||
@Data
|
||||
public class User implements Serializable {
|
||||
/**
|
||||
* id
|
||||
*/
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 小程序openId
|
||||
*/
|
||||
private String miniOpenId;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 积分
|
||||
*/
|
||||
private Integer points;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
private String userRole;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 是否删除
|
||||
*/
|
||||
private Integer isDelete;
|
||||
|
||||
@TableField(exist = false)
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -49,7 +49,7 @@ public class UserInfo implements Serializable {
|
|||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
private Object userRole;
|
||||
private String userRole;
|
||||
|
||||
/**
|
||||
* 上级用户id
|
||||
|
|
|
@ -1,67 +0,0 @@
|
|||
package com.greenorange.promotion.model.vo.user;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 用户表 视图对象
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "用户表 视图对象")
|
||||
public class UserVO implements Serializable {
|
||||
|
||||
/**
|
||||
* 用户id
|
||||
*/
|
||||
@Schema(description = "用户id", example = "1")
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 账号
|
||||
*/
|
||||
@Schema(description = "账号", example = "${field.example}")
|
||||
private String userAccount;
|
||||
|
||||
/**
|
||||
* 密码
|
||||
*/
|
||||
@Schema(description = "密码", example = "${field.example}")
|
||||
private String userPassword;
|
||||
|
||||
/**
|
||||
* 小程序openId
|
||||
*/
|
||||
@Schema(description = "小程序openId", example = "${field.example}")
|
||||
private String miniOpenId;
|
||||
|
||||
/**
|
||||
* 用户昵称
|
||||
*/
|
||||
@Schema(description = "用户昵称", example = "${field.example}")
|
||||
private String userName;
|
||||
|
||||
/**
|
||||
* 用户头像
|
||||
*/
|
||||
@Schema(description = "用户头像", example = "${field.example}")
|
||||
private String userAvatar;
|
||||
|
||||
/**
|
||||
* 积分
|
||||
*/
|
||||
@Schema(description = "积分", example = "${field.example}")
|
||||
private Integer points;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "${field.example}")
|
||||
private String userRole;
|
||||
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package com.greenorange.promotion.service.user;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.greenorange.promotion.model.dto.user.UserInfoQueryRequest;
|
||||
import com.greenorange.promotion.model.entity.UserInfo;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
|
||||
|
@ -10,4 +12,9 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
*/
|
||||
public interface UserInfoService extends IService<UserInfo> {
|
||||
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*/
|
||||
QueryWrapper<UserInfo> getQueryWrapper(UserInfoQueryRequest userInfoQueryRequest);
|
||||
}
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
package com.greenorange.promotion.service.user;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.greenorange.promotion.model.dto.CommonBatchRequest;
|
||||
import com.greenorange.promotion.model.dto.CommonRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserAddRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserQueryRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserUpdateRequest;
|
||||
import com.greenorange.promotion.model.entity.User;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.greenorange.promotion.model.vo.user.UserVO;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 35880
|
||||
* @description 针对表【user(用户表)】的数据库操作Service
|
||||
* @createDate 2025-03-30 23:03:14
|
||||
*/
|
||||
public interface UserService extends IService<User> {
|
||||
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*/
|
||||
QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询用户
|
||||
*/
|
||||
Page<UserVO> listUserByPage(UserQueryRequest userQueryRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 根据id查询用户
|
||||
*/
|
||||
UserVO queryUserById(CommonRequest commonRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
*/
|
||||
Long addUser(UserAddRequest userAddRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*/
|
||||
boolean updateUser(UserUpdateRequest userUpdateRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*/
|
||||
boolean deleteUser(CommonRequest commonRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 批量删除用户
|
||||
*/
|
||||
boolean delBatchUser(CommonBatchRequest commonBatchRequest);
|
||||
|
||||
|
||||
/**
|
||||
* 校验用户是否登录
|
||||
*/
|
||||
User getLoginUser(HttpServletRequest request);
|
||||
}
|
|
@ -1,9 +1,11 @@
|
|||
package com.greenorange.promotion.service.user.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.greenorange.promotion.mapper.UserInfoMapper;
|
||||
import com.greenorange.promotion.model.dto.user.UserInfoQueryRequest;
|
||||
import com.greenorange.promotion.model.entity.UserInfo;
|
||||
import com.greenorange.promotion.service.user.UserInfoService;
|
||||
import com.greenorange.promotion.mapper.UserInfoMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
|
@ -15,6 +17,13 @@ import org.springframework.stereotype.Service;
|
|||
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
|
||||
implements UserInfoService{
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryWrapper<UserInfo> getQueryWrapper(UserInfoQueryRequest userInfoQueryRequest) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,187 +0,0 @@
|
|||
package com.greenorange.promotion.service.user.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.greenorange.promotion.common.ErrorCode;
|
||||
import com.greenorange.promotion.common.ResultUtils;
|
||||
import com.greenorange.promotion.constant.CommonConstant;
|
||||
import com.greenorange.promotion.exception.BusinessException;
|
||||
import com.greenorange.promotion.exception.ThrowUtils;
|
||||
import com.greenorange.promotion.model.dto.CommonBatchRequest;
|
||||
import com.greenorange.promotion.model.dto.CommonRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserAddRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserQueryRequest;
|
||||
import com.greenorange.promotion.model.dto.user.UserUpdateRequest;
|
||||
import com.greenorange.promotion.model.entity.User;
|
||||
import com.greenorange.promotion.model.enums.UserRoleEnum;
|
||||
import com.greenorange.promotion.model.vo.user.UserVO;
|
||||
import com.greenorange.promotion.service.common.CommonService;
|
||||
import com.greenorange.promotion.service.user.UserService;
|
||||
import com.greenorange.promotion.mapper.UserMapper;
|
||||
import com.greenorange.promotion.utils.SqlUtils;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.greenorange.promotion.constant.UserConstant.USER_LOGIN_STATE;
|
||||
|
||||
/**
|
||||
* @author 35880
|
||||
* @description 针对表【user(用户表)】的数据库操作Service实现
|
||||
* @createDate 2025-03-30 23:03:14
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
|
||||
|
||||
|
||||
@Resource
|
||||
private CommonService commonService;
|
||||
|
||||
/**
|
||||
* 获取查询条件
|
||||
*/
|
||||
@Override
|
||||
public QueryWrapper<User> getQueryWrapper(UserQueryRequest userQueryRequest) {
|
||||
Long id = userQueryRequest.getId();
|
||||
String userName = userQueryRequest.getUserName();
|
||||
String userRole = userQueryRequest.getUserRole();
|
||||
String sortField = userQueryRequest.getSortField();
|
||||
String sortOrder = userQueryRequest.getSortOrder();
|
||||
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq(id != null, "id", id);
|
||||
queryWrapper.like(StringUtils.isNotBlank(userName), "userName", userName);
|
||||
queryWrapper.eq(StringUtils.isNotBlank(userRole), "userRole", userRole);
|
||||
queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
|
||||
sortField);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询用户
|
||||
*/
|
||||
@Override
|
||||
public Page<UserVO> listUserByPage(UserQueryRequest userQueryRequest) {
|
||||
if (userQueryRequest == null) throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
long current = userQueryRequest.getCurrent();
|
||||
long pageSize = userQueryRequest.getPageSize();
|
||||
QueryWrapper<User> queryWrapper = this.getQueryWrapper(userQueryRequest);
|
||||
Page<User> page = this.page(new Page<>(current, pageSize), queryWrapper);
|
||||
List<User> userList = page.getRecords();
|
||||
List<UserVO> userVOList = commonService.convertList(userList, UserVO.class);
|
||||
Page<UserVO> voPage = new Page<>();
|
||||
voPage.setRecords(userVOList);
|
||||
voPage.setPages(page.getPages());
|
||||
voPage.setCurrent(page.getCurrent());
|
||||
voPage.setTotal(page.getTotal());
|
||||
voPage.setSize(page.getSize());
|
||||
return voPage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据id查询用户
|
||||
*/
|
||||
@Override
|
||||
public UserVO queryUserById(CommonRequest commonRequest) {
|
||||
if (commonRequest == null || commonRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = this.getById(commonRequest.getId());
|
||||
ThrowUtils.throwIf(user == null, ErrorCode.OPERATION_ERROR, "用户不存在");
|
||||
return commonService.copyProperties(user, UserVO.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户
|
||||
*/
|
||||
@Override
|
||||
public Long addUser(UserAddRequest userAddRequest) {
|
||||
if (userAddRequest == null) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = commonService.copyProperties(userAddRequest, User.class);
|
||||
boolean result = this.save(user);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户添加失败");
|
||||
return user.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新用户
|
||||
*/
|
||||
@Override
|
||||
public boolean updateUser(UserUpdateRequest userUpdateRequest) {
|
||||
if (userUpdateRequest == null || userUpdateRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
User user = commonService.copyProperties(userUpdateRequest, User.class);
|
||||
boolean result = this.updateById(user);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户更新失败");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*/
|
||||
@Override
|
||||
public boolean deleteUser(CommonRequest commonRequest) {
|
||||
if (commonRequest == null || commonRequest.getId() <= 0) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
Long id = commonRequest.getId();
|
||||
boolean result = this.removeById(id);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户删除失败");
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除用户
|
||||
*/
|
||||
@Override
|
||||
public boolean delBatchUser(CommonBatchRequest commonBatchRequest) {
|
||||
if (commonBatchRequest == null || CollectionUtils.isEmpty(commonBatchRequest.getIds())) {
|
||||
throw new BusinessException(ErrorCode.PARAMS_ERROR);
|
||||
}
|
||||
List<Long> ids = commonBatchRequest.getIds();
|
||||
boolean result = this.removeByIds(ids);
|
||||
ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "用户批量删除失败");
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前登录用户
|
||||
*/
|
||||
@Override
|
||||
public User getLoginUser(HttpServletRequest request) {
|
||||
HttpSession session = request.getSession();
|
||||
Object userObj = session.getAttribute(USER_LOGIN_STATE);
|
||||
User currentUser = (User) userObj;
|
||||
if (currentUser == null || currentUser.getId() == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
|
||||
}
|
||||
//根据id进行查询
|
||||
Long userId = currentUser.getId();
|
||||
currentUser = this.getById(userId);
|
||||
if (currentUser == null) {
|
||||
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
|
||||
}
|
||||
//被封号
|
||||
if (UserRoleEnum.BAN.getValue().equals(currentUser.getUserRole())) {
|
||||
throw new BusinessException(ErrorCode.FORBIDDEN_ERROR);
|
||||
}
|
||||
return currentUser;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
<?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.greenorange.promotion.mapper.UserMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.greenorange.promotion.model.entity.User">
|
||||
<id property="id" column="id" jdbcType="BIGINT"/>
|
||||
<result property="userAccount" column="userAccount" jdbcType="VARCHAR"/>
|
||||
<result property="userPassword" column="userPassword" jdbcType="VARCHAR"/>
|
||||
<result property="miniOpenId" column="miniOpenId" jdbcType="VARCHAR"/>
|
||||
<result property="userName" column="userName" jdbcType="VARCHAR"/>
|
||||
<result property="userAvatar" column="userAvatar" jdbcType="VARCHAR"/>
|
||||
<result property="points" column="points" jdbcType="INTEGER"/>
|
||||
<result property="userRole" column="userRole" jdbcType="VARCHAR"/>
|
||||
<result property="createTime" column="createTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="updateTime" column="updateTime" jdbcType="TIMESTAMP"/>
|
||||
<result property="isDelete" column="isDelete" jdbcType="TINYINT"/>
|
||||
</resultMap>
|
||||
|
||||
<sql id="Base_Column_List">
|
||||
id,userAccount,userPassword,
|
||||
miniOpenId,userName,userAvatar,
|
||||
points,userRole,createTime,
|
||||
updateTime,isDelete
|
||||
</sql>
|
||||
</mapper>
|
Loading…
Reference in New Issue
Block a user