From 790c2ba4a0b46edf191e3bac84931f796bd42b8f Mon Sep 17 00:00:00 2001
From: zhangf <1603559716@qq.com>
Date: 星期三, 24 七月 2024 09:02:49 +0800
Subject: [PATCH] 三方对接接口优化

---
 ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/ThreeInstitutionService.java |   85 ++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 85 insertions(+), 0 deletions(-)

diff --git a/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/ThreeInstitutionService.java b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/ThreeInstitutionService.java
new file mode 100644
index 0000000..f2b4374
--- /dev/null
+++ b/ruoyi-framework/src/main/java/com/ruoyi/framework/web/service/ThreeInstitutionService.java
@@ -0,0 +1,85 @@
+package com.ruoyi.framework.web.service;
+
+import com.alibaba.fastjson2.JSONObject;
+
+import com.ruoyi.common.constant.ResultConstants;
+import com.ruoyi.common.core.domain.model.InstitutionUser;
+import com.ruoyi.common.exception.BusinessException;
+import com.ruoyi.common.signature.AESUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.ip.IpUtils;
+import com.ruoyi.framework.web.domain.threeAccess.req.AccessReqDTO;
+import com.ruoyi.framework.web.domain.threeAccess.resp.AccessRespDTO;
+import com.ruoyi.system.domain.InstitutionalManager;
+import com.ruoyi.system.service.InstitutionalManagerService;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import java.time.LocalDateTime;
+
+@Component
+public class ThreeInstitutionService{
+    @Autowired
+    private InstitutionalManagerService managerService;
+    @Autowired
+    private TokenService tokenService;
+
+
+    public String getToken(JSONObject jsonObject){
+         String data = jsonObject.getString("data");
+        if(StringUtils.isEmpty(data)){
+            throw new BusinessException(this.getClass(), ResultConstants.THREE_INSTITUTION_PARAMM_NULL);
+        }
+        //解密
+        String decrypt = "";
+        try {
+            decrypt = AESUtils.decrypt(data);
+        }catch (Exception e){
+            throw new BusinessException(this.getClass(), ResultConstants.THREE_INSTITUTION_PARAMM_NULL);
+        }
+        //反序列化
+        AccessReqDTO accessReqDTO = JSONObject.parseObject(decrypt, AccessReqDTO.class);
+        if(accessReqDTO==null){
+            throw new BusinessException(this.getClass(), ResultConstants.THREE_INSTITUTION_PARAMM_ERROR);
+        }
+        if (StringUtils.isEmpty(accessReqDTO.getAccessKey())){
+            throw new BusinessException(this.getClass(), ResultConstants.ACCESSkEY_ERROR_NULL);
+        }
+        if (StringUtils.isEmpty(accessReqDTO.getSecretKey())){
+            throw new BusinessException(this.getClass(), ResultConstants.SECRETKEY_ERROR_NULL);
+        }
+        InstitutionalManager institutional = managerService.getInstitutionalByAccessKey(accessReqDTO.getAccessKey());
+        if(institutional==null){
+            throw new BusinessException(this.getClass(), ResultConstants.ACCESSkEY_INVALID);
+        }
+        //简单校验
+        if(!institutional.getSecretKey().equals(accessReqDTO.getSecretKey())){
+            throw new BusinessException(this.getClass(), ResultConstants.INSTITUTION_AUTHENTICATION);
+        }
+        //封装数据
+        InstitutionUser institutionUser = new InstitutionUser();
+        BeanUtils.copyProperties(institutional,institutionUser);
+        String threeInToken = tokenService.createThreeInToken(institutionUser);
+        //封装
+        AccessRespDTO accessRespDTO = new AccessRespDTO();
+        accessRespDTO.setExpireTime(institutionUser.getExpireTime());
+        accessRespDTO.setAccessToken(threeInToken);
+        String jsonString = JSONObject.toJSONString(accessRespDTO);
+        //加密
+        String encrypt = AESUtils.encrypt(jsonString);
+
+        //记录访问请求token时间以及地址
+        recordInstitution(institutional.getId());
+        return encrypt;
+    }
+
+    private void recordInstitution(Long institutionId) {
+        InstitutionalManager institutionalManager = new InstitutionalManager();
+        institutionalManager.setId(institutionId);
+        institutionalManager.setAccessIp(IpUtils.getIpAddr());
+        institutionalManager.setAccessTime(LocalDateTime.now());
+        managerService.updateById(institutionalManager);
+    }
+
+}

--
Gitblit v1.9.2