| @@ -1,11 +1,7 @@ | |||||
| package com.iformall.douyin.miniapp.api.impl; | package com.iformall.douyin.miniapp.api.impl; | ||||
| import cn.binarywang.wx.miniapp.api.WxMaUserService; | |||||
| import cn.binarywang.wx.miniapp.api.impl.WxMaUserServiceImpl; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; | ||||
| import cn.binarywang.wx.miniapp.config.WxMaConfig; | import cn.binarywang.wx.miniapp.config.WxMaConfig; | ||||
| import com.aliyun.openservices.shade.com.alibaba.fastjson.JSON; | |||||
| import com.aliyun.openservices.shade.com.alibaba.fastjson.JSONObject; | |||||
| import com.google.common.base.Joiner; | import com.google.common.base.Joiner; | ||||
| import com.google.gson.Gson; | import com.google.gson.Gson; | ||||
| import com.google.gson.JsonParser; | import com.google.gson.JsonParser; | ||||
| @@ -161,16 +157,44 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||||
| @Override | @Override | ||||
| public String get(String url, String queryParam) throws WxErrorException { | public String get(String url, String queryParam) throws WxErrorException { | ||||
| return execute(SimpleGetRequestExecutor.create(this), url, queryParam); | |||||
| if (url.contains("access_token=")) { | |||||
| throw new IllegalArgumentException("uri参数中不允许有access_token: " + url); | |||||
| } | |||||
| String accessToken = getAccessToken(false); | |||||
| String uriWithAccessToken = url + (url.contains("?") ? "&" : "?") + "access_token=" + accessToken; | |||||
| return execute(SimpleGetRequestExecutor.create(this), uriWithAccessToken, queryParam); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public String post(String url, String postData) throws WxErrorException { | public String post(String url, String postData) throws WxErrorException { | ||||
| return execute(SimplePostRequestExecutor.create(this), url, postData); | |||||
| if (url.contains("access_token=")) { | |||||
| throw new IllegalArgumentException("uri参数中不允许有access_token: " + url); | |||||
| } | |||||
| String accessToken = getAccessToken(false); | |||||
| String uriWithAccessToken = url + (url.contains("?") ? "&" : "?") + "access_token=" + accessToken; | |||||
| return execute(SimplePostRequestExecutor.create(this), uriWithAccessToken, postData); | |||||
| } | } | ||||
| @Override | @Override | ||||
| public String post(String url, Object obj) throws WxErrorException { | public String post(String url, Object obj) throws WxErrorException { | ||||
| if (url.contains("access_token=")) { | |||||
| throw new IllegalArgumentException("uri参数中不允许有access_token: " + url); | |||||
| } | |||||
| try { | |||||
| String accessToken = getAccessToken(false); | |||||
| Class<?> aClass = obj.getClass(); | |||||
| if(!aClass.equals(String.class)){ | |||||
| Field accessToken1 = aClass.getDeclaredField("access_token"); | |||||
| accessToken1.setAccessible(true); | |||||
| accessToken1.set(obj,accessToken); | |||||
| } | |||||
| } catch (Exception e) { | |||||
| e.printStackTrace(); | |||||
| log.error("【请求参数】:{}添加accessToken失败" + obj.getClass()); | |||||
| } | |||||
| return this.execute(SimplePostRequestExecutor.create(this), url, TtMaGsonBuilder.create().toJson(obj)); | return this.execute(SimplePostRequestExecutor.create(this), url, TtMaGsonBuilder.create().toJson(obj)); | ||||
| } | } | ||||
| @@ -213,26 +237,9 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||||
| private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | private <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException { | ||||
| E dataForLog = DataUtils.handleDataWithSecret(data); | E dataForLog = DataUtils.handleDataWithSecret(data); | ||||
| if (uri.contains("access_token=")) { | |||||
| throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri); | |||||
| } | |||||
| String accessToken = getAccessToken(false); | |||||
| String uriWithAccessToken = uri + (uri.contains("?") ? "&" : "?") + "access_token=" + accessToken; | |||||
| try { | |||||
| Class<?> aClass = data.getClass(); | |||||
| if(!aClass.equals(String.class)){ | |||||
| Field accessToken1 = aClass.getDeclaredField("access_token"); | |||||
| accessToken1.setAccessible(true); | |||||
| accessToken1.set(data,accessToken); | |||||
| uriWithAccessToken = uri; | |||||
| } | |||||
| } catch (Exception e) { | |||||
| log.error("【请求参数】:{}添加accessToken失败" + data.getClass()); | |||||
| } | |||||
| try { | try { | ||||
| T result = executor.execute(uriWithAccessToken, data, WxType.MiniApp); | |||||
| log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, data, result); | |||||
| T result = executor.execute(uri, data, WxType.MiniApp); | |||||
| log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uri, data, result); | |||||
| return result; | return result; | ||||
| } catch (WxErrorException e) { | } catch (WxErrorException e) { | ||||
| WxError error = e.getError(); | WxError error = e.getError(); | ||||
| @@ -250,12 +257,12 @@ public class TtMaServiceImpl implements TtMaService, RequestHttp<CloseableHttpCl | |||||
| } | } | ||||
| if (error.getErrorCode() != 0) { | if (error.getErrorCode() != 0) { | ||||
| log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error); | |||||
| log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uri, dataForLog, error); | |||||
| throw new WxErrorException(error, e); | throw new WxErrorException(error, e); | ||||
| } | } | ||||
| return null; | return null; | ||||
| } catch (IOException e) { | } catch (IOException e) { | ||||
| log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage()); | |||||
| log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uri, dataForLog, e.getMessage()); | |||||
| throw new RuntimeException(e); | throw new RuntimeException(e); | ||||
| } | } | ||||
| } | } | ||||