|
@ -0,0 +1,67 @@
|
|
|
package com.yihu.iot.service.monitorPlatform;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yihu.jw.util.date.DateUtil;
|
|
|
import com.yihu.jw.util.http.HttpClientUtil;
|
|
|
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.Date;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by yeshijie on 2020/5/11.
|
|
|
*/
|
|
|
@Service
|
|
|
public class MonitorPlatformService {
|
|
|
|
|
|
@Value("${spring.wlyy.url}")
|
|
|
private String wlyyUrl;
|
|
|
@Value("${spring.wlyy.appid}")
|
|
|
private String appid;
|
|
|
@Value("${spring.wlyy.appsecret}")
|
|
|
private String appSecret;
|
|
|
public static Map<String,String> tokenMap = new HashMap<>();
|
|
|
|
|
|
@Autowired
|
|
|
private HttpClientUtil httpClientUtil;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 返回accessToken
|
|
|
* @return
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
private synchronized String getAccessToken(){
|
|
|
String token = "";
|
|
|
if(tokenMap.get("token")!=null){
|
|
|
token = tokenMap.get("token");
|
|
|
String time = tokenMap.get("time");
|
|
|
Date start = DateUtil.strToDate(time);
|
|
|
long m = (System.currentTimeMillis()-start.getTime())/1000;
|
|
|
long overTime = 15*6*60;//1.5小时
|
|
|
if(m<overTime){
|
|
|
return token;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JSONObject params = new JSONObject();
|
|
|
params.put("appid", appid);
|
|
|
params.put("appSecret", appSecret);
|
|
|
String url = "/gc/accesstoken";
|
|
|
|
|
|
String response = httpClientUtil.sendPost(wlyyUrl + url, params.toString());
|
|
|
JSONObject jsonObject = JSON.parseObject(response);
|
|
|
if(jsonObject.getInteger("status")==10000){
|
|
|
String accesstoken = jsonObject.getJSONObject("result").getString("accesstoken");
|
|
|
tokenMap.put("token",accesstoken);
|
|
|
tokenMap.put("time", DateUtil.dateToStrLong(new Date()));
|
|
|
return accesstoken;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
}
|