View Javadoc
1   package com.foxinmy.weixin4j.qy.api;
2   
3   import com.alibaba.fastjson.JSON;
4   import com.alibaba.fastjson.JSONObject;
5   import com.foxinmy.weixin4j.exception.WeixinException;
6   import com.foxinmy.weixin4j.http.weixin.ApiResult;
7   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
8   import com.foxinmy.weixin4j.model.Token;
9   import com.foxinmy.weixin4j.qy.model.AgentInfo;
10  import com.foxinmy.weixin4j.qy.model.AgentSetter;
11  import com.foxinmy.weixin4j.qy.model.OUserInfo;
12  import com.foxinmy.weixin4j.qy.model.User;
13  import com.foxinmy.weixin4j.qy.suite.WeixinSuitePreCodeCreator;
14  import com.foxinmy.weixin4j.qy.suite.WeixinSuiteTokenCreator;
15  import com.foxinmy.weixin4j.qy.suite.WeixinTokenSuiteCreator;
16  import com.foxinmy.weixin4j.token.PerTicketManager;
17  import com.foxinmy.weixin4j.token.TicketManager;
18  import com.foxinmy.weixin4j.token.TokenCreator;
19  import com.foxinmy.weixin4j.token.TokenManager;
20  
21  /**
22   * 第三方应用套件
23   *
24   * @className SuiteApi
25   * @author jinyu(foxinmy@gmail.com)
26   * @date 2015年6月17日
27   * @since JDK 1.6
28   * @see <a href="http://work.weixin.qq.com/api/doc#10968">第三方应用授权</a>
29   */
30  public class SuiteApi extends QyApi {
31  	/**
32  	 * 应用套件token
33  	 */
34  	private final TokenManager tokenManager;
35  	/**
36  	 * 应用套件ticket
37  	 */
38  	private final TicketManager ticketManager;
39  	/**
40  	 * 应用套件pre_code
41  	 */
42  	private final TokenManager preCodeManager;
43  
44  	/**
45  	 *
46  	 * @param ticketManager
47  	 *            套件ticket存取
48  	 */
49  	public SuiteApi(TicketManager ticketManager) {
50  		this.ticketManager = ticketManager;
51  		this.tokenManager = new TokenManager(new WeixinSuiteTokenCreator(
52  				ticketManager), ticketManager.getCacheStorager());
53  		this.preCodeManager = new TokenManager(new WeixinSuitePreCodeCreator(
54  				tokenManager, ticketManager.getThirdId()),
55  				ticketManager.getCacheStorager());
56  	}
57  
58  	/**
59  	 * 应用套件token
60  	 *
61  	 * @return 应用套件的token管理
62  	 */
63  	public TokenManager getTokenManager() {
64  		return this.tokenManager;
65  	}
66  
67  	/**
68  	 * 应用套件ticket
69  	 *
70  	 * @return 应用套件的ticket管理
71  	 */
72  	public TicketManager getTicketManager() {
73  		return this.ticketManager;
74  	}
75  
76  	/**
77  	 * 应用套件预授权码
78  	 *
79  	 * @return 应用套件的precode管理
80  	 */
81  	public TokenManager getPreCodeManager() {
82  		return this.preCodeManager;
83  	}
84  
85  	/**
86  	 * 应用套件永久授权码:企业号的永久授权码
87  	 *
88  	 * @param authCorpid
89  	 *            授权方corpid
90  	 * @return 应用套件的preticket管理
91  	 */
92  	public PerTicketManager getPerTicketManager(String authCorpId) {
93  		return new PerTicketManager(authCorpId, ticketManager.getThirdId(),
94  				ticketManager.getThirdSecret(),
95  				ticketManager.getCacheStorager());
96  	}
97  
98  	/**
99  	 * 获取企业号access_token(永久授权码)
100 	 *
101 	 * @param authCorpid
102 	 *            授权方corpid
103 	 * @return 企业号token
104 	 */
105 	public TokenManager getPerTokenManager(String authCorpId) {
106 		return new TokenManager(new WeixinTokenSuiteCreator(
107 				getPerTicketManager(authCorpId), tokenManager),
108 				ticketManager.getCacheStorager());
109 	}
110 
111 	/**
112 	 * 设置套件授权配置:如果需要对某次授权进行配置,则调用本接口,目前仅可以设置哪些应用可以授权,不调用则默认允许所有应用进行授权。
113 	 *
114 	 * @param appids
115 	 *            允许进行授权的应用id,如1、2、3
116 	 * @return 处理结果
117 	 * @throws WeixinException
118 	 * @see <a href=
119 	 *      "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.AE.BE.E7.BD.AE.E6.8E.88.E6.9D.83.E9.85.8D.E7.BD.AE"
120 	 *      >设置套件授权配置</a>
121 	 */
122 	public ApiResult setSuiteSession(int... appids) throws WeixinException {
123 		String suite_set_session_uri = getRequestUri("suite_set_session_uri");
124 		JSONObject para = new JSONObject();
125 		para.put("pre_auth_code", preCodeManager.getAccessToken());
126 		JSONObject appid = new JSONObject();
127 		appid.put("appid", appids);
128 		para.put("session_info", appid);
129 		WeixinResponse response = weixinExecutor.post(
130 				String.format(suite_set_session_uri,
131 						tokenManager.getAccessToken()), para.toJSONString());
132 		return response.getAsResult();
133 	}
134 
135 	/**
136 	 * 临时授权码换取授权方的永久授权码,并换取授权信息、企业access_token
137 	 *
138 	 * @param authCode
139 	 *            临时授权码会在授权成功时附加在redirect_uri中跳转回应用提供商网站。
140 	 * @return 授权得到的信息
141 	 * @throws WeixinException
142 	 * @see com.foxinmy.weixin4j.qy.model.OUserInfo
143 	 * @see <a href=
144 	 *      "https://work.weixin.qq.com/api/doc#10975/获取企业永久授权码">获取企业号的永久授权码</a>
145 	 */
146 	public OUserInfo exchangeAuthInfo(String authCode) throws WeixinException {
147 		String suite_get_permanent_uri = getRequestUri("suite_get_permanent_uri");
148 		JSONObject obj = new JSONObject();
149 		obj.put("suite_id", ticketManager.getThirdId());
150 		obj.put("auth_code", authCode);
151 		WeixinResponse response = weixinExecutor.post(
152 				String.format(suite_get_permanent_uri,
153 						tokenManager.getAccessToken()), obj.toJSONString());
154 		obj = response.getAsJson();
155 		obj.put("corp_info", obj.remove("auth_corp_info"));
156 		obj.put("user_info", obj.remove("auth_user_info"));
157 		OUserInfo oInfo = JSON.toJavaObject(obj, OUserInfo.class);
158 		// 微信授权企业号的永久授权码
159 		PerTicketManager perTicketManager = getPerTicketManager(oInfo
160 				.getCorpInfo().getCorpId());
161 		// 缓存微信企业号的access_token
162 		TokenCreator tokenCreator = new WeixinTokenSuiteCreator(
163 				perTicketManager, tokenManager);
164 		Token token = new Token(obj.getString("access_token"),
165 				obj.getLongValue("expires_in") * 1000l);
166 		ticketManager.getCacheStorager().caching(tokenCreator.key(), token);
167 		// 缓存微信企业号的永久授权码
168 		perTicketManager.cachingTicket(obj.getString("permanent_code"));
169 		return oInfo;
170 	}
171 
172 	/**
173 	 * 获取企业号的授权信息
174 	 *
175 	 * @param authCorpId
176 	 *            授权方corpid
177 	 * @return 授权方信息
178 	 * @throws WeixinException
179 	 * @see com.foxinmy.weixin4j.qy.model.OUserInfo
180 	 * @see <a href=
181 	 *      "https://work.weixin.qq.com/api/doc#10975/获取企业授权信息">获取企业号的授权信息</a>
182 	 */
183 	public OUserInfo getAuthInfo(String authCorpId) throws WeixinException {
184 		String suite_get_authinfo_uri = getRequestUri("suite_get_authinfo_uri");
185 		JSONObject obj = new JSONObject();
186 		obj.put("suite_id", ticketManager.getThirdId());
187 		obj.put("auth_corpid", authCorpId);
188 		obj.put("permanent_code", getPerTicketManager(authCorpId)
189 				.getAccessTicket());
190 		WeixinResponse response = weixinExecutor.post(
191 				String.format(suite_get_authinfo_uri,
192 						tokenManager.getAccessToken()), obj.toJSONString());
193 		obj = response.getAsJson();
194 		obj.put("corp_info", obj.remove("auth_corp_info"));
195 		obj.put("user_info", obj.remove("auth_user_info"));
196 		return JSON.toJavaObject(obj, OUserInfo.class);
197 	}
198 
199 	/**
200 	 * 获取企业号应用
201 	 *
202 	 * @param authCorpId
203 	 *            授权方corpid
204 	 * @param agentid
205 	 *            授权方应用id
206 	 * @return 应用信息
207 	 * @see com.foxinmy.weixin4j.qy.model.AgentInfo
208 	 * @see <a href=
209 	 *      "https://work.weixin.qq.com/api/doc#10975/获取企业授权信息">获取企业号应用</a>
210 	 * @throws WeixinException
211 	 */
212 	public AgentInfo getAgent(String authCorpId, int agentid)
213 			throws WeixinException {
214 		String suite_get_agent_uri = getRequestUri("suite_get_agent_uri");
215 		JSONObject obj = new JSONObject();
216 		obj.put("suite_id", ticketManager.getThirdId());
217 		obj.put("auth_corpid", authCorpId);
218 		obj.put("permanent_code", getPerTicketManager(authCorpId)
219 				.getAccessTicket());
220 		obj.put("agentid", agentid);
221 		WeixinResponse response = weixinExecutor.post(
222 				String.format(suite_get_agent_uri,
223 						tokenManager.getAccessToken()), obj.toJSONString());
224 		JSONObject jsonObj = response.getAsJson();
225 		AgentInfo agent = JSON.toJavaObject(jsonObj, AgentInfo.class);
226 		agent.setAllowUsers(JSON.parseArray(
227 				jsonObj.getJSONObject("allow_userinfos").getString("user"),
228 				User.class));
229 		agent.setAllowPartys(JSON.parseArray(
230 				jsonObj.getJSONObject("allow_partys").getString("partyid"),
231 				Integer.class));
232 		agent.setAllowTags(JSON.parseArray(jsonObj.getJSONObject("allow_tags")
233 				.getString("tagid"), Integer.class));
234 		return agent;
235 	}
236 
237 	/**
238 	 * 设置企业应用的选项设置信息,如:地理位置上报等
239 	 *
240 	 * @param authCorpId
241 	 *            授权方corpid
242 	 * @param agentSet
243 	 *            设置信息
244 	 * @see com.foxinmy.weixin4j.qy.model.AgentSetter
245 	 * @see <a href=
246 	 *      "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%AE%BE%E7%BD%AE%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%BA%94%E7%94%A8">设置企业号信息</a>
247 	 * @return 处理结果
248 	 * @throws WeixinException
249 	 */
250 	public ApiResult setAgent(String authCorpId, AgentSetter agentSet)
251 			throws WeixinException {
252 		String suite_set_agent_uri = getRequestUri("suite_set_agent_uri");
253 		JSONObject obj = new JSONObject();
254 		obj.put("suite_id", ticketManager.getThirdId());
255 		obj.put("auth_corpid", authCorpId);
256 		obj.put("permanent_code", getPerTicketManager(authCorpId)
257 				.getAccessTicket());
258 		obj.put("agent", agentSet);
259 		WeixinResponse response = weixinExecutor.post(
260 				String.format(suite_set_agent_uri,
261 						tokenManager.getAccessToken()),
262 				JSON.toJSONString(obj, AgentApi.typeFilter));
263 		return response.getAsResult();
264 	}
265 }