|
@@ -0,0 +1,86 @@
|
|
|
+package com.miaxis.test;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.miaxis.NbjkApplication;
|
|
|
+import com.miaxis.common.sms.MD5Utils;
|
|
|
+import com.miaxis.feign.service.IWxMapService;
|
|
|
+import com.miaxis.wx.dto.WxMaterialList;
|
|
|
+import com.miaxis.wx.service.IWxGzhService;
|
|
|
+import com.miaxis.wx.service.IWxService;
|
|
|
+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;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.TreeMap;
|
|
|
+
|
|
|
+@SpringBootTest(classes = NbjkApplication.class)
|
|
|
+@RunWith(SpringRunner.class)
|
|
|
+public class WxMapTest {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IWxMapService wxMapService;
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void getLocation() {
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
+ String key = "TYIBZ-T6FH7-EEBXW-PQNLS-NPAL7-ZVFYT";
|
|
|
+ String location = "39.984154,116.307490";
|
|
|
+ String secret = "FpPyvw07CBEcYmJFraYkYea64fpoqaX";
|
|
|
+ String apiName = "/ws/geocoder/v1?";
|
|
|
+
|
|
|
+ params.put("key", key);
|
|
|
+ params.put("location", location);
|
|
|
+
|
|
|
+ String sign = WxMapTest.getSign(params, secret, apiName);
|
|
|
+ String sig = MD5Utils.MD5Encode(sign);
|
|
|
+
|
|
|
+ System.out.println("sign = " + sign);
|
|
|
+ System.out.println("sig = " + sig);
|
|
|
+
|
|
|
+ String result = wxMapService.getLocation(key,location,sig);
|
|
|
+ System.out.println(result);
|
|
|
+ JSONObject json = JSONObject.parseObject(result);
|
|
|
+ String status = json.get("status").toString();
|
|
|
+ System.out.println("status = " + status);
|
|
|
+
|
|
|
+ if(status.equals("0")) { //调用成功
|
|
|
+ JSONObject resultNode = (JSONObject)json.get("result");
|
|
|
+ JSONObject addressComponent = (JSONObject) resultNode.get("address_component");
|
|
|
+ String province = addressComponent.get("province").toString();
|
|
|
+ String city = addressComponent.get("city").toString();
|
|
|
+ System.out.println("province = " + province);
|
|
|
+ System.out.println("city = " + city);
|
|
|
+ } else {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String getSign(Map<String, Object> params, String secret, String apiName) {
|
|
|
+ Map<String, Object> sortMap = new TreeMap<String, Object>();
|
|
|
+ sortMap.putAll(params);
|
|
|
+ // 以k1=v1&k2=v2...方式拼接参数
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ for (Map.Entry<String, Object> s : sortMap.entrySet()) {
|
|
|
+ String k = s.getKey();
|
|
|
+ Object v = s.getValue();
|
|
|
+ builder.append(k).append("=").append(v).append("&");
|
|
|
+ }
|
|
|
+ if (!sortMap.isEmpty()) {
|
|
|
+ builder.deleteCharAt(builder.length() - 1);
|
|
|
+ }
|
|
|
+ builder.append(secret);
|
|
|
+ builder.insert(0, apiName);
|
|
|
+ return builder.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|