Browse Source

优化省市区,增加氚云api导入

ruqinhu 5 years ago
parent
commit
166e0aa1f0

+ 50 - 0
src/main/java/com/ygj/yuemum/service/scheduled/ScheduledService.java

@@ -0,0 +1,50 @@
+package com.ygj.yuemum.service.scheduled;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.ygj.yuemum.utils.HttpUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Service;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+@Service
+public class ScheduledService {
+
+    public JSONObject listFormData(String EngineCode, String EngineSecret, String SchemaCode, int FromRowNum, int ToRowNum) throws Exception {
+        Map<String, String> paramMap = new HashMap();
+        paramMap.put("ActionName",   "LoadBizObjects");
+        paramMap.put("SchemaCode", SchemaCode);
+
+        JSONObject filterJsonObj = new JSONObject();
+        // 分页查询,从第几条开始
+        filterJsonObj.put("FromRowNum", FromRowNum);
+        // 查询的总行数
+        filterJsonObj.put("ToRowNum", ToRowNum);
+        // 返回的字段,不填返回所有
+        filterJsonObj.put("RequireCount", Boolean.FALSE);
+        // 排序字段,目前不支持使用,默认置空
+        filterJsonObj.put("ReturnItems", Collections.EMPTY_LIST);
+        // 分页查询,第几条结束
+        filterJsonObj.put("SortByCollection", Collections.EMPTY_LIST);
+        // 查询条件
+        filterJsonObj.put("Matcher", "{\"Type\":\"And\",\"Matchers\":[]}");
+
+        paramMap.put("Filter",filterJsonObj.toJSONString());
+
+        //身份认证参数
+        Map<String, String> headers = new HashMap();
+        headers.put("EngineCode", EngineCode);
+        headers.put("EngineSecret", EngineSecret);
+
+        HttpResponse result = HttpUtils.doPost("https://www.h3yun.com", "/OpenApi/Invoke", headers, null, JSON.toJSONString(paramMap));
+        String strResult = EntityUtils.toString(result.getEntity());
+        JSONObject jsonObject = JSONObject.parseObject(strResult);
+
+        return jsonObject;
+    }
+
+}

+ 348 - 0
src/main/java/com/ygj/yuemum/utils/HttpUtils.java

@@ -0,0 +1,348 @@
+package com.ygj.yuemum.utils;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.config.AuthSchemes;
+import org.apache.http.client.config.CookieSpecs;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.config.Registry;
+import org.apache.http.config.RegistryBuilder;
+import org.apache.http.conn.socket.ConnectionSocketFactory;
+import org.apache.http.conn.socket.PlainConnectionSocketFactory;
+import org.apache.http.conn.ssl.NoopHostnameVerifier;
+import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
+import org.apache.http.message.BasicNameValuePair;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @program:
+ * @description:
+ * @author:
+ * @create: 2019-11-06 16:48
+ **/
+public class HttpUtils {
+
+	/**
+	 * 获取 HttpClient
+	 *
+	 * @param host
+	 * @param path
+	 * @return
+	 */
+	public static HttpClient wrapClient(String host, String path) {
+//        HttpClient httpClient = HttpClientBuilder.create().build();
+		CloseableHttpClient httpClient = HttpClients.custom().build();
+		if (host != null && host.startsWith("https://")) {
+			return sslClient();
+		} else if (StringUtils.isBlank(host) && path != null && path.startsWith("https://")) {
+			return sslClient();
+		}
+		return httpClient;
+	}
+
+	/**
+	 * 在调用SSL之前需要重写验证方法,取消检测SSL
+	 * 创建ConnectionManager,添加Connection配置信息
+	 *
+	 * @return HttpClient 支持https
+	 */
+	public static HttpClient sslClient() {
+		try {
+			// 在调用SSL之前需要重写验证方法,取消检测SSL
+			X509TrustManager trustManager = new X509TrustManager() {
+				@Override
+				public X509Certificate[] getAcceptedIssuers() {
+					return null;
+				}
+
+				@Override
+				public void checkClientTrusted(X509Certificate[] xcs, String str) {
+				}
+
+				@Override
+				public void checkServerTrusted(X509Certificate[] xcs, String str) {
+				}
+			};
+			SSLContext ctx = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
+			ctx.init(null, new TrustManager[]{trustManager}, null);
+			SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE);
+			// 创建Registry
+			RequestConfig requestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.STANDARD_STRICT)
+					.setExpectContinueEnabled(Boolean.TRUE).setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST))
+					.setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).setConnectTimeout(2000).build();
+			Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
+					.register("http", PlainConnectionSocketFactory.INSTANCE)
+					.register("https", socketFactory).build();
+
+
+			// 创建ConnectionManager,添加Connection配置信息
+			PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
+
+			connectionManager.setDefaultMaxPerRoute(20);
+			connectionManager.setMaxTotal(200);
+
+			CloseableHttpClient closeableHttpClient = HttpClients.custom()
+					.setConnectionManager(connectionManager)
+					.setDefaultRequestConfig(requestConfig).build();
+
+			return closeableHttpClient;
+		} catch (KeyManagementException ex) {
+			throw new RuntimeException(ex);
+		} catch (NoSuchAlgorithmException ex) {
+			throw new RuntimeException(ex);
+		}
+	}
+
+
+	/**
+	 * get
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doGet(String host, String path,
+									 Map<String, String> headers,
+									 Map<String, String> querys)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpGet request = new HttpGet(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * post form
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @param bodys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path,
+									  Map<String, String> headers,
+									  Map<String, String> querys,
+									  Map<String, String> bodys)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpPost request = new HttpPost(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		if (bodys != null) {
+			List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
+
+			for (String key : bodys.keySet()) {
+				nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
+			}
+			UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
+			formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
+			request.setEntity(formEntity);
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * Post String
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path,
+									  Map<String, String> headers,
+									  Map<String, String> querys,
+									  String body)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpPost request = new HttpPost(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		if (StringUtils.isNotBlank(body)) {
+			request.setEntity(new StringEntity(body, "utf-8"));
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * Post stream
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPost(String host, String path,
+									  Map<String, String> headers,
+									  Map<String, String> querys,
+									  byte[] body)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpPost request = new HttpPost(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		if (body != null) {
+			request.setEntity(new ByteArrayEntity(body));
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * Put String
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPut(String host, String path,
+									 Map<String, String> headers,
+									 Map<String, String> querys,
+									 String body)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpPut request = new HttpPut(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		if (StringUtils.isNotBlank(body)) {
+			request.setEntity(new StringEntity(body, "utf-8"));
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * Put stream
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @param body
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doPut(String host, String path,
+									 Map<String, String> headers,
+									 Map<String, String> querys,
+									 byte[] body)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpPut request = new HttpPut(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		if (body != null) {
+			request.setEntity(new ByteArrayEntity(body));
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * Delete
+	 *
+	 * @param host
+	 * @param path
+	 * @param headers
+	 * @param querys
+	 * @return
+	 * @throws Exception
+	 */
+	public static HttpResponse doDelete(String host, String path,
+										Map<String, String> headers,
+										Map<String, String> querys)
+			throws Exception {
+		HttpClient httpClient = wrapClient(host, path);
+		HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
+		for (Map.Entry<String, String> e : headers.entrySet()) {
+			request.addHeader(e.getKey(), e.getValue());
+		}
+		return httpClient.execute(request);
+	}
+
+	/**
+	 * 构建请求的 url
+	 *
+	 * @param host
+	 * @param path
+	 * @param querys
+	 * @return
+	 * @throws UnsupportedEncodingException
+	 */
+	private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
+		StringBuilder sbUrl = new StringBuilder();
+		if (!StringUtils.isBlank(host)) {
+			sbUrl.append(host);
+		}
+		if (!StringUtils.isBlank(path)) {
+			sbUrl.append(path);
+		}
+		if (null != querys) {
+			StringBuilder sbQuery = new StringBuilder();
+			for (Map.Entry<String, String> query : querys.entrySet()) {
+				if (0 < sbQuery.length()) {
+					sbQuery.append("&");
+				}
+				if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
+					sbQuery.append(query.getValue());
+				}
+				if (!StringUtils.isBlank(query.getKey())) {
+					sbQuery.append(query.getKey());
+					if (!StringUtils.isBlank(query.getValue())) {
+						sbQuery.append("=");
+						sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
+					}
+				}
+			}
+			if (0 < sbQuery.length()) {
+				sbUrl.append("?").append(sbQuery);
+			}
+		}
+		return sbUrl.toString();
+	}
+
+}

+ 3 - 3
src/main/resources/mybatis/mapper/customer/CustomerInfoBasicDao.xml

@@ -84,7 +84,7 @@
                 entry_date,
             </if>
             <if test ='null != create_person'>
-                create_person
+                create_person,
             </if>
             <if test ='null != fb_date'>
                 fb_date
@@ -131,7 +131,7 @@
                 #{entry_date},
             </if>
             <if test ='null != create_person'>
-                #{create_person}
+                #{create_person},
             </if>
             <if test ='null != fb_date'>
                 #{fb_date}
@@ -160,7 +160,7 @@
             <if test ='null != address_detail'>address_detail = #{address_detail},</if>
             <if test ='null != fb_remarks'>fb_remarks = #{fb_remarks},</if>
             <if test ='null != entry_date'>entry_date = #{entry_date},</if>
-            <if test ='null != create_person'>create_person = #{create_person}</if>
+            <if test ='null != create_person'>create_person = #{create_person},</if>
             <if test ='null != fb_date'>fb_date = #{fb_date}</if>
         </set>
         WHERE id = #{id}

+ 78 - 0
src/test/java/com/ygj/yuemum/chuanyun/ImportTest.java

@@ -0,0 +1,78 @@
+package com.ygj.yuemum.chuanyun;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
+import com.ygj.yuemum.utils.HttpUtils;
+import org.apache.http.HttpResponse;
+import org.apache.http.util.EntityUtils;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class ImportTest {
+
+    @Test
+    public void testLoadObjects() throws Exception {
+        Map<String, String> paramMap = new HashMap();
+
+        paramMap.put("ActionName",   "LoadBizObjects");
+
+        paramMap.put("SchemaCode", "e3i1ixenjat08xho5hy3zp2c4");
+
+        paramMap.put("Filter","{\"FromRowNum\":   0,\"RequireCount\": false,\"ReturnItems\": [],   " +
+                "\"SortByCollection\": [],\"ToRowNum\": 500,   \"Matcher\": { \"Type\": \"And\",   \"Matchers\": []}}");
+       //身份认证参数
+
+        Map headers = new HashMap();
+
+        headers.put("EngineCode","ae483ce1607kh089");
+
+        headers.put("EngineSecret","aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==");
+
+
+
+        Gson gson = new Gson();
+
+        HttpResponse result = HttpUtils.doPost("https://www.h3yun.com", "/OpenApi/Invoke", headers, null, gson.toJson(paramMap));
+
+        String strResult = EntityUtils.toString(result.getEntity());
+
+        JSONObject jsonObject = JSONObject.parseObject(strResult);
+
+        System.out.println(strResult);
+
+    }
+
+
+    @Test
+    public void testLoadObject() throws Exception {
+        Map<String, String> paramMap = new HashMap();
+
+        paramMap.put("ActionName",   "LoadBizObject");
+
+        paramMap.put("SchemaCode", "ajxhpu8n5e3n2272xda8k38o3");
+
+        paramMap.put("BizObjectId", "97914666-af2a-4e4b-83ff-6bb0fa366cc0");
+
+        //身份认证参数
+        Map headers = new HashMap();
+
+        headers.put("EngineCode","ae483ce1607kh089");
+
+        headers.put("EngineSecret","aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==");
+
+
+
+        Gson gson = new Gson();
+
+        HttpResponse result = HttpUtils.doPost("https://www.h3yun.com", "/OpenApi/Invoke", headers, null, gson.toJson(paramMap));
+
+        String strResult = EntityUtils.toString(result.getEntity());
+
+        JSONObject jsonObject = JSONObject.parseObject(strResult);
+
+        System.out.println(strResult);
+    }
+
+}

+ 18 - 0
src/test/java/com/ygj/yuemum/chuanyun/ScheduledServiceTest.java

@@ -0,0 +1,18 @@
+package com.ygj.yuemum.chuanyun;
+
+import com.alibaba.fastjson.JSONObject;
+import com.ygj.yuemum.service.scheduled.ScheduledService;
+import org.junit.Test;
+
+public class ScheduledServiceTest {
+
+    @Test
+    public void testListFormData() throws Exception {
+        ScheduledService scheduledService = new ScheduledService();
+        JSONObject jsonObject = scheduledService.listFormData("ae483ce1607kh089", "aDKyV5OYQa7HMv4qWvqH8bAlXxmzD3bQK2/klBFoHlu6XHRxh7Q2lg==",
+                "e3i1ixenjat08xho5hy3zp2c4", 1, 10);
+        String strResult = jsonObject.toJSONString();
+        System.out.println(strResult);
+    }
+
+}