View Javadoc
1   package com.foxinmy.weixin4j.qy.suite;
2   
3   import com.alibaba.fastjson.JSONObject;
4   import com.foxinmy.weixin4j.exception.WeixinException;
5   import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
6   import com.foxinmy.weixin4j.model.Token;
7   import com.foxinmy.weixin4j.qy.type.URLConsts;
8   import com.foxinmy.weixin4j.token.TokenCreator;
9   import com.foxinmy.weixin4j.token.TokenManager;
10  
11  /**
12   * 微信企业号应用套件预授权码创建
13   *
14   * @className WeixinSuitePreCodeCreator
15   * @author jinyu(foxinmy@gmail.com)
16   * @date 2015年6月17日
17   * @since JDK 1.6
18   * @see <a href= "http://work.weixin.qq.com/api/doc#10975/获取预授权码">
19   *      获取应用套件预授权码</a>
20   * @see com.foxinmy.weixin4j.model.Token
21   */
22  public class WeixinSuitePreCodeCreator extends TokenCreator {
23  
24  	private final TokenManager suiteTokenManager;
25  	private final String suiteId;
26  
27  	/**
28  	 *
29  	 * @param suiteTokenManager
30  	 *            应用套件的token
31  	 * @param suiteId
32  	 *            应用套件ID
33  	 */
34  	public WeixinSuitePreCodeCreator(TokenManager suiteTokenManager,
35  			String suiteId) {
36  		this.suiteTokenManager = suiteTokenManager;
37  		this.suiteId = suiteId;
38  	}
39  
40  	@Override
41  	public String name() {
42  		return "qy_suite_precode";
43  	}
44  
45  	@Override
46  	public String uniqueid() {
47  		return suiteId;
48  	}
49  
50  	@Override
51  	public Token create() throws WeixinException {
52  		WeixinResponse response = weixinExecutor.post(
53  				String.format(URLConsts.SUITE_PRE_CODE_URL,
54  						suiteTokenManager.getAccessToken()),
55  				String.format("{\"suite_id\":\"%s\"}", suiteId));
56  		JSONObject result = response.getAsJson();
57  		return new Token(result.getString("pre_auth_code"),
58  				result.getLongValue("expires_in") * 1000l);
59  	}
60  }