View Javadoc
1   package com.foxinmy.weixin4j.wxa;
2   
3   import java.util.Properties;
4   
5   import com.foxinmy.weixin4j.cache.CacheStorager;
6   import com.foxinmy.weixin4j.cache.FileCacheStorager;
7   import com.foxinmy.weixin4j.model.Token;
8   import com.foxinmy.weixin4j.model.WeixinAccount;
9   import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
10  import com.foxinmy.weixin4j.token.TokenCreator;
11  import com.foxinmy.weixin4j.token.TokenManager;
12  import com.foxinmy.weixin4j.wxa.api.CustomMessageApi;
13  import com.foxinmy.weixin4j.wxa.api.LoginApi;
14  import com.foxinmy.weixin4j.wxa.api.QrCodeApi;
15  import com.foxinmy.weixin4j.wxa.api.SecCheckApi;
16  import com.foxinmy.weixin4j.wxa.api.TemplateApi;
17  import com.foxinmy.weixin4j.wxa.api.TemplateMessageApi;
18  import com.foxinmy.weixin4j.wxa.api.SubscribeMessageApi;
19  
20  /**
21   * The facade of WeChat Mini Program APIs.
22   *
23   * @since 1.8
24   */
25  public class WeixinAppFacade {
26  
27  	private final LoginApi loginApi;
28  
29  	private final QrCodeApi qrCodeApi;
30  
31  	private final TemplateApi templateApi;
32  
33  	private final TemplateMessageApi templateMessageApi;
34  
35  	private final CustomMessageApi customMessageApi;
36  
37  	private final SecCheckApi secCheckApi;
38  
39  	private final SubscribeMessageApi subscribeMessageApi;
40  
41  	/**
42  	 * Constructs {@link WeixinAppFacade} using {@link FileCacheStorager}.
43  	 *
44  	 * @param weixinAccount the {@link WeixinAccount}.
45  	 */
46  	public WeixinAppFacade(
47  		WeixinAccount weixinAccount
48  	) {
49  		this(
50  			weixinAccount,
51  			new FileCacheStorager<Token>()
52  		);
53  	}
54  
55  	/**
56  	 * Constructs {@link WeixinAppFacade} using specified {@link CacheStorager}.
57  	 *
58  	 * @param weixinAccount the {@link WeixinAccount}.
59  	 * @param cacheStorager the {@link CacheStorager}.
60  	 */
61  	public WeixinAppFacade(
62  		WeixinAccount weixinAccount,
63  		CacheStorager<Token> cacheStorager
64  	) {
65  		this(
66  			weixinAccount,
67  			cacheStorager,
68  			null
69  		);
70  	}
71  
72  	/**
73  	 * Constructs {@link WeixinAppFacade} using specified {@link CacheStorager},
74  	 * and overrides properties defined in {@code weixin.properties}.
75  	 *
76  	 * @param weixinAccount the {@link WeixinAccount}.
77  	 * @param cacheStorager the {@link CacheStorager}.
78  	 * @param properties properties to overrides the properties defined in {@code weixin.properties}.
79  	 */
80  	public WeixinAppFacade(
81  		WeixinAccount weixinAccount,
82  		CacheStorager<Token> cacheStorager,
83  		Properties properties
84  	) {
85  		this(
86  			weixinAccount,
87  			new WeixinTokenCreator(weixinAccount.getId(), weixinAccount.getSecret()),
88  			cacheStorager,
89  			properties
90  		);
91  	}
92  
93  	private WeixinAppFacade(
94  		WeixinAccount weixinAccount,
95  		TokenCreator tokenCreator,
96  		CacheStorager<Token> cacheStorager,
97  		Properties properties
98  	) {
99  		if (weixinAccount == null) {
100 			throw new IllegalArgumentException(
101 				"weixinAccount must not be empty");
102 		}
103 
104 		if (tokenCreator == null) {
105 			throw new IllegalArgumentException(
106 				"tokenCreator must not be empty");
107 		}
108 
109 		if (cacheStorager == null) {
110 			throw new IllegalArgumentException(
111 				"cacheStorager must not be empty");
112 		}
113 
114 		final TokenManager tokenManager = new TokenManager(tokenCreator, cacheStorager);
115 
116 		this.loginApi = new LoginApi(weixinAccount, properties);
117 		this.qrCodeApi = new QrCodeApi(tokenManager, properties);
118 		this.templateApi = new TemplateApi(tokenManager, properties);
119 		this.templateMessageApi = new TemplateMessageApi(tokenManager, properties);
120 		this.customMessageApi = new CustomMessageApi(tokenManager, properties);
121 		this.secCheckApi = new SecCheckApi(tokenManager, properties);
122 		this.subscribeMessageApi = new SubscribeMessageApi(tokenManager, properties);
123 	}
124 
125 	/**
126 	 * 获取登录相关的 API。
127 	 *
128 	 * @return 登录相关 API。
129 	 */
130 	public LoginApi getLoginApi() {
131 		return loginApi;
132 	}
133 
134 	/**
135 	 * 获取小程序码、小程序二维码相关的 API。
136 	 *
137 	 * @return 小程序码、小程序二维码相关的 API。
138 	 */
139 	public QrCodeApi getQrCodeApi() {
140 		return qrCodeApi;
141 	}
142 
143 	/**
144 	 * 获取模版消息管理相关的 API。
145 	 *
146 	 * @return 模版消息管理相关的 API。
147 	 */
148 	public TemplateApi getTemplateApi() {
149 		return templateApi;
150 	}
151 
152 	/**
153 	 * 获取模板消息相关的 API。
154 	 *
155 	 * @return 模板消息相关的 API。
156 	 */
157 	public TemplateMessageApi getTemplateMessageApi() {
158 		return templateMessageApi;
159 	}
160 
161 	/**
162 	 * 获取订阅消息相关的 API。
163 	 *
164 	 * @return 订阅消息相关的 API。
165 	 * @since 1.9
166 	 */
167 	public SubscribeMessageApi getSubscribeMessageApi() {
168 		return subscribeMessageApi;
169 	}
170 
171 	/**
172 	 * 获取客服消息相关的 API。
173 	 *
174 	 * @return 客服消息相关的 API。
175 	 */
176 	public CustomMessageApi getCustomMessageApi() {
177 		return customMessageApi;
178 	}
179 
180 	/**
181 	 * 获取内容安全相关的 API。
182 	 *
183 	 * @return 内容安全相关的 API。
184 	 * @since 1.9
185 	 */
186 	public SecCheckApi getSecCheckApi() {
187 		return secCheckApi;
188 	}
189 
190 }