From 17fe73899cccbfc88a9f2c1696daff4c1761b992 Mon Sep 17 00:00:00 2001 From: "jianliang.bai" Date: Fri, 21 Oct 2022 10:42:11 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=E3=80=90=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=91=E6=96=B0=E5=A2=9E=E5=BE=85=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=8E=B7=E5=8F=96=E5=B8=A6=E5=8F=82=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E9=93=BE=E6=8E=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/bean/WxTpCustomizedAuthUrl.java | 40 +++++++++++++++++++ .../weixin/cp/constant/WxCpApiPathConsts.java | 5 +++ .../weixin/cp/tp/service/WxCpTpService.java | 16 ++++++++ .../service/impl/BaseWxCpTpServiceImpl.java | 14 +++++++ 4 files changed, 75 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxTpCustomizedAuthUrl.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxTpCustomizedAuthUrl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxTpCustomizedAuthUrl.java new file mode 100644 index 0000000000..5edf2b7366 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxTpCustomizedAuthUrl.java @@ -0,0 +1,40 @@ +package me.chanjar.weixin.cp.bean; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +/** + * @author freedom + * @date 2022/10/20 16:36 + */ +@Data +public class WxTpCustomizedAuthUrl extends WxCpBaseResp { + + /** + * 可用来生成二维码的授权url,需要开发者自行生成为二维码 + */ + @SerializedName("qrcode_url") + private String qrCodeURL; + + /** + * 有效期(秒)。10天过期。 + */ + @SerializedName("expires_in") + private Integer expiresIn; + + /** + * From json wx cp tp customized auth url. + * + * @param json the json + * @return the wx cp tp customized auth url + */ + public static WxTpCustomizedAuthUrl fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxTpCustomizedAuthUrl.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java index 9bf5ff5f5a..24f0112bc5 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java @@ -775,6 +775,11 @@ interface Tp { */ String GET_LOGIN_INFO = "/cgi-bin/service/get_login_info"; + /** + * The constant GET_CUSTOMIZED_AUTH_URL. + */ + String GET_CUSTOMIZED_AUTH_URL = "/cgi-bin/service/get_customized_auth_url"; + /** * The constant CONTACT_SEARCH. diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java index 838e5e43dd..ecb7084c73 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java @@ -10,6 +10,10 @@ import me.chanjar.weixin.cp.bean.*; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import java.util.List; + /** * 企业微信第三方应用API的Service. * @@ -389,6 +393,18 @@ public interface WxCpTpService { */ WxTpLoginInfo getLoginInfo(String authCode) throws WxErrorException; + /** + * 获取带参授权链接 + *

+ * 文档地址:https://developer.work.weixin.qq.com/document/path/95436 + * + * @param state state + * @param templateIdList 代开发自建应用模版ID列表,数量不能超过9个 + * @return customized auth url + * @throws WxErrorException the wx error exception + */ + WxTpCustomizedAuthUrl getCustomizedAuthUrl(@NotBlank String state, @NotEmpty List templateIdList) throws WxErrorException; + /** * 获取服务商providerToken * diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java index e3e95a3e8f..c089cb3e22 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java @@ -22,15 +22,19 @@ import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor; import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor; import me.chanjar.weixin.common.util.json.GsonParser; +import me.chanjar.weixin.common.util.json.WxGsonBuilder; import me.chanjar.weixin.cp.bean.*; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; import me.chanjar.weixin.cp.tp.service.*; import org.apache.commons.lang3.StringUtils; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; import java.io.File; import java.io.IOException; import java.net.URLEncoder; import java.util.HashMap; +import java.util.List; import java.util.Map; import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.*; @@ -534,6 +538,16 @@ public WxTpLoginInfo getLoginInfo(String authCode) throws WxErrorException { return WxTpLoginInfo.fromJson(responseText); } + @Override + public WxTpCustomizedAuthUrl getCustomizedAuthUrl(@NotBlank String state, @NotEmpty List templateIdList) throws WxErrorException { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("state", state); + jsonObject.add("templateid_list", WxGsonBuilder.create().toJsonTree(templateIdList).getAsJsonArray()); + + String responseText = post(configStorage.getApiUrl(GET_CUSTOMIZED_AUTH_URL) + "?provider_access_token=" + getWxCpProviderToken(), jsonObject.toString(), true); + return WxTpCustomizedAuthUrl.fromJson(responseText); + } + @Override public String getWxCpProviderToken() throws WxErrorException { if (this.configStorage.isProviderTokenExpired()) { From 8cd84c5a8ec2edee36968f018e8a8de983378137 Mon Sep 17 00:00:00 2001 From: "jianliang.bai" Date: Fri, 21 Oct 2022 15:27:21 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=E3=80=90=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E3=80=91=E6=96=B0=E5=A2=9E=E5=BE=85=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=BA=94=E7=94=A8=E8=8E=B7=E5=8F=96=E5=B8=A6=E5=8F=82=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E9=93=BE=E6=8E=A5=E6=8E=A5=E5=8F=A3=E5=8D=95=E5=85=83?= =?UTF-8?q?=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- weixin-java-cp/pom.xml | 7 ++ .../impl/BaseWxCpTpServiceImplTest.java | 70 +++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/weixin-java-cp/pom.xml b/weixin-java-cp/pom.xml index b1c6677208..c92ff08586 100644 --- a/weixin-java-cp/pom.xml +++ b/weixin-java-cp/pom.xml @@ -88,6 +88,13 @@ moco-runner test + + + com.fasterxml.jackson.core + jackson-core + 2.13.4 + test + diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java index edb284f253..8235e48f93 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java @@ -2,16 +2,27 @@ import com.google.gson.JsonObject; import me.chanjar.weixin.common.error.WxErrorException; +import me.chanjar.weixin.common.redis.RedissonWxRedisOps; import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo; import me.chanjar.weixin.cp.bean.WxCpTpCorp; import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo; +import me.chanjar.weixin.cp.bean.WxTpCustomizedAuthUrl; import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl; +import me.chanjar.weixin.cp.config.impl.WxCpTpRedissonConfigImpl; import me.chanjar.weixin.cp.tp.service.WxCpTpService; +import org.apache.http.util.Asserts; import org.mockito.Mockito; +import org.redisson.Redisson; +import org.redisson.api.RedissonClient; +import org.redisson.config.Config; import org.testng.Assert; +import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Objects; @@ -27,6 +38,55 @@ public class BaseWxCpTpServiceImplTest { private final WxCpTpService tpService = Mockito.spy(new WxCpTpServiceApacheHttpClientImpl()); + /** + * The constant PROVIDER_CORP_ID. + */ + public static final String PROVIDER_CORP_ID = "xxxxxx"; + /** + * The constant PROVIDER_SECRET. + */ + public static final String PROVIDER_SECRET = "xxxxxx"; + /** + * The constant REDIS_ADDR. + */ + public static final String REDIS_ADDR = "redis://xxx.xxx.xxx.xxx:6379"; + /** + * The constant REDIS_PASSWD. + */ + public static final String REDIS_PASSWD = "xxxxxx"; + + private WxCpTpService wxCpTpService; + + /** + * Sets up. + */ + @BeforeMethod + public void setUp() { + wxCpTpService = new WxCpTpServiceApacheHttpClientImpl(); + wxCpTpService.setWxCpTpConfigStorage(wxCpTpConfigStorage()); + } + + /** + * Wx cp tp config storage wx cp tp config storage. + * + * @return the wx cp tp config storage + */ + public WxCpTpConfigStorage wxCpTpConfigStorage() { + return WxCpTpRedissonConfigImpl.builder().corpId(PROVIDER_CORP_ID).providerSecret(PROVIDER_SECRET).wxRedisOps(new RedissonWxRedisOps(redissonClient())).build(); + } + + /** + * Redisson client redisson client. + * + * @return the redisson client + */ + public RedissonClient redissonClient() { + Config config = new Config(); + config.useSingleServer().setAddress(REDIS_ADDR).setConnectTimeout(10 * 1000).setDatabase(6) + .setPassword(REDIS_PASSWD).setConnectionMinimumIdleSize(2).setConnectionPoolSize(2); + return Redisson.create(config); + } + /** * Test check signature. */ @@ -444,4 +504,14 @@ public void testSetTmpDirFile() { @Test public void testGetRequestHttp() { } + + @Test + public void testGetCustomizedAuthUrl() throws WxErrorException { + String state = "test"; + List templateIdList = Arrays.asList(""); + + final WxTpCustomizedAuthUrl customizedAuthUrl = wxCpTpService.getCustomizedAuthUrl(state, templateIdList); + Assert.assertNotNull(customizedAuthUrl); + Assert.assertEquals((long) customizedAuthUrl.getErrcode(), 0); + } }