1 package com.foxinmy.weixin4j.qy.api;
2
3 import java.util.List;
4
5 import com.alibaba.fastjson.JSON;
6 import com.alibaba.fastjson.JSONObject;
7 import com.alibaba.fastjson.serializer.ValueFilter;
8 import com.foxinmy.weixin4j.exception.WeixinException;
9 import com.foxinmy.weixin4j.http.weixin.ApiResult;
10 import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
11 import com.foxinmy.weixin4j.model.Token;
12 import com.foxinmy.weixin4j.qy.model.AgentInfo;
13 import com.foxinmy.weixin4j.qy.model.AgentOverview;
14 import com.foxinmy.weixin4j.qy.model.AgentSetter;
15 import com.foxinmy.weixin4j.qy.model.User;
16 import com.foxinmy.weixin4j.token.TokenManager;
17
18
19
20
21
22
23
24
25
26
27 public class AgentApi extends QyApi {
28 private final TokenManager tokenManager;
29
30 public AgentApi(TokenManager tokenManager) {
31 this.tokenManager = tokenManager;
32 }
33
34
35
36
37
38
39
40
41
42
43
44 public AgentInfo getAgent(int agentid) throws WeixinException {
45 String agent_get_uri = getRequestUri("agent_get_uri");
46 Token token = tokenManager.getCache();
47 WeixinResponse response = weixinExecutor.get(String.format(
48 agent_get_uri, token.getAccessToken(), agentid));
49 JSONObject jsonObj = response.getAsJson();
50 AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
51 agent.setAllowUsers(JSON.parseArray(
52 jsonObj.getJSONObject("allow_userinfos").getString("user"),
53 User.class));
54 agent.setAllowPartys(JSON.parseArray(
55 jsonObj.getJSONObject("allow_partys").getString("partyid"),
56 Integer.class));
57 agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
58 .getString("tagid"), Integer.class));
59 return agent;
60 }
61
62
63
64
65
66
67
68
69
70
71
72 public ApiResult setAgent(AgentSetter agentSet) throws WeixinException {
73 String agent_set_uri = getRequestUri("agent_set_uri");
74 Token token = tokenManager.getCache();
75 WeixinResponse response = weixinExecutor.post(
76 String.format(agent_set_uri, token.getAccessToken()),
77 JSON.toJSONString(agentSet, typeFilter));
78 return response.getAsResult();
79 }
80
81 public final static ValueFilter typeFilter;
82 static {
83 typeFilter = new ValueFilter() {
84 @Override
85 public Object process(Object object, String name, Object value) {
86 if (value instanceof Boolean) {
87 return ((Boolean) value) ? 1 : 0;
88 }
89 if (value instanceof Enum) {
90 return ((Enum<?>) value).ordinal();
91 }
92 return value;
93 }
94 };
95 }
96
97
98
99
100
101
102
103
104
105 public List<AgentOverview> listAgentOverview() throws WeixinException {
106 String agent_list_uri = getRequestUri("agent_list_uri");
107 Token token = tokenManager.getCache();
108 WeixinResponse response = weixinExecutor.get(String.format(
109 agent_list_uri, token.getAccessToken()));
110
111 return JSON.parseArray(response.getAsJson().getString("agentlist"),
112 AgentOverview.class);
113 }
114 }