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
index 3fbc981b..feea8acb 100644
--- 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
@@ -3,9 +3,12 @@ package me.chanjar.weixin.cp.api;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpAgent;
+import java.util.List;
+
/**
*
* 管理企业号应用
+ * 文档地址:https://work.weixin.qq.com/api/doc#10087
* Created by huansinho on 2018/4/13.
*
*
@@ -17,7 +20,7 @@ public interface WxCpAgentService {
*
* 获取企业号应用信息
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
- * 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
+ * 详情请见: https://work.weixin.qq.com/api/doc#10087
*
*
* @param agentId 企业应用的id
@@ -25,4 +28,25 @@ public interface WxCpAgentService {
*/
WxCpAgent get(Integer agentId) throws WxErrorException;
+ /**
+ *
+ * 设置应用.
+ * 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。
+ * 详情请见: https://work.weixin.qq.com/api/doc#10088
+ *
+ *
+ * @param agentInfo 应用信息
+ */
+ void set(WxCpAgent agentInfo) throws WxErrorException;
+
+ /**
+ *
+ * 获取应用列表.
+ * 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。
+ * 详情请见: https://work.weixin.qq.com/api/doc#11214
+ *
+ *
+ */
+ List list() throws WxErrorException;
+
}
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
index 45179fb5..e4914134 100644
--- 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
@@ -1,9 +1,17 @@
package me.chanjar.weixin.cp.api.impl;
+import com.google.gson.Gson;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
+import com.google.gson.reflect.TypeToken;
+import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.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.util.json.WxCpGsonBuilder;
+
+import java.util.List;
/**
@@ -15,6 +23,8 @@ import me.chanjar.weixin.cp.bean.WxCpAgent;
* @author huansinho
*/
public class WxCpAgentServiceImpl implements WxCpAgentService {
+ private static final JsonParser JSON_PARSER = new JsonParser();
+
private WxCpService mainService;
public WxCpAgentServiceImpl(WxCpService mainService) {
@@ -32,4 +42,27 @@ public class WxCpAgentServiceImpl implements WxCpAgentService {
return WxCpAgent.fromJson(responseContent);
}
+ @Override
+ public void set(WxCpAgent agentInfo) throws WxErrorException {
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/set";
+ String responseContent = this.mainService.post(url, agentInfo.toJson());
+ JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
+ if (jsonObject.get("errcode").getAsInt() != 0) {
+ throw new WxErrorException(WxError.fromJson(responseContent));
+ }
+ }
+
+ @Override
+ public List list() throws WxErrorException {
+ String url = "https://qyapi.weixin.qq.com/cgi-bin/agent/list";
+ String responseContent = this.mainService.get(url, null);
+ JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject();
+ if (jsonObject.get("errcode").getAsInt() != 0) {
+ throw new WxErrorException(WxError.fromJson(responseContent));
+ }
+
+ return WxCpGsonBuilder.create().fromJson(jsonObject.get("agentlist").toString(), new TypeToken>() {
+ }.getType());
+ }
+
}
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
index d606e1c6..b70571f0 100644
--- 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
@@ -1,7 +1,10 @@
package me.chanjar.weixin.cp.bean;
import com.google.gson.annotations.SerializedName;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
import lombok.Data;
+import lombok.NoArgsConstructor;
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
import java.io.Serializable;
@@ -16,6 +19,9 @@ import java.util.List;
* @author huansinho
*/
@Data
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
public class WxCpAgent implements Serializable {
private static final long serialVersionUID = 5002894979081127234L;
@@ -34,6 +40,9 @@ public class WxCpAgent implements Serializable {
@SerializedName("square_logo_url")
private String squareLogoUrl;
+ @SerializedName("logo_mediaid")
+ private String logoMediaId;
+
@SerializedName("description")
private String description;
@@ -41,7 +50,7 @@ public class WxCpAgent implements Serializable {
private Users allowUserInfos;
@SerializedName("allow_partys")
- private Partys allowParties;
+ private Parties allowParties;
@SerializedName("allow_tags")
private Tags allowTags;
@@ -82,7 +91,7 @@ public class WxCpAgent implements Serializable {
}
@Data
- public class Partys {
+ public class Parties {
@SerializedName("partyid")
private List partyIds = null;
}
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
index f69df698..4ce49724 100644
--- 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
@@ -1,13 +1,21 @@
package me.chanjar.weixin.cp.api.impl;
+import com.google.inject.Inject;
+import me.chanjar.weixin.common.error.WxErrorException;
+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 org.testng.Assert;
+import org.testng.annotations.Guice;
import org.testng.annotations.Test;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
/**
@@ -18,24 +26,64 @@ import static org.mockito.Mockito.when;
*
* @author huansinho
*/
+@Guice(modules = ApiTestModule.class)
public class WxCpAgentServiceImplTest {
-
- protected WxCpService wxService = mock(WxCpService.class);
+ @Inject
+ private WxCpService wxCpService;
@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));
+ final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId();
+ WxCpAgent wxCpAgent = this.wxCpService.getAgentService().get(agentId);
+
+ assertThat(wxCpAgent.getAgentId()).isEqualTo(agentId);
+
+ assertThat(wxCpAgent.getAllowUserInfos().getUsers().toArray()).isNotEmpty();
+ assertThat(wxCpAgent.getAllowParties().getPartyIds().toArray()).isNotEmpty();
+ assertThat(wxCpAgent.getAllowTags().getTagIds().toArray()).isNotEmpty();
+ }
+
+ @Test
+ public void testSet() throws WxErrorException {
+ final Integer agentId = this.wxCpService.getWxCpConfigStorage().getAgentId();
+
+ this.wxCpService.getAgentService().set(WxCpAgent.builder()
+ .description("abcddd")
+ .logoMediaId("aaaaaaaaaaaaaa")
+ .agentId(agentId)
+ .build());
+ }
+
+ @Test
+ public void testList() throws WxErrorException {
+ List list = this.wxCpService.getAgentService().list();
+
+ assertThat(list).isNotEmpty();
+
+ assertThat(list.get(0).getAgentId()).isNotNull();
+ assertThat(list.get(0).getName()).isNotEmpty();
+ assertThat(list.get(0).getSquareLogoUrl()).isNotEmpty();
+ }
+
+ public static class MockTest {
+ private 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);
- WxCpAgentService wxAgentService = this.wxService.getAgentService();
- WxCpAgent wxCpAgent = wxAgentService.get(9);
+ assertEquals(9, wxCpAgent.getAgentId().intValue());
- Assert.assertEquals(9, wxCpAgent.getAgentId().intValue());
+ assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
- Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
+ assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
- Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
+ }
}