From fddb2f7a182df67ef98fdef1d20019ddd249a635 Mon Sep 17 00:00:00 2001 From: ArBing Date: Wed, 13 Oct 2021 13:49:16 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20#2345=E3=80=90=E4=BC=81=E4=B8=9A?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E3=80=91=E5=A2=9E=E5=8A=A0=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E8=B0=83=E7=94=A8wx.agentConfig=E6=97=B6=E6=89=80=E9=9C=80?= =?UTF-8?q?=E8=A6=81=E7=9A=84=E7=AD=BE=E5=90=8D=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/chanjar/weixin/cp/api/WxCpService.java | 13 ++++++++ .../cp/api/impl/BaseWxCpServiceImpl.java | 25 +++++++++++++++ .../cp/bean/WxCpAgentJsapiSignature.java | 31 +++++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgentJsapiSignature.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java index 345b3bb344..94cd212637 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpService.java @@ -8,6 +8,7 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; import me.chanjar.weixin.common.util.http.RequestExecutor; import me.chanjar.weixin.common.util.http.RequestHttp; +import me.chanjar.weixin.cp.bean.WxCpAgentJsapiSignature; import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; import me.chanjar.weixin.cp.bean.WxCpProviderToken; import me.chanjar.weixin.cp.config.WxCpConfigStorage; @@ -124,6 +125,18 @@ public interface WxCpService extends WxService { */ WxJsapiSignature createJsapiSignature(String url) throws WxErrorException; + /** + *
+   *   创建调用wx.agentConfig时所需要的签名
+   *
+   * 详情请见:https://open.work.weixin.qq.com/api/doc/90000/90136/94313
+   * 
+ * + * @param url url + * @return the agent jsapi signature + * @throws WxErrorException + */ + WxCpAgentJsapiSignature createAgentJsapiSignature(String url) throws WxErrorException; /** * 小程序登录凭证校验 diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java index 0a06571c97..89221f1a1e 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/BaseWxCpServiceImpl.java @@ -20,6 +20,7 @@ import me.chanjar.weixin.common.util.http.*; import me.chanjar.weixin.common.util.json.GsonParser; import me.chanjar.weixin.cp.api.*; +import me.chanjar.weixin.cp.bean.WxCpAgentJsapiSignature; import me.chanjar.weixin.cp.bean.WxCpMaJsCode2SessionResult; import me.chanjar.weixin.cp.bean.WxCpProviderToken; import me.chanjar.weixin.cp.config.WxCpConfigStorage; @@ -171,6 +172,30 @@ public WxJsapiSignature createJsapiSignature(String url) throws WxErrorException return jsapiSignature; } + @Override + public WxCpAgentJsapiSignature createAgentJsapiSignature(String url) throws WxErrorException { + long timestamp = System.currentTimeMillis() / 1000; + String noncestr = RandomUtils.getRandomStr(); + String jsapiTicket = getAgentJsapiTicket(false); + String signature = SHA1.genWithAmple( + "jsapi_ticket=" + jsapiTicket, + "noncestr=" + noncestr, + "timestamp=" + timestamp, + "url=" + url + ); + + WxCpAgentJsapiSignature jsapiSignature = new WxCpAgentJsapiSignature(); + jsapiSignature.setTimestamp(timestamp); + jsapiSignature.setNonceStr(noncestr); + jsapiSignature.setUrl(url); + jsapiSignature.setSignature(signature); + + jsapiSignature.setCorpid(this.configStorage.getCorpId()); + jsapiSignature.setAgentid(this.configStorage.getAgentId()); + + return jsapiSignature; + } + @Override public WxCpMaJsCode2SessionResult jsCode2Session(String jsCode) throws WxErrorException { Map params = new HashMap<>(2); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgentJsapiSignature.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgentJsapiSignature.java new file mode 100644 index 0000000000..4562d9b9b0 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgentJsapiSignature.java @@ -0,0 +1,31 @@ +package me.chanjar.weixin.cp.bean; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + +/** + * 调用wx.agentConfig时所需要的签名信息 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class WxCpAgentJsapiSignature implements Serializable { + private static final long serialVersionUID = 2650119900835832545L; + + private String url; + + private String corpid; + + private Integer agentid; + + private long timestamp; + + private String nonceStr; + + private String signature; +}