From 2a353849e840fc0d8abe7cf5f9186f384f744928 Mon Sep 17 00:00:00 2001 From: lqh Date: Fri, 13 Apr 2018 16:16:36 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=AE=9A=E4=B9=89=E3=80=8A=E4=BC=81?= =?UTF-8?q?=E4=B8=9A=E5=8F=B7=E5=BA=94=E7=94=A8=E3=80=8B=E7=9A=84bean?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/chanjar/weixin/cp/bean/WxCpAgent.java | 96 +++++++++++++++++++ .../chanjar/weixin/cp/bean/WxCpAgentTest.java | 25 +++++ 2 files changed, 121 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java create mode 100644 weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java new file mode 100644 index 0000000000..eb7069bdc8 --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAgent.java @@ -0,0 +1,96 @@ +package me.chanjar.weixin.cp.bean; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +import java.io.Serializable; +import java.util.List; + +/** + *
+ * 企业号应用信息.
+ * Created by huansinho on 2018/4/13.
+ * 
+ * + * @author huansinho + */ +@Data +public class WxCpAgent implements Serializable { + + @SerializedName("errcode") + private Integer errcode; + + @SerializedName("errmsg") + private String errmsg; + + @SerializedName("agentid") + private Integer agentid; + + @SerializedName("name") + private String name; + + @SerializedName("square_logo_url") + private String squareLogoUrl; + + @SerializedName("description") + private String description; + + @SerializedName("allow_userinfos") + private Users allowUserinfos; + + @SerializedName("allow_partys") + private Partys allowPartys; + + @SerializedName("allow_tags") + private Tags allowTags; + + @SerializedName("close") + private Integer close; + + @SerializedName("redirect_domain") + private String redirectDomain; + + @SerializedName("report_location_flag") + private Integer reportLocationFlag; + + @SerializedName("isreportenter") + private Integer isreportenter; + + @SerializedName("home_url") + private String homeUrl; + + public static WxCpAgent fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpAgent.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + + @Data + public static class Users implements Serializable { + @SerializedName("user") + private List user; + } + + + @Data + public class User implements Serializable { + @SerializedName("userid") + private String userid; + } + + @Data + public class Partys { + @SerializedName("partyid") + private List partyids = null; + } + + @Data + public class Tags { + @SerializedName("tagid") + private List tagids = null; + } + +} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java new file mode 100644 index 0000000000..6a2b87c588 --- /dev/null +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/bean/WxCpAgentTest.java @@ -0,0 +1,25 @@ +package me.chanjar.weixin.cp.bean; + +import org.testng.Assert; +import org.testng.annotations.Test; + +/** + * Created by huansinho on 2018/4/13. + */ +@Test +public class WxCpAgentTest { + + public void testDeserialize() { + 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\": \"\"}"; + + WxCpAgent wxCpAgent = WxCpAgent.fromJson(json); + + Assert.assertEquals(9, wxCpAgent.getAgentid().intValue()); + + Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowPartys().getPartyids().toArray()); + + Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagids().toArray()); + + } + +} From 71172c1cb4f83054ecd761b85b61b42cc2a44f56 Mon Sep 17 00:00:00 2001 From: lqh Date: Fri, 13 Apr 2018 17:00:50 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E3=80=8A=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E4=BC=81=E4=B8=9A=E5=8F=B7=E5=BA=94=E7=94=A8=E3=80=8B?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../weixin/cp/api/WxCpAgentService.java | 29 ++++++++++++ .../me/chanjar/weixin/cp/api/WxCpService.java | 2 + .../cp/api/impl/WxCpAgentServiceImpl.java | 44 +++++++++++++++++++ .../cp/api/impl/WxCpServiceAbstractImpl.java | 10 +++++ 4 files changed, 85 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java new file mode 100644 index 0000000000..26e3f6938f --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpAgentService.java @@ -0,0 +1,29 @@ +package me.chanjar.weixin.cp.api; + +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.bean.WxCpAgent; +import me.chanjar.weixin.cp.bean.WxCpDepart; + +/** + *
+ *  管理企业号应用
+ *  Created by huansinho on 2018/4/13.
+ * 
+ * + * @author huansinho + */ +public interface WxCpAgentService { + + /** + *
+   * 获取企业号应用信息
+   * 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
+   * 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
+   * 
+ * + * @param agentId 企业应用的id + * @return 部门id + */ + WxCpAgent get(Integer agentId) throws WxErrorException; + +} 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 3a63de9899..ed912bf8d7 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 @@ -249,6 +249,8 @@ public interface WxCpService { */ WxCpUserService getUserService(); + WxCpAgentService getAgentService(); + /** * http请求对象 */ diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java new file mode 100644 index 0000000000..878f7f2d8d --- /dev/null +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java @@ -0,0 +1,44 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.gson.JsonElement; +import com.google.gson.JsonParser; +import com.google.gson.reflect.TypeToken; +import me.chanjar.weixin.common.exception.WxErrorException; +import me.chanjar.weixin.cp.api.WxCpAgentService; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpAgent; +import me.chanjar.weixin.cp.bean.WxCpDepart; +import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; + +import java.util.List; + + +/** + *
+ *  管理企业号应用
+ *  Created by huansinho on 2018/4/13.
+ * 
+ * + * @author huansinho + */ +public class WxCpAgentServiceImpl implements WxCpAgentService { + private WxCpService mainService; + + public WxCpAgentServiceImpl(WxCpService mainService) { + this.mainService = mainService; + } + + @Override + public WxCpAgent get(Integer agentId) throws WxErrorException { + + String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/get"; + if (agentId != null) { + url += "?agentid=" + agentId; + } else { + throw new IllegalArgumentException("缺少agentid参数"); + } + String responseContent = this.mainService.get(url, null); + return WxCpAgent.fromJson(responseContent); + } + +} diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java index 797e4a4c97..c7ebfa1d29 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpServiceAbstractImpl.java @@ -39,6 +39,7 @@ public abstract class WxCpServiceAbstractImpl implements WxCpService, Requ private WxCpMenuService menuService = new WxCpMenuServiceImpl(this); private WxCpOAuth2Service oauth2Service = new WxCpOAuth2ServiceImpl(this); private WxCpTagService tagService = new WxCpTagServiceImpl(this); + private WxCpAgentService agentService = new WxCpAgentServiceImpl(this); /** * 全局的是否正在刷新access token的锁 @@ -368,4 +369,13 @@ public void setOauth2Service(WxCpOAuth2Service oauth2Service) { public void setTagService(WxCpTagService tagService) { this.tagService = tagService; } + + @Override + public WxCpAgentService getAgentService() { + return agentService; + } + + public void setAgentService(WxCpAgentService agentService) { + this.agentService = agentService; + } } From 22fe3bce84901cd160f27088a921e25a39994443 Mon Sep 17 00:00:00 2001 From: lqh Date: Fri, 13 Apr 2018 17:46:49 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BC=81=E4=B8=9A=E5=8F=B7=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E6=B5=8B=E8=AF=95=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cp/api/impl/WxCpAgentServiceImplTest.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java new file mode 100644 index 0000000000..f4b2aac052 --- /dev/null +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java @@ -0,0 +1,52 @@ +package me.chanjar.weixin.cp.api.impl; + +import com.google.inject.Inject; +import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.bean.menu.WxMenu; +import me.chanjar.weixin.common.bean.menu.WxMenuButton; +import me.chanjar.weixin.cp.api.ApiTestModule; +import me.chanjar.weixin.cp.api.WxCpAgentService; +import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.bean.WxCpAgent; +import me.chanjar.weixin.cp.config.WxCpInMemoryConfigStorage; +import org.mockito.Mock; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Guice; +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertNotNull; +import static org.mockito.Mockito.*; + + +/** + *
+ *  管理企业号应用-测试
+ *  Created by huansinho on 2018/4/13.
+ * 
+ * + * @author huansinho + */ +public class WxCpAgentServiceImplTest { + + protected WxCpService wxService = mock(WxCpService.class); + + @Test + public void testGet() throws Exception { + 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\": \"\"}"; + when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/agent/get?agentid=9", null)).thenReturn(returnJson); + when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService)); + + WxCpAgentService wxAgentService = this.wxService.getAgentService(); + WxCpAgent wxCpAgent = wxAgentService.get(9); + + Assert.assertEquals(9, wxCpAgent.getAgentid().intValue()); + + Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowPartys().getPartyids().toArray()); + + Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagids().toArray()); + + } + +} From df572b549c241b0c23626c642e15dc8998ae674e Mon Sep 17 00:00:00 2001 From: lqh Date: Mon, 16 Apr 2018 11:15:19 +0800 Subject: [PATCH 4/4] =?UTF-8?q?tag=20service=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=A0=87=E7=AD=BE=E6=88=90=E5=91=98=E6=96=B9=E6=B3=95?= =?UTF-8?q?=20http://qydev.weixin.qq.com/wiki/index.php=3Ftitle=3D?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chanjar/weixin/cp/api/WxCpTagService.java | 13 +++++ .../cp/api/impl/WxCpTagServiceImpl.java | 15 +++++ .../weixin/cp/bean/WxCpTagGetResult.java | 57 +++++++++++++++++++ .../cp/api/impl/WxCpTagServiceImplTest.java | 25 ++++++++ 4 files changed, 110 insertions(+) create mode 100644 weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpTagGetResult.java diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java index c96318b80b..1cffaf350d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/WxCpTagService.java @@ -3,6 +3,7 @@ import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult; +import me.chanjar.weixin.cp.bean.WxCpTagGetResult; import me.chanjar.weixin.cp.bean.WxCpUser; import java.util.List; @@ -63,4 +64,16 @@ public interface WxCpTagService { * @param userIds 用户id列表 */ WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List userIds) throws WxErrorException; + + + /** + * 获取标签成员 + * 对应: http://qydev.weixin.qq.com/wiki/index.php?title=管理标签 中的get接口 + * + * @param tagId + * @return + * @throws WxErrorException + */ + WxCpTagGetResult get(String tagId) throws WxErrorException; + } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java index ff995d9724..b6f64b628f 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImpl.java @@ -7,6 +7,7 @@ import me.chanjar.weixin.cp.api.WxCpTagService; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult; +import me.chanjar.weixin.cp.bean.WxCpTagGetResult; import me.chanjar.weixin.cp.bean.WxCpUser; import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; @@ -113,4 +114,18 @@ public WxCpTagAddOrRemoveUsersResult removeUsersFromTag(String tagId, List + * 管理企业号应用-测试 + * Created by huansinho on 2018/4/16. + * + * + * @author huansinho + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class WxCpTagGetResult implements Serializable { + + @SerializedName("errcode") + private Integer errcode; + + @SerializedName("errmsg") + private String errmsg; + + /** + * 用户列表 + */ + @SerializedName("userlist") + private List userlist; + + /** + * 部门列表 + */ + @SerializedName("partylist") + private List partylist; + + /** + * 标签名称 + */ + @SerializedName("tagname") + private String tagname; + + public static WxCpTagGetResult fromJson(String json) { + return WxCpGsonBuilder.create().fromJson(json, WxCpTagGetResult.class); + } + + public String toJson() { + return WxCpGsonBuilder.create().toJson(this); + } + +} diff --git a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java index 94ac3b09db..0a7a6d335c 100644 --- a/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java +++ b/weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpTagServiceImplTest.java @@ -2,15 +2,20 @@ import com.google.common.base.Splitter; import com.google.inject.Inject; +import me.chanjar.weixin.common.exception.WxErrorException; import me.chanjar.weixin.cp.api.ApiTestModule; import me.chanjar.weixin.cp.api.WxCpService; +import me.chanjar.weixin.cp.api.WxCpTagService; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpTagAddOrRemoveUsersResult; +import me.chanjar.weixin.cp.bean.WxCpTagGetResult; import me.chanjar.weixin.cp.bean.WxCpUser; import org.testng.annotations.*; import java.util.List; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; import static org.testng.Assert.*; /** @@ -72,4 +77,24 @@ public void testDelete() throws Exception { this.wxService.getTagService().delete(this.tagId); } + @Test + public void testGet() throws WxErrorException { + String apiResultJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"userlist\": [{\"userid\": \"0124035\",\"name\": \"王五\"},{\"userid\": \"0114035\",\"name\": \"梦雪\"}],\"partylist\": [9576,9567,9566],\"tagname\": \"测试标签-001\"}"; + WxCpService wxService = mock(WxCpService.class); + when(wxService.get("https://qyapi.weixin.qq.com/cgi-bin/tag/get?tagId=150", null)).thenReturn(apiResultJson); + when(wxService.getTagService()).thenReturn(new WxCpTagServiceImpl(wxService)); + + WxCpTagService wxCpTagService = wxService.getTagService(); + + WxCpTagGetResult wxCpTagGetResult = wxCpTagService.get(String.valueOf(150)); + + assertEquals(0, wxCpTagGetResult.getErrcode().intValue()); + + assertEquals(2, wxCpTagGetResult.getUserlist().size()); + assertEquals(3, wxCpTagGetResult.getPartylist().size()); + assertEquals("测试标签-001", wxCpTagGetResult.getTagname()); + + + } + }