Skip to content

Commit c7b1abc

Browse files
committed
🎨 优化部分代码,重构OAuth2网页授权、网页登录等相关接口,方便接入open模块
1 parent 6cc5ebd commit c7b1abc

26 files changed

Lines changed: 352 additions & 367 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package me.chanjar.weixin.common.bean;
2+
3+
4+
import com.google.gson.annotations.SerializedName;
5+
import lombok.Data;
6+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* oauth2用户个人信息.
12+
*
13+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
14+
* @date 2020-10-11
15+
*/
16+
@Data
17+
public class WxOAuth2UserInfo implements Serializable {
18+
private static final long serialVersionUID = 3181943506448954725L;
19+
20+
/**
21+
* openid 普通用户的标识,对当前开发者帐号唯一
22+
*/
23+
private String openid;
24+
/**
25+
* nickname 普通用户昵称
26+
*/
27+
private String nickname;
28+
/**
29+
* sex 普通用户性别,1为男性,2为女性
30+
*/
31+
private Integer sex;
32+
/**
33+
* city 普通用户个人资料填写的城市
34+
*/
35+
private String city;
36+
37+
/**
38+
* province 普通用户个人资料填写的省份
39+
*/
40+
private String province;
41+
/**
42+
* country 国家,如中国为CN
43+
*/
44+
private String country;
45+
/**
46+
* headimgurl 用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),
47+
* 用户没有头像时该项为空
48+
*/
49+
@SerializedName("headimgurl")
50+
private String headImgUrl;
51+
/**
52+
* unionid 用户统一标识。针对一个微信开放平台帐号下的应用,同一用户的unionid是唯一的。
53+
*/
54+
@SerializedName("unionid")
55+
private String unionId;
56+
57+
/**
58+
* privilege 用户特权信息,json数组,如微信沃卡用户为(chinaunicom)
59+
*/
60+
@SerializedName("privilege")
61+
private String[] privileges;
62+
63+
public static WxOAuth2UserInfo fromJson(String json) {
64+
return WxGsonBuilder.create().fromJson(json, WxOAuth2UserInfo.class);
65+
}
66+
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/bean/result/WxMpOAuth2AccessToken.java renamed to weixin-java-common/src/main/java/me/chanjar/weixin/common/bean/oauth2/WxOAuth2AccessToken.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
package me.chanjar.weixin.mp.bean.result;
2-
3-
import java.io.Serializable;
1+
package me.chanjar.weixin.common.bean.oauth2;
42

3+
import com.google.gson.annotations.SerializedName;
54
import lombok.Data;
6-
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
5+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
6+
7+
import java.io.Serializable;
78

89
/**
910
* https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
11+
*
12+
* @author Daniel Qian
1013
*/
1114
@Data
12-
public class WxMpOAuth2AccessToken implements Serializable {
15+
public class WxOAuth2AccessToken implements Serializable {
1316
private static final long serialVersionUID = -1345910558078620805L;
1417

18+
@SerializedName("access_token")
1519
private String accessToken;
1620

21+
@SerializedName("expires_in")
1722
private int expiresIn = -1;
1823

24+
@SerializedName("refresh_token")
1925
private String refreshToken;
2026

27+
@SerializedName("openid")
2128
private String openId;
2229

30+
@SerializedName("scope")
2331
private String scope;
2432

2533
/**
2634
* https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&announce_id=11513156443eZYea&version=&lang=zh_CN.
2735
* 本接口在scope参数为snsapi_base时不再提供unionID字段。
2836
*/
37+
@SerializedName("unionid")
2938
private String unionId;
3039

31-
public static WxMpOAuth2AccessToken fromJson(String json) {
32-
return WxMpGsonBuilder.create().fromJson(json, WxMpOAuth2AccessToken.class);
40+
public static WxOAuth2AccessToken fromJson(String json) {
41+
return WxGsonBuilder.create().fromJson(json, WxOAuth2AccessToken.class);
3342
}
3443

3544
@Override
3645
public String toString() {
37-
return WxMpGsonBuilder.create().toJson(this);
46+
return WxGsonBuilder.create().toJson(this);
3847
}
3948
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/SimpleGetRequestExecutor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package me.chanjar.weixin.common.util.http;
22

3-
import java.io.IOException;
4-
53
import me.chanjar.weixin.common.enums.WxType;
64
import me.chanjar.weixin.common.error.WxError;
75
import me.chanjar.weixin.common.error.WxErrorException;
86
import me.chanjar.weixin.common.util.http.apache.ApacheSimpleGetRequestExecutor;
97
import me.chanjar.weixin.common.util.http.jodd.JoddHttpSimpleGetRequestExecutor;
108
import me.chanjar.weixin.common.util.http.okhttp.OkHttpSimpleGetRequestExecutor;
119

10+
import java.io.IOException;
11+
1212
/**
1313
* 简单的GET请求执行器.
1414
* 请求的参数是String, 返回的结果也是String
@@ -27,7 +27,7 @@ public void execute(String uri, String data, ResponseHandler<String> handler, Wx
2727
handler.handle(this.execute(uri, data, wxType));
2828
}
2929

30-
public static RequestExecutor<String, String> create(RequestHttp requestHttp) {
30+
public static RequestExecutor<String, String> create(RequestHttp<?, ?> requestHttp) {
3131
switch (requestHttp.getRequestType()) {
3232
case APACHE_HTTP:
3333
return new ApacheSimpleGetRequestExecutor(requestHttp);
Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package me.chanjar.weixin.cp.api.impl;
22

33
import com.google.gson.JsonObject;
4-
import me.chanjar.weixin.common.enums.WxType;
54
import me.chanjar.weixin.common.bean.WxAccessToken;
5+
import me.chanjar.weixin.common.enums.WxType;
66
import me.chanjar.weixin.common.error.WxError;
77
import me.chanjar.weixin.common.error.WxErrorException;
88
import me.chanjar.weixin.common.error.WxRuntimeException;
99
import me.chanjar.weixin.common.util.json.GsonParser;
10+
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1011
import me.chanjar.weixin.cp.constant.WxCpApiPathConsts;
1112
import org.apache.http.client.config.RequestConfig;
1213
import org.apache.http.client.methods.CloseableHttpResponse;
@@ -30,28 +31,28 @@
3031
* Updated by yuanqixun on 2020-05-13
3132
* </pre>
3233
*
33-
*
3434
* @author <a href="https://github.com/binarywang">Binary Wang</a>
3535
*/
3636
public class WxCpServiceImpl extends WxCpServiceApacheHttpClientImpl {
3737
@Override
3838
public String getAccessToken(boolean forceRefresh) throws WxErrorException {
39-
if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
40-
return getWxCpConfigStorage().getAccessToken();
39+
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
40+
if (!configStorage.isAccessTokenExpired() && !forceRefresh) {
41+
return configStorage.getAccessToken();
4142
}
42-
Lock lock = getWxCpConfigStorage().getAccessTokenLock();
43+
Lock lock = configStorage.getAccessTokenLock();
4344
lock.lock();
4445
try {
4546
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
46-
if (!getWxCpConfigStorage().isAccessTokenExpired() && !forceRefresh) {
47-
return getWxCpConfigStorage().getAccessToken();
47+
if (!configStorage.isAccessTokenExpired() && !forceRefresh) {
48+
return configStorage.getAccessToken();
4849
}
49-
String url = String.format(getWxCpConfigStorage().getApiUrl(WxCpApiPathConsts.GET_TOKEN), this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
50+
String url = String.format(configStorage.getApiUrl(WxCpApiPathConsts.GET_TOKEN),
51+
this.configStorage.getCorpId(), this.configStorage.getCorpSecret());
5052
try {
5153
HttpGet httpGet = new HttpGet(url);
5254
if (getRequestHttpProxy() != null) {
53-
RequestConfig config = RequestConfig.custom()
54-
.setProxy(getRequestHttpProxy()).build();
55+
RequestConfig config = RequestConfig.custom().setProxy(getRequestHttpProxy()).build();
5556
httpGet.setConfig(config);
5657
}
5758
String resultContent;
@@ -67,60 +68,62 @@ public String getAccessToken(boolean forceRefresh) throws WxErrorException {
6768
}
6869

6970
WxAccessToken accessToken = WxAccessToken.fromJson(resultContent);
70-
getWxCpConfigStorage().updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
71+
configStorage.updateAccessToken(accessToken.getAccessToken(), accessToken.getExpiresIn());
7172
} catch (IOException e) {
7273
throw new WxRuntimeException(e);
7374
}
7475
} finally {
7576
lock.unlock();
7677
}
77-
return getWxCpConfigStorage().getAccessToken();
78+
return configStorage.getAccessToken();
7879
}
7980

8081
@Override
8182
public String getAgentJsapiTicket(boolean forceRefresh) throws WxErrorException {
83+
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
8284
if (forceRefresh) {
83-
getWxCpConfigStorage().expireAgentJsapiTicket();
85+
configStorage.expireAgentJsapiTicket();
8486
}
85-
if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) {
86-
Lock lock = getWxCpConfigStorage().getAgentJsapiTicketLock();
87+
if (configStorage.isAgentJsapiTicketExpired()) {
88+
Lock lock = configStorage.getAgentJsapiTicketLock();
8789
lock.lock();
8890
try {
8991
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
90-
if (getWxCpConfigStorage().isAgentJsapiTicketExpired()) {
91-
String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_AGENT_CONFIG_TICKET), null);
92+
if (configStorage.isAgentJsapiTicketExpired()) {
93+
String responseContent = this.get(configStorage.getApiUrl(GET_AGENT_CONFIG_TICKET), null);
9294
JsonObject jsonObject = GsonParser.parse(responseContent);
93-
getWxCpConfigStorage().updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
95+
configStorage.updateAgentJsapiTicket(jsonObject.get("ticket").getAsString(),
9496
jsonObject.get("expires_in").getAsInt());
9597
}
9698
} finally {
9799
lock.unlock();
98100
}
99101
}
100-
return getWxCpConfigStorage().getAgentJsapiTicket();
102+
return configStorage.getAgentJsapiTicket();
101103
}
102104

103105
@Override
104106
public String getJsapiTicket(boolean forceRefresh) throws WxErrorException {
107+
final WxCpConfigStorage configStorage = getWxCpConfigStorage();
105108
if (forceRefresh) {
106-
getWxCpConfigStorage().expireJsapiTicket();
109+
configStorage.expireJsapiTicket();
107110
}
108111

109-
if (getWxCpConfigStorage().isJsapiTicketExpired()) {
110-
Lock lock = getWxCpConfigStorage().getJsapiTicketLock();
112+
if (configStorage.isJsapiTicketExpired()) {
113+
Lock lock = configStorage.getJsapiTicketLock();
111114
lock.lock();
112115
try {
113116
// 拿到锁之后,再次判断一下最新的token是否过期,避免重刷
114-
if (getWxCpConfigStorage().isJsapiTicketExpired()) {
115-
String responseContent = this.get(getWxCpConfigStorage().getApiUrl(GET_JSAPI_TICKET), null);
117+
if (configStorage.isJsapiTicketExpired()) {
118+
String responseContent = this.get(configStorage.getApiUrl(GET_JSAPI_TICKET), null);
116119
JsonObject tmpJsonObject = GsonParser.parse(responseContent);
117-
getWxCpConfigStorage().updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
120+
configStorage.updateJsapiTicket(tmpJsonObject.get("ticket").getAsString(),
118121
tmpJsonObject.get("expires_in").getAsInt());
119122
}
120123
} finally {
121124
lock.unlock();
122125
}
123126
}
124-
return getWxCpConfigStorage().getJsapiTicket();
127+
return configStorage.getJsapiTicket();
125128
}
126129
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaSubscribeMessage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ public class WxMaSubscribeMessage implements Serializable {
6565
/**
6666
* 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
6767
*/
68-
private String miniprogramState = WxMaConstants.MiniprogramState.FORMAL;
68+
private String miniprogramState = WxMaConstants.MiniProgramState.FORMAL;
6969

7070
/**
7171
* 进入小程序查看的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
7272
*/
73-
private String lang = WxMaConstants.MiniprogramLang.ZH_CN;
73+
private String lang = WxMaConstants.MiniProgramLang.ZH_CN;
7474

7575
public WxMaSubscribeMessage addData(Data datum) {
7676
if (this.data == null) {

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaConstants.java

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
*
88
* @author <a href="https://github.com/binarywang">Binary Wang</a>
99
*/
10-
public class WxMaConstants {
10+
public abstract class WxMaConstants {
11+
private WxMaConstants() {
12+
}
13+
1114
/**
1215
* 微信接口返回的参数errcode.
1316
*/
@@ -16,7 +19,7 @@ public class WxMaConstants {
1619
/**
1720
* 素材类型.
1821
*/
19-
public static class MediaType {
22+
public abstract static class MediaType {
2023
/**
2124
* 图片.
2225
*/
@@ -26,7 +29,7 @@ public static class MediaType {
2629
/**
2730
* 消息格式.
2831
*/
29-
public static class MsgDataFormat {
32+
public abstract static class MsgDataFormat {
3033
public static final String XML = "XML";
3134
public static final String JSON = "JSON";
3235
}
@@ -72,7 +75,7 @@ public static final class SecCheckMediaType {
7275
/**
7376
* 快递账号绑定类型
7477
*/
75-
public static final class BindAccountType{
78+
public static final class BindAccountType {
7679

7780
/**
7881
* 绑定
@@ -88,7 +91,7 @@ public static final class BindAccountType{
8891
/**
8992
* 快递下单订单来源
9093
*/
91-
public static final class OrderAddSource{
94+
public static final class OrderAddSource {
9295

9396
/**
9497
* 小程序
@@ -104,7 +107,11 @@ public static final class OrderAddSource{
104107
/**
105108
* 快递下单保价
106109
*/
107-
public static final class OrderAddInsured{
110+
public static final class OrderAddInsured {
111+
private OrderAddInsured() {
112+
113+
}
114+
108115
/**
109116
* 不保价
110117
*/
@@ -121,13 +128,15 @@ public static final class OrderAddInsured{
121128
public static final int DEFAULT_INSURED_VALUE = 0;
122129
}
123130

124-
125131
/**
126132
* 小程序订阅消息跳转小程序类型
127-
*
133+
* <p>
128134
* developer为开发版;trial为体验版;formal为正式版;默认为正式版
129135
*/
130-
public static final class MiniprogramState{
136+
public static final class MiniProgramState {
137+
private MiniProgramState() {
138+
}
139+
131140
/**
132141
* 开发版
133142
*/
@@ -149,7 +158,10 @@ public static final class MiniprogramState{
149158
* 进入小程序查看的语言类型
150159
* 支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
151160
*/
152-
public static final class MiniprogramLang{
161+
public static final class MiniProgramLang {
162+
private MiniProgramLang() {
163+
}
164+
153165
/**
154166
* 简体中文
155167
*/

0 commit comments

Comments
 (0)