Selaa lähdekoodia

随身邦 E签宝 token相关 基础工具类提交

qxp 4 vuotta sitten
vanhempi
commit
836147dd7c

+ 5 - 0
suishenbang-api/pom.xml

@@ -20,6 +20,10 @@
             <groupId>com.dgtly</groupId>
             <artifactId>suishenbang-apiframework</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>com.dgtly</groupId>
@@ -36,6 +40,7 @@
             <version>4.0.0</version>
         </dependency>
 
+
     </dependencies>
 
     <build>

+ 4 - 0
suishenbang-api/src/main/resources/application-dev.yml

@@ -105,3 +105,7 @@ qiyeweixin:
   corpsecret: Cs9omiMVyz3cLm2D5T51i4bCDarHhIdl5uTaj__E-38
   #应用id
   agentId: 1000038
+esign:
+  appId: 7438822756
+  appSecret: fb4f6df1dc934c06e2c84e87e8a3546f
+  serviceUrl: https://smlopenapi.esign.cn

+ 26 - 0
suishenbang-api/src/test/java/test/MyTest.java

@@ -0,0 +1,26 @@
+package test;
+
+
+import com.dgtly.ApiApplication;
+import com.dgtly.wxportal.utils.ESign.ESignTokenUtil;
+import com.dgtly.wxportal.utils.qywxutils.QyWxAccessTokenUtil;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.junit4.SpringRunner;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest(classes=ApiApplication.class)
+public class MyTest {
+    @Autowired
+    private ESignTokenUtil eSignTokenUtil;
+
+
+    @Test
+    public void test(){
+        String token = eSignTokenUtil.getAccessToken();
+        System.out.println(token);
+
+    }
+}

+ 43 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/config/ESignConfig.java

@@ -0,0 +1,43 @@
+package com.dgtly.wxportal.config;
+
+import com.dgtly.wxportal.utils.ESign.ESignTokenUtil;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.context.annotation.Bean;
+import org.springframework.stereotype.Component;
+
+@Component
+public class ESignConfig {
+
+    @Value(value = "${esign.appId:none}")
+    private String appId;
+
+    @Value(value = "${esign.appSecret:none}")
+    private String appSecret;
+
+    @Value(value = "${esign.serviceUrl:none}")
+    private String eSignServiceUrl ;
+    private ESignTokenUtil eSignTokenUtil ;
+
+    @Bean
+    public ESignTokenUtil eSignTokenUtil(){
+        ESignTokenUtil eSignTokenUtil =  new ESignTokenUtil();
+        this.eSignTokenUtil = eSignTokenUtil;
+        return eSignTokenUtil;
+    }
+
+    public String getAppId() {
+        return appId;
+    }
+
+    public String getAppSecret() {
+        return appSecret;
+    }
+
+    public String geteSignServiceUrl() {
+        return eSignServiceUrl;
+    }
+
+    public ESignTokenUtil geteSignTokenUtil() {
+        return eSignTokenUtil;
+    }
+}

+ 15 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/exception/ESignException.java

@@ -0,0 +1,15 @@
+package com.dgtly.wxportal.exception;
+
+import com.dgtly.common.exception.BusinessException;
+import com.dgtly.wxportal.utils.ESign.ESignErrCode;
+import com.dgtly.wxportal.utils.qywxutils.QyWxErrCode;
+
+public class ESignException extends BusinessException{
+
+    public ESignException(String code, String url,String message) {
+        super("获取E签宝接口访问返回异常,请求地址为:"+url+"异常代码errcode:"+code+"---errmsg:"+message+"<=======>"+ESignErrCode.errMsg(code),new Throwable());
+    }
+    public ESignException(Integer code, String url,String message) {
+        super("获取E签宝接口访问返回异常,请求地址为:"+url+"异常代码errcode:"+code+"---errmsg:"+message+"<=======>"+ESignErrCode.errMsg(code),new Throwable());
+    }
+}

+ 3 - 3
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/exception/QyWeixinException.java

@@ -1,14 +1,14 @@
 package com.dgtly.wxportal.exception;
 
 import com.dgtly.common.exception.BusinessException;
-import com.dgtly.wxportal.utils.qywxutils.ErrCode;
+import com.dgtly.wxportal.utils.qywxutils.QyWxErrCode;
 
 public class QyWeixinException extends BusinessException{
 
     public QyWeixinException(String errcode,String url) {
-        super("企业微信API访问返回异常,请求地址为:"+url+"异常代码errcode:"+errcode+"---errmsg:"+ErrCode.errMsg(errcode),new Throwable());
+        super("企业微信API访问返回异常,请求地址为:"+url+"异常代码errcode:"+errcode+"---errmsg:"+QyWxErrCode.errMsg(errcode),new Throwable());
     }
     public QyWeixinException(Integer errcode,String url) {
-        super("企业微信API访问返回异常,请求地址为:"+url+"异常代码errcode:"+errcode+"---errmsg:"+ErrCode.errMsg(errcode),new Throwable());
+        super("企业微信API访问返回异常,请求地址为:"+url+"异常代码errcode:"+errcode+"---errmsg:"+QyWxErrCode.errMsg(errcode),new Throwable());
     }
 }

+ 34 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignErrCode.java

@@ -0,0 +1,34 @@
+
+package com.dgtly.wxportal.utils.ESign;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * E签宝平台全局返回码说明
+ */
+public class ESignErrCode {
+	
+	private static Map<String,String> codeMap = new HashMap<String,String>();
+	
+	static {
+		codeMap.put("-1","系统繁忙");
+		codeMap.put("0","请求成功");
+		codeMap.put("401","apiurl和应用ID的环境不对应,例如apiurl是模拟环境,应用ID是正式环境的");
+
+	}
+	
+	public static String errMsg(Integer errCode){
+		if(errCode != null && codeMap.containsKey(errCode+"")){
+			return codeMap.get(errCode+"");
+		}
+		return null;
+	}
+    public static String errMsg(String errCode){
+        if(errCode != null && codeMap.containsKey(errCode)){
+            return codeMap.get(errCode+"");
+        }
+        return null;
+    }
+	
+}

+ 76 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignTokenUtil.java

@@ -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;
+    }
+
+
+
+}

+ 66 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignUrlUtil.java

@@ -0,0 +1,66 @@
+package com.dgtly.wxportal.utils.ESign;
+
+import com.dgtly.wxportal.config.ESignConfig;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.PostConstruct;
+import java.util.EnumSet;
+import java.util.Formatter;
+
+
+public enum ESignUrlUtil {
+    AccessToken("/v1/oauth2/access_token?appId=%s&secret=%s&grantType=client_credentials",0)
+
+    ;
+    private String url;
+    private int flag;
+    private ESignConfig eSignConfig ;
+
+    ESignUrlUtil(String url,int flag){
+
+        this.url = url;
+        this.flag = flag;
+    }
+
+
+
+    public String getUrl() {
+        String str = eSignConfig.geteSignServiceUrl()+url;
+        switch (flag){
+            case 0:
+                str= new Formatter().format(str, eSignConfig.getAppId(),eSignConfig.getAppSecret()).toString();
+                break;
+            case 1:
+                str= new Formatter().format(str, eSignConfig.geteSignTokenUtil().getAccessToken()).toString();
+                break;
+        }
+        return str;
+    }
+
+    public void setESignConfig(ESignConfig eSignConfig){
+        this.eSignConfig = eSignConfig;
+    }
+
+
+
+
+
+
+    @Component
+    public static class ESignServiceAutowired {
+
+        @Autowired
+        private ESignConfig eSignConfig ;
+
+        @PostConstruct
+        public void postConstruct() {
+            for (ESignUrlUtil type : EnumSet.allOf(ESignUrlUtil.class)){
+                type.setESignConfig(eSignConfig);
+            }
+        }
+    }
+
+
+}

+ 3 - 3
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/qywxutils/ErrCode.java

@@ -1,5 +1,5 @@
 /*
- * FileName:ErrCode.java 
+ * FileName:QyWxErrCode.java
  * <p>
  * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
  * <p>
@@ -24,10 +24,10 @@ import java.util.Map;
 /**
  * 微信公众平台全局返回码说明
  */
-public class ErrCode {
+public class QyWxErrCode {
 	
 	private static Map<String,String> codeMap = new HashMap<String,String>();
-	
+
 	static {
 		codeMap.put("-1","系统繁忙");
 		codeMap.put("0","请求成功");

+ 6 - 13
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/qywxutils/QyWxServiceUrl.java

@@ -1,10 +1,12 @@
 package com.dgtly.wxportal.utils.qywxutils;
 
+
 import java.util.Formatter;
 
 /**
  * 企业微信 接口地址枚举类
  */
+
 public enum QyWxServiceUrl {
 
     /**
@@ -44,22 +46,13 @@ public enum QyWxServiceUrl {
     ;
 
 
-
-
-
-
-
-
-
-
-
-
+    QyWxServiceUrl(String url){
+        this.url =url;
+    }
 
 
     private String url ;
-    private QyWxServiceUrl(String url){
-        this.url = url;
-    }
+
 
     public String getformatUrl(Object... args) {
         return new Formatter().format(this.url, args).toString();