package com.ruoyi.system.utils.util; import com.alibaba.fastjson2.JSONObject; import com.ruoyi.common.constant.Constants; import com.ruoyi.common.core.domain.R; import com.ruoyi.common.core.exception.ServiceException; import com.ruoyi.common.core.redis.RedisCache; import com.ruoyi.common.utils.http.HttpUtils; import com.ruoyi.system.model.TSysAiConfig; import com.ruoyi.system.service.TSysAiConfigService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.util.concurrent.TimeUnit; @Slf4j @Component public class AIUtil { @Autowired private TSysAiConfigService sysAiConfigService; @Autowired private RedisCache redisCache; /** * 获取企业应用access_token */ public static final String ACCESS_TOKEN_URL = "https://www.ai-tongue.com/backend/auth/invoker/pwd/signin"; /** * 会员单点登录 */ public static final String SSO_URL = "https://www.ai-tongue.com/h5/sso"; /** * 初始化会员信息 */ public static final String INIT_USER_URL = "https://www.ai-tongue.com/backend/check/i/secret/thirdUser/init"; /** * 获取企业应用access_token * @return */ public String getAccessToken() { String accessToken = redisCache.getCacheObject(Constants.H5AI_ACCESS_TOKEN); if(StringUtils.hasLength(accessToken)){ return accessToken; } TSysAiConfig sysAiConfig = sysAiConfigService.getById(1); String result = HttpUtils.sendPost(ACCESS_TOKEN_URL, "devid="+sysAiConfig.getDevId()+"&devsecret="+sysAiConfig.getDevSecret()); log.info("获取access_token:{}", result); JSONObject object = JSONObject.parseObject(result); if(object.getInteger("code") != 0){ throw new ServiceException(object.getString("msg")); } JSONObject data = object.getJSONObject("data"); accessToken = data.getString("access_token"); Integer expiresIn = data.getInteger("expires_in"); redisCache.setCacheObject(Constants.H5AI_ACCESS_TOKEN, accessToken, expiresIn-20, TimeUnit.SECONDS); return accessToken; } /** * 会员单点登录 * @return */ public R ssoLogin() { String accessToken = redisCache.getCacheObject(Constants.H5AI_ACCESS_TOKEN); if(!StringUtils.hasLength(accessToken)){ accessToken = getAccessToken(); } TSysAiConfig sysAiConfig = sysAiConfigService.getById(1); String result = HttpUtils.sendGet(SSO_URL, "access_token="+accessToken +"&encryptedThirdId="+sysAiConfig.getRsaPublicKey() +"&signEncryptedThirdId="+sysAiConfig.getDevRsaPublicKey() +"&capture=all&diseaseCode=C00.D00"); log.info("会员单点登录结果:{}", result); JSONObject object = JSONObject.parseObject(result); if(object.getInteger("code") != 0){ throw new ServiceException(object.getString("msg")); } return R.ok(); } /** * 会员创建接口 * @param nickName * @param sex * @param birthday * @return */ public R initUser(String userId,String nickName, Integer sex, String birthday) { String accessToken = redisCache.getCacheObject(Constants.H5AI_ACCESS_TOKEN); if(!StringUtils.hasLength(accessToken)){ accessToken = getAccessToken(); } TSysAiConfig sysAiConfig = sysAiConfigService.getById(1); String result = HttpUtils.sendPost(INIT_USER_URL, "devId="+sysAiConfig.getDevId() +"&thirdId="+userId +"&signEncryptedThirdId="+sysAiConfig.getDevRsaPublicKey() +"&thirdName="+nickName +"&sex="+sex +"&birthday="+birthday,accessToken); log.info("会员创建初始化结果:{}", result); JSONObject object = JSONObject.parseObject(result); if(object.getInteger("code") != 0){ throw new ServiceException(object.getString("msg")); } return R.ok(); } }