1 package com.foxinmy.weixin4j.mp.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.mp.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
22
23 public class WeixinTicketCreator extends TokenCreator {
24
25 private final TicketType ticketType;
26 private final TokenManager weixinTokenManager;
27
28
29
30
31
32
33
34
35
36 public WeixinTicketCreator(TicketType ticketType, TokenManager weixinTokenManager) {
37 this.ticketType = ticketType;
38 this.weixinTokenManager = weixinTokenManager;
39 }
40
41 @Override
42 public String name() {
43 return String.format("mp_ticket_%s", ticketType.name());
44 }
45
46 @Override
47 public String uniqueid() {
48 return weixinTokenManager.getWeixinId();
49 }
50
51 @Override
52 public Token create() throws WeixinException {
53 WeixinResponse response = weixinExecutor
54 .get(String.format(URLConsts.JS_TICKET_URL, weixinTokenManager.getAccessToken(), ticketType.name()));
55 JSONObject result = response.getAsJson();
56 return new Token(result.getString("ticket"), result.getLongValue("expires_in") * 1000l);
57 }
58 }