package com.ruoyi.system.utils.util;
|
|
import com.alibaba.fastjson2.JSONObject;
|
import com.ruoyi.common.constant.Constants;
|
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 H5AIUtil {
|
|
@Autowired
|
private TSysAiConfigService sysAiConfigService;
|
@Autowired
|
private RedisCache redisCache;
|
|
public static final String H5_AI_URL = "https://www.ai-tongue.com/backend/auth/invoker/pwd/signin";
|
|
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(H5_AI_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;
|
}
|
|
}
|