Kaynağa Gözat

随身邦 E签宝 接口请求相关 调整

qxp 4 yıl önce
ebeveyn
işleme
3abe0a85fb

+ 14 - 6
suishenbang-api/src/test/java/test/MyTest.java

@@ -1,9 +1,11 @@
 package test;
 
 
+import com.alibaba.fastjson.JSONObject;
 import com.dgtly.ApiApplication;
-import com.dgtly.wxportal.utils.ESign.ESignTokenUtil;
-import com.dgtly.wxportal.utils.qywxutils.QyWxAccessTokenUtil;
+import com.dgtly.wxportal.utils.ESign.ESignHttpUtil;
+import com.dgtly.wxportal.utils.ESign.ESignToken;
+import com.dgtly.wxportal.utils.ESign.ESignUrl;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -13,14 +15,20 @@ import org.springframework.test.context.junit4.SpringRunner;
 @RunWith(SpringRunner.class)
 @SpringBootTest(classes=ApiApplication.class)
 public class MyTest {
-    @Autowired
-    private ESignTokenUtil eSignTokenUtil;
 
 
+    @Autowired
+    private ESignHttpUtil eSignHttpUtil;
     @Test
     public void test(){
-        String token = eSignTokenUtil.getAccessToken();
-        System.out.println(token);
+        JSONObject json = new JSONObject();
+        json.put("thirdPartyUserId","18638353809");
+        json.put("name","屈星鹏");
+        json.put("idType","CRED_PSN_CH_IDCARD");
+        json.put("idNumber","410304199209191012");
+        json.put("mobile","18638353809");
+        String s=  eSignHttpUtil.doPost(ESignUrl.CreatePersonalAccount,json);
+        System.out.println(s);
 
     }
 }

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

@@ -1,6 +1,6 @@
 package com.dgtly.wxportal.config;
 
-import com.dgtly.wxportal.utils.ESign.ESignTokenUtil;
+import com.dgtly.wxportal.utils.ESign.ESignToken;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Bean;
 import org.springframework.stereotype.Component;
@@ -16,13 +16,13 @@ public class ESignConfig {
 
     @Value(value = "${esign.serviceUrl:none}")
     private String eSignServiceUrl ;
-    private ESignTokenUtil eSignTokenUtil ;
+    private ESignToken eSignToken;
 
     @Bean
-    public ESignTokenUtil eSignTokenUtil(){
-        ESignTokenUtil eSignTokenUtil =  new ESignTokenUtil();
-        this.eSignTokenUtil = eSignTokenUtil;
-        return eSignTokenUtil;
+    public ESignToken eSignTokenUtil(){
+        ESignToken eSignToken =  new ESignToken();
+        this.eSignToken = eSignToken;
+        return eSignToken;
     }
 
     public String getAppId() {
@@ -37,7 +37,7 @@ public class ESignConfig {
         return eSignServiceUrl;
     }
 
-    public ESignTokenUtil geteSignTokenUtil() {
-        return eSignTokenUtil;
+    public ESignToken geteSignToken() {
+        return eSignToken;
     }
 }

+ 4 - 1
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignErrCode.java

@@ -14,8 +14,11 @@ public class ESignErrCode {
 	static {
 		codeMap.put("-1","系统繁忙");
 		codeMap.put("0","请求成功");
-		codeMap.put("401","apiurl和应用ID的环境不对应,例如apiurl是模拟环境,应用ID是正式环境的");
+		codeMap.put("401","token过期了或者 header请求头不正确 或 apiurl和应用ID的环境不对应,例如apiurl是模拟环境,应用ID是正式环境的");
 
+
+
+		codeMap.put("53000000","个人账号已存在的");
 	}
 	
 	public static String errMsg(Integer errCode){

+ 214 - 0
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignHttpUtil.java

@@ -0,0 +1,214 @@
+package com.dgtly.wxportal.utils.ESign;
+
+import com.alibaba.fastjson.JSONObject;
+import com.dgtly.common.exception.BusinessException;
+import com.dgtly.common.utils.http.HttpUtils;
+import com.dgtly.wxportal.config.ESignConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.net.ssl.*;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.ConnectException;
+import java.net.SocketTimeoutException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.SecureRandom;
+import java.security.cert.X509Certificate;
+
+@Component
+public class ESignHttpUtil {
+    private static final Logger log = LoggerFactory.getLogger(ESignHttpUtil.class);
+
+
+    @Autowired
+    private ESignConfig eSignConfig;
+
+    public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
+        String url = eurl.getUrl(querys);
+        return doPost(url,param.toJSONString());
+    }
+
+    public String doPost(ESignUrl eurl, JSONObject param){
+        String url = eurl.getUrl();
+        return doPost(url,param.toJSONString());
+    }
+    /**
+     * 向指定 URL 发送POST方法的请求
+     *
+     * @param url 发送请求的 URL
+     * @param json 请求参数,
+     * @return 所代表远程资源的响应结果
+     */
+    public String doPost(String url , String json)
+    {
+        PrintWriter out = null;
+        BufferedReader in = null;
+        StringBuilder result = new StringBuilder();
+        try
+        {
+
+            log.info("sendPost - {}", url);
+            // 打开和URL之间的连接
+            SSLContext sc = SSLContext.getInstance("SSL");
+            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
+            // 从上述SSLContext对象中得到SSLSocketFactory对象
+            SSLSocketFactory ssf = sc.getSocketFactory();
+
+            java.net.URL realUrl = new URL(url);
+
+            HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
+
+            conn.setSSLSocketFactory(ssf);
+
+            conn.setRequestProperty("accept", "*/*");
+            conn.setRequestProperty("X-Tsign-Open-App-Id",eSignConfig.getAppId());
+            conn.setRequestProperty("X-Tsign-Open-Token",eSignConfig.geteSignToken().getAccessToken());
+            conn.setRequestProperty("connection", "Keep-Alive");
+            conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
+            conn.setUseCaches(false);//设置不要缓存
+            conn.setDoOutput(true);
+            conn.setDoInput(true);
+            out = new PrintWriter(conn.getOutputStream());
+            out.write(json);
+            out.flush();
+            in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null)
+            {
+                result.append(line);
+            }
+            log.info("recv - {}", result);
+        }
+        catch (ConnectException e)
+        {
+            log.error("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param_body=" + json, e);
+            throw new BusinessException("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param=" + json );
+        }
+        catch (SocketTimeoutException e)
+        {
+            log.error("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json, e);
+            throw new BusinessException("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json );
+        }
+        catch (IOException e)
+        {
+            log.error("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json, e);
+            throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
+        }
+        catch (Exception e)
+        {
+            log.error("调用HttpsUtil.sendJsonPost Exception, url=" + url + ",param=" + json, e);
+            throw new BusinessException("调用ESignHttpUtil.sendJsonPost Exception, url=" + url + ",param=" + json );
+        }
+        finally
+        {
+            try
+            {
+                if (out != null)
+                {
+                    out.close();
+                }
+                if (in != null)
+                {
+                    in.close();
+                }
+            }
+            catch (IOException ex)
+            {
+                log.error("调用in.close Exception, url=" + url + ",param=" + json, ex);
+            }
+        }
+        return result.toString();
+    }
+
+    public static String doGet(String url)
+    {
+        StringBuilder result = new StringBuilder();
+        BufferedReader in = null;
+        try
+        {
+            String urlNameString = url;
+            log.info("sendGet - {}", urlNameString);
+            URL realUrl = new URL(urlNameString);
+
+            URLConnection connection = realUrl.openConnection();
+            connection.setRequestProperty("accept", "*/*");
+            connection.connect();
+            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+            String line;
+            while ((line = in.readLine()) != null)
+            {
+                result.append(line);
+            }
+            log.info("recv - {}", result);
+        }
+        catch(ConnectException e)
+        {
+            log.error("调用ESignHttpUtil.sendGet ConnectException, url=" + url , e);
+            throw new BusinessException("调用ESignHttpUtil.sendGet ConnectException, url=" + url );
+        }
+        catch (SocketTimeoutException e)
+        {
+            log.error("调用ESignHttpUtil.sendGet SocketTimeoutException, url=" + url , e);
+            throw new BusinessException("调用ESignHttpUtil.sendGet SocketTimeoutException, url=" + url );
+        }
+        catch (IOException e)
+        {
+            log.error("调用ESignHttpUtil.sendGet IOException, url=" + url, e);
+            throw new BusinessException("调用ESignHttpUtil.sendGet IOException, url=" + url );
+        }
+        catch (Exception e)
+        {
+            log.error("调用HttpsUtil.sendGet Exception, url=" + url , e);
+            throw new BusinessException("调用ESignHttpUtil.sendGet Exception, url=" + url );
+        }
+        finally
+        {
+            try
+            {
+                if (in != null)
+                {
+                    in.close();
+                }
+            }
+            catch (Exception ex)
+            {
+                log.error("调用in.close Exception, url=" + url , ex);
+            }
+        }
+        return result.toString();
+    }
+
+    private static class TrustAnyTrustManager implements X509TrustManager
+    {
+        @Override
+        public void checkClientTrusted(X509Certificate[] chain, String authType)
+        {
+        }
+
+        @Override
+        public void checkServerTrusted(X509Certificate[] chain, String authType)
+        {
+        }
+
+        @Override
+        public X509Certificate[] getAcceptedIssuers()
+        {
+            return new X509Certificate[] {};
+        }
+    }
+
+    private static class TrustAnyHostnameVerifier implements HostnameVerifier
+    {
+        @Override
+        public boolean verify(String hostname, SSLSession session)
+        {
+            return true;
+        }
+    }
+}

+ 3 - 5
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignTokenUtil.java

@@ -6,16 +6,14 @@ 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 {
+public class ESignToken {
 
 
-    private static final Logger log = LoggerFactory.getLogger(ESignTokenUtil.class);
+    private static final Logger log = LoggerFactory.getLogger(ESignToken.class);
 
     private String accessToken;
     /**
@@ -48,7 +46,7 @@ public class ESignTokenUtil {
                 return this.accessToken;
             }
             //获取请求地址
-            url = ESignUrlUtil.AccessToken.getUrl();
+            url = ESignUrl.AccessToken.getUrl();
             log.info("开始请求E签宝接口:url==>",url);
             json = JSONObject.parseObject(HttpUtils.sendGet(url));
             log.info("结果返回==》",json);

+ 11 - 14
suishenbang-wxportal/suishenbang-wxportal-common/src/main/java/com/dgtly/wxportal/utils/ESign/ESignUrlUtil.java

@@ -2,7 +2,6 @@ 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;
@@ -10,30 +9,28 @@ import java.util.EnumSet;
 import java.util.Formatter;
 
 
-public enum ESignUrlUtil {
-    AccessToken("/v1/oauth2/access_token?appId=%s&secret=%s&grantType=client_credentials",0)
-
+public enum ESignUrl {
+    //获取token方法
+    AccessToken("/v1/oauth2/access_token?appId=%s&secret=%s&grantType=client_credentials")
+    ,CreatePersonalAccount("/v1/accounts/createByThirdPartyUserId")
     ;
     private String url;
-    private int flag;
     private ESignConfig eSignConfig ;
 
-    ESignUrlUtil(String url,int flag){
+    ESignUrl(String url){
 
         this.url = url;
-        this.flag = flag;
     }
 
 
-
-    public String getUrl() {
+    public String getUrl(Object ... args) {
         String str = eSignConfig.geteSignServiceUrl()+url;
-        switch (flag){
-            case 0:
+        switch (this){
+            case AccessToken:
                 str= new Formatter().format(str, eSignConfig.getAppId(),eSignConfig.getAppSecret()).toString();
                 break;
-            case 1:
-                str= new Formatter().format(str, eSignConfig.geteSignTokenUtil().getAccessToken()).toString();
+            default:
+                str= new Formatter().format(str, args).toString();
                 break;
         }
         return str;
@@ -56,7 +53,7 @@ public enum ESignUrlUtil {
 
         @PostConstruct
         public void postConstruct() {
-            for (ESignUrlUtil type : EnumSet.allOf(ESignUrlUtil.class)){
+            for (ESignUrl type : EnumSet.allOf(ESignUrl.class)){
                 type.setESignConfig(eSignConfig);
             }
         }