Skip to content

Commit 63fb5a0

Browse files
Fix issue binarywang#1746: 企业微信第三方应用增加授权配置接口
1 parent c0af379 commit 63fb5a0

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,20 @@ public interface WxCpTpService {
154154
*/
155155
String getPreAuthUrl(String redirectUri, String state) throws WxErrorException;
156156

157+
/**
158+
* <pre>
159+
* 获取预授权链接,测试环境下使用
160+
* @Link https://work.weixin.qq.com/api/doc/90001/90143/90602
161+
* </pre>
162+
*
163+
* @param redirectUri 授权完成后的回调网址
164+
* @param state a-zA-Z0-9的参数值(不超过128个字节),用于第三方自行校验session,防止跨域攻击
165+
* @param authType 授权类型:0 正式授权, 1 测试授权。
166+
* @return pre auth url
167+
* @throws WxErrorException the wx error exception
168+
*/
169+
String getPreAuthUrl(String redirectUri, String state, int authType) throws WxErrorException;
170+
157171
/**
158172
* 获取企业的授权信息
159173
*
@@ -278,7 +292,7 @@ public interface WxCpTpService {
278292
* <pre>
279293
* 获取访问用户敏感信息
280294
* </pre>
281-
*
295+
*
282296
* @param userTicket
283297
* @return
284298
*/

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.chanjar.weixin.cp.tp.service.impl;
22

33
import com.google.common.base.Joiner;
4+
import com.google.gson.Gson;
45
import com.google.gson.JsonObject;
56
import lombok.SneakyThrows;
67
import lombok.extern.slf4j.Slf4j;
@@ -210,6 +211,30 @@ public String getPreAuthUrl(String redirectUri, String state) throws WxErrorExce
210211
return preAuthUrl;
211212
}
212213

214+
@Override
215+
@SneakyThrows
216+
public String getPreAuthUrl(String redirectUri, String state, int authType) throws WxErrorException {
217+
String result = get(configStorage.getApiUrl(GET_PREAUTH_CODE), null);
218+
WxCpTpPreauthCode preAuthCode = WxCpTpPreauthCode.fromJson(result);
219+
String setSessionUrl = "https://qyapi.weixin.qq.com/cgi-bin/service/set_session_info";
220+
221+
Map<String,Object> sessionInfo = new HashMap<>(1);
222+
sessionInfo.put("auth_type", authType);
223+
Map<String,Object> param = new HashMap<>(2);
224+
param.put("pre_auth_code", preAuthCode.getPreAuthCode());
225+
param.put("session_info", sessionInfo);
226+
String postData = new Gson().toJson(param);
227+
228+
post(setSessionUrl, postData);
229+
230+
String preAuthUrl = "https://open.work.weixin.qq.com/3rdapp/install?suite_id=" + configStorage.getSuiteId() +
231+
"&pre_auth_code=" + preAuthCode.getPreAuthCode() + "&redirect_uri=" + URLEncoder.encode(redirectUri, "utf-8");
232+
if (StringUtils.isNotBlank(state)) {
233+
preAuthUrl += "&state=" + state;
234+
}
235+
return preAuthUrl;
236+
}
237+
213238
@Override
214239
public WxCpTpAuthInfo getAuthInfo(String authCorpId, String permanentCode) throws WxErrorException {
215240
JsonObject jsonObject = new JsonObject();

0 commit comments

Comments
 (0)