mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
规范化小程序模块的部分代码
This commit is contained in:
@@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaMsgService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
@@ -30,7 +31,7 @@ public class WxMaMsgServiceImpl implements WxMaMsgService {
|
||||
public void sendTemplateMsg(WxMaTemplateMessage templateMessage) throws WxErrorException {
|
||||
String responseContent = this.wxMaService.post(TEMPLATE_MSG_SEND_URL, templateMessage.toJson());
|
||||
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
|
||||
if (jsonObject.get("errcode").getAsInt() != 0) {
|
||||
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
|
||||
throw new WxErrorException(WxError.fromJson(responseContent));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package cn.binarywang.wx.miniapp.api.impl;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.*;
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
|
||||
import com.google.gson.JsonParser;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
@@ -147,6 +148,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
|
||||
/**
|
||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
||||
*/
|
||||
@Override
|
||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
|
||||
int retryTimes = 0;
|
||||
do {
|
||||
@@ -195,11 +197,10 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
|
||||
WxError error = e.getError();
|
||||
/*
|
||||
* 发生以下情况时尝试刷新access_token
|
||||
* 40001 获取access_token时AppSecret错误,或者access_token无效
|
||||
* 42001 access_token超时
|
||||
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期)
|
||||
*/
|
||||
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
|
||||
if (error.getErrorCode() == WxMaConstants.ErrorCode.ERR_40001
|
||||
|| error.getErrorCode() == WxMaConstants.ErrorCode.ERR_42001
|
||||
|| error.getErrorCode() == WxMaConstants.ErrorCode.ERR_40014) {
|
||||
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
|
||||
this.getWxMaConfig().expireAccessToken();
|
||||
if (this.getWxMaConfig().autoRefreshToken()) {
|
||||
|
||||
@@ -19,14 +19,14 @@ import java.util.Map;
|
||||
public class WxMaUserServiceImpl implements WxMaUserService {
|
||||
private WxMaService service;
|
||||
|
||||
WxMaUserServiceImpl(WxMaService service) {
|
||||
public WxMaUserServiceImpl(WxMaService service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxMaJscode2SessionResult getSessionInfo(String jsCode) throws WxErrorException {
|
||||
final WxMaConfig config = service.getWxMaConfig();
|
||||
Map<String, String> params = new HashMap<>();
|
||||
Map<String, String> params = new HashMap<>(8);
|
||||
params.put("appid", config.getAppid());
|
||||
params.put("secret", config.getSecret());
|
||||
params.put("js_code", jsCode);
|
||||
|
||||
Reference in New Issue
Block a user