|
@@ -0,0 +1,126 @@
|
|
|
+package com.jkcredit.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.jkcredit.config.BaseResult;
|
|
|
+import com.jkcredit.entity.Car;
|
|
|
+import com.jkcredit.service.CarService;
|
|
|
+import com.jkcredit.utils.DESUtil;
|
|
|
+import com.jkcredit.utils.ResultUtil;
|
|
|
+import com.jkcredit.utils.SignUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.http.HttpEntity;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @description:
|
|
|
+ * @author: sunzhaoning
|
|
|
+ * @create: 2019-05-27 10:50
|
|
|
+ * @version: V1.0
|
|
|
+ **/
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class CarServiceImpl implements CarService {
|
|
|
+
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ @Value("${api.app_key}")
|
|
|
+ private String appKey;
|
|
|
+
|
|
|
+ @Value("${api.secret}")
|
|
|
+ private String secret;
|
|
|
+
|
|
|
+ @Value("${api.url}")
|
|
|
+ private String url;
|
|
|
+
|
|
|
+ @Value("${api.ip}")
|
|
|
+ private String ip;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public BaseResult car(Map<String, String> params) throws UnsupportedEncodingException {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ String transactionId = UUID.randomUUID() + String.valueOf(startTime);
|
|
|
+ Car car;
|
|
|
+ try {
|
|
|
+ String sign = params.get("sign");
|
|
|
+ sign = DESUtil.decryptor(new String(sign.getBytes(),"UTF-8"));
|
|
|
+ car = JSONObject.parseObject(sign, Car.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("返回数据:{}", JSONObject.toJSONString(ResultUtil.error(9004, "签名错误", transactionId)));
|
|
|
+ return ResultUtil.error(9004, "签名错误", transactionId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ log.warn("传入参数:{},交易ID:{}", JSONObject.toJSONString(car), transactionId);
|
|
|
+ String result;
|
|
|
+ Map<String, Object> param = SignUtils.getSign(car, params.get("api"), ip, appKey, secret);
|
|
|
+ if (param != null) {
|
|
|
+ //发送http请求 返回jsonObject
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Content-Type", MediaType.APPLICATION_JSON_UTF8_VALUE);
|
|
|
+ log.info("传入接口的json:{}", JSONObject.toJSONString(param));
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<>(JSONObject.toJSONString(param), headers);
|
|
|
+ JSONObject jsonObject = null;
|
|
|
+ try {
|
|
|
+ jsonObject = restTemplate.postForObject(url, formEntity, JSONObject.class);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ log.info("接口返回:{}", jsonObject);
|
|
|
+
|
|
|
+ return ResultUtil.success(DESUtil.encrypt(jsonObject.toJSONString()), transactionId);
|
|
|
+// String Q_Message = jsonObject.getString("Q_Message");
|
|
|
+// JSONObject jsonObject1 = JSON.parseObject(Q_Message);
|
|
|
+//
|
|
|
+// String ServiceDescript = jsonObject1.getString("ServiceDescript");
|
|
|
+// JSONObject ServiceDescriptJson = JSON.parseObject(ServiceDescript);
|
|
|
+//
|
|
|
+// String respData = ServiceDescriptJson.getString("RespData");
|
|
|
+// JSONObject respDataJson = JSON.parseObject(respData);
|
|
|
+// //获取接口返回码
|
|
|
+// String status = respDataJson.getString("status");
|
|
|
+//
|
|
|
+// long endTime = System.currentTimeMillis();
|
|
|
+// //计算耗时
|
|
|
+// long costTime = endTime - startTime;
|
|
|
+// return ResultUtil.success(DESUtil.encrypt(jsonObject.toJSONString()), transactionId);
|
|
|
+ //如果状态码是200 返回成功数据
|
|
|
+// if (("200.0").equals(status)) {
|
|
|
+// result = jsonObject.getString("result");
|
|
|
+// log.warn("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.success(result, transactionId)), costTime);
|
|
|
+// return ResultUtil.success(DESUtil.encrypt(result), transactionId);
|
|
|
+// } else if (("2").equals(status)) {
|
|
|
+// log.error("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.error(9001, "服务请求参数非法", transactionId)), costTime);
|
|
|
+// return ResultUtil.error(9001, "服务请求参数非法", transactionId);
|
|
|
+// } else if (("14").equals(status)) {
|
|
|
+// log.error("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.error(9001, "服务请求参数非法", transactionId)), costTime);
|
|
|
+// return ResultUtil.error(9001, "服务请求参数非法", transactionId);
|
|
|
+// }else if (("10").equals(status)) {
|
|
|
+// log.error("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.error(9001, "服务请求时间格式有误", transactionId)), costTime);
|
|
|
+// return ResultUtil.error(9001, "服务请求时间格式有误", transactionId);
|
|
|
+// }else if (("16").equals(status)) {
|
|
|
+// log.error("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.error(9001, "用户当日访问该服务成功次数达到上限", transactionId)), costTime);
|
|
|
+// return ResultUtil.error(9001, "用户当日访问该服务成功次数达到上限", transactionId);
|
|
|
+// } else {
|
|
|
+// log.error("返回数据:{},总耗时:{}ms", JSONObject.toJSONString(ResultUtil.error(9002, "服务异常", transactionId)), costTime);
|
|
|
+// return ResultUtil.error(9002, "服务异常", transactionId);
|
|
|
+// }
|
|
|
+ } else {
|
|
|
+ log.error("返回数据:{}", JSONObject.toJSONString(ResultUtil.error(9002, "API名称不存在", transactionId)));
|
|
|
+ return ResultUtil.error(9003, "API名称不存在", transactionId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|