ZkhApiUtil.java 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * CopyRight © 2018 ZKH All Rights Reserved.
  3. */
  4. package com.zkh360.core.util;
  5. import com.alibaba.fastjson.JSON;
  6. import com.zkh360.api.dto.GWResponseEnum;
  7. import com.zkh360.api.dto.GWResponseVO;
  8. import com.zkh360.api.dto.ResponseCode;
  9. import com.zkh360.core.exception.TokenPassException;
  10. import com.zkh360.core.exception.WebsiteRuntimeException;
  11. import com.zkh360.core.exception.ZkhRuntimeException;
  12. import com.zkh360.core.task.TokenScheduled;
  13. import com.zkh360.core.util.redis.RedisService;
  14. import lombok.extern.log4j.Log4j;
  15. import net.sf.json.JSONObject;
  16. import org.apache.commons.lang.StringUtils;
  17. import org.springframework.beans.factory.annotation.Autowired;
  18. import org.springframework.stereotype.Component;
  19. import java.lang.reflect.ParameterizedType;
  20. import java.lang.reflect.Type;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. @Component
  25. @Log4j
  26. public class ZkhApiUtil {
  27. @Autowired
  28. private ZkhHttpUtil zkhHttpUtil;
  29. private static RedisService redisService;
  30. private static TokenScheduled tokenScheduled;
  31. private static final String post = "POST";
  32. private static final String get = "GET";
  33. private static final String put = "PUT";
  34. private static final String delete = "DELETE";
  35. String[] needInvoiceUrls = new String[]{
  36. "/zkhUserInvoices/"+"/invoiceInfoTitleType",
  37. };
  38. public <T> GWResponseVO<T> callZKHGWPost(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
  39. String method = post;
  40. String resultJson = callGWMethod(method,api, params);
  41. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  42. }
  43. public <T> GWResponseVO<T> callZKHGWPut(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
  44. String method = put;
  45. String resultJson = callGWMethod(method,api, params);
  46. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  47. }
  48. public <T> GWResponseVO<T> callZKHGWPostByJA(String api, List<T> list, Class<T> clazz)throws Exception {
  49. String resultJson = callGWPostByJA(api, list);
  50. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  51. }
  52. public <T> GWResponseVO<T> callZKHGWGet(String api, Map<String, Object> params, Class<T> clazz) throws Exception {
  53. String method = get;
  54. String resultJson = callGWMethod(method,api, params);
  55. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  56. }
  57. public <T> GWResponseVO<T> callZKHGWDelete(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
  58. String method = delete;
  59. String resultJson = callGWMethod(method,api, params);
  60. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  61. }
  62. public <T> GWResponseVO<T> callZKHGWPutNoInvoice(String api, Map<String, Object> params, Class<T> clazz) throws Exception{
  63. String method = put;
  64. String resultJson = callGWMethodNoInvoice(method,api, params);
  65. return (GWResponseVO<T>) dealReseponse(api,resultJson);
  66. }
  67. private String callGWMethod(String method,String api, Map<String, Object> params) {
  68. assert api != null;
  69. Map<String,Object> map = getMapParams(params);
  70. String newApi = "";
  71. //登录接口
  72. if(api.equals("/zkhUser/login")){
  73. map.put("invoiceInfoId", map.get("invoiceInfoId"));
  74. newApi = api + "?access_token="+ redisService.get("access_token");
  75. }else{
  76. newApi = spliceTokenAndUserInvoiceId(api);
  77. }
  78. try {
  79. long startTime = System.currentTimeMillis();
  80. String s = zkhHttpUtil.requestWithJSON(method, newApi, map);
  81. log.info("耗时 : " + (System.currentTimeMillis() - startTime));
  82. return s;
  83. } catch (Exception e) {
  84. e.printStackTrace();
  85. }
  86. return null;
  87. }
  88. private String callGWMethodNoInvoice(String method,String api, Map<String, Object> params) {
  89. assert api != null;
  90. Map<String,Object> map = getMapParams(params);
  91. String newApi = "";
  92. try {
  93. String token = getToken();
  94. newApi = api + "&access_token="+ token;
  95. long startTime = System.currentTimeMillis();
  96. String s = zkhHttpUtil.requestWithJSON(method, newApi, map);
  97. log.info("耗时 : " + (System.currentTimeMillis() - startTime));
  98. return s;
  99. } catch (Exception e) {
  100. e.printStackTrace();
  101. }
  102. return null;
  103. }
  104. private String postByJA(String api, List list) {
  105. assert api != null;
  106. String newApi = spliceTokenAndUserInvoiceId(api);
  107. try {
  108. return zkhHttpUtil.postRequestWithJSONArray(newApi,list);
  109. } catch (Exception e) {
  110. }
  111. return null;
  112. }
  113. private static Map<String,Object> getMapParams(Map<String, Object> params){
  114. Map<String, Object> map;
  115. if(params != null) {
  116. map = params;
  117. } else {
  118. map = new HashMap<>();
  119. }
  120. return map;
  121. }
  122. private String spliceTokenAndUserInvoiceId(String api){
  123. String newApi = "";
  124. String token = getToken();
  125. if(isNeedInvoiceId(api)){
  126. newApi = api + "?access_token="+ token;
  127. }else{
  128. if(ZkhGWInvoiceId.getInvoiceId() == null){
  129. newApi = api + "?access_token="+ token +"&userInvoiceId="+ "";
  130. }else{
  131. newApi = api + "?access_token="+ token +"&userInvoiceId="+ redisService.get(ZkhGWInvoiceId.getInvoiceId());
  132. }
  133. };
  134. return newApi;
  135. }
  136. /**
  137. * @Description: 发票ID是否作为单独的参数
  138. * @param uri
  139. */
  140. private boolean isNeedInvoiceId(String uri) {
  141. for (String needInvoiceUrls : needInvoiceUrls) {
  142. if(uri.contains(needInvoiceUrls)) {
  143. return true;
  144. }
  145. }
  146. return false;
  147. }
  148. private GWResponseVO<Object> dealReseponse(String api,String resultJson)throws Exception{
  149. GWResponseVO<Object> gwResponseVO = new GWResponseVO<>();
  150. log.info("官网返回值:"+resultJson);
  151. String apiName = "";
  152. if(StringUtils.isBlank(resultJson)){
  153. throw new RuntimeException("接口异常");
  154. }
  155. gwResponseVO = JSON.parseObject(resultJson, GWResponseVO.class);
  156. for (ZkhGWapiEnum e : ZkhGWapiEnum.values()) {
  157. if (api.equals(e.getApi())) {
  158. apiName = e.getDesc();
  159. }
  160. }
  161. if(null != gwResponseVO.getCode() && GWResponseEnum.CONFIG_ANNOUNCEMENT.getCode().equals(gwResponseVO.getCode())
  162. && gwResponseVO.getMessage().equals(GWResponseEnum.CONFIG_ANNOUNCEMENT.getMessage())){
  163. throw new ZkhRuntimeException(GWResponseEnum.CONFIG_ANNOUNCEMENT.getMessage());
  164. }
  165. if(null != gwResponseVO.getCode() && GWResponseEnum.MIN_SKU_COUNT_ERROR.getCode().equals(gwResponseVO.getCode())){
  166. throw new WebsiteRuntimeException(ResponseCode.CODE_1601, GWResponseEnum.MIN_SKU_COUNT_ERROR.getMessage());
  167. }
  168. if(null != gwResponseVO.getCode() && GWResponseEnum.SKU_STOCK_NOT_USE.getCode().equals(gwResponseVO.getCode())){
  169. throw new WebsiteRuntimeException(ResponseCode.CODE_1602, GWResponseEnum.SKU_STOCK_NOT_USE.getMessage());
  170. }
  171. if(null != gwResponseVO.getCode() && GWResponseEnum.SKU_SELLED.getCode().equals(gwResponseVO.getCode())){
  172. throw new WebsiteRuntimeException(ResponseCode.CODE_1603, GWResponseEnum.SKU_SELLED.getMessage());
  173. }
  174. if (null != gwResponseVO.getCode() && gwResponseVO.getCode().equals(1)) {
  175. throw new TokenPassException(gwResponseVO.getCode(), apiName + gwResponseVO.getMessage());
  176. }
  177. if (!gwResponseVO.isSuccess()) {
  178. throw new RuntimeException(apiName + gwResponseVO.getMessage());
  179. }
  180. return gwResponseVO;
  181. }
  182. public String callGWPostByJA(String api, List list) {
  183. return postByJA(api, list);
  184. }
  185. public static boolean isJson(String content) {
  186. try {
  187. JSONObject.fromObject(content);
  188. return true;
  189. } catch (Exception e) {
  190. return false;
  191. }
  192. }
  193. interface GenericType {
  194. Type genericType(Class<?> rawClass, Class<?> actualTypeClass);
  195. }
  196. static class GenericTypeImpl implements GenericType {
  197. @Override
  198. public Type genericType(final Class<?> rawClass, final Class<?> actualTypeClass) {
  199. return new ParameterizedType() {
  200. @Override
  201. public Type[] getActualTypeArguments() {
  202. return new Type[] { actualTypeClass };
  203. }
  204. @Override
  205. public Type getRawType() {
  206. return rawClass;
  207. }
  208. @Override
  209. public Type getOwnerType() {
  210. return null;
  211. }
  212. };
  213. }
  214. }
  215. @Autowired
  216. public void setRedisService(RedisService redisService) {
  217. ZkhApiUtil.redisService = redisService;
  218. }
  219. @Autowired
  220. public void setTokenScheduled(TokenScheduled tokenScheduled) {
  221. ZkhApiUtil.tokenScheduled = tokenScheduled;
  222. }
  223. /**
  224. * 获取token
  225. * @return
  226. */
  227. public String getToken(){
  228. String token = "";
  229. if(null != redisService){
  230. try{
  231. boolean tokenExists = redisService.exists("access_token");
  232. if(!tokenExists){
  233. token = tokenScheduled.getToken();
  234. redisService.set("access_token",token,7200L);
  235. }else{
  236. token = (String) redisService.get("access_token");
  237. }
  238. }catch (Exception e){
  239. token = tokenScheduled.getToken();
  240. }
  241. }else{
  242. token = tokenScheduled.getToken();
  243. }
  244. return token;
  245. }
  246. }