|
|
@@ -0,0 +1,206 @@
|
|
|
+package com.zfire.jiasm.syncdata.common;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyuncs.utils.StringUtils;
|
|
|
+import com.zfire.jiasm.syncdata.constant.Constant;
|
|
|
+import com.zfire.jiasm.syncdata.exception.RemoteServiceException;
|
|
|
+import com.zfire.jiasm.syncdata.gree.CommonParam;
|
|
|
+import com.zfire.jiasm.syncdata.gree.OutAppDefaultReq;
|
|
|
+import com.zfire.jiasm.syncdata.helper.GreeResponseHelper;
|
|
|
+import com.zfire.jiasm.syncdata.helper.ResponseHelper;
|
|
|
+import com.zfire.jiasm.syncdata.http.HttpUtils;
|
|
|
+import com.zfire.jiasm.syncdata.utils.RedisUtil;
|
|
|
+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.stereotype.Service;
|
|
|
+import org.springframework.util.LinkedMultiValueMap;
|
|
|
+import org.springframework.util.MultiValueMap;
|
|
|
+import org.springframework.web.client.HttpStatusCodeException;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 格力总部接口
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class GreeLogic {
|
|
|
+ @Value("${gree.gz.appid}")
|
|
|
+ private String gzAppId;
|
|
|
+ @Value("${gree.fs.appid}")
|
|
|
+ private String fsAppId;
|
|
|
+ @Value("${uploadbaseurl}")
|
|
|
+ private String uploadbaseurl = "";
|
|
|
+ @Value("${gree.gz.appkey}")
|
|
|
+ private String gzAppKey;
|
|
|
+ @Value("${gree.fs.appkey}")
|
|
|
+ private String fsAppKey;
|
|
|
+ @Value("${gree.client.id}")
|
|
|
+ private String clientId;
|
|
|
+ @Value("${gree.V3.client.secert}")
|
|
|
+ private String clientSecert;
|
|
|
+ @Value("${gree.V3.gettoken}")
|
|
|
+ private String tokenUrl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ RedisUtil redisUtil;
|
|
|
+ @Autowired
|
|
|
+ CommonLogic commonLogic;
|
|
|
+
|
|
|
+ public String getAppId(Integer belongType){
|
|
|
+ if(belongType == 1){
|
|
|
+ return gzAppId;
|
|
|
+ }else{
|
|
|
+ return fsAppId;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getAppKey(Integer belongType){
|
|
|
+ if(belongType == 1){
|
|
|
+ return gzAppKey;
|
|
|
+ }else{
|
|
|
+ return fsAppKey;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取总部接口的请求token
|
|
|
+ * @return
|
|
|
+ * @throws RemoteServiceException
|
|
|
+ */
|
|
|
+ public String getAccessToken() throws RemoteServiceException {
|
|
|
+ String redisKey = Constant.RedisPrefix.TOKEN_GREE;
|
|
|
+ String token = (String)redisUtil.get(redisKey);
|
|
|
+ if(token != null){
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+ MultiValueMap<String, String> forms= new LinkedMultiValueMap<String, String>();
|
|
|
+
|
|
|
+ forms.put("client_id", Collections.singletonList(clientId));
|
|
|
+ forms.put("client_secret", Collections.singletonList(clientSecert));
|
|
|
+ forms.put("grant_type", Collections.singletonList("client_credentials"));
|
|
|
+ String s = HttpUtils.requestXwww(tokenUrl, forms);
|
|
|
+ //String s = HttpUtils.requestPostForm(tokenUrl, map, null);
|
|
|
+ log.info("【获取总部token】response:{}",s);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ String accessToken = jsonObject.getString("access_token");
|
|
|
+ if(StringUtils.isNotEmpty(accessToken)) {
|
|
|
+ redisUtil.set(Constant.RedisPrefix.TOKEN_GREE, accessToken, 2 * 60 * 60);
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求总部接口
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String post(String apiPath,Map<String,Object> bizContent,Integer belongType) throws Exception {
|
|
|
+ CommonParam commonParam = new CommonParam();
|
|
|
+ commonParam.setAppId(this.getAppId(belongType));
|
|
|
+ commonParam.setAppKey(this.getAppKey(belongType));
|
|
|
+ commonParam.setAppPath(apiPath);
|
|
|
+ commonParam.setJson(JSONObject.toJSONString(bizContent));
|
|
|
+ OutAppDefaultReq outAppDefaultReq = commonLogic.supplyParam(commonParam);
|
|
|
+ Map<String,String> header = new HashMap<>();
|
|
|
+ header.put("authorization","Bearer "+this.getAccessToken());
|
|
|
+ log.info("accessToken:{}", "Bearer "+this.getAccessToken());
|
|
|
+
|
|
|
+ log.info("【请求总部接口】request:{}",JSONObject.toJSONString(outAppDefaultReq));
|
|
|
+ String s = HttpUtils.requestPostBody(uploadbaseurl, JSONObject.toJSONString(outAppDefaultReq), header);
|
|
|
+ log.info("【请求总部接口】response:{}",s);
|
|
|
+ GreeResponseHelper greeResponseHelper = JSONObject.parseObject(s, GreeResponseHelper.class);
|
|
|
+ if(greeResponseHelper.getStatus() != 200){
|
|
|
+ throw new RemoteServiceException("对接失败总部接口失败:"+greeResponseHelper.getMsg());
|
|
|
+ }
|
|
|
+ return (String)greeResponseHelper.getData();
|
|
|
+ }
|
|
|
+
|
|
|
+ public JSONObject post(String url,List param){
|
|
|
+ Map<String, String> header = new HashMap<>();
|
|
|
+ header.put("authorization", "Bearer " + this.getAccessToken());
|
|
|
+ log.info("【总部接口对接】request:{}", JSON.toJSONString(param));
|
|
|
+ String s = HttpUtils.requestPostBody(url, JSON.toJSONString(param), header);
|
|
|
+ log.info("【总部接口对接】response:{}",s);
|
|
|
+
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ Integer statusCode = jsonObject.getInteger("status");
|
|
|
+ if (statusCode != 200) {
|
|
|
+ log.error("接口地址:{},对接总部接口失败",url);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ private JSONObject post(String url,Map<String,Object> map){
|
|
|
+ Map<String, String> header = new HashMap<>();
|
|
|
+ header.put("authorization", "Bearer " + this.getAccessToken());
|
|
|
+ log.info("【总部接口对接】request:{}",JSONObject.toJSONString(map));
|
|
|
+ String s = HttpUtils.requestPostBody(url, JSONObject.toJSONString(map), header);
|
|
|
+ log.info("【总部接口对接】response:{}",s);
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(s);
|
|
|
+ Integer statusCode = jsonObject.getInteger("statusCode");
|
|
|
+ if (statusCode != 200) {
|
|
|
+ log.error("接口地址:{},对接总部接口失败",url);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return jsonObject;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求总部接口
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public GreeResponseHelper greePost(String apiPath,String bizContent,Integer belongType) throws Exception {
|
|
|
+ CommonParam commonParam = new CommonParam();
|
|
|
+ commonParam.setAppId(this.getAppId(belongType));
|
|
|
+ commonParam.setAppKey(this.getAppKey(belongType));
|
|
|
+ commonParam.setAppPath(apiPath);
|
|
|
+ commonParam.setJson(bizContent);
|
|
|
+ OutAppDefaultReq outAppDefaultReq = commonLogic.supplyParam(commonParam);
|
|
|
+ Map<String,String> header = new HashMap<>();
|
|
|
+ header.put("authorization","Bearer "+this.getAccessToken());
|
|
|
+
|
|
|
+ log.info("【请求总部接口】request:{}",JSONObject.toJSONString(outAppDefaultReq));
|
|
|
+// String s = HttpUtils.post(apiUrl, JSONObject.toJSONString(outAppDefaultReq), header);
|
|
|
+ // 设置请求头
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.setContentType(MediaType.APPLICATION_JSON);
|
|
|
+
|
|
|
+ for(String str : header.keySet()){
|
|
|
+ headers.set(str,header.get(str));
|
|
|
+ }
|
|
|
+
|
|
|
+ RestTemplate restTemplate = new RestTemplate();
|
|
|
+ HttpEntity<String> httpEntity = new HttpEntity<>(JSONObject.toJSONString(outAppDefaultReq), headers);
|
|
|
+ //获取返回数据
|
|
|
+ try {
|
|
|
+ String body = restTemplate.postForObject(uploadbaseurl, httpEntity, String.class);
|
|
|
+ log.info("【请求总部接口】response:{}",body);
|
|
|
+ return JSONObject.parseObject(body, GreeResponseHelper.class);
|
|
|
+ }catch(HttpStatusCodeException ex) {
|
|
|
+ log.error("rawStatusCode = {}",ex.getRawStatusCode());
|
|
|
+ GreeResponseHelper greeResponseHelper = new GreeResponseHelper();
|
|
|
+ greeResponseHelper.setStatus(ex.getRawStatusCode());
|
|
|
+// greeResponseHelper.setMsg(GreeResponseCodeEnum.valueOf(ex.getRawStatusCode()));
|
|
|
+ greeResponseHelper.setMsg(ex.getMessage().substring(0,100));
|
|
|
+ ex.printStackTrace();
|
|
|
+ return greeResponseHelper;
|
|
|
+ }catch(Exception e){
|
|
|
+ GreeResponseHelper greeResponseHelper = new GreeResponseHelper();
|
|
|
+ greeResponseHelper.setStatus(ResponseHelper.ResponseCode_COMMON);
|
|
|
+ greeResponseHelper.setMsg("非法请求");
|
|
|
+ e.printStackTrace();
|
|
|
+ return greeResponseHelper;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|