1 package com.foxinmy.weixin4j.token;
2
3 import com.foxinmy.weixin4j.cache.CacheStorager;
4 import com.foxinmy.weixin4j.exception.WeixinException;
5 import com.foxinmy.weixin4j.model.Token;
6
7
8
9
10
11
12
13
14
15
16 public class TicketManager {
17
18
19
20
21 private final String thirdId;
22
23
24
25 private final String thirdSecret;
26
27
28
29 private final CacheStorager<Token> cacheStorager;
30
31
32
33
34
35
36
37
38
39
40 public TicketManager(String thirdId, String thirdSecret,
41 CacheStorager<Token> cacheStorager) {
42 this.thirdId = thirdId;
43 this.thirdSecret = thirdSecret;
44 this.cacheStorager = cacheStorager;
45 }
46
47
48
49
50
51
52
53 public Token getTicket() throws WeixinException {
54 return cacheStorager.lookup(getCacheKey());
55 }
56
57
58
59
60
61
62
63 public String getAccessTicket() throws WeixinException {
64 return getTicket().getAccessToken();
65 }
66
67
68
69
70
71
72 public String getCacheKey() {
73 return String.format("%sthird_party_ticket_%s",
74 TokenCreator.CACHEKEY_PREFIX, thirdId);
75 }
76
77
78
79
80
81
82
83
84 public void cachingTicket(String ticket) throws WeixinException {
85 Token token = new Token(ticket);
86 cacheStorager.caching(getCacheKey(), token);
87 }
88
89 public String getThirdId() {
90 return thirdId;
91 }
92
93 public String getThirdSecret() {
94 return thirdSecret;
95 }
96
97 public CacheStorager<Token> getCacheStorager() {
98 return cacheStorager;
99 }
100 }