mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 16:18:51 +08:00
#707 企业微信增加应用管理里的设置和列表接口
This commit is contained in:
parent
28b09f7ed6
commit
4dd67f46b8
@ -3,9 +3,12 @@ package me.chanjar.weixin.cp.api;
|
|||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpAgent;
|
import me.chanjar.weixin.cp.bean.WxCpAgent;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <pre>
|
* <pre>
|
||||||
* 管理企业号应用
|
* 管理企业号应用
|
||||||
|
* 文档地址:https://work.weixin.qq.com/api/doc#10087
|
||||||
* Created by huansinho on 2018/4/13.
|
* Created by huansinho on 2018/4/13.
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
@ -17,7 +20,7 @@ public interface WxCpAgentService {
|
|||||||
* <pre>
|
* <pre>
|
||||||
* 获取企业号应用信息
|
* 获取企业号应用信息
|
||||||
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
|
* 该API用于获取企业号某个应用的基本信息,包括头像、昵称、帐号类型、认证类型、可见范围等信息
|
||||||
* 详情请见: http://qydev.weixin.qq.com/wiki/index.php?title=获取企业号应用
|
* 详情请见: https://work.weixin.qq.com/api/doc#10087
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param agentId 企业应用的id
|
* @param agentId 企业应用的id
|
||||||
@ -25,4 +28,25 @@ public interface WxCpAgentService {
|
|||||||
*/
|
*/
|
||||||
WxCpAgent get(Integer agentId) throws WxErrorException;
|
WxCpAgent get(Integer agentId) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 设置应用.
|
||||||
|
* 仅企业可调用,可设置当前凭证对应的应用;第三方不可调用。
|
||||||
|
* 详情请见: https://work.weixin.qq.com/api/doc#10088
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param agentInfo 应用信息
|
||||||
|
*/
|
||||||
|
void set(WxCpAgent agentInfo) throws WxErrorException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* 获取应用列表.
|
||||||
|
* 企业仅可获取当前凭证对应的应用;第三方仅可获取被授权的应用。
|
||||||
|
* 详情请见: https://work.weixin.qq.com/api/doc#11214
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
List<WxCpAgent> list() throws WxErrorException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,17 @@
|
|||||||
package me.chanjar.weixin.cp.api.impl;
|
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.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.cp.api.WxCpAgentService;
|
import me.chanjar.weixin.cp.api.WxCpAgentService;
|
||||||
import me.chanjar.weixin.cp.api.WxCpService;
|
import me.chanjar.weixin.cp.api.WxCpService;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpAgent;
|
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 <a href="https://github.com/huansinho">huansinho</a>
|
* @author <a href="https://github.com/huansinho">huansinho</a>
|
||||||
*/
|
*/
|
||||||
public class WxCpAgentServiceImpl implements WxCpAgentService {
|
public class WxCpAgentServiceImpl implements WxCpAgentService {
|
||||||
|
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||||
|
|
||||||
private WxCpService mainService;
|
private WxCpService mainService;
|
||||||
|
|
||||||
public WxCpAgentServiceImpl(WxCpService mainService) {
|
public WxCpAgentServiceImpl(WxCpService mainService) {
|
||||||
@ -32,4 +42,27 @@ public class WxCpAgentServiceImpl implements WxCpAgentService {
|
|||||||
return WxCpAgent.fromJson(responseContent);
|
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<WxCpAgent> 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<List<WxCpAgent>>() {
|
||||||
|
}.getType());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package me.chanjar.weixin.cp.bean;
|
package me.chanjar.weixin.cp.bean;
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName;
|
import com.google.gson.annotations.SerializedName;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
@ -16,6 +19,9 @@ import java.util.List;
|
|||||||
* @author <a href="https://github.com/huansinho">huansinho</a>
|
* @author <a href="https://github.com/huansinho">huansinho</a>
|
||||||
*/
|
*/
|
||||||
@Data
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
public class WxCpAgent implements Serializable {
|
public class WxCpAgent implements Serializable {
|
||||||
private static final long serialVersionUID = 5002894979081127234L;
|
private static final long serialVersionUID = 5002894979081127234L;
|
||||||
|
|
||||||
@ -34,6 +40,9 @@ public class WxCpAgent implements Serializable {
|
|||||||
@SerializedName("square_logo_url")
|
@SerializedName("square_logo_url")
|
||||||
private String squareLogoUrl;
|
private String squareLogoUrl;
|
||||||
|
|
||||||
|
@SerializedName("logo_mediaid")
|
||||||
|
private String logoMediaId;
|
||||||
|
|
||||||
@SerializedName("description")
|
@SerializedName("description")
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@ -41,7 +50,7 @@ public class WxCpAgent implements Serializable {
|
|||||||
private Users allowUserInfos;
|
private Users allowUserInfos;
|
||||||
|
|
||||||
@SerializedName("allow_partys")
|
@SerializedName("allow_partys")
|
||||||
private Partys allowParties;
|
private Parties allowParties;
|
||||||
|
|
||||||
@SerializedName("allow_tags")
|
@SerializedName("allow_tags")
|
||||||
private Tags allowTags;
|
private Tags allowTags;
|
||||||
@ -82,7 +91,7 @@ public class WxCpAgent implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class Partys {
|
public class Parties {
|
||||||
@SerializedName("partyid")
|
@SerializedName("partyid")
|
||||||
private List<Integer> partyIds = null;
|
private List<Integer> partyIds = null;
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,21 @@
|
|||||||
package me.chanjar.weixin.cp.api.impl;
|
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.WxCpAgentService;
|
||||||
import me.chanjar.weixin.cp.api.WxCpService;
|
import me.chanjar.weixin.cp.api.WxCpService;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpAgent;
|
import me.chanjar.weixin.cp.bean.WxCpAgent;
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
|
import org.testng.annotations.Guice;
|
||||||
import org.testng.annotations.Test;
|
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.mock;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,9 +26,47 @@ import static org.mockito.Mockito.when;
|
|||||||
*
|
*
|
||||||
* @author <a href="https://github.com/huansinho">huansinho</a>
|
* @author <a href="https://github.com/huansinho">huansinho</a>
|
||||||
*/
|
*/
|
||||||
|
@Guice(modules = ApiTestModule.class)
|
||||||
public class WxCpAgentServiceImplTest {
|
public class WxCpAgentServiceImplTest {
|
||||||
|
@Inject
|
||||||
|
private WxCpService wxCpService;
|
||||||
|
|
||||||
protected WxCpService wxService = mock(WxCpService.class);
|
@Test
|
||||||
|
public void testGet() throws Exception {
|
||||||
|
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<WxCpAgent> 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
|
@Test
|
||||||
public void testGet() throws Exception {
|
public void testGet() throws Exception {
|
||||||
@ -31,11 +77,13 @@ public class WxCpAgentServiceImplTest {
|
|||||||
WxCpAgentService wxAgentService = this.wxService.getAgentService();
|
WxCpAgentService wxAgentService = this.wxService.getAgentService();
|
||||||
WxCpAgent wxCpAgent = wxAgentService.get(9);
|
WxCpAgent wxCpAgent = wxAgentService.get(9);
|
||||||
|
|
||||||
Assert.assertEquals(9, wxCpAgent.getAgentId().intValue());
|
assertEquals(9, wxCpAgent.getAgentId().intValue());
|
||||||
|
|
||||||
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
|
assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
|
||||||
|
|
||||||
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
|
assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user