/* * CopyRight © 2018 ZKH All Rights Reserved. */ package com.zkh360.core.util; import com.alibaba.fastjson.JSON; import com.zkh360.api.dto.GWResponseEnum; import com.zkh360.api.dto.GWResponseVO; import com.zkh360.api.dto.ResponseCode; import com.zkh360.core.exception.TokenPassException; import com.zkh360.core.exception.WebsiteRuntimeException; import com.zkh360.core.exception.ZkhRuntimeException; import com.zkh360.core.task.TokenScheduled; import com.zkh360.core.util.redis.RedisService; import lombok.extern.log4j.Log4j; import net.sf.json.JSONObject; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.HashMap; import java.util.List; import java.util.Map; @Component @Log4j public class ZkhApiUtil { @Autowired private ZkhHttpUtil zkhHttpUtil; private static RedisService redisService; private static TokenScheduled tokenScheduled; private static final String post = "POST"; private static final String get = "GET"; private static final String put = "PUT"; private static final String delete = "DELETE"; String[] needInvoiceUrls = new String[]{ "/zkhUserInvoices/"+"/invoiceInfoTitleType", }; public GWResponseVO callZKHGWPost(String api, Map params, Class clazz) throws Exception{ String method = post; String resultJson = callGWMethod(method,api, params); return (GWResponseVO) dealReseponse(api,resultJson); } public GWResponseVO callZKHGWPut(String api, Map params, Class clazz) throws Exception{ String method = put; String resultJson = callGWMethod(method,api, params); return (GWResponseVO) dealReseponse(api,resultJson); } public GWResponseVO callZKHGWPostByJA(String api, List list, Class clazz)throws Exception { String resultJson = callGWPostByJA(api, list); return (GWResponseVO) dealReseponse(api,resultJson); } public GWResponseVO callZKHGWGet(String api, Map params, Class clazz) throws Exception { String method = get; String resultJson = callGWMethod(method,api, params); return (GWResponseVO) dealReseponse(api,resultJson); } public GWResponseVO callZKHGWDelete(String api, Map params, Class clazz) throws Exception{ String method = delete; String resultJson = callGWMethod(method,api, params); return (GWResponseVO) dealReseponse(api,resultJson); } public GWResponseVO callZKHGWPutNoInvoice(String api, Map params, Class clazz) throws Exception{ String method = put; String resultJson = callGWMethodNoInvoice(method,api, params); return (GWResponseVO) dealReseponse(api,resultJson); } private String callGWMethod(String method,String api, Map params) { assert api != null; Map map = getMapParams(params); String newApi = ""; //登录接口 if(api.equals("/zkhUser/login")){ map.put("invoiceInfoId", map.get("invoiceInfoId")); newApi = api + "?access_token="+ redisService.get("access_token"); }else{ newApi = spliceTokenAndUserInvoiceId(api); } try { long startTime = System.currentTimeMillis(); String s = zkhHttpUtil.requestWithJSON(method, newApi, map); log.info("耗时 : " + (System.currentTimeMillis() - startTime)); return s; } catch (Exception e) { e.printStackTrace(); } return null; } private String callGWMethodNoInvoice(String method,String api, Map params) { assert api != null; Map map = getMapParams(params); String newApi = ""; try { String token = getToken(); newApi = api + "&access_token="+ token; long startTime = System.currentTimeMillis(); String s = zkhHttpUtil.requestWithJSON(method, newApi, map); log.info("耗时 : " + (System.currentTimeMillis() - startTime)); return s; } catch (Exception e) { e.printStackTrace(); } return null; } private String postByJA(String api, List list) { assert api != null; String newApi = spliceTokenAndUserInvoiceId(api); try { return zkhHttpUtil.postRequestWithJSONArray(newApi,list); } catch (Exception e) { } return null; } private static Map getMapParams(Map params){ Map map; if(params != null) { map = params; } else { map = new HashMap<>(); } return map; } private String spliceTokenAndUserInvoiceId(String api){ String newApi = ""; String token = getToken(); if(isNeedInvoiceId(api)){ newApi = api + "?access_token="+ token; }else{ if(ZkhGWInvoiceId.getInvoiceId() == null){ newApi = api + "?access_token="+ token +"&userInvoiceId="+ ""; }else{ newApi = api + "?access_token="+ token +"&userInvoiceId="+ redisService.get(ZkhGWInvoiceId.getInvoiceId()); } }; return newApi; } /** * @Description: 发票ID是否作为单独的参数 * @param uri */ private boolean isNeedInvoiceId(String uri) { for (String needInvoiceUrls : needInvoiceUrls) { if(uri.contains(needInvoiceUrls)) { return true; } } return false; } private GWResponseVO dealReseponse(String api,String resultJson)throws Exception{ GWResponseVO gwResponseVO = new GWResponseVO<>(); log.info("官网返回值:"+resultJson); String apiName = ""; if(StringUtils.isBlank(resultJson)){ throw new RuntimeException("接口异常"); } gwResponseVO = JSON.parseObject(resultJson, GWResponseVO.class); for (ZkhGWapiEnum e : ZkhGWapiEnum.values()) { if (api.equals(e.getApi())) { apiName = e.getDesc(); } } if(null != gwResponseVO.getCode() && GWResponseEnum.CONFIG_ANNOUNCEMENT.getCode().equals(gwResponseVO.getCode()) && gwResponseVO.getMessage().equals(GWResponseEnum.CONFIG_ANNOUNCEMENT.getMessage())){ throw new ZkhRuntimeException(GWResponseEnum.CONFIG_ANNOUNCEMENT.getMessage()); } if(null != gwResponseVO.getCode() && GWResponseEnum.MIN_SKU_COUNT_ERROR.getCode().equals(gwResponseVO.getCode())){ throw new WebsiteRuntimeException(ResponseCode.CODE_1601, GWResponseEnum.MIN_SKU_COUNT_ERROR.getMessage()); } if(null != gwResponseVO.getCode() && GWResponseEnum.SKU_STOCK_NOT_USE.getCode().equals(gwResponseVO.getCode())){ throw new WebsiteRuntimeException(ResponseCode.CODE_1602, GWResponseEnum.SKU_STOCK_NOT_USE.getMessage()); } if(null != gwResponseVO.getCode() && GWResponseEnum.SKU_SELLED.getCode().equals(gwResponseVO.getCode())){ throw new WebsiteRuntimeException(ResponseCode.CODE_1603, GWResponseEnum.SKU_SELLED.getMessage()); } if (null != gwResponseVO.getCode() && gwResponseVO.getCode().equals(1)) { throw new TokenPassException(gwResponseVO.getCode(), apiName + gwResponseVO.getMessage()); } if (!gwResponseVO.isSuccess()) { throw new RuntimeException(apiName + gwResponseVO.getMessage()); } return gwResponseVO; } public String callGWPostByJA(String api, List list) { return postByJA(api, list); } public static boolean isJson(String content) { try { JSONObject.fromObject(content); return true; } catch (Exception e) { return false; } } interface GenericType { Type genericType(Class rawClass, Class actualTypeClass); } static class GenericTypeImpl implements GenericType { @Override public Type genericType(final Class rawClass, final Class actualTypeClass) { return new ParameterizedType() { @Override public Type[] getActualTypeArguments() { return new Type[] { actualTypeClass }; } @Override public Type getRawType() { return rawClass; } @Override public Type getOwnerType() { return null; } }; } } @Autowired public void setRedisService(RedisService redisService) { ZkhApiUtil.redisService = redisService; } @Autowired public void setTokenScheduled(TokenScheduled tokenScheduled) { ZkhApiUtil.tokenScheduled = tokenScheduled; } /** * 获取token * @return */ public String getToken(){ String token = ""; if(null != redisService){ try{ boolean tokenExists = redisService.exists("access_token"); if(!tokenExists){ token = tokenScheduled.getToken(); redisService.set("access_token",token,7200L); }else{ token = (String) redisService.get("access_token"); } }catch (Exception e){ token = tokenScheduled.getToken(); } }else{ token = tokenScheduled.getToken(); } return token; } }