Skip to content

Commit 9b78acd

Browse files
authored
🆕 #2846 【企业微信】增加待开发应用获取带参授权链接的接口
1 parent b085309 commit 9b78acd

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed

weixin-java-cp/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@
8888
<artifactId>moco-runner</artifactId>
8989
<scope>test</scope>
9090
</dependency>
91+
92+
<dependency>
93+
<groupId>com.fasterxml.jackson.core</groupId>
94+
<artifactId>jackson-core</artifactId>
95+
<version>2.13.4</version>
96+
<scope>test</scope>
97+
</dependency>
9198
</dependencies>
9299

93100
<build>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
/**
8+
* @author freedom
9+
* @date 2022/10/20 16:36
10+
*/
11+
@Data
12+
public class WxTpCustomizedAuthUrl extends WxCpBaseResp {
13+
14+
/**
15+
* 可用来生成二维码的授权url,需要开发者自行生成为二维码
16+
*/
17+
@SerializedName("qrcode_url")
18+
private String qrCodeURL;
19+
20+
/**
21+
* 有效期(秒)。10天过期。
22+
*/
23+
@SerializedName("expires_in")
24+
private Integer expiresIn;
25+
26+
/**
27+
* From json wx cp tp customized auth url.
28+
*
29+
* @param json the json
30+
* @return the wx cp tp customized auth url
31+
*/
32+
public static WxTpCustomizedAuthUrl fromJson(String json) {
33+
return WxCpGsonBuilder.create().fromJson(json, WxTpCustomizedAuthUrl.class);
34+
}
35+
36+
public String toJson() {
37+
return WxCpGsonBuilder.create().toJson(this);
38+
}
39+
40+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/constant/WxCpApiPathConsts.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ interface Tp {
781781
*/
782782
String GET_LOGIN_INFO = "/cgi-bin/service/get_login_info";
783783

784+
/**
785+
* The constant GET_CUSTOMIZED_AUTH_URL.
786+
*/
787+
String GET_CUSTOMIZED_AUTH_URL = "/cgi-bin/service/get_customized_auth_url";
788+
784789

785790
/**
786791
* The constant CONTACT_SEARCH.

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
import me.chanjar.weixin.cp.bean.*;
1111
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
1212

13+
import javax.validation.constraints.NotBlank;
14+
import javax.validation.constraints.NotEmpty;
15+
import java.util.List;
16+
1317
/**
1418
* 企业微信第三方应用API的Service.
1519
*
@@ -389,6 +393,18 @@ public interface WxCpTpService {
389393
*/
390394
WxTpLoginInfo getLoginInfo(String authCode) throws WxErrorException;
391395

396+
/**
397+
* 获取带参授权链接
398+
* <p>
399+
* 文档地址:https://developer.work.weixin.qq.com/document/path/95436
400+
*
401+
* @param state state
402+
* @param templateIdList 代开发自建应用模版ID列表,数量不能超过9个
403+
* @return customized auth url
404+
* @throws WxErrorException the wx error exception
405+
*/
406+
WxTpCustomizedAuthUrl getCustomizedAuthUrl(@NotBlank String state, @NotEmpty List<String> templateIdList) throws WxErrorException;
407+
392408
/**
393409
* 获取服务商providerToken
394410
*

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@
2222
import me.chanjar.weixin.common.util.http.SimpleGetRequestExecutor;
2323
import me.chanjar.weixin.common.util.http.SimplePostRequestExecutor;
2424
import me.chanjar.weixin.common.util.json.GsonParser;
25+
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
2526
import me.chanjar.weixin.cp.bean.*;
2627
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
2728
import me.chanjar.weixin.cp.tp.service.*;
2829
import org.apache.commons.lang3.StringUtils;
2930

31+
import javax.validation.constraints.NotBlank;
32+
import javax.validation.constraints.NotEmpty;
3033
import java.io.File;
3134
import java.io.IOException;
3235
import java.net.URLEncoder;
3336
import java.util.HashMap;
37+
import java.util.List;
3438
import java.util.Map;
3539

3640
import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Tp.*;
@@ -534,6 +538,16 @@ public WxTpLoginInfo getLoginInfo(String authCode) throws WxErrorException {
534538
return WxTpLoginInfo.fromJson(responseText);
535539
}
536540

541+
@Override
542+
public WxTpCustomizedAuthUrl getCustomizedAuthUrl(@NotBlank String state, @NotEmpty List<String> templateIdList) throws WxErrorException {
543+
JsonObject jsonObject = new JsonObject();
544+
jsonObject.addProperty("state", state);
545+
jsonObject.add("templateid_list", WxGsonBuilder.create().toJsonTree(templateIdList).getAsJsonArray());
546+
547+
String responseText = post(configStorage.getApiUrl(GET_CUSTOMIZED_AUTH_URL) + "?provider_access_token=" + getWxCpProviderToken(), jsonObject.toString(), true);
548+
return WxTpCustomizedAuthUrl.fromJson(responseText);
549+
}
550+
537551
@Override
538552
public String getWxCpProviderToken() throws WxErrorException {
539553
if (this.configStorage.isProviderTokenExpired()) {

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImplTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@
22

33
import com.google.gson.JsonObject;
44
import me.chanjar.weixin.common.error.WxErrorException;
5+
import me.chanjar.weixin.common.redis.RedissonWxRedisOps;
56
import me.chanjar.weixin.cp.bean.WxCpTpAuthInfo;
67
import me.chanjar.weixin.cp.bean.WxCpTpCorp;
78
import me.chanjar.weixin.cp.bean.WxCpTpPermanentCodeInfo;
9+
import me.chanjar.weixin.cp.bean.WxTpCustomizedAuthUrl;
810
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
11+
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
912
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
13+
import me.chanjar.weixin.cp.config.impl.WxCpTpRedissonConfigImpl;
1014
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
15+
import org.apache.http.util.Asserts;
1116
import org.mockito.Mockito;
17+
import org.redisson.Redisson;
18+
import org.redisson.api.RedissonClient;
19+
import org.redisson.config.Config;
1220
import org.testng.Assert;
21+
import org.testng.annotations.BeforeMethod;
1322
import org.testng.annotations.Test;
1423

24+
import java.util.Arrays;
25+
import java.util.Collections;
1526
import java.util.List;
1627
import java.util.Objects;
1728

@@ -27,6 +38,55 @@
2738
public class BaseWxCpTpServiceImplTest {
2839
private final WxCpTpService tpService = Mockito.spy(new WxCpTpServiceApacheHttpClientImpl());
2940

41+
/**
42+
* The constant PROVIDER_CORP_ID.
43+
*/
44+
public static final String PROVIDER_CORP_ID = "xxxxxx";
45+
/**
46+
* The constant PROVIDER_SECRET.
47+
*/
48+
public static final String PROVIDER_SECRET = "xxxxxx";
49+
/**
50+
* The constant REDIS_ADDR.
51+
*/
52+
public static final String REDIS_ADDR = "redis://xxx.xxx.xxx.xxx:6379";
53+
/**
54+
* The constant REDIS_PASSWD.
55+
*/
56+
public static final String REDIS_PASSWD = "xxxxxx";
57+
58+
private WxCpTpService wxCpTpService;
59+
60+
/**
61+
* Sets up.
62+
*/
63+
@BeforeMethod
64+
public void setUp() {
65+
wxCpTpService = new WxCpTpServiceApacheHttpClientImpl();
66+
wxCpTpService.setWxCpTpConfigStorage(wxCpTpConfigStorage());
67+
}
68+
69+
/**
70+
* Wx cp tp config storage wx cp tp config storage.
71+
*
72+
* @return the wx cp tp config storage
73+
*/
74+
public WxCpTpConfigStorage wxCpTpConfigStorage() {
75+
return WxCpTpRedissonConfigImpl.builder().corpId(PROVIDER_CORP_ID).providerSecret(PROVIDER_SECRET).wxRedisOps(new RedissonWxRedisOps(redissonClient())).build();
76+
}
77+
78+
/**
79+
* Redisson client redisson client.
80+
*
81+
* @return the redisson client
82+
*/
83+
public RedissonClient redissonClient() {
84+
Config config = new Config();
85+
config.useSingleServer().setAddress(REDIS_ADDR).setConnectTimeout(10 * 1000).setDatabase(6)
86+
.setPassword(REDIS_PASSWD).setConnectionMinimumIdleSize(2).setConnectionPoolSize(2);
87+
return Redisson.create(config);
88+
}
89+
3090
/**
3191
* Test check signature.
3292
*/
@@ -444,4 +504,14 @@ public void testSetTmpDirFile() {
444504
@Test
445505
public void testGetRequestHttp() {
446506
}
507+
508+
@Test
509+
public void testGetCustomizedAuthUrl() throws WxErrorException {
510+
String state = "test";
511+
List<String> templateIdList = Arrays.asList("");
512+
513+
final WxTpCustomizedAuthUrl customizedAuthUrl = wxCpTpService.getCustomizedAuthUrl(state, templateIdList);
514+
Assert.assertNotNull(customizedAuthUrl);
515+
Assert.assertEquals((long) customizedAuthUrl.getErrcode(), 0);
516+
}
447517
}

0 commit comments

Comments
 (0)