xuhy
18 小时以前 14fcda5bc8f77d6013ff057b10b19f3529a938bd
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
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;
    }
 
}