|
@@ -0,0 +1,258 @@
|
|
|
+package com.yh.saas.plugin.yiliangyiyun.controller;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.CacheComponent;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.CarInfo;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.entity.view.BiViewInfo;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.util.Const;
|
|
|
+import com.yh.saas.plugin.yiliangyiyun.util.SignUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.web.bind.annotation.PostMapping;
|
|
|
+import org.springframework.web.bind.annotation.RequestBody;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.message.BasicHeader;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+@RestController
|
|
|
+@RequestMapping("/CarPostionController")
|
|
|
+public class CarPostionController {
|
|
|
+ @Autowired
|
|
|
+ private CacheComponent cacheComponent;
|
|
|
+
|
|
|
+ private static final String openapi_url = "http://open.aichezaixian.com/route/rest";
|
|
|
+
|
|
|
+ // 申请来的appKey和appSecret
|
|
|
+ private static final String app_key = "8FB345B8693CCD0066D0B7AACB06A271";
|
|
|
+ private static final String app_secret = "7f1457dc02744cc99f9856a649eed309";
|
|
|
+
|
|
|
+ @PostMapping("/api/getlocation")
|
|
|
+ public String getlocation() throws IOException {
|
|
|
+ String token ="";
|
|
|
+ String tokenRedis = cacheComponent.getRaw(Const.ADMIN_CARPOSTIONTOKEN);;
|
|
|
+ if (StringUtils.isEmpty(tokenRedis)) {
|
|
|
+ token = gettoken();
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ token = tokenRedis;
|
|
|
+ }
|
|
|
+ Map<String, String> headerMap = new HashMap<String, String>();
|
|
|
+ headerMap.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+
|
|
|
+ Map<String, String> paramMap = new HashMap<String, String>();
|
|
|
+ // 公共参数
|
|
|
+ paramMap.put("access_token", token);
|
|
|
+ paramMap.put("imeis", "868120299541554");
|
|
|
+ paramMap.put("map_type", "GOOGLE");
|
|
|
+ paramMap.put("method", "jimi.device.location.get");
|
|
|
+ paramMap.put("app_key", app_key);
|
|
|
+ paramMap.put("v", "0.9");
|
|
|
+ paramMap.put("timestamp", getCurrentDate());
|
|
|
+ paramMap.put("format", "json");
|
|
|
+ paramMap.put("sign_method", "md5");
|
|
|
+ // 获取token
|
|
|
+ HttpEntity entity = sendPost(headerMap, paramMap);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(entity, "utf-8"));
|
|
|
+ if ("0".equals(jsonObject.getString("code"))) {
|
|
|
+ String data = jsonObject.getString("result");
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String token ="";
|
|
|
+ @PostMapping("/api/gettrack")
|
|
|
+ public String gettrack(@RequestBody CarInfo carInfo) throws IOException {
|
|
|
+
|
|
|
+ String tokenRedis = cacheComponent.getRaw(Const.ADMIN_CARPOSTIONTOKEN);
|
|
|
+ if (StringUtils.isEmpty(tokenRedis)) {
|
|
|
+ token = gettoken();
|
|
|
+ }
|
|
|
+ else{
|
|
|
+ token = tokenRedis;
|
|
|
+ }
|
|
|
+ Map<String, String> headerMap = new HashMap<String, String>();
|
|
|
+ headerMap.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+
|
|
|
+ Map<String, String> paramMap = new HashMap<String, String>();
|
|
|
+ // 公共参数
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String a = formatter.format(carInfo.getStartDate());
|
|
|
+ paramMap.put("access_token", token);
|
|
|
+ paramMap.put("imei", "868120299541554");
|
|
|
+ paramMap.put("begin_time", formatter.format(carInfo.getStartDate()));
|
|
|
+ paramMap.put("end_time", formatter.format(carInfo.getEndDate()));
|
|
|
+ paramMap.put("map_type", "GOOGLE");
|
|
|
+ paramMap.put("method", "jimi.device.track.list");
|
|
|
+ paramMap.put("sign_method", "md5");
|
|
|
+ paramMap.put("app_key", app_key);
|
|
|
+ paramMap.put("v", "0.9");
|
|
|
+ paramMap.put("timestamp", getCurrentDate());
|
|
|
+ paramMap.put("format", "json");
|
|
|
+ // 获取token
|
|
|
+ HttpEntity entity = sendPost(headerMap, paramMap);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(EntityUtils.toString(entity, "utf-8"));
|
|
|
+ if ("0".equals(jsonObject.getString("code"))) {
|
|
|
+ String data = jsonObject.getString("result");
|
|
|
+// JSONObject data = JSONObject.parseObject(jsonObject.getString("result"));
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+ else if("1004".equals(jsonObject.getString("code"))){
|
|
|
+ token= refresh(token);
|
|
|
+ gettrack(carInfo);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String gettoken() throws IOException {
|
|
|
+ Map<String, String> headerMap = new HashMap<String, String>();
|
|
|
+ headerMap.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+
|
|
|
+ Map<String, String> paramMap = new HashMap<String, String>();
|
|
|
+ // 公共参数
|
|
|
+ paramMap.put("app_key", app_key);
|
|
|
+ paramMap.put("v", "1.0");
|
|
|
+ paramMap.put("timestamp", getCurrentDate());
|
|
|
+ paramMap.put("sign_method", "md5");
|
|
|
+ paramMap.put("format", "json");
|
|
|
+ paramMap.put("method", "jimi.oauth.token.get");
|
|
|
+
|
|
|
+ // 私有参数
|
|
|
+ paramMap.put("user_id", "HLJZTHYMY");
|
|
|
+ paramMap.put("user_pwd_md5", DigestUtils.md5Hex("Zthy888888"));
|
|
|
+ paramMap.put("expires_in", "7200");
|
|
|
+
|
|
|
+ // 计算签名
|
|
|
+ String sign = "";
|
|
|
+ try {
|
|
|
+ sign = SignUtils.signTopRequest(paramMap, app_secret, "md5");
|
|
|
+ paramMap.put("sign", sign);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取token
|
|
|
+ HttpEntity entity = sendPost(headerMap, paramMap);
|
|
|
+ String body = EntityUtils.toString(entity, "utf-8");
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
+ if ("0".equals(jsonObject.getString("code"))) {
|
|
|
+ JSONObject data = JSONObject.parseObject(jsonObject.getString("result"));
|
|
|
+ String accessToken = data.getString("accessToken");
|
|
|
+ cacheComponent.putRaw(Const.ADMIN_CARPOSTIONTOKEN, accessToken, 5400);
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public String refresh(String token) throws IOException {
|
|
|
+ Map<String, String> headerMap = new HashMap<String, String>();
|
|
|
+ headerMap.put("Content-Type", "application/x-www-form-urlencoded");
|
|
|
+
|
|
|
+ Map<String, String> paramMap = new HashMap<String, String>();
|
|
|
+ // 公共参数
|
|
|
+ paramMap.put("app_key", app_key);
|
|
|
+ paramMap.put("v", "1.0");
|
|
|
+ paramMap.put("timestamp", getCurrentDate());
|
|
|
+ paramMap.put("sign_method", "md5");
|
|
|
+ paramMap.put("format", "json");
|
|
|
+ paramMap.put("method", "jimi.oauth.token.refresh");
|
|
|
+
|
|
|
+ // 私有参数
|
|
|
+ paramMap.put("access_token", token);
|
|
|
+ paramMap.put("refresh_token", token);
|
|
|
+ paramMap.put("expires_in", "7200");
|
|
|
+
|
|
|
+ // 计算签名
|
|
|
+ String sign = "";
|
|
|
+ try {
|
|
|
+ sign = SignUtils.signTopRequest(paramMap, app_secret, "md5");
|
|
|
+ paramMap.put("sign", sign);
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println(e);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取token
|
|
|
+ HttpEntity entity = sendPost(headerMap, paramMap);
|
|
|
+ String body = EntityUtils.toString(entity, "utf-8");
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(body);
|
|
|
+ if ("0".equals(jsonObject.getString("code"))) {
|
|
|
+ JSONObject data = JSONObject.parseObject(jsonObject.getString("result"));
|
|
|
+ String accessToken = data.getString("accessToken");
|
|
|
+ cacheComponent.putRaw(Const.ADMIN_CARPOSTIONTOKEN, accessToken, 5400);
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private HttpEntity sendPost(Map<String, String> headerMap, Map<String, String> paramMap) {
|
|
|
+ try {
|
|
|
+ HttpPost post = new HttpPost(openapi_url);
|
|
|
+ List<NameValuePair> list = new ArrayList<NameValuePair>();
|
|
|
+ for (String key : paramMap.keySet()) {
|
|
|
+ list.add(new BasicNameValuePair(key, paramMap.get(key)));
|
|
|
+ }
|
|
|
+ post.setEntity(new UrlEncodedFormEntity(list, "UTF-8"));
|
|
|
+ if (null != headerMap) {
|
|
|
+ post.setHeaders(assemblyHeader(headerMap));
|
|
|
+ }
|
|
|
+
|
|
|
+ CloseableHttpClient httpClient = HttpClients.createDefault();
|
|
|
+ HttpResponse response = httpClient.execute(post);
|
|
|
+ HttpEntity entity = response.getEntity();
|
|
|
+// System.out.println(EntityUtils.toString(entity, "utf-8"));
|
|
|
+ return entity;
|
|
|
+
|
|
|
+ } catch (IOException e) {
|
|
|
+ System.err.println(e);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 组装头部信息
|
|
|
+ *
|
|
|
+ * @param headers
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static Header[] assemblyHeader(Map<String, String> headers) {
|
|
|
+ Header[] allHeader = new BasicHeader[headers.size()];
|
|
|
+ int i = 0;
|
|
|
+ for (String str : headers.keySet()) {
|
|
|
+ allHeader[i] = new BasicHeader(str, headers.get(str));
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ return allHeader;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getCurrentDate() {
|
|
|
+ SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+ String result = formatter.format(new Date());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+}
|