zhoutg преди 5 години
родител
ревизия
9ad7411de4

+ 11 - 0
common/src/main/java/com/diagbot/util/RSAEncrypt.java

@@ -114,4 +114,15 @@ public class RSAEncrypt {
         return decrypt(str, privateKey);
     }
 
+
+    /**
+     * RSA公钥加密
+     *
+     * @param str 需要加密的字符串
+     * @return 密文
+     * @throws Exception 加密过程中的异常信息
+     */
+    public static String encrypt( String str) throws Exception{
+        return encrypt(str, pubKey);
+    }
 }

+ 16 - 0
gateway-service/src/main/java/com/diagbot/client/TranServiceClient.java

@@ -3,6 +3,7 @@ package com.diagbot.client;
 import com.diagbot.client.hystrix.TranServiceHystrix;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.dto.SysSetInfoDTO;
+import com.diagbot.vo.HospitalListVO;
 import com.diagbot.vo.HospitalSetVO;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -24,4 +25,19 @@ public interface TranServiceClient {
      */
     @PostMapping("/sysSet/getSysSetInfoDatas")
     RespDTO<List<SysSetInfoDTO>> getSysSetInfoDatas(@Valid @RequestBody HospitalSetVO hospitalSetVO);
+
+    /**
+     * @param hospitalListVO
+     * @return
+     */
+    @PostMapping("/sysSet/getSysSetInfoDatasByList")
+    RespDTO<List<SysSetInfoDTO>> getSysSetInfoDataByList(@RequestBody HospitalListVO hospitalListVO);
+
+    /**
+     * 更新licence剩余天数
+     *
+     * @return
+     */
+    @PostMapping("/sysSet/refreshLicence")
+    RespDTO<List<SysSetInfoDTO>> refreshLicence();
 }

+ 14 - 2
gateway-service/src/main/java/com/diagbot/client/hystrix/TranServiceHystrix.java

@@ -3,6 +3,7 @@ package com.diagbot.client.hystrix;
 import com.diagbot.client.TranServiceClient;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.dto.SysSetInfoDTO;
+import com.diagbot.vo.HospitalListVO;
 import com.diagbot.vo.HospitalSetVO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
@@ -24,6 +25,17 @@ public class TranServiceHystrix implements TranServiceClient {
         log.error("【hystrix】调用{}异常", "getSysSetInfoDatas");
         return null;
     }
-    
-    
+
+    @Override
+    public RespDTO<List<SysSetInfoDTO>> getSysSetInfoDataByList(HospitalListVO hospitalListVO) {
+        log.error("【hystrix】调用{}异常", "getSysSetInfoDataByList");
+        return null;
+    }
+
+    @Override
+    public RespDTO<List<SysSetInfoDTO>> refreshLicence() {
+        log.error("【hystrix】调用{}异常", "refreshLicence");
+        return null;
+    }
+
 }

+ 81 - 21
gateway-service/src/main/java/com/diagbot/filter/GlobalGatewayFilter.java

@@ -17,7 +17,7 @@ import com.diagbot.util.EnDecodeUtil;
 import com.diagbot.util.GsonUtil;
 import com.diagbot.util.RSAEncrypt;
 import com.diagbot.util.StringUtil;
-import com.diagbot.vo.HospitalSetVO;
+import com.diagbot.vo.HospitalListVO;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -35,6 +35,7 @@ import reactor.core.publisher.Mono;
 
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -53,10 +54,13 @@ import java.util.Map;
 public class GlobalGatewayFilter implements GlobalFilter {
 
     private static final String GATE_WAY_PREFIX = "/api";
-    private static final String LICENCE = "licence_day";
+    private static final String LICENCE_EXPIRE = "licence_expire";
+    private static final String LICENCE_REMAIN = "licence_remain";
+    private static final String LICENCE_START = "licence_start";
     private static Boolean IS_GENERATE = false;
     private static Map<String, Long> SERVICE_FILTER = new HashMap<>();
     private static Map<String, String> licenceMap = new HashMap<>(); // licence对象
+    private static String START_LIC = "1"; //是否启用licence
     private static boolean initLicence = false; // 是否初始化licence
 
 
@@ -157,8 +161,11 @@ public class GlobalGatewayFilter implements GlobalFilter {
         }
 
         // 进行licence校验
-        String hospitalCode = request.getHeaders().getFirst("hospitalCode");
-        verifyLicence(hospitalCode, serviceName);
+        initLicenceMap();
+        if (START_LIC.equals(licenceMap.get(LICENCE_START))) {
+            String hospitalCode = request.getHeaders().getFirst("hospitalCode");
+            verifyLicence(hospitalCode, serviceName);
+        }
 
         ServerHttpRequest.Builder builder = serverWebExchange.getRequest().mutate();
         builder.header("Authorization", "Authorization Bearer token");
@@ -203,31 +210,39 @@ public class GlobalGatewayFilter implements GlobalFilter {
      * @param serviceName 服务
      */
     public void verifyLicence(String hospitalCode, String serviceName) {
-        // 初始化licence
-        if (!initLicence) {
-            HospitalSetVO hospitalSetVO = new HospitalSetVO();
-            hospitalSetVO.setCode(LICENCE);
-            RespDTO<List<SysSetInfoDTO>> respDTO = tranServiceClient.getSysSetInfoDatas(hospitalSetVO);
-            if (respDTO != null) {
-                for (SysSetInfoDTO setInfoDTO : respDTO.data) {
-                    String serName = SysTypeEnum.getName(setInfoDTO.getSysType());
-                    licenceMap.put(setInfoDTO.getHospitalCode() + serName.substring(0, serName.indexOf("-")), setInfoDTO.getValue());
-                }
+        // 校验截止日期
+        verifyLicenceExpire(hospitalCode, serviceName);
+        // 校验剩余天数
+        verifyLicenceRemain(hospitalCode, serviceName);
+    }
+
+
+    public void refreshLicenceMap() {
+        tranServiceClient.refreshLicence();
+        for (String key : licenceMap.keySet()) {
+            if (key.indexOf(LICENCE_REMAIN) > -1) {
+
             }
         }
+    }
+
 
-        // 校验
-        String value = licenceMap.get(hospitalCode + serviceName);
-        if (StringUtil.isNotEmpty(value)) {
+    /**
+     * 校验licence到期
+     *
+     * @param hospitalCode 医院编码
+     * @param serviceName 服务名称
+     */
+    public void verifyLicenceExpire(String hospitalCode, String serviceName) {
+        String licenceExpire = licenceMap.get(hospitalCode + LICENCE_EXPIRE + serviceName);
+        if (StringUtil.isNotEmpty(licenceExpire)) {
             String licence = "";
             try {
-                licence = RSAEncrypt.decrypt(value);
+                licence = RSAEncrypt.decrypt(licenceExpire);
             } catch (Exception e) { // licence 被篡改
                 throw new CommonException(CommonErrorCode.LICENCE_ERROR);
             }
             String nowStr = DateUtil.format(new Date(), "YYYY-MM-dd HH:mm:ss");
-            System.out.println(nowStr);
-            System.out.println(licence);
             if (licence.compareTo(nowStr) < 0) { // licence 已到期
                 throw new CommonException(CommonErrorCode.LICENCE_EXPIRE);
             }
@@ -235,7 +250,52 @@ public class GlobalGatewayFilter implements GlobalFilter {
     }
 
 
-    public void refreshLicence() {
+    /**
+     * 校验licence剩余天数
+     *
+     * @param hospitalCode 医院编码
+     * @param serviceName 服务名称
+     */
+    public void verifyLicenceRemain(String hospitalCode, String serviceName) {
+        String licenceRemain = licenceMap.get(hospitalCode + LICENCE_REMAIN + serviceName);
+        if (StringUtil.isNotEmpty(licenceRemain)) {
+            int day = 0;
+            try {
+                day = Integer.parseInt(RSAEncrypt.decrypt(licenceRemain));
+            } catch (Exception e) { // licence 被篡改
+                throw new CommonException(CommonErrorCode.LICENCE_ERROR);
+            }
+            if (day < 0) { // licence 已到期
+                throw new CommonException(CommonErrorCode.LICENCE_EXPIRE);
+            }
+        }
+    }
+
 
+    /**
+     * 初始化licenceMap数据
+     */
+    public void initLicenceMap() {
+        // 初始化licence
+        licenceMap.clear();
+        if (!initLicence) {
+            HospitalListVO hospitalListVO = new HospitalListVO();
+            List<String> codeList = new ArrayList<>();
+            codeList.add(LICENCE_EXPIRE);
+            codeList.add(LICENCE_REMAIN);
+            codeList.add(LICENCE_START);
+            hospitalListVO.setCodeList(codeList);
+            RespDTO<List<SysSetInfoDTO>> respDTO = tranServiceClient.getSysSetInfoDataByList(hospitalListVO);
+            if (respDTO != null) {
+                for (SysSetInfoDTO setInfoDTO : respDTO.data) {
+                    String serName = SysTypeEnum.getName(setInfoDTO.getSysType());
+                    if (LICENCE_START.equals(setInfoDTO.getCode())) {
+                        licenceMap.put(setInfoDTO.getCode(), setInfoDTO.getValue());
+                    } else {
+                        licenceMap.put(setInfoDTO.getHospitalCode() + setInfoDTO.getCode() + serName.substring(0, serName.indexOf("-")), setInfoDTO.getValue());
+                    }
+                }
+            }
+        }
     }
 }

+ 34 - 0
gateway-service/src/main/java/com/diagbot/vo/HospitalListVO.java

@@ -0,0 +1,34 @@
+package com.diagbot.vo;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import java.util.List;
+
+/**
+ * 
+ * @author ztg
+ * @Description: TODO
+ * @date 2019年6月12日 下午3:30:26
+ */
+@Setter
+@Getter
+public class HospitalListVO {
+
+	private List<String> hospitalCodeList;
+	
+	/**
+     * 访问的系统类型 1:user-service,2:diagbotman-service,3:uaa-service,4:log-service,5:bi-service,6:knowledge-service,7:feedback-service,8:icss-web
+     */
+    private List<Integer> sysTypeList;
+
+    /**
+     * 方案编号
+     */
+    private List<Integer> planList;
+
+    /**
+     * 配置编码
+     */
+    private List<String> codeList;
+}

+ 37 - 4
tran-service/src/main/java/com/diagbot/facade/SysSetFacade.java

@@ -1,14 +1,20 @@
 package com.diagbot.facade;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.diagbot.dto.SysSetInfoDTO;
 import com.diagbot.entity.SysSet;
+import com.diagbot.exception.CommonErrorCode;
+import com.diagbot.exception.CommonException;
 import com.diagbot.service.impl.SysSetServiceImpl;
 import com.diagbot.util.BeanUtil;
+import com.diagbot.util.DateUtil;
 import com.diagbot.util.IntegerUtil;
 import com.diagbot.util.ListUtil;
+import com.diagbot.util.RSAEncrypt;
 import com.diagbot.vo.HospitalListVO;
 import com.diagbot.vo.HospitalSetVO;
+import org.bouncycastle.jcajce.provider.asymmetric.RSA;
 import org.springframework.stereotype.Component;
 
 import java.util.List;
@@ -43,12 +49,39 @@ public class SysSetFacade extends SysSetServiceImpl {
      */
     public List<SysSetInfoDTO> getSysSetInfoDataByList(HospitalListVO hospitalListVO) {
         List<SysSet> sysSetData = this.list(new QueryWrapper<SysSet>()
-                .in(ListUtil.isNotEmpty( hospitalListVO.getHospitalCode()), "hospital_code", hospitalListVO.getHospitalCode())
-                .in(ListUtil.isNotEmpty( hospitalListVO.getSysType()), "sys_type", hospitalListVO.getSysType())
-                .in(ListUtil.isNotEmpty( hospitalListVO.getPlan()), "plan", hospitalListVO.getPlan())
-                .in(ListUtil.isNotEmpty( hospitalListVO.getCode()), "code", hospitalListVO.getCode())
+                .in(ListUtil.isNotEmpty( hospitalListVO.getHospitalCodeList()),
+                        "hospital_code", hospitalListVO.getHospitalCodeList())
+                .in(ListUtil.isNotEmpty( hospitalListVO.getSysTypeList()),
+                        "sys_type", hospitalListVO.getSysTypeList())
+                .in(ListUtil.isNotEmpty( hospitalListVO.getPlanList()),
+                        "plan", hospitalListVO.getPlanList())
+                .in(ListUtil.isNotEmpty( hospitalListVO.getCodeList()),
+                        "code", hospitalListVO.getCodeList())
         );
         List<SysSetInfoDTO> data = BeanUtil.listCopyTo(sysSetData, SysSetInfoDTO.class);
         return data;
     }
+
+
+    /**
+     * 更新licence失败
+     * @return
+     */
+    public Boolean refreshLicence() {
+        try {
+            List<SysSet> sysSetList = this.list(new QueryWrapper<SysSet>()
+                .eq("code", "licence_remain")
+            );
+            for (SysSet sysSet : sysSetList) {
+                String val = RSAEncrypt.decrypt(sysSet.getValue());
+                sysSet.setValue(RSAEncrypt.encrypt(String.valueOf(Integer.parseInt(val) - 1)));
+                sysSet.setGmtModified(DateUtil.now());
+            }
+            this.updateBatchById(sysSetList);
+        } catch (Exception e) {
+            e.printStackTrace();
+            throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "更新licence失败");
+        }
+        return true;
+    }
 }

+ 4 - 4
tran-service/src/main/java/com/diagbot/vo/HospitalListVO.java

@@ -15,20 +15,20 @@ import java.util.List;
 @Getter
 public class HospitalListVO {
 
-	private List<String> hospitalCode;
+	private List<String> hospitalCodeList;
 	
 	/**
      * 访问的系统类型 1:user-service,2:diagbotman-service,3:uaa-service,4:log-service,5:bi-service,6:knowledge-service,7:feedback-service,8:icss-web
      */
-    private List<Integer> sysType;
+    private List<Integer> sysTypeList;
 
     /**
      * 方案编号
      */
-    private List<Integer> plan;
+    private List<Integer> planList;
 
     /**
      * 配置编码
      */
-    private List<String> code;
+    private List<String> codeList;
 }

+ 18 - 3
tran-service/src/main/java/com/diagbot/web/SysSetController.java

@@ -4,6 +4,7 @@ import com.diagbot.annotation.SysLogger;
 import com.diagbot.dto.RespDTO;
 import com.diagbot.dto.SysSetInfoDTO;
 import com.diagbot.facade.SysSetFacade;
+import com.diagbot.vo.HospitalListVO;
 import com.diagbot.vo.HospitalSetVO;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -42,11 +43,25 @@ public class SysSetController {
     }
 
 
-    @ApiOperation(value = "根据医院编码获取配置信息[by:wangfeng]", notes = "hospitalCode :医院code  必填<br> ")
+    @ApiOperation(value = "获取配置信息[by:zhoutg]",
+            notes = "hospitalCodeList:医院code列表<br>" +
+                    "sysTypeList:系统类型列表<br>" +
+                    "planList:方案编号列表<br>" +
+                    "codeList:配置编码列表<br>")
     @PostMapping("/getSysSetInfoDatasByList")
     @SysLogger("getSysSetInfoDatasByList")
-    public RespDTO<List<SysSetInfoDTO>> getSysSetInfoDataByList(@Valid @RequestBody HospitalSetVO hospitalSetVO) {
-        List<SysSetInfoDTO> data = sysSetFacade.getSysSetInfoData(hospitalSetVO);
+    public RespDTO<List<SysSetInfoDTO>> getSysSetInfoDataByList(@RequestBody HospitalListVO hospitalListVO) {
+        List<SysSetInfoDTO> data = sysSetFacade.getSysSetInfoDataByList(hospitalListVO);
+        return RespDTO.onSuc(null);
+    }
+
+
+    @ApiOperation(value = "刷新licence,将剩余天数减1[by:zhoutg]",
+            notes = "")
+    @PostMapping("/refreshLicence")
+    @SysLogger("refreshLicence")
+    public RespDTO<Boolean> refreshLicence() {
+        Boolean data = sysSetFacade.refreshLicence();
         return RespDTO.onSuc(data);
     }
 }