View Javadoc
1   package com.foxinmy.weixin4j.wxa.api;
2   
3   import java.util.HashMap;
4   import java.util.List;
5   import java.util.Map;
6   import java.util.Properties;
7   
8   import com.foxinmy.weixin4j.exception.WeixinException;
9   import com.foxinmy.weixin4j.model.paging.Pageable;
10  import com.foxinmy.weixin4j.model.paging.Pagedata;
11  import com.foxinmy.weixin4j.token.TokenManager;
12  import com.foxinmy.weixin4j.wxa.model.template.Template;
13  
14  /**
15   * 模板消息管理。
16   *
17   * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/template-message.html">模板消息管理</a>
18   * @since 1.8
19   */
20  public class TemplateApi extends TokenManagerApi {
21  
22  	public TemplateApi(TokenManager tokenManager) {
23  		this(tokenManager, null);
24  	}
25  
26  	public TemplateApi(TokenManager tokenManager, Properties properties) {
27  		super(tokenManager, properties);
28  	}
29  
30  	/**
31  	 * 获取小程序模板库标题列表
32  	 *
33  	 * @param pageable the pagination information.
34  	 * @return templates in library.
35  	 * @throws WeixinException indicates getting access token failed or getting templates failed.
36  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.getTemplateLibraryList.html">获取小程序模板库标题列表</a>
37  	 */
38  	public Pagedata<Template> getTemplatesInLibrary(final Pageable pageable) throws WeixinException {
39  		final TemplateListResult r = post(
40  			"wxopen_template_library_list",
41  			toPageableRequestParams(pageable),
42  			TemplateListResult.TYPE_REFERENCE
43  		);
44  		return r.toPage(pageable);
45  	}
46  
47  	/**
48  	 * 获取模板库某个模板标题下关键词库
49  	 *
50  	 * @param id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
51  	 * @return the template in library with specified ID.
52  	 * @throws WeixinException indicates getting access token failed or getting template failed.
53  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.getTemplateLibraryById.html">获取模板库某个模板标题下关键词库</a>
54  	 */
55  	public Template getTemplateInLibrary(String id) throws WeixinException {
56  		final Map<String, String> params = new HashMap<String, String>(1);
57  		params.put("id", id);
58  		final TemplateResult r = this.post(
59  			"wxopen_template_library_get",
60  			params,
61  			TemplateResult.TYPE_REFERENCE
62  		);
63  		return r.toTemplate();
64  	}
65  
66  	/**
67  	 * 组合模板并添加至帐号下的个人模板库
68  	 *
69  	 * @param id 模板标题id,可通过接口获取,也可登录小程序后台查看获取
70  	 * @param keywordIds 开发者自行组合好的模板关键词列表,关键词顺序可以自由搭配(例如[3,5,4]或[4,5,3]),最多支持10个关键词组合
71  	 * @return the added template ID.
72  	 * @throws WeixinException indicates getting access token failed or adding template failed.
73  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.addTemplate.html">组合模板并添加至帐号下的个人模板库</a>
74  	 */
75  	public String addTemplate(String id, int[] keywordIds) throws WeixinException {
76  		final AddTemplateParameter params = new AddTemplateParameter(id, keywordIds);
77  		final TemplateResult r = this.post(
78  			"wxopen_template_add",
79  			params,
80  			TemplateResult.TYPE_REFERENCE
81  		);
82  		return r.toTemplate().getId();
83  	}
84  
85  	/**
86  	 * 获取帐号下已存在的模板列表
87  	 *
88  	 * @param pageable the pagination information.
89  	 * @return the templates.
90  	 * @throws WeixinException indicates getting access token failed or getting template failed.
91  	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.getTemplateList.html">获取帐号下已存在的模板列表</a>
92  	 */
93  	public List<Template> getTemplates(final Pageable pageable) throws WeixinException {
94  		final TemplateListResult r = post(
95  			"wxopen_template_list",
96  			toPageableRequestParams(pageable),
97  			TemplateListResult.TYPE_REFERENCE
98  		);
99  		return r.toList();
100 	}
101 
102 	/**
103 	 * 删除帐号下的某个模板
104 	 *
105 	 * @param id 要删除的模板id
106 	 * @throws WeixinException indicates getting access token failed or deleting template failed.
107 	 * @see <a href="https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/template-message/templateMessage.deleteTemplate.html">删除帐号下的某个模板</a>
108 	 */
109 	public void deleteTemplate(String id) throws WeixinException {
110 		final Map<String, String> params = new HashMap<String, String>(1);
111 		params.put("template_id", id);
112 		final WxaApiResult r = this.post(
113 			"wxopen_template_del",
114 			params,
115 			WxaApiResult.TYPE_REFERENCE
116 		);
117 		r.checkErrCode();
118 	}
119 
120 	private Map<String, Integer> toPageableRequestParams(final Pageable pageable) {
121 		final Map<String, Integer> params = new HashMap<String, Integer>(2);
122 		params.put("offset", pageable.getOffset());
123 		params.put("count", pageable.getPageSize());
124 		return params;
125 	}
126 
127 }