123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /*
- * 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 <T> GWResponseVO<T> callZKHGWPost(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
- String method = post;
- String resultJson = callGWMethod(method,api, params);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- public <T> GWResponseVO<T> callZKHGWPut(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
- String method = put;
- String resultJson = callGWMethod(method,api, params);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- public <T> GWResponseVO<T> callZKHGWPostByJA(String api, List<T> list, Class<T> clazz)throws Exception {
- String resultJson = callGWPostByJA(api, list);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- public <T> GWResponseVO<T> callZKHGWGet(String api, Map<String, Object> params, Class<T> clazz) throws Exception {
- String method = get;
- String resultJson = callGWMethod(method,api, params);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- public <T> GWResponseVO<T> callZKHGWDelete(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
- String method = delete;
- String resultJson = callGWMethod(method,api, params);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- public <T> GWResponseVO<T> callZKHGWPutNoInvoice(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
- String method = put;
- String resultJson = callGWMethodNoInvoice(method,api, params);
- return (GWResponseVO<T>) dealReseponse(api,resultJson);
- }
- private String callGWMethod(String method,String api, Map<String, Object> params) {
- assert api != null;
- Map<String,Object> 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<String, Object> params) {
- assert api != null;
- Map<String,Object> 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<String,Object> getMapParams(Map<String, Object> params){
- Map<String, Object> 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<Object> dealReseponse(String api,String resultJson)throws Exception{
- GWResponseVO<Object> 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;
- }
- }
|