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); } }