瀏覽代碼

优化代码和添加权限 aop

xiaochan 3 年之前
父節點
當前提交
e6654c50e0

+ 10 - 8
src/main/java/thyyxxk/webserver/aop/aspect/NeedYbCodeAop.java

@@ -1,15 +1,14 @@
 package thyyxxk.webserver.aop.aspect;
 
 import lombok.extern.slf4j.Slf4j;
-import org.aspectj.lang.JoinPoint;
 import org.aspectj.lang.annotation.Aspect;
 import org.aspectj.lang.annotation.Before;
 import org.aspectj.lang.annotation.Pointcut;
 import org.springframework.stereotype.Component;
 import thyyxxk.webserver.config.exception.BizException;
 import thyyxxk.webserver.config.exception.ExceptionEnum;
-import thyyxxk.webserver.dao.his.LoginDao;
 import thyyxxk.webserver.entity.login.UserInfo;
+import thyyxxk.webserver.service.PublicServer;
 import thyyxxk.webserver.service.redislike.RedisLikeService;
 import thyyxxk.webserver.utils.StringUtil;
 import thyyxxk.webserver.utils.TokenUtil;
@@ -28,21 +27,24 @@ public class NeedYbCodeAop {
     }
 
     @Pointcut("@annotation(thyyxxk.webserver.aop.interfaces.NeedYbCode)")
-    public void NeedYbCode() {
+    public void needYbCode() {
     }
 
 
-    @Before("NeedYbCode()")
-    public void doBefore(JoinPoint joinPoint) {
-        String token = TokenUtil.getTokenUserId();
-        UserInfo userInfo = redisLikeService.getUserInfo(token);
+    @Before("needYbCode()")
+    public void doBefore() {
+        UserInfo userInfo = redisLikeService.getUserInfoByToken();
         if (userInfo == null) {
             throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有查询到该员工信息");
         }
-        if (StringUtil.isBlank(userInfo.getYbCode())) {
+        if (getYbCode(userInfo)) {
             throw new BizException(ExceptionEnum.LOGICAL_HTML_ERROR, String.format("您没有医保编码无法进行操作," +
                     "请联系<span style='color:red'>医保科</span>,为您添加医保编码,您的工号为:<span style='color:red'>【%s】</span>", userInfo.getCodeRs()));
         }
     }
 
+    public Boolean getYbCode(UserInfo userInfo) {
+        return StringUtil.isBlank(userInfo.getYbCode());
+    }
+
 }

+ 40 - 0
src/main/java/thyyxxk/webserver/aop/aspect/NoDeedRule.java

@@ -0,0 +1,40 @@
+package thyyxxk.webserver.aop.aspect;
+
+import lombok.extern.slf4j.Slf4j;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Before;
+import org.aspectj.lang.annotation.Pointcut;
+import org.springframework.stereotype.Component;
+import thyyxxk.webserver.aop.interfaces.NoNeedRule;
+import thyyxxk.webserver.config.exception.BizException;
+import thyyxxk.webserver.config.exception.ExceptionEnum;
+import thyyxxk.webserver.service.PublicServer;
+
+
+/**
+ * @author 肖蟾
+ */
+@Aspect
+@Component
+@Slf4j
+public class NoDeedRule {
+
+    private final PublicServer publicServer;
+
+    public NoDeedRule(PublicServer publicServer) {
+        this.publicServer = publicServer;
+    }
+
+    @Pointcut("@annotation(thyyxxk.webserver.aop.interfaces.NoNeedRule)")
+    public void noDeedRule() {
+    }
+
+    @Before("noDeedRule() && @annotation(noNeedRule)")
+    public void before(NoNeedRule noNeedRule) {
+        if (publicServer.needRule(noNeedRule.value())) {
+            return;
+        }
+        throw new BizException(ExceptionEnum.LOGICAL_ERROR, "您没有权限。");
+    }
+
+}

+ 16 - 0
src/main/java/thyyxxk/webserver/aop/interfaces/NoNeedRule.java

@@ -0,0 +1,16 @@
+package thyyxxk.webserver.aop.interfaces;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author 肖蟾
+ * 权限注解
+ */
+@Target({ElementType.METHOD, ElementType.TYPE})
+@Retention(RetentionPolicy.RUNTIME)
+public @interface NoNeedRule {
+    int[] value() default {};
+}

+ 4 - 0
src/main/java/thyyxxk/webserver/controller/inpatient/XiangMuLuRuController.java

@@ -6,12 +6,16 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import thyyxxk.webserver.aop.interfaces.NeedYbCode;
+import thyyxxk.webserver.aop.interfaces.NoNeedRule;
 import thyyxxk.webserver.config.auth.PassToken;
+import thyyxxk.webserver.config.exception.BizException;
+import thyyxxk.webserver.config.exception.ExceptionEnum;
 import thyyxxk.webserver.entity.ResultVo;
 import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
 import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
 import thyyxxk.webserver.entity.inpatient.ZyActpatient;
 import thyyxxk.webserver.service.inpatient.XiangMuLuRuService;
+import thyyxxk.webserver.utils.ResultVoUtil;
 
 import javax.servlet.http.HttpServletResponse;
 import java.util.List;

+ 6 - 2
src/main/java/thyyxxk/webserver/service/LoginService.java

@@ -10,6 +10,7 @@ import thyyxxk.webserver.entity.dictionary.CodeName;
 import thyyxxk.webserver.entity.login.UserInfo;
 import thyyxxk.webserver.entity.login.VueMenu;
 import thyyxxk.webserver.entity.settings.permissions.MenuItem;
+import thyyxxk.webserver.service.redislike.RedisLikeService;
 import thyyxxk.webserver.utils.*;
 
 import java.util.*;
@@ -22,11 +23,13 @@ import java.util.*;
 public class LoginService {
     private final LoginDao dao;
     private final TokenService tokenService;
+    private final RedisLikeService redisLikeService;
 
     @Autowired
-    public LoginService(LoginDao dao, TokenService tokenService) {
+    public LoginService(LoginDao dao, TokenService tokenService, RedisLikeService redisLikeService) {
         this.dao = dao;
         this.tokenService = tokenService;
+        this.redisLikeService = redisLikeService;
     }
 
     public ResultVo<UserInfo> login(UserInfo userInfo) {
@@ -47,6 +50,7 @@ public class LoginService {
         tempUserInfo.setSid(makeSid(tempUserInfo.getCode(), token, userInfo.getSid()));
         tempUserInfo.setRoles(dao.getUserRoles(tempUserInfo.getCode()));
         tempUserInfo.setDeptName(dao.getDeptName(tempUserInfo.getDeptCode()));
+        redisLikeService.handleUserLogin(tempUserInfo.getCode());
         return ResultVoUtil.success(tempUserInfo);
     }
 
@@ -92,7 +96,7 @@ public class LoginService {
         String flakeIdSub = "-" + SnowFlakeId.instance().nextId() + "-";
         int random = new Random().nextInt(80);
         String tokenSub = tempToken.substring(random, random + 18);
-        if (StringUtil.notBlank(sid) && sid.equals("fromTriageScreen")) {
+        if (StringUtil.notBlank(sid) && "fromTriageScreen".equals(sid)) {
             tokenSub += "-triageFloorScreen";
         }
         return code + flakeIdSub + tokenSub;

+ 7 - 2
src/main/java/thyyxxk/webserver/service/PublicServer.java

@@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.baomidou.dynamic.datasource.annotation.DS;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.catalina.User;
 import org.apache.commons.collections4.ListUtils;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.scheduling.annotation.Async;
@@ -32,6 +33,7 @@ import thyyxxk.webserver.entity.zhuyuanyisheng.jianyanjiancha.YshYjReq;
 import thyyxxk.webserver.entity.zhuyuanyisheng.shoushu.OpRecord;
 import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.XinZhenYiZhu;
 import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YaoPinXiangMu;
+import thyyxxk.webserver.service.redislike.RedisLikeService;
 import thyyxxk.webserver.utils.*;
 import thyyxxk.webserver.websocket.WebSocketServer;
 
@@ -56,10 +58,13 @@ public class PublicServer {
 
     private final TransferInOfExpensesDao transferInOfExpensesDao;
 
+    private final RedisLikeService redisLikeService;
 
-    public PublicServer(PublicDao dao, TransferInOfExpensesDao transferInOfExpensesDao) {
+
+    public PublicServer(PublicDao dao, TransferInOfExpensesDao transferInOfExpensesDao, RedisLikeService redisLikeService) {
         this.dao = dao;
         this.transferInOfExpensesDao = transferInOfExpensesDao;
+        this.redisLikeService = redisLikeService;
     }
 
     /**
@@ -90,7 +95,7 @@ public class PublicServer {
      * @return 返回角色
      */
     public ResultVo<List<Integer>> getRoleCode() {
-        return ResultVoUtil.success(dao.huoQuJueSe(TokenUtil.getTokenUserId()));
+        return ResultVoUtil.success(redisLikeService.getUserInfoByToken().getRoles());
     }
 
     /**

+ 13 - 3
src/main/java/thyyxxk/webserver/service/redislike/RedisLikeService.java

@@ -5,6 +5,7 @@ import thyyxxk.webserver.dao.his.redislike.RedisLikeDao;
 import thyyxxk.webserver.entity.dictionary.PureCodeName;
 import thyyxxk.webserver.entity.login.UserInfo;
 import thyyxxk.webserver.utils.StringUtil;
+import thyyxxk.webserver.utils.TokenUtil;
 
 import java.util.HashMap;
 import java.util.Map;
@@ -136,7 +137,7 @@ public class RedisLikeService {
         return name;
     }
 
-    public UserInfo getUserInfo(String code) {
+    public UserInfo getUserInfoByCode(String code) {
         if (StringUtil.invalidValue(code)) {
             return null;
         }
@@ -150,15 +151,24 @@ public class RedisLikeService {
         return userInfo;
     }
 
+    public UserInfo getUserInfoByToken() {
+        return getUserInfoByCode(TokenUtil.getTokenUserId());
+    }
+
     public void handleUserRoleChanged(String code) {
-        UserInfo userInfo = getUserInfo(code);
+        UserInfo userInfo = getUserInfoByCode(code);
         userInfo.setRoles(dao.selectUserRoles(code));
         USER_MAP.replace(code, userInfo);
     }
 
     public void handleUserPwdChanged(String code, String pwd) {
-        UserInfo userInfo = getUserInfo(code);
+        UserInfo userInfo = getUserInfoByCode(code);
         userInfo.setPassword(pwd);
         USER_MAP.replace(code, userInfo);
     }
+
+    public void handleUserLogin(String code) {
+        USER_MAP.remove(code);
+        getUserInfoByCode(code);
+    }
 }

+ 6 - 0
src/main/test/thyyxxk/webserver/WebServerApplicationTest.java

@@ -1,6 +1,7 @@
 package thyyxxk.webserver;
 
 import com.alibaba.fastjson.JSON;
+import com.baomidou.mybatisplus.annotation.TableField;
 import lombok.extern.slf4j.Slf4j;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -28,4 +29,9 @@ public class WebServerApplicationTest {
         server.更新过敏源的拼音和五笔();
     }
 
+    @Test
+    public void 测试修改变量() {
+
+    }
+
 }