View Javadoc
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   * @className AgentApi
22   * @author jinyu(foxinmy@gmail.com)
23   * @date 2015年3月16日
24   * @since JDK 1.6
25   * @see <a href="https://work.weixin.qq.com/api/doc#10025">管理应用接口说明</a>
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  	 * @param agentid
38  	 *            授权方应用id
39  	 * @return 应用信息
40  	 * @see com.foxinmy.weixin4j.qy.model.AgentInfo
41  	 * @see <a href="https://work.weixin.qq.com/api/doc#10087">企业号应用的信息</a>
42  	 * @throws WeixinException
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  	 * @param agentSet
66  	 *            设置信息
67  	 * @see com.foxinmy.weixin4j.qy.model.AgentSetter
68  	 * @see <a href="https://work.weixin.qq.com/api/doc#10088">设置企业号信息</a>
69  	 * @return 处理结果
70  	 * @throws WeixinException
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 	 * @see com.foxinmy.weixin4j.qy.model.AgentOverview
101 	 * @see <a href="https://work.weixin.qq.com/api/doc#11214">获取应用概况</a>
102 	 * @return 应用概况列表
103 	 * @throws WeixinException
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 }