liusuyi
2024-10-23 37aeda6f5a45a220955eafd55bf5154c71c2d705
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
package com.ard.tianyu.config;
 
import com.ard.tianyu.http.TyClient;
import com.ard.tianyu.model.dto.TokenDTO;
import com.ard.tianyu.model.receiver.AccessTokenReceiver;
import com.ard.tianyu.model.receiver.TyHttpCommonReceiver;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.DigestUtils;
 
import javax.annotation.Resource;
import java.util.Date;
 
/**
 * TianyuConfiguration
 *
 * @author liusuyi
 * @date 2024年10月21日15:16
 **/
@Configuration
@Slf4j
@Data
@ConfigurationProperties(prefix = "tianyu")
public class TyConfiguration {
 
    private String endpoint;
 
    private String accesskeyid;
 
    private String accesskeysecret;
 
    private String tcpserver;
 
    private Integer tcpport;
 
    private long timestamp;
 
    public long getTimeStamp() {
        timestamp = new Date().getTime();
        return timestamp;
    }
 
    private String encryptstr;
 
    public String getEncryptStr() {
        String sb = new StringBuilder()
                .append(accesskeyid)
                .append(accesskeysecret)
                .append(getTimeStamp())
                .toString();
        encryptstr = DigestUtils.md5DigestAsHex(sb.getBytes());
        return encryptstr;
    }
 
    public String getIpPort() {
        return tcpserver + ":" + tcpport;
    }
 
    @Resource
    private TyClient tyClient;
 
    private AccessTokenReceiver accessToken;
 
    public AccessTokenReceiver getAccessToken() {
        if (accessToken != null)
            return accessToken;
        // 准备请求的json
        TokenDTO gtd = TokenDTO.builder()
                .accessKeyId(accesskeyid)
                .encryptStr(getEncryptStr())
                .timeStamp(timestamp)
                .build();
        try {
            TyHttpCommonReceiver<AccessTokenReceiver> getTokenRes = tyClient.getToken(gtd);
            accessToken = getTokenRes.getContent();
        } catch (Exception e) {
            log.error("获取token失败:{}", e.getMessage());
        }
        //{"state":0,"content":{"companyId":4127,"accessToken":"sOONT0MMVdoKcMBibb40xxgrUrq0Hw9RFiajic3KOzoNtk"}}
        return accessToken;
    }
}