From d3462828ed4fda45cd3d6ca99cb6dffcfa5b5b7b Mon Sep 17 00:00:00 2001 From: wangw <1594593906@qq.com> Date: Sat, 24 May 2025 11:21:26 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=A6=81=E7=94=A8=20?= =?UTF-8?q?=E6=8B=89=E9=BB=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sqx/common/aspect/AppApiMethodAspect.java | 34 ++++++++++-- .../common/exception/SqxExceptionHandler.java | 9 ++++ .../sqx/common/exception/UserException.java | 52 +++++++++++++++++++ .../app/controller/UserController.java | 28 +++++----- .../sqx/modules/app/service/UserService.java | 2 +- .../app/service/impl/UserServiceImpl.java | 26 ++++++---- 6 files changed, 121 insertions(+), 30 deletions(-) create mode 100644 src/main/java/com/sqx/common/exception/UserException.java diff --git a/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java b/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java index 575f9970..5fba3cbb 100644 --- a/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java +++ b/src/main/java/com/sqx/common/aspect/AppApiMethodAspect.java @@ -2,10 +2,16 @@ package com.sqx.common.aspect; import cn.hutool.core.thread.ThreadUtil; import com.google.gson.Gson; +import com.sqx.common.exception.CzgException; import com.sqx.common.exception.LimitException; +import com.sqx.common.exception.UserException; import com.sqx.common.utils.HttpContextUtils; import com.sqx.common.utils.IPUtils; +import com.sqx.modules.app.entity.UserEntity; +import com.sqx.modules.app.service.UserService; +import com.sqx.modules.app.utils.JwtUtils; import com.sqx.modules.common.service.CommonInfoService; +import io.jsonwebtoken.Claims; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; @@ -13,14 +19,11 @@ import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Profile; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; - /** * 方法调用统一切面处理 */ @@ -32,14 +35,37 @@ public class AppApiMethodAspect { @Autowired private CommonInfoService commonInfoService; + @Autowired + private UserService userService; + @Autowired + private JwtUtils jwtUtils; @Pointcut("!execution(public * (com.sqx.modules.sys.controller.SysLoginController).*(..)) " + "&& execution(public * (com.sqx.modules.*.controller..*).*(..)))") public void pkg() { } + @Around("pkg()") public Object around(ProceedingJoinPoint pjp) throws Throwable { + HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); + String token = request.getHeader("token"); + if (StringUtils.isBlank(token)) { + Claims claims = jwtUtils.getClaimByToken(token); + if (claims != null && !jwtUtils.isTokenExpired(claims.getExpiration())) { + Long userId = Long.parseLong(claims.getSubject()); + UserEntity userInfo = userService.selectUserById(userId); + if (userInfo == null) { + throw new CzgException("用户不存在"); + } else if (userInfo.getStatus() == 0) { + throw new UserException("用户被拉黑", 701); + } else if (userInfo.getStatus() == 2) { + throw new UserException("用户被禁用", 702); + } + } + } + + long start = System.currentTimeMillis(); //避免回填 Object[] args = pjp.getArgs(); @@ -50,8 +76,6 @@ public class AppApiMethodAspect { args[i] = null; } } - - HttpServletRequest request = HttpContextUtils.getHttpServletRequest(); String requestIp = IPUtils.getIpAddr(request); // Integer maxAccessCount = Integer.parseInt(commonInfoService.findOne(935).getValue()); diff --git a/src/main/java/com/sqx/common/exception/SqxExceptionHandler.java b/src/main/java/com/sqx/common/exception/SqxExceptionHandler.java index 1e6567d0..d44e6b08 100644 --- a/src/main/java/com/sqx/common/exception/SqxExceptionHandler.java +++ b/src/main/java/com/sqx/common/exception/SqxExceptionHandler.java @@ -37,6 +37,15 @@ public class SqxExceptionHandler { return r; } + @ExceptionHandler(UserException.class) + public Result handleUserException(UserException e) { + Result r = new Result(); + r.put("code", e.getCode()); + r.put("msg", e.getMessage()); + + return r; + } + @ExceptionHandler(NoHandlerFoundException.class) public Result handlerNoFoundException(Exception e, WebRequest webRequest) { logErrorInfo(webRequest); diff --git a/src/main/java/com/sqx/common/exception/UserException.java b/src/main/java/com/sqx/common/exception/UserException.java new file mode 100644 index 00000000..afc979cc --- /dev/null +++ b/src/main/java/com/sqx/common/exception/UserException.java @@ -0,0 +1,52 @@ +package com.sqx.common.exception; + +/** + * 自定义异常 + * + */ +public class UserException extends RuntimeException { + private static final long serialVersionUID = 1L; + + private String msg; + private int code = 701; + + public UserException(String msg) { + super(msg); + this.msg = msg; + } + + public UserException(String msg, Throwable e) { + super(msg, e); + this.msg = msg; + } + + public UserException(String msg, int code) { + super(msg); + this.msg = msg; + this.code = code; + } + + public UserException(String msg, int code, Throwable e) { + super(msg, e); + this.msg = msg; + this.code = code; + } + + public String getMsg() { + return msg; + } + + public void setMsg(String msg) { + this.msg = msg; + } + + public int getCode() { + return code; + } + + public void setCode(int code) { + this.code = code; + } + + +} diff --git a/src/main/java/com/sqx/modules/app/controller/UserController.java b/src/main/java/com/sqx/modules/app/controller/UserController.java index f19bc24e..d02d5cd5 100644 --- a/src/main/java/com/sqx/modules/app/controller/UserController.java +++ b/src/main/java/com/sqx/modules/app/controller/UserController.java @@ -196,14 +196,23 @@ public class UserController { @RequestMapping(value = "/updateUserStatusByUserId", method = RequestMethod.GET) @ApiOperation("禁用或启用用户") @ResponseBody - public Result updateUserByUserId(Long userId) { + public Result updateUserByUserId(Long userId, Integer status) { UserEntity byId = userService.getById(userId); - if (byId.getStatus().equals(1)) { - byId.setStatus(2); - } else { - byId.setStatus(1); + if (byId == null) { + return Result.error("用户不存在"); + } + + if (status.equals(1)) { + byId.setStatus(1); + userService.upUserBlack(byId, 1); + } else if (status.equals(2)) { + byId.setStatus(2); + userService.updateById(byId); + } else if (status.equals(0)) { + userService.upUserBlack(byId, 0); + } else { + return Result.error("状态不正确"); } - userService.updateById(byId); return Result.success(); } @@ -444,12 +453,5 @@ public class UserController { return userService.updateUserInviteAmount(userInviteDTO); } - @ApiOperation("更新用户邀请奖励金额") - @RequestMapping(value = "/removeUserBlack", method = RequestMethod.GET) - @ResponseBody - public Result removeUserBlack(Long userId, Integer status) { - return userService.removeUserBlack(userId, status); - } - } diff --git a/src/main/java/com/sqx/modules/app/service/UserService.java b/src/main/java/com/sqx/modules/app/service/UserService.java index e4d25b01..e6cb294f 100644 --- a/src/main/java/com/sqx/modules/app/service/UserService.java +++ b/src/main/java/com/sqx/modules/app/service/UserService.java @@ -235,7 +235,7 @@ public interface UserService extends IService { Result updateUserInviteAmount(UserInviteDTO userInviteDTO); - Result removeUserBlack(Long userId, Integer status); + Result upUserBlack(UserEntity userEntity, Integer status); Map queryPayAndExtractInfo(); diff --git a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java index a5130233..90ecf47e 100644 --- a/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java +++ b/src/main/java/com/sqx/modules/app/service/impl/UserServiceImpl.java @@ -899,10 +899,14 @@ public class UserServiceImpl extends ServiceImpl implements return Result.error("账号或密码不正确!"); } - if (!userInfo.getStatus().equals(1)) { + if (userInfo.getStatus().equals(0)) { return Result.error("账号已被禁用,请联系客服处理!"); } +// if (!userInfo.getStatus().equals(1)) { +// return Result.error("账号已被禁用,请联系客服处理!"); +// } + // 实名认证信息 UserInfo idCardAuth = userInfoService.getByUserId(userInfo.getUserId()); if (idCardAuth != null && StrUtil.isNotBlank(idCardAuth.getCertNo())) { @@ -1349,8 +1353,10 @@ public class UserServiceImpl extends ServiceImpl implements CommonInfo three = commonInfoService.findOne(238); CommonInfo four = commonInfoService.findOne(239); CommonInfo name = commonInfoService.findOne(81); - String testUsername = three.getValue(); //在短信宝注册的用户名 - String testPassword = four.getValue(); //在短信宝注册的密码 + //在短信宝注册的用户名 + String testUsername = three.getValue(); + //在短信宝注册的密码 + String testPassword = four.getValue(); String value = ""; switch (state) { case "register": @@ -1584,7 +1590,8 @@ public class UserServiceImpl extends ServiceImpl implements GTNotification notification = new GTNotification(); pushDTO.setPushMessage(pushMessage); // 配置通知栏图标 - notification.setLogo(commonInfoService.findOne(19).getValue() + "/logo.png"); //配置通知栏图标,需要在客户端开发时嵌入,默认为push.png + //配置通知栏图标,需要在客户端开发时嵌入,默认为push.png + notification.setLogo(commonInfoService.findOne(19).getValue() + "/logo.png"); // 配置通知栏网络图标 notification.setLogoUrl(commonInfoService.findOne(19).getValue() + "/logo.png"); notification.setTitle(title); @@ -1670,8 +1677,9 @@ public class UserServiceImpl extends ServiceImpl implements }); } + // 按 counts 降序 userInfoList = userInfoList.stream() - .sorted((u1, u2) -> Integer.compare(u2.getCounts(), u1.getCounts())) // 按 counts 降序 + .sorted((u1, u2) -> Integer.compare(u2.getCounts(), u1.getCounts())) .collect(Collectors.toList()); return Result.success().put("data", PageUtils.page(new PageInfo<>(userInfoList), true)); } @@ -1783,12 +1791,8 @@ public class UserServiceImpl extends ServiceImpl implements } @Override - public Result removeUserBlack(Long userId, Integer status) { - UserEntity userEntity = getById(userId); - if (userEntity == null) { - return Result.error("用户不存在"); - } - + public Result upUserBlack(UserEntity userEntity, Integer status) { + Long userId = userEntity.getUserId(); if (status == 0) { userEntity.setStatus(0); update(userEntity, new LambdaUpdateWrapper().eq(UserEntity::getUserId, userId));