zhangf
2024-06-24 21362fd048558832cdcaca8ee957d2d7aa753be2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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);
    }
 
}