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
18
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
34
35
36
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
51
52
53
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
70
71
72
73
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
89
90
91
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
106
107
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 }