Skip to content

Commit a0240e7

Browse files
huansinhobinarywang
authored andcommitted
#536 企业号模块增加获取企业号应用相关接口
* 定义《企业号应用》的bean * 增加《获取企业号应用》接口实现 * 增加获取测试企业号应用信息测试类
1 parent 617f861 commit a0240e7

File tree

7 files changed

+258
-0
lines changed

7 files changed

+258
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package me.chanjar.weixin.cp.api;
2+
3+
import me.chanjar.weixin.common.exception.WxErrorException;
4+
import me.chanjar.weixin.cp.bean.WxCpAgent;
5+
import me.chanjar.weixin.cp.bean.WxCpDepart;
6+
7+
/**
8+
* <pre>
9+
* 管理企业号应用
10+
* Created by huansinho on 2018/4/13.
11+
* </pre>
12+
*
13+
* @author <a href="https://github.com/huansinho">huansinho</a>
14+
*/
15+
public interface WxCpAgentService {
16+
17+
/**
18+
* <pre>
19+
* 获取企业号应用信息
20+
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
21+
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
22+
* </pre>
23+
*
24+
* @param agentId 企业应用的id
25+
* @return 部门id
26+
*/
27+
WxCpAgent get(Integer agentId) throws WxErrorException;
28+
29+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ public interface WxCpService {
249249
*/
250250
WxCpUserService getUserService();
251251

252+
WxCpAgentService getAgentService();
253+
252254
/**
253255
* http请求对象
254256
*/
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.gson.JsonElement;
4+
import com.google.gson.JsonParser;
5+
import com.google.gson.reflect.TypeToken;
6+
import me.chanjar.weixin.common.exception.WxErrorException;
7+
import me.chanjar.weixin.cp.api.WxCpAgentService;
8+
import me.chanjar.weixin.cp.api.WxCpService;
9+
import me.chanjar.weixin.cp.bean.WxCpAgent;
10+
import me.chanjar.weixin.cp.bean.WxCpDepart;
11+
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
12+
13+
import java.util.List;
14+
15+
16+
/**
17+
* <pre>
18+
* 管理企业号应用
19+
* Created by huansinho on 2018/4/13.
20+
* </pre>
21+
*
22+
* @author <a href="https://github.com/huansinho">huansinho</a>
23+
*/
24+
public class WxCpAgentServiceImpl implements WxCpAgentService {
25+
private WxCpService mainService;
26+
27+
public WxCpAgentServiceImpl(WxCpService mainService) {
28+
this.mainService = mainService;
29+
}
30+
31+
@Override
32+
public WxCpAgent get(Integer agentId) throws WxErrorException {
33+
34+
String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get";
35+
if (agentId != null) {
36+
url += "?agentid=" + agentId;
37+
} else {
38+
throw new IllegalArgumentException("缺少agentid参数");
39+
}
40+
String responseContent = this.mainService.get(url, null);
41+
return WxCpAgent.fromJson(responseContent);
42+
}
43+
44+
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public abstract class WxCpServiceAbstractImpl<H, P> implements WxCpService, Requ
3939
private WxCpMenuService menuService = new WxCpMenuServiceImpl(this);
4040
private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this);
4141
private WxCpTagService tagService = new WxCpTagServiceImpl(this);
42+
private WxCpAgentService agentService = new WxCpAgentServiceImpl(this);
4243

4344
/**
4445
* 全局的是否正在刷新access token的锁
@@ -368,4 +369,13 @@ public void setOauth2Service(WxCpOAuth2Service oauth2Service) {
368369
public void setTagService(WxCpTagService tagService) {
369370
this.tagService = tagService;
370371
}
372+
373+
@Override
374+
public WxCpAgentService getAgentService() {
375+
return agentService;
376+
}
377+
378+
public void setAgentService(WxCpAgentService agentService) {
379+
this.agentService = agentService;
380+
}
371381
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
import java.io.Serializable;
8+
import java.util.List;
9+
10+
/**
11+
* <pre>
12+
* 企业号应用信息.
13+
* Created by huansinho on 2018/4/13.
14+
* </pre>
15+
*
16+
* @author <a href="https://github.com/huansinho">huansinho</a>
17+
*/
18+
@Data
19+
public class WxCpAgent implements Serializable {
20+
21+
@SerializedName("errcode")
22+
private Integer errcode;
23+
24+
@SerializedName("errmsg")
25+
private String errmsg;
26+
27+
@SerializedName("agentid")
28+
private Integer agentid;
29+
30+
@SerializedName("name")
31+
private String name;
32+
33+
@SerializedName("square_logo_url")
34+
private String squareLogoUrl;
35+
36+
@SerializedName("description")
37+
private String description;
38+
39+
@SerializedName("allow_userinfos")
40+
private Users allowUserinfos;
41+
42+
@SerializedName("allow_partys")
43+
private Partys allowPartys;
44+
45+
@SerializedName("allow_tags")
46+
private Tags allowTags;
47+
48+
@SerializedName("close")
49+
private Integer close;
50+
51+
@SerializedName("redirect_domain")
52+
private String redirectDomain;
53+
54+
@SerializedName("report_location_flag")
55+
private Integer reportLocationFlag;
56+
57+
@SerializedName("isreportenter")
58+
private Integer isreportenter;
59+
60+
@SerializedName("home_url")
61+
private String homeUrl;
62+
63+
public static WxCpAgent fromJson(String json) {
64+
return WxCpGsonBuilder.create().fromJson(json, WxCpAgent.class);
65+
}
66+
67+
public String toJson() {
68+
return WxCpGsonBuilder.create().toJson(this);
69+
}
70+
71+
@Data
72+
public static class Users implements Serializable {
73+
@SerializedName("user")
74+
private List<User> user;
75+
}
76+
77+
78+
@Data
79+
public class User implements Serializable {
80+
@SerializedName("userid")
81+
private String userid;
82+
}
83+
84+
@Data
85+
public class Partys {
86+
@SerializedName("partyid")
87+
private List<Integer> partyids = null;
88+
}
89+
90+
@Data
91+
public class Tags {
92+
@SerializedName("tagid")
93+
private List<Integer> tagids = null;
94+
}
95+
96+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package me.chanjar.weixin.cp.api.impl;
2+
3+
import com.google.inject.Inject;
4+
import me.chanjar.weixin.common.api.WxConsts;
5+
import me.chanjar.weixin.common.bean.menu.WxMenu;
6+
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
7+
import me.chanjar.weixin.cp.api.ApiTestModule;
8+
import me.chanjar.weixin.cp.api.WxCpAgentService;
9+
import me.chanjar.weixin.cp.api.WxCpService;
10+
import me.chanjar.weixin.cp.bean.WxCpAgent;
11+
import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage;
12+
import org.mockito.Mock;
13+
import org.testng.Assert;
14+
import org.testng.annotations.DataProvider;
15+
import org.testng.annotations.Guice;
16+
import org.testng.annotations.Test;
17+
18+
import static org.testng.Assert.assertEquals;
19+
import static org.testng.Assert.assertNotNull;
20+
import static org.mockito.Mockito.*;
21+
22+
23+
/**
24+
* <pre>
25+
* 管理企业号应用-测试
26+
* Created by huansinho on 2018/4/13.
27+
* </pre>
28+
*
29+
* @author <a href="https://github.com/huansinho">huansinho</a>
30+
*/
31+
public class WxCpAgentServiceImplTest {
32+
33+
protected WxCpService wxService = mock(WxCpService.class);
34+
35+
@Test
36+
public void testGet() throws Exception {
37+
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\": \"\"}";
38+
when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson);
39+
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
40+
41+
WxCpAgentService wxAgentService = this.wxService.getAgentService();
42+
WxCpAgent wxCpAgent = wxAgentService.get(9);
43+
44+
Assert.assertEquals(9, wxCpAgent.getAgentid().intValue());
45+
46+
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowPartys().getPartyids().toArray());
47+
48+
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagids().toArray());
49+
50+
}
51+
52+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package me.chanjar.weixin.cp.bean;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
6+
/**
7+
* Created by huansinho on 2018/4/13.
8+
*/
9+
@Test
10+
public class WxCpAgentTest {
11+
12+
public void testDeserialize() {
13+
String json = "{\"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\": \"\"}";
14+
15+
WxCpAgent wxCpAgent = WxCpAgent.fromJson(json);
16+
17+
Assert.assertEquals(9, wxCpAgent.getAgentid().intValue());
18+
19+
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowPartys().getPartyids().toArray());
20+
21+
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagids().toArray());
22+
23+
}
24+
25+
}

0 commit comments

Comments
 (0)