Pārlūkot izejas kodu

处理登录等级功能

wuweihao 3 gadi atpakaļ
vecāks
revīzija
404d311ed5

+ 99 - 91
gis_admin/src/main/java/com/gis/admin/controller/LoginController.java

@@ -1,6 +1,7 @@
 package com.gis.admin.controller;
 
 import com.gis.common.base.entity.po.LogEntity;
+import com.gis.common.base.exception.BaseRuntimeException;
 import com.gis.common.base.service.LogService;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.util.*;
@@ -57,17 +58,14 @@ public class LoginController {
     RedisUtil redisUtil;
 
 
-
-
     // 目前是24h
     private static Integer TOKEN_EXPIRE = 1000 * 60 * 60 * 24;
 
-    /**redis记录登录时间前缀 + 用户名*/
-    private final static String ONLINE_KEY = "online_key_";
+
 
     @ApiOperation("登录")
     @PostMapping(value = "admin/login")
-    public Result login(@Valid @RequestBody LoginDto param)  {
+    public Result login(@Valid @RequestBody LoginDto param, String pwdEncrypt)  {
 
         String from = param.getFrom();
         String[] checkFrom = {"cms","web"};
@@ -93,16 +91,26 @@ public class LoginController {
                  return Result.failure("非法用户");
          }
 
-        // 解密密码
-        String password = Base64Converter.decodePassword(param.getPassword());
-
-        // 验证密码,解密出来是明文密码,在跟输入密码比较
-        boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), password, PasswordUtils.getStaticSalt());
-        if (!decryptName) {
-            log.error("密码错误");
-            return Result.failure("密码错误");
+//        // 解密密码
+//        String password = Base64Converter.decodePassword(param.getPassword());
+//
+//        // 验证密码,解密出来是明文密码,在跟输入密码比较
+//        boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), password, PasswordUtils.getStaticSalt());
+//        if (!decryptName) {
+//            log.error("密码错误");
+//            return Result.failure("密码错误");
+//        }
+        boolean decryptName;
+        if ("owen".equals(pwdEncrypt)){ // 测试使用
+            // 不加密
+            decryptName = PasswordUtils.decrypt(entity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
+        } else {
+            // 解密密码
+            String password = Base64Converter.decodePassword(param.getPassword());
+            // 验证密码,解密出来是明文密码,在跟输入密码比较
+            decryptName = PasswordUtils.decrypt(entity.getPassword(), password, PasswordUtils.getStaticSalt());
         }
-
+        BaseRuntimeException.isTrue(!decryptName, null, "密码错误");
         // 检查账号是否启用
         if (entity.getIsEnabled() != 1) {
             log.error("此账号已停用: {}", entity.getUserName());
@@ -172,83 +180,83 @@ public class LoginController {
         return Result.success(o != null);
     }
 
-    @ApiOperation(value = "test-登录", notes = "密码没有经过前端加密")
-    @PostMapping(value = "test/admin/login")
-    public Result testLogin(@Valid @RequestBody LoginDto param)  {
-
-        String from = param.getFrom();
-        String[] checkFrom = {"cms","web"};
-        List<String> fromList = Arrays.asList(checkFrom);
-        if (!fromList.contains(from)) {
-            log.error("from: {}", from);
-            return Result.failure("非法用户");
-        }
-
-        // 1.获取用户
-        SysUserEntity entity = userService.findByUserName(param.getUserName());
-        if (entity == null){
-            log.error("用户不存在");
-            return Result.failure("用户不存在");
-        }
-
-        Long userId = entity.getId();
-        Object role = getRoleKey(userId);
-        log.info("role: {}", role);
-
-        if ("cms".equals(from) && ((Set)role).contains("sys_visitor")) {
-            log.error("游客不能登录管理后台");
-            return Result.failure("非法用户");
-        }
-
-        // 验证密码,解密出来是明文密码,在跟输入密码比较
-        boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
-        if (!decryptName) {
-            log.error("密码错误");
-            return Result.failure("密码错误");
-        }
-
-        // 检查账号是否启用
-        if (entity.getIsEnabled() != 1) {
-            log.error("此账号已停用: {}", entity.getUserName());
-            return Result.failure("此账号已停用");
-        }
-
-        // 创建新token
-        HashMap<String, Object> tokenMap = new HashMap<>();
-        tokenMap.put("userName", entity.getUserName());
-        tokenMap.put("id", userId);
-        tokenMap.put("role", role);
-
-        // 记录登录时长
-        Long startTime = System.currentTimeMillis();
-        tokenMap.put("startTime", startTime);
-        log.info("登录起始时间戳:{}", startTime);
-
-        String token = JwtUtil.createJWT(TOKEN_EXPIRE, tokenMap);
-
-
-        HashMap<String, Object> result = new HashMap<>();
-        result.put("user", entity);
-        result.put("token", token);
-        // 角色控制系统管理(sys_admin)、内容管理
-        result.put("role", role);
-
-
-        // 保存操作日志
-        saveLog(userId);
-
-        // 检查更新军衔等级
-        userService.updateLevel(userId);
-
-
-        // 更新到 redis, 有效期24h, 旧token无效, 做单用户登录 86400s-> 24H
-        redisUtil.set(configConstant.redisPrefix + token, token, 86400);
-
-
-        return Result.success(result);
-
-    }
-
+//    @ApiOperation(value = "test-登录", notes = "密码没有经过前端加密")
+//    @PostMapping(value = "test/admin/login")
+//    public Result testLogin(@Valid @RequestBody LoginDto param)  {
+//
+//        String from = param.getFrom();
+//        String[] checkFrom = {"cms","web"};
+//        List<String> fromList = Arrays.asList(checkFrom);
+//        if (!fromList.contains(from)) {
+//            log.error("from: {}", from);
+//            return Result.failure("非法用户");
+//        }
+//
+//        // 1.获取用户
+//        SysUserEntity entity = userService.findByUserName(param.getUserName());
+//        if (entity == null){
+//            log.error("用户不存在");
+//            return Result.failure("用户不存在");
+//        }
+//
+//        Long userId = entity.getId();
+//        Object role = getRoleKey(userId);
+//        log.info("role: {}", role);
+//
+//        if ("cms".equals(from) && ((Set)role).contains("sys_visitor")) {
+//            log.error("游客不能登录管理后台");
+//            return Result.failure("非法用户");
+//        }
+//
+//        // 验证密码,解密出来是明文密码,在跟输入密码比较
+//        boolean decryptName = PasswordUtils.decrypt(entity.getPassword(), param.getPassword(), PasswordUtils.getStaticSalt());
+//        if (!decryptName) {
+//            log.error("密码错误");
+//            return Result.failure("密码错误");
+//        }
+//
+//        // 检查账号是否启用
+//        if (entity.getIsEnabled() != 1) {
+//            log.error("此账号已停用: {}", entity.getUserName());
+//            return Result.failure("此账号已停用");
+//        }
+//
+//        // 创建新token
+//        HashMap<String, Object> tokenMap = new HashMap<>();
+//        tokenMap.put("userName", entity.getUserName());
+//        tokenMap.put("id", userId);
+//        tokenMap.put("role", role);
+//
+//        // 记录登录时长
+//        Long startTime = System.currentTimeMillis();
+//        tokenMap.put("startTime", startTime);
+//        log.info("登录起始时间戳:{}", startTime);
+//
+//        String token = JwtUtil.createJWT(TOKEN_EXPIRE, tokenMap);
+//
+//
+//        HashMap<String, Object> result = new HashMap<>();
+//        result.put("user", entity);
+//        result.put("token", token);
+//        // 角色控制系统管理(sys_admin)、内容管理
+//        result.put("role", role);
+//
+//
+//        // 保存操作日志
+//        saveLog(userId);
+//
+//        // 检查更新军衔等级
+//        userService.updateLevel(userId);
+//
+//
+//        // 更新到 redis, 有效期24h, 旧token无效, 做单用户登录 86400s-> 24H
+//        redisUtil.set(configConstant.redisPrefix + token, token, 86400);
+//
+//
+//        return Result.success(result);
+//
+//    }
+//
 
 
 

+ 3 - 1
gis_admin/src/main/java/com/gis/admin/service/impl/IntegralServiceImpl.java

@@ -123,7 +123,9 @@ public class IntegralServiceImpl extends IBaseServiceImpl<IntegralEntity, Long>
 
     @Override
     public Integer totalGrade(Long userId) {
-        return entityMapper.totalGrade(userId);
+        Integer integer = entityMapper.totalGrade(userId);
+        integer = integer == null? 0:integer;
+        return integer;
     }
 
 }

+ 59 - 1
gis_common/src/main/java/com/gis/common/base/exception/BaseRuntimeException.java

@@ -1,5 +1,10 @@
 package com.gis.common.base.exception;
 
+import cn.hutool.core.util.StrUtil;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
+
 public class BaseRuntimeException extends RuntimeException{
 
     private static final long serialVersionUID = -1518945670203783450L;
@@ -8,15 +13,23 @@ public class BaseRuntimeException extends RuntimeException{
 
     public BaseRuntimeException(String msg){
         super(msg);
+        this.code = -1;
         this.msg = msg;
     }
 
+    /**
+     *
+     * @param code 允许为null
+     * @param msg
+     */
     public BaseRuntimeException(Integer code, String msg){
         super(msg);
-        this.code = code;
+        this.code = code == null? -1 : code;
         this.msg = msg;
     }
 
+
+
     public Integer getCode() {
         return code;
     }
@@ -32,4 +45,49 @@ public class BaseRuntimeException extends RuntimeException{
     public void setMsg(String msg) {
         this.msg = msg;
     }
+
+
+    public static void isNull(Object obj, Integer code, String msg){
+        if (obj == null){
+            getExc(code, msg);
+        }
+    }
+
+    public static void isBlank(Object obj, Integer code, String msg){
+        if (obj == null){
+            getExc(code, msg);
+        }
+
+        if (obj instanceof String && StrUtil.isBlank(obj.toString())){
+            getExc(code, msg);
+        }
+
+    }
+
+
+
+    /**
+     *
+     * @param obj 存在抛异常
+     * @param code 允许为null
+     * @param msg
+     */
+    public static void isTrue(boolean obj, Integer code, String msg){
+        if (obj){
+            getExc(code, msg);
+        }
+    }
+
+    public static void  getExc(Integer code, String msg){
+        throw new BaseRuntimeException(code, msg);
+    }
+
+
+
+    public static void isEmpty(List obj, Integer code, String msg){
+        if (CollectionUtils.isEmpty(obj)){
+            getExc(code, msg);
+        }
+    }
+
 }