Skip to content

Commit c2bff92

Browse files
committed
#997 企业微信增加小程序临时登录凭证校验接口
1 parent 9d4847d commit c2bff92

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
88
import me.chanjar.weixin.common.util.http.RequestExecutor;
99
import me.chanjar.weixin.common.util.http.RequestHttp;
10+
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
1011
import me.chanjar.weixin.cp.bean.WxCpMessage;
1112
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
1213
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -23,6 +24,7 @@ public interface WxCpService {
2324
String BATCH_REPLACE_PARTY = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty";
2425
String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser";
2526
String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid=";
27+
String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session";
2628

2729
/**
2830
* <pre>
@@ -124,6 +126,13 @@ public interface WxCpService {
124126
*/
125127
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException;
126128

129+
/**
130+
* 小程序登录凭证校验
131+
*
132+
* @param jsCode 登录时获取的 code
133+
*/
134+
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException;
135+
127136
/**
128137
* <pre>
129138
* 获取微信服务器的ip段

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.common.base.Joiner;
34
import com.google.gson.JsonArray;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
@@ -18,6 +19,7 @@
1819
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
1920
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
2021
import me.chanjar.weixin.cp.api.*;
22+
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult;
2123
import me.chanjar.weixin.cp.bean.WxCpMessage;
2224
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
2325
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@@ -26,6 +28,8 @@
2628

2729
import java.io.File;
2830
import java.io.IOException;
31+
import java.util.HashMap;
32+
import java.util.Map;
2933

3034
/**
3135
* @author chanjarster
@@ -167,6 +171,16 @@ public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorExce
167171
return WxCpMessageSendResult.fromJson(this.post(WxCpService.MESSAGE_SEND, message.toJson()));
168172
}
169173

174+
@Override
175+
public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException {
176+
Map<String, String> params = new HashMap<>(2);
177+
params.put("js_code", jsCode);
178+
params.put("grant_type", "authorization_code");
179+
180+
String result = this.get(JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params));
181+
return WxCpMaJsCode2SessionResult.fromJson(result);
182+
}
183+
170184
@Override
171185
public String[] getCallbackIp() throws WxErrorException {
172186
String responseContent = get(WxCpService.GET_CALLBACK_IP, null);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import com.google.gson.annotations.SerializedName;
4+
import lombok.Data;
5+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
6+
7+
import java.io.Serializable;
8+
9+
/**
10+
* <pre>
11+
* 小程序登录凭证校验
12+
* 文档地址:https://work.weixin.qq.com/api/doc#90000/90136/90289/wx.qy.login
13+
* </pre>
14+
* @author <a href="https://github.com/binarywang">Binary Wang</a>
15+
*/
16+
@Data
17+
public class WxCpMaJsCode2SessionResult implements Serializable {
18+
private static final long serialVersionUID = 6229609023682814765L;
19+
20+
@SerializedName("session_key")
21+
private String sessionKey;
22+
23+
@SerializedName("userid")
24+
private String userId;
25+
26+
@SerializedName("corpid")
27+
private String corpId;
28+
29+
public static WxCpMaJsCode2SessionResult fromJson(String json) {
30+
return WxCpGsonBuilder.create().fromJson(json, WxCpMaJsCode2SessionResult.class);
31+
}
32+
33+
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImplTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public void testGetAgentJsapiTicket() throws WxErrorException {
2828
assertThat(this.wxService.getAgentJsapiTicket()).isNotEmpty();
2929
assertThat(this.wxService.getAgentJsapiTicket(true)).isNotEmpty();
3030
}
31+
32+
@Test
33+
public void testJsCode2Session() throws WxErrorException {
34+
assertThat(this.wxService.jsCode2Session("111")).isNotNull();
35+
}
3136
}

0 commit comments

Comments
 (0)