Skip to content

Commit 4dd67f4

Browse files
committed
#707 企业微信增加应用管理里的设置和列表接口
1 parent 28b09f7 commit 4dd67f4

File tree

4 files changed

+127
-13
lines changed

4 files changed

+127
-13
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.cp.bean.WxCpAgent;
55

6+
import java.util.List;
7+
68
/**
79
* <pre>
810
* 管理企业号应用
11+
* 文档地址:https://work.weixin.qq.com/api/doc#10087
912
* Created by huansinho on 2018/4/13.
1013
* </pre>
1114
*
@@ -17,12 +20,33 @@ public interface WxCpAgentService {
1720
* <pre>
1821
* 获取企业号应用信息
1922
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
20-
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
23+
* 详情请见: https://work.weixin.qq.com/api/doc#10087
2124
* </pre>
2225
*
2326
* @param agentId 企业应用的id
2427
* @return 部门id
2528
*/
2629
WxCpAgent get(Integer agentId) throws WxErrorException;
2730

31+
/**
32+
* <pre>
33+
* 设置应用.
34+
* 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。
35+
* 详情请见: https://work.weixin.qq.com/api/doc#10088
36+
* </pre>
37+
*
38+
* @param agentInfo 应用信息
39+
*/
40+
void set(WxCpAgent agentInfo) throws WxErrorException;
41+
42+
/**
43+
* <pre>
44+
* 获取应用列表.
45+
* 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。
46+
* 详情请见: https://work.weixin.qq.com/api/doc#11214
47+
* </pre>
48+
*
49+
*/
50+
List<WxCpAgent> list() throws WxErrorException;
51+
2852
}

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

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

3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.JsonParser;
6+
import com.google.gson.reflect.TypeToken;
7+
import me.chanjar.weixin.common.error.WxError;
38
import me.chanjar.weixin.common.error.WxErrorException;
49
import me.chanjar.weixin.cp.api.WxCpAgentService;
510
import me.chanjar.weixin.cp.api.WxCpService;
611
import me.chanjar.weixin.cp.bean.WxCpAgent;
12+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
13+
14+
import java.util.List;
715

816

917
/**
@@ -15,6 +23,8 @@
1523
* @author <a href="https://github.com/huansinho">huansinho</a>
1624
*/
1725
public class WxCpAgentServiceImpl implements WxCpAgentService {
26+
private static final JsonParser JSON_PARSER = new JsonParser();
27+
1828
private WxCpService mainService;
1929

2030
public WxCpAgentServiceImpl(WxCpService mainService) {
@@ -32,4 +42,27 @@ public WxCpAgent get(Integer agentId) throws WxErrorException {
3242
return WxCpAgent.fromJson(responseContent);
3343
}
3444

45+
@Override
46+
public void set(WxCpAgent agentInfo) throws WxErrorException {
47+
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set";
48+
String responseContent = this.mainService.post(url, agentInfo.toJson());
49+
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
50+
if (jsonObject.get("errcode").getAsInt() != 0) {
51+
throw new WxErrorException(WxError.fromJson(responseContent));
52+
}
53+
}
54+
55+
@Override
56+
public List<WxCpAgent> list() throws WxErrorException {
57+
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list";
58+
String responseContent = this.mainService.get(url, null);
59+
JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
60+
if (jsonObject.get("errcode").getAsInt() != 0) {
61+
throw new WxErrorException(WxError.fromJson(responseContent));
62+
}
63+
64+
return WxCpGsonBuilder.create().fromJson(jsonObject.get("agentlist").toString(), new TypeToken<List<WxCpAgent>>() {
65+
}.getType());
66+
}
67+
3568
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package me.chanjar.weixin.cp.bean;
22

33
import com.google.gson.annotations.SerializedName;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Builder;
46
import lombok.Data;
7+
import lombok.NoArgsConstructor;
58
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
69

710
import java.io.Serializable;
@@ -16,6 +19,9 @@
1619
* @author <a href="https://github.com/huansinho">huansinho</a>
1720
*/
1821
@Data
22+
@Builder
23+
@NoArgsConstructor
24+
@AllArgsConstructor
1925
public class WxCpAgent implements Serializable {
2026
private static final long serialVersionUID = 5002894979081127234L;
2127

@@ -34,14 +40,17 @@ public class WxCpAgent implements Serializable {
3440
@SerializedName("square_logo_url")
3541
private String squareLogoUrl;
3642

43+
@SerializedName("logo_mediaid")
44+
private String logoMediaId;
45+
3746
@SerializedName("description")
3847
private String description;
3948

4049
@SerializedName("allow_userinfos")
4150
private Users allowUserInfos;
4251

4352
@SerializedName("allow_partys")
44-
private Partys allowParties;
53+
private Parties allowParties;
4554

4655
@SerializedName("allow_tags")
4756
private Tags allowTags;
@@ -82,7 +91,7 @@ public class User implements Serializable {
8291
}
8392

8493
@Data
85-
public class Partys {
94+
public class Parties {
8695
@SerializedName("partyid")
8796
private List<Integer> partyIds = null;
8897
}

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

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.common.error.WxErrorException;
5+
import me.chanjar.weixin.cp.api.ApiTestModule;
36
import me.chanjar.weixin.cp.api.WxCpAgentService;
47
import me.chanjar.weixin.cp.api.WxCpService;
58
import me.chanjar.weixin.cp.bean.WxCpAgent;
69
import org.testng.Assert;
10+
import org.testng.annotations.Guice;
711
import org.testng.annotations.Test;
812

13+
import java.util.List;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
916
import static org.mockito.Mockito.mock;
1017
import static org.mockito.Mockito.when;
18+
import static org.testng.Assert.assertEquals;
1119

1220

1321
/**
@@ -18,24 +26,64 @@
1826
*
1927
* @author <a href="https://github.com/huansinho">huansinho</a>
2028
*/
29+
@Guice(modules = ApiTestModule.class)
2130
public class WxCpAgentServiceImplTest {
22-
23-
protected WxCpService wxService = mock(WxCpService.class);
31+
@Inject
32+
private WxCpService wxCpService;
2433

2534
@Test
2635
public void testGet() throws Exception {
27-
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
28-
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson);
29-
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
36+
final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId();
37+
WxCpAgent wxCpAgent = this.wxCpService.getAgentService().get(agentId);
38+
39+
assertThat(wxCpAgent.getAgentId()).isEqualTo(agentId);
40+
41+
assertThat(wxCpAgent.getAllowUserInfos().getUsers().toArray()).isNotEmpty();
42+
assertThat(wxCpAgent.getAllowParties().getPartyIds().toArray()).isNotEmpty();
43+
assertThat(wxCpAgent.getAllowTags().getTagIds().toArray()).isNotEmpty();
44+
}
45+
46+
@Test
47+
public void testSet() throws WxErrorException {
48+
final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId();
49+
50+
this.wxCpService.getAgentService().set(WxCpAgent.builder()
51+
.description("abcddd")
52+
.logoMediaId("aaaaaaaaaaaaaa")
53+
.agentId(agentId)
54+
.build());
55+
}
56+
57+
@Test
58+
public void testList() throws WxErrorException {
59+
List<WxCpAgent> list = this.wxCpService.getAgentService().list();
60+
61+
assertThat(list).isNotEmpty();
62+
63+
assertThat(list.get(0).getAgentId()).isNotNull();
64+
assertThat(list.get(0).getName()).isNotEmpty();
65+
assertThat(list.get(0).getSquareLogoUrl()).isNotEmpty();
66+
}
67+
68+
public static class MockTest {
69+
private WxCpService wxService = mock(WxCpService.class);
70+
71+
@Test
72+
public void testGet() throws Exception {
73+
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
74+
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson);
75+
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
76+
77+
WxCpAgentService wxAgentService = this.wxService.getAgentService();
78+
WxCpAgent wxCpAgent = wxAgentService.get(9);
3079

31-
WxCpAgentService wxAgentService = this.wxService.getAgentService();
32-
WxCpAgent wxCpAgent = wxAgentService.get(9);
80+
assertEquals(9, wxCpAgent.getAgentId().intValue());
3381

34-
Assert.assertEquals(9, wxCpAgent.getAgentId().intValue());
82+
assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
3583

36-
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
84+
assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
3785

38-
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
86+
}
3987

4088
}
4189

0 commit comments

Comments
 (0)