public class DhFluorConstant {
public static final String APP_ID = "xxxxxxxxxxxxxxxxx";
public static final String APP_SECRET = "xxxxxxxxxxxxxxxxx";
public static final String REDIS_DH_FLUOR_ACCESSTOKEN_KEY = "DH_FLUOR_ACCESSTOKEN_KEY";
public static final String GETACCESSTOKEN_URL = "https://openapi.lechange.cn/openapi/accessToken";
}
@Slf4j
@Component
public class DhFluorTool {
@Autowired
private RestTemplate restTemplate;
@Autowired
private RedisCache redisCache;
private JSONObject getBaseRequestDate() {
long time = System.currentTimeMillis() / 1000;
String nonce = UUID.randomUUID().toString();
JSONObject data = new JSONObject();
data.put("system", new JSONObject()
.fluentPut("ver", "1.0")
.fluentPut("appId", DhFluorConstant.APP_ID)
.fluentPut("sign", getSign(time, nonce))
.fluentPut("time", time)
.fluentPut("nonce", nonce)
);
data.put("id", UUID.randomUUID().toString());
return data;
}
private String getSign(long time, String nonce) {
String oriSign = String.format("time:%s,nonce:%s,appSecret:%s", time, nonce, DhFluorConstant.APP_SECRET);
return MD5(oriSign);
}
private String MD5(String str) {
char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
'e', 'f'};
try {
byte[] strTemp = str.getBytes();
MessageDigest mdTemp = MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);
byte tmp[] = mdTemp.digest();
char strs[] = new char[16 * 2];
int k = 0;
for (int i = 0; i < 16; i++) {
byte byte0 = tmp[i];
strs[k++] = hexDigits[byte0 >>> 4 & 0xf];
strs[k++] = hexDigits[byte0 & 0xf];
}
return new String(strs);
} catch (Exception e) {
return null;
}
}
public String getAccessToken() throws FluorException {
String accessToken = redisCache.getCacheObject(DhFluorConstant.REDIS_DH_FLUOR_ACCESSTOKEN_KEY);
if (StringUtils.isNotEmpty(accessToken)) {
log.info("获取大华AccessToken,缓存获取======" + accessToken);
return accessToken;
}
HttpHeaders headers = new HttpHeaders();
headers.set(HttpHeaders.CONTENT_TYPE, "application/json");
headers.set(HttpHeaders.ACCEPT_CHARSET, "UTF-8");
log.info("获取大华AccessToken入参======" + getBaseRequestDate().toJSONString());
HttpEntity<JSONObject> request = new HttpEntity<JSONObject>(getBaseRequestDate(), headers);
String response = null;
try {
String url = DhFluorConstant.GETACCESSTOKEN_URL;
response = restTemplate.postForObject(url, request, String.class);
} catch (HttpClientErrorException e) {
throw new FluorException("获取大华AccessToken请求失败", 500);
}
JSONObject data = JSON.parseObject(response);
log.info("获取大华AccessToken返回信息======" + JSON.toJSONString(data));
JSONObject resp = data.getJSONObject("result");
if (null == resp) {
throw new FluorException("为获取到大华AccessToken返回信息");
}
if ("0".equals(resp.getString("code"))) {
accessToken = resp.getJSONObject("data").getString("accessToken");
Long expireTime = resp.getJSONObject("data").getLong("expireTime");
redisCache.setCacheObject(DhFluorConstant.REDIS_DH_FLUOR_ACCESSTOKEN_KEY, accessToken, expireTime, TimeUnit.SECONDS);
} else {
throw new FluorException(resp.getString("msg"), 500);
}
return accessToken;
}
}