1 package com.foxinmy.weixin4j.qy.token;
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 import com.foxinmy.weixin4j.type.TicketType;
11
12
13
14
15
16
17
18
19
20
21 public class WeixinTicketCreator extends TokenCreator {
22
23 private final TicketType ticketType;
24 private final TokenManager weixinTokenManager;
25
26
27
28
29
30
31
32 public WeixinTicketCreator(TicketType ticketType,
33 TokenManager weixinTokenManager) {
34 this.ticketType = ticketType;
35 this.weixinTokenManager = weixinTokenManager;
36 }
37
38 @Override
39 public String name() {
40 return String.format("qy_ticket_%s", ticketType.name());
41 }
42
43 @Override
44 public String uniqueid() {
45 return weixinTokenManager.getWeixinId();
46 }
47
48 @Override
49 public Token create() throws WeixinException {
50 WeixinResponse response = null;
51 if (ticketType == TicketType.jsapi) {
52 response = weixinExecutor.get(String.format(
53 URLConsts.JS_TICKET_URL, weixinTokenManager.getCache()
54 .getAccessToken()));
55 } else {
56 response = weixinExecutor.get(String.format(
57 URLConsts.SUITE_TICKET_URL, weixinTokenManager.getCache()
58 .getAccessToken(), ticketType.name()));
59 }
60 JSONObject result = response.getAsJson();
61 return new Token(result.getString("ticket"),
62 result.getLong("expires_in") * 1000l).pushExtra("group_id",
63 result.getString("group_id"));
64 }
65 }