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.PerTicketManager;
9 import com.foxinmy.weixin4j.token.TokenCreator;
10 import com.foxinmy.weixin4j.token.TokenManager;
11
12
13
14
15
16
17
18
19
20
21
22
23 public class WeixinTokenSuiteCreator extends TokenCreator {
24
25 private final PerTicketManager perTicketManager;
26 private final TokenManager suiteTokenManager;
27
28
29
30
31
32
33
34
35 public WeixinTokenSuiteCreator(PerTicketManager perTicketManager,
36 TokenManager suiteTokenManager) {
37 this.perTicketManager = perTicketManager;
38 this.suiteTokenManager = suiteTokenManager;
39 }
40
41 @Override
42 public String name() {
43 return String.format("qy_token_suite_%s_%s",
44 perTicketManager.getThirdId(), perTicketManager.getAuthAppId());
45 }
46
47 @Override
48 public String uniqueid() {
49 throw new UnsupportedOperationException();
50 }
51
52 @Override
53 public Token create() throws WeixinException {
54 JSONObject obj = new JSONObject();
55 obj.put("suite_id", perTicketManager.getThirdId());
56 obj.put("auth_corpid", perTicketManager.getAuthAppId());
57 obj.put("permanent_code", perTicketManager.getAccessTicket());
58 WeixinResponse response = weixinExecutor
59 .post(String.format(URLConsts.TOKEN_SUITE_URL,
60 suiteTokenManager.getAccessToken()), obj.toJSONString());
61 obj = response.getAsJson();
62 return new Token(obj.getString("access_token"),
63 obj.getLongValue("expires_in") * 1000l);
64 }
65 }