|
|
@@ -1,6 +1,5 @@ |
|
|
|
package cn.binarywang.wx.miniapp.api.impl; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
@@ -12,8 +11,6 @@ import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
import org.apache.http.client.methods.HttpGet; |
|
|
|
import org.apache.http.impl.client.BasicResponseHandler; |
|
|
|
import org.apache.http.impl.client.CloseableHttpClient; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaAnalysisService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaCodeService; |
|
|
@@ -22,6 +19,7 @@ import cn.binarywang.wx.miniapp.api.WxMaMediaService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaMsgService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaQrcodeService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaRunService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaSecCheckService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaSettingService; |
|
|
|
import cn.binarywang.wx.miniapp.api.WxMaShareService; |
|
|
@@ -31,14 +29,13 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; |
|
|
|
import cn.binarywang.wx.miniapp.config.WxMaConfig; |
|
|
|
import com.google.common.base.Joiner; |
|
|
|
import com.google.gson.Gson; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import me.chanjar.weixin.common.bean.WxAccessToken; |
|
|
|
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult; |
|
|
|
import me.chanjar.weixin.common.error.WxError; |
|
|
|
import me.chanjar.weixin.common.error.WxErrorException; |
|
|
|
import me.chanjar.weixin.common.util.DataUtils; |
|
|
|
import me.chanjar.weixin.common.util.crypto.SHA1; |
|
|
|
import me.chanjar.weixin.common.util.http.HttpType; |
|
|
|
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; |
|
|
|
import me.chanjar.weixin.common.util.http.RequestExecutor; |
|
|
|
import me.chanjar.weixin.common.util.http.RequestHttp; |
|
|
|
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; |
|
|
@@ -51,9 +48,8 @@ import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*; |
|
|
|
/** |
|
|
|
* @author <a href="https://github.com/binarywang">Binary Wang</a> |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpClient, HttpHost> { |
|
|
|
private final Logger log = LoggerFactory.getLogger(this.getClass()); |
|
|
|
|
|
|
|
private CloseableHttpClient httpClient; |
|
|
|
private HttpHost httpProxy; |
|
|
|
private WxMaConfig wxMaConfig; |
|
|
@@ -69,6 +65,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
private WxMaJsapiService jsapiService = new WxMaJsapiServiceImpl(this); |
|
|
|
private WxMaShareService shareService = new WxMaShareServiceImpl(this); |
|
|
|
private WxMaRunService runService = new WxMaRunServiceImpl(this); |
|
|
|
private WxMaSecCheckService secCheckService = new WxMaSecCheckServiceImpl(this); |
|
|
|
|
|
|
|
private int retrySleepMillis = 1000; |
|
|
|
private int maxRetryTimes = 5; |
|
|
@@ -153,13 +150,6 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
return this.getWxMaConfig().getAccessToken(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public boolean imgSecCheck(File file) throws WxErrorException { |
|
|
|
//这里只是借用MediaUploadRequestExecutor,并不使用其返回值WxMediaUploadResult |
|
|
|
WxMediaUploadResult result = this.execute(MediaUploadRequestExecutor.create(this.getRequestHttp()), IMG_SEC_CHECK_URL, file); |
|
|
|
return result != null; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxMaJscode2SessionResult jsCode2SessionInfo(String jsCode) throws WxErrorException { |
|
|
|
final WxMaConfig config = getWxMaConfig(); |
|
|
@@ -209,7 +199,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
return this.executeInternal(executor, uri, data); |
|
|
|
} catch (WxErrorException e) { |
|
|
|
if (retryTimes + 1 > this.maxRetryTimes) { |
|
|
|
this.log.warn("重试达到最大次数【{}】", maxRetryTimes); |
|
|
|
log.warn("重试达到最大次数【{}】", maxRetryTimes); |
|
|
|
//最后一次重试失败后,直接抛出异常,不再等待 |
|
|
|
throw new RuntimeException("微信服务端异常,超出重试次数"); |
|
|
|
} |
|
|
@@ -219,7 +209,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
if (error.getErrorCode() == -1) { |
|
|
|
int sleepMillis = this.retrySleepMillis * (1 << retryTimes); |
|
|
|
try { |
|
|
|
this.log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1); |
|
|
|
log.warn("微信系统繁忙,{} ms 后重试(第{}次)", sleepMillis, retryTimes + 1); |
|
|
|
Thread.sleep(sleepMillis); |
|
|
|
} catch (InterruptedException e1) { |
|
|
|
throw new RuntimeException(e1); |
|
|
@@ -230,7 +220,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
} |
|
|
|
} while (retryTimes++ < this.maxRetryTimes); |
|
|
|
|
|
|
|
this.log.warn("重试达到最大次数【{}】", this.maxRetryTimes); |
|
|
|
log.warn("重试达到最大次数【{}】", this.maxRetryTimes); |
|
|
|
throw new RuntimeException("微信服务端异常,超出重试次数"); |
|
|
|
} |
|
|
|
|
|
|
@@ -246,7 +236,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
|
|
|
|
try { |
|
|
|
T result = executor.execute(uriWithAccessToken, data); |
|
|
|
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result); |
|
|
|
log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", uriWithAccessToken, dataForLog, result); |
|
|
|
return result; |
|
|
|
} catch (WxErrorException e) { |
|
|
|
WxError error = e.getError(); |
|
|
@@ -264,12 +254,12 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
} |
|
|
|
|
|
|
|
if (error.getErrorCode() != 0) { |
|
|
|
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error); |
|
|
|
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【错误信息】:{}", uriWithAccessToken, dataForLog, error); |
|
|
|
throw new WxErrorException(error, e); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} catch (IOException e) { |
|
|
|
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage()); |
|
|
|
log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", uriWithAccessToken, dataForLog, e.getMessage()); |
|
|
|
throw new RuntimeException(e); |
|
|
|
} |
|
|
|
} |
|
|
@@ -349,4 +339,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl |
|
|
|
public WxMaRunService getRunService() { |
|
|
|
return this.runService; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxMaSecCheckService getSecCheckService() { |
|
|
|
return this.secCheckService; |
|
|
|
} |
|
|
|
} |