AccessTokenService.java
4.68 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cn.com.glsx.modules.utils;
import cn.com.glsx.common.Constants;
import cn.com.glsx.common.redis.AccessTokenRedis;
import cn.com.glsx.modules.model.param.AccessToken;
import cn.com.glsx.modules.model.param.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* 获取涂鸦令牌
*
* @author zhouhaibao
* @date 2021/3/4 14:39
*/
@Service
@Slf4j
public class AccessTokenService {
@Autowired
private AccessTokenRedis accessTokenRedis;
@Value("${tuya.tokenFlashUrl}")
private String tokenFlashUrl;
@Value("${tuya.sign_method}")
private String sign_method;
@Value("${tuya.client_id}")
private String client_id;
@Value("${tuya.secret}")
private String secret;
@Value("${tuya.getTokenUrl}")
private String getTokenUrl;
@Value("${tuya.registerUserUrl}")
private String registerUserUrl;
/**
* 获取涂鸦令牌
*
* @return
*/
public String getAccessToken() {
String access_Token = null;
AccessToken accessToken = accessTokenRedis.readCache(Constants.ACCESSTOKEN_REDISKEY);
if (null != accessToken) {
if ((accessToken.getExpire_time() - System.currentTimeMillis()) >= 1000 * 60 * 60 * 2) {
ResponseBody responseBody = sendRequestFlashToken(accessToken.getRefresh_token());
if (responseBody != null && responseBody.isSuccess()) {
String result = responseBody.getResult();
AccessToken token = JSONObject.parseObject(result, AccessToken.class);
token.setExpire_time(System.currentTimeMillis());
accessTokenRedis.add(Constants.ACCESSTOKEN_REDISKEY, token);
access_Token = token.getAccess_token();
} else {
log.info("调用刷新令牌接口异常");
}
} else {
access_Token = accessToken.getAccess_token();
}
} else {
ResponseBody responseBody = sendRequestGetToken();
if (responseBody != null && responseBody.isSuccess()) {
String result = responseBody.getResult();
AccessToken token = JSONObject.parseObject(result, AccessToken.class);
token.setExpire_time(System.currentTimeMillis());
accessTokenRedis.add(Constants.ACCESSTOKEN_REDISKEY, token);
access_Token = token.getAccess_token();
} else {
log.info("调用获取accesstoken接口异常");
}
}
return access_Token;
}
/**
* 发送刷新令牌请求
*
* @return
*/
private ResponseBody sendRequestFlashToken(String flashtoken) {
ResponseBody responseBody = null;
long timeMillis = System.currentTimeMillis();
String sign = TuyaUtils.getLoginSign(timeMillis, client_id, secret);
Map<String, String> headerParams = new HashMap<>();
headerParams.put("client_id", client_id);
headerParams.put("t", timeMillis + "");
headerParams.put("sign_method", "HMAC-SHA256");
headerParams.put("sign", sign);
try {
JSONObject jsonObject = HttpUtil.sendGet(tokenFlashUrl + flashtoken, null, headerParams);
responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
} catch (IOException e) {
e.printStackTrace();
log.info("请求刷新token接口异常,异常信息{}", e);
}
return responseBody;
}
/**
* 发送请求获取涂鸦token
*
* @return
*/
private ResponseBody sendRequestGetToken() {
ResponseBody responseBody = null;
long timeMillis = System.currentTimeMillis();
String loginSign = TuyaUtils.getLoginSign(timeMillis, client_id, secret);
Map<String, String> headerParams = new HashMap<>();
headerParams.put("client_id", client_id);
headerParams.put("t", timeMillis + "");
headerParams.put("sign_method", "HMAC-SHA256");
headerParams.put("sign", loginSign);
try {
JSONObject jsonObject = HttpUtil.sendGet(getTokenUrl, null, headerParams);
responseBody = JSON.toJavaObject(jsonObject, ResponseBody.class);
} catch (IOException e) {
e.printStackTrace();
log.info("请求获取token接口异常,异常信息{}", e);
}
return responseBody;
}
}