mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-21 11:07:37 +08:00
🎨 优化重构部分代码,抽取公共常量
This commit is contained in:
@@ -1,14 +1,30 @@
|
||||
package me.chanjar.weixin.common.api;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static me.chanjar.weixin.common.error.WxMpErrorMsgEnum.*;
|
||||
|
||||
/**
|
||||
* 微信开发所使用到的常量类.
|
||||
*
|
||||
* @author Daniel Qian & binarywang
|
||||
*/
|
||||
public class WxConsts {
|
||||
/**
|
||||
* access_token 相关错误代码
|
||||
* <pre>
|
||||
* 发生以下情况时尝试刷新access_token
|
||||
* 40001 获取access_token时AppSecret错误,或者access_token无效
|
||||
* 42001 access_token超时
|
||||
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
|
||||
* </pre>
|
||||
*/
|
||||
public static final List<Integer> ACCESS_TOKEN_ERROR_CODES = Arrays.asList(CODE_40001.getCode(),
|
||||
CODE_40014.getCode(), CODE_42001.getCode());
|
||||
|
||||
/**
|
||||
* 微信推送过来的消息的类型,和发送给微信xml格式消息的消息类型.
|
||||
*/
|
||||
|
@@ -4,8 +4,9 @@ import com.google.common.base.Joiner;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.enums.WxType;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.session.StandardSessionManager;
|
||||
@@ -277,13 +278,8 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
return result;
|
||||
} catch (WxErrorException e) {
|
||||
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 (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
|
||||
// 强制设置wxCpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
|
||||
this.configStorage.expireAccessToken();
|
||||
if (this.getWxCpConfigStorage().autoRefreshToken()) {
|
||||
|
@@ -10,6 +10,7 @@ import com.google.common.collect.Maps;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
@@ -32,8 +33,6 @@ import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
|
||||
import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @see #doGetAccessTokenRequest
|
||||
@@ -169,8 +168,8 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
/**
|
||||
* 通过网络请求获取AccessToken
|
||||
*
|
||||
* @return
|
||||
* @throws IOException
|
||||
* @return .
|
||||
* @throws IOException .
|
||||
*/
|
||||
protected abstract String doGetAccessTokenRequest() throws IOException;
|
||||
|
||||
@@ -244,12 +243,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
||||
return result;
|
||||
} catch (WxErrorException e) {
|
||||
WxError error = e.getError();
|
||||
/*
|
||||
* 发生以下情况时尝试刷新access_token
|
||||
*/
|
||||
if (error.getErrorCode() == ERR_40001
|
||||
|| error.getErrorCode() == ERR_42001
|
||||
|| error.getErrorCode() == ERR_40014) {
|
||||
if (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
|
||||
// 强制设置WxMaConfig的access token过期了,这样在下一次请求里就会刷新access token
|
||||
Lock lock = this.getWxMaConfig().getAccessTokenLock();
|
||||
lock.lock();
|
||||
|
@@ -53,23 +53,6 @@ public class WxMaConstants {
|
||||
public static final String MA_PAGE = "miniprogrampage";
|
||||
}
|
||||
|
||||
public static final class ErrorCode {
|
||||
/**
|
||||
* 40001 获取access_token时AppSecret错误,或者access_token无效.
|
||||
*/
|
||||
public static final int ERR_40001 = 40001;
|
||||
|
||||
/**
|
||||
* 42001 access_token超时.
|
||||
*/
|
||||
public static final int ERR_42001 = 42001;
|
||||
|
||||
/**
|
||||
* 40014 不合法的access_token,请开发者认真比对access_token的有效性(如是否过期).
|
||||
*/
|
||||
public static final int ERR_40014 = 40014;
|
||||
}
|
||||
|
||||
/**
|
||||
* 内容安全检测的媒体类型
|
||||
*/
|
||||
|
@@ -7,6 +7,7 @@ import com.google.gson.JsonObject;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||
import me.chanjar.weixin.common.api.WxOcrService;
|
||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||
@@ -377,13 +378,7 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
|
||||
return result;
|
||||
} catch (WxErrorException e) {
|
||||
WxError error = e.getError();
|
||||
/*
|
||||
* 发生以下情况时尝试刷新access_token
|
||||
* 40001 获取 access_token 时 AppSecret 错误,或者 access_token 无效。请开发者认真比对 AppSecret 的正确性,或查看是否正在为恰当的公众号调用接口
|
||||
* 42001 access_token 超时,请检查 access_token 的有效期,请参考基础支持 - 获取 access_token 中,对 access_token 的详细机制说明
|
||||
* 40014 不合法的 access_token ,请开发者认真比对 access_token 的有效性(如是否过期),或查看是否正在为恰当的公众号调用接口
|
||||
*/
|
||||
if (error.getErrorCode() == 42001 || error.getErrorCode() == 40001 || error.getErrorCode() == 40014) {
|
||||
if (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
|
||||
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
|
||||
Lock lock = this.getWxMpConfigStorage().getAccessTokenLock();
|
||||
lock.lock();
|
||||
|
@@ -146,14 +146,8 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
|
||||
return getWxOpenService().post(uriWithComponentAccessToken, postData);
|
||||
} catch (WxErrorException e) {
|
||||
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) {
|
||||
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
|
||||
if (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
|
||||
// 强制设置access token过期,这样在下一次请求里就会刷新access token
|
||||
Lock lock = this.getWxOpenConfigStorage().getComponentAccessTokenLock();
|
||||
lock.lock();
|
||||
try {
|
||||
@@ -190,13 +184,7 @@ public class WxOpenComponentServiceImpl implements WxOpenComponentService {
|
||||
return getWxOpenService().get(uriWithComponentAccessToken, null);
|
||||
} catch (WxErrorException e) {
|
||||
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 (WxConsts.ACCESS_TOKEN_ERROR_CODES.contains(error.getErrorCode())) {
|
||||
// 强制设置wxMpConfigStorage它的access token过期了,这样在下一次请求里就会刷新access token
|
||||
Lock lock = this.getWxOpenConfigStorage().getComponentAccessTokenLock();
|
||||
lock.lock();
|
||||
|
Reference in New Issue
Block a user