@@ -7,6 +7,7 @@ import me.chanjar.weixin.common.session.WxSessionManager; | |||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | ||||
import me.chanjar.weixin.common.util.http.RequestExecutor; | import me.chanjar.weixin.common.util.http.RequestExecutor; | ||||
import me.chanjar.weixin.common.util.http.RequestHttp; | import me.chanjar.weixin.common.util.http.RequestHttp; | ||||
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | import me.chanjar.weixin.cp.bean.WxCpMessage; | ||||
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult; | import me.chanjar.weixin.cp.bean.WxCpMessageSendResult; | ||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage; | import me.chanjar.weixin.cp.config.WxCpConfigStorage; | ||||
@@ -23,6 +24,7 @@ public interface WxCpService { | |||||
String BATCH_REPLACE_PARTY = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty"; | String BATCH_REPLACE_PARTY = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceparty"; | ||||
String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser"; | String BATCH_REPLACE_USER = "https://qyapi.weixin.qq.com/cgi-bin/batch/replaceuser"; | ||||
String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid="; | String BATCH_GET_RESULT = "https://qyapi.weixin.qq.com/cgi-bin/batch/getresult?jobid="; | ||||
String JSCODE_TO_SESSION_URL = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session"; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
@@ -124,6 +126,13 @@ public interface WxCpService { | |||||
*/ | */ | ||||
WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException; | WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException; | ||||
/** | |||||
* 小程序登录凭证校验 | |||||
* | |||||
* @param jsCode 登录时获取的 code | |||||
*/ | |||||
WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException; | |||||
/** | /** | ||||
* <pre> | * <pre> | ||||
* 获取微信服务器的ip段 | * 获取微信服务器的ip段 | ||||
@@ -1,5 +1,6 @@ | |||||
package me.chanjar.weixin.cp.api.impl; | package me.chanjar.weixin.cp.api.impl; | ||||
import com.google.common.base.Joiner; | |||||
import com.google.gson.JsonArray; | import com.google.gson.JsonArray; | ||||
import com.google.gson.JsonElement; | import com.google.gson.JsonElement; | ||||
import com.google.gson.JsonObject; | import com.google.gson.JsonObject; | ||||
@@ -18,6 +19,7 @@ import me.chanjar.weixin.common.util.http.RequestHttp; | |||||
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; | ||||
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; | ||||
import me.chanjar.weixin.cp.api.*; | import me.chanjar.weixin.cp.api.*; | ||||
import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; | |||||
import me.chanjar.weixin.cp.bean.WxCpMessage; | import me.chanjar.weixin.cp.bean.WxCpMessage; | ||||
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult; | import me.chanjar.weixin.cp.bean.WxCpMessageSendResult; | ||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage; | import me.chanjar.weixin.cp.config.WxCpConfigStorage; | ||||
@@ -26,6 +28,8 @@ import org.slf4j.LoggerFactory; | |||||
import java.io.File; | import java.io.File; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
/** | /** | ||||
* @author chanjarster | * @author chanjarster | ||||
@@ -167,6 +171,16 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH | |||||
return WxCpMessageSendResult.fromJson(this.post(WxCpService.MESSAGE_SEND, message.toJson())); | return WxCpMessageSendResult.fromJson(this.post(WxCpService.MESSAGE_SEND, message.toJson())); | ||||
} | } | ||||
@Override | |||||
public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException { | |||||
Map<String, String> params = new HashMap<>(2); | |||||
params.put("js_code", jsCode); | |||||
params.put("grant_type", "authorization_code"); | |||||
String result = this.get(JSCODE_TO_SESSION_URL, Joiner.on("&").withKeyValueSeparator("=").join(params)); | |||||
return WxCpMaJsCode2SessionResult.fromJson(result); | |||||
} | |||||
@Override | @Override | ||||
public String[] getCallbackIp() throws WxErrorException { | public String[] getCallbackIp() throws WxErrorException { | ||||
String responseContent = get(WxCpService.GET_CALLBACK_IP, null); | String responseContent = get(WxCpService.GET_CALLBACK_IP, null); | ||||
@@ -0,0 +1,33 @@ | |||||
package me.chanjar.weixin.cp.bean; | |||||
import com.google.gson.annotations.SerializedName; | |||||
import lombok.Data; | |||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | |||||
import java.io.Serializable; | |||||
/** | |||||
* <pre> | |||||
* 小程序登录凭证校验 | |||||
* 文档地址:https://work.weixin.qq.com/api/doc#90000/90136/90289/wx.qy.login | |||||
* </pre> | |||||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | |||||
*/ | |||||
@Data | |||||
public class WxCpMaJsCode2SessionResult implements Serializable { | |||||
private static final long serialVersionUID = 6229609023682814765L; | |||||
@SerializedName("session_key") | |||||
private String sessionKey; | |||||
@SerializedName("userid") | |||||
private String userId; | |||||
@SerializedName("corpid") | |||||
private String corpId; | |||||
public static WxCpMaJsCode2SessionResult fromJson(String json) { | |||||
return WxCpGsonBuilder.create().fromJson(json, WxCpMaJsCode2SessionResult.class); | |||||
} | |||||
} |
@@ -28,4 +28,9 @@ public class BaseWxCpServiceImplTest { | |||||
assertThat(this.wxService.getAgentJsapiTicket()).isNotEmpty(); | assertThat(this.wxService.getAgentJsapiTicket()).isNotEmpty(); | ||||
assertThat(this.wxService.getAgentJsapiTicket(true)).isNotEmpty(); | assertThat(this.wxService.getAgentJsapiTicket(true)).isNotEmpty(); | ||||
} | } | ||||
@Test | |||||
public void testJsCode2Session() throws WxErrorException { | |||||
assertThat(this.wxService.jsCode2Session("111")).isNotNull(); | |||||
} | |||||
} | } |