Skip to content

Commit a5a375e

Browse files
authored
🎨 #3186 【企业微信】增加获取用户登录身份和获取用户二次验证信息的接口
1 parent e6923cd commit a5a375e

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpOAuth2Service.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
55
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
6+
import me.chanjar.weixin.cp.bean.workbench.WxCpSecondVerificatioInformation;
67

78
/**
89
* <pre>
@@ -116,4 +117,38 @@ public interface WxCpOAuth2Service {
116117
* @throws WxErrorException 异常
117118
*/
118119
WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException;
120+
121+
/**
122+
* <pre>
123+
* 获取用户登录身份
124+
* https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=ACCESS_TOKEN&code=CODE
125+
* 该接口可使用用户登录成功颁发的code来获取成员信息,适用于自建应用与代开发应用
126+
*
127+
* 注意: 旧的/user/getuserinfo 接口的url已变更为auth/getuserinfo,不过旧接口依旧可以使用,建议是关注新接口即可
128+
*
129+
* 适用范围:身份验证中网页授权开发和企业微信Web登录的获取用户登录身份
130+
* </pre>
131+
*
132+
* @param code 通过成员授权获取到的code,最大为512字节。每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。
133+
* @return WxCpOauth2UserInfo user info
134+
* @throws WxErrorException 异常
135+
* @see #getUserInfo(Integer, String) #getUserInfo(Integer, String)
136+
*/
137+
WxCpOauth2UserInfo getAuthUserInfo(String code) throws WxErrorException;
138+
139+
/**
140+
* 获取用户二次验证信息
141+
*
142+
* https://qyapi.weixin.qq.com/cgi-bin/auth/get_tfa_info?access_token=ACCESS_TOKEN
143+
*
144+
* @author Hugo
145+
* @date 2023/12/14 10:29
146+
* @param code 用户进入二次验证页面时,企业微信颁发的code,每次成员授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期
147+
* @return me.chanjar.weixin.cp.bean.workbench.WxCpSecondVerificatioInformation 二次验证授权码,开发者可以调用通过二次验证接口,解锁企业微信终端.tfa_code有效期五分钟,且只能使用一次。
148+
*
149+
* 权限说明:仅『通讯录同步』或者自建应用可调用,如用自建应用调用,用户需要在二次验证范围和应用可见范围内。
150+
*
151+
* 并发限制:20
152+
*/
153+
WxCpSecondVerificatioInformation get_tfa_info(String code) throws WxErrorException;
119154
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpOAuth2ServiceImpl.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import me.chanjar.weixin.cp.api.WxCpService;
1111
import me.chanjar.weixin.cp.bean.WxCpOauth2UserInfo;
1212
import me.chanjar.weixin.cp.bean.WxCpUserDetail;
13+
import me.chanjar.weixin.cp.bean.workbench.WxCpSecondVerificatioInformation;
1314
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
1415

1516
import static me.chanjar.weixin.common.api.WxConsts.OAuth2Scope.*;
@@ -106,4 +107,27 @@ public WxCpUserDetail getUserDetail(String userTicket) throws WxErrorException {
106107
param.toString());
107108
return WxCpGsonBuilder.create().fromJson(responseText, WxCpUserDetail.class);
108109
}
110+
111+
@Override
112+
public WxCpOauth2UserInfo getAuthUserInfo(String code) throws WxErrorException {
113+
String responseText =
114+
this.mainService.get(String.format(this.mainService.getWxCpConfigStorage().getApiUrl(GET_USER_AUTH_INFO), code), null);
115+
JsonObject jo = GsonParser.parse(responseText);
116+
117+
return WxCpOauth2UserInfo.builder()
118+
.userId(GsonHelper.getString(jo, "UserId"))
119+
.openId(GsonHelper.getString(jo, "OpenId"))
120+
.userTicket(GsonHelper.getString(jo, "user_ticket"))
121+
.externalUserId(GsonHelper.getString(jo, "external_userid"))
122+
.build();
123+
}
124+
125+
@Override
126+
public WxCpSecondVerificatioInformation get_tfa_info(String code) throws WxErrorException {
127+
JsonObject param = new JsonObject();
128+
param.addProperty("code", code);
129+
String responseText = this.mainService.post(this.mainService.getWxCpConfigStorage().getApiUrl(GET_TFA_INFO),
130+
param.toString());
131+
return WxCpGsonBuilder.create().fromJson(responseText, WxCpSecondVerificatioInformation.class);
132+
}
109133
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package me.chanjar.weixin.cp.bean.workbench;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import lombok.experimental.Accessors;
8+
9+
/**
10+
* @author Hugo
11+
* <pre>
12+
* 获取用户二次验证信息的结果类
13+
* </pre>
14+
* <p>
15+
* 文档1:https://developer.work.weixin.qq.com/document/path/99499
16+
*/
17+
@Data
18+
@Accessors(chain = true)
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
@Builder
22+
public class WxCpSecondVerificatioInformation {
23+
private static final long serialVersionUID = -4301564507150486556L;
24+
25+
private String userId;
26+
private String tfa_code;
27+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,14 @@ interface OAuth2 {
148148
* The constant URL_OAUTH2_AUTHORIZE.
149149
*/
150150
String URL_OAUTH2_AUTHORIZE = "https://open.weixin.qq.com/connect/oauth2/authorize";
151+
/**
152+
* The constant GET_USER_INFO without agentId.
153+
*/
154+
String GET_USER_AUTH_INFO = "/cgi-bin/auth/getuserinfo?code=%s";
155+
/**
156+
* The constant GET_TFA_INFO.
157+
*/
158+
String GET_TFA_INFO = "/cgi-bin/auth/get_tfa_info";
151159
}
152160

153161
/**

0 commit comments

Comments
 (0)