|
@@ -0,0 +1,76 @@
|
|
|
|
+package com.dgtly.wxportal.utils.ESign;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
+import com.dgtly.common.utils.http.HttpUtils;
|
|
|
|
+import com.dgtly.wxportal.exception.ESignException;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+public class ESignTokenUtil {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(ESignTokenUtil.class);
|
|
|
|
+
|
|
|
|
+ private String accessToken;
|
|
|
|
+ /**
|
|
|
|
+ * 创建时间
|
|
|
|
+ */
|
|
|
|
+ private long createTime;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 过期时间
|
|
|
|
+ */
|
|
|
|
+ private long expires;
|
|
|
|
+ /**
|
|
|
|
+ * 过期时间
|
|
|
|
+ */
|
|
|
|
+ private String refreshToken;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取access_token
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String getAccessToken() {
|
|
|
|
+ JSONObject json;
|
|
|
|
+ String url;
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ //token未过期使用缓存的token
|
|
|
|
+ long currentTiem = new Date().getTime();
|
|
|
|
+ if(this.accessToken!=null &&(currentTiem-createTime)<(expires)){
|
|
|
|
+ log.info("本身的token为{}",this.accessToken);
|
|
|
|
+ return this.accessToken;
|
|
|
|
+ }
|
|
|
|
+ //获取请求地址
|
|
|
|
+ url = ESignUrlUtil.AccessToken.getUrl();
|
|
|
|
+ log.info("开始请求E签宝接口:url==>",url);
|
|
|
|
+ json = JSONObject.parseObject(HttpUtils.sendGet(url));
|
|
|
|
+ log.info("结果返回==》",json);
|
|
|
|
+ }catch (Exception e){
|
|
|
|
+ this.accessToken=null;
|
|
|
|
+ log.error("获取E签宝token错误",e);
|
|
|
|
+ throw e;
|
|
|
|
+ }
|
|
|
|
+ Integer code = json.getInteger("code");
|
|
|
|
+ if(code!=0){
|
|
|
|
+ this.accessToken=null;
|
|
|
|
+ throw new ESignException(code,url,json.getString("message"));
|
|
|
|
+ }
|
|
|
|
+ JSONObject data = json.getJSONObject("data");
|
|
|
|
+ this.expires = data.getLong("expiresIn");
|
|
|
|
+ this.createTime = new Date().getTime();
|
|
|
|
+ this.accessToken = data.getString("token");
|
|
|
|
+ this.refreshToken = data.getString("refreshToken");
|
|
|
|
+
|
|
|
|
+ return this.accessToken;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+}
|