UNPKG

6.4 kBJavaScriptView Raw
1'use strict';
2
3var util = require('./util');
4var wrapper = util.wrapper;
5var postJSON = util.postJSON;
6
7/**
8 * 设置所属行业
9 * Examples:
10 * ```
11 * var industryIds = {
12 * "industry_id1":'1',
13 * "industry_id2":"4"
14 * };
15 * api.setIndustry(industryIds, callback);
16 * ```
17 * Callback:
18 *
19 * - `err`, 调用失败时得到的异常
20 * - `result`, 调用正常时得到的对象
21 *
22 * @param {Object} industryIds 公众号模板消息所属行业编号
23 */
24exports.setIndustry = function(industryIds, callback){
25 this.preRequest(this._setIndustry, arguments);
26};
27
28exports._setIndustry = function (industryIds, callback) {
29 var apiUrl = this.endpoint + '/cgi-bin/template/api_set_industry?access_token=' + this.token.accessToken;
30 this.request(apiUrl, postJSON(industryIds), wrapper(callback));
31};
32
33/**
34 * 获取设置的行业信息
35 * Examples:
36 * ```
37 * api.getIndustry(callback);
38 * ```
39 * Callback:
40 *
41 * - `err`, 调用失败时得到的异常
42 * - `result`, 调用正常时得到的对象
43 *
44 * Result:
45 * ```
46 * // 结果示例
47 * {
48 * "primary_industry":{"first_class":"运输与仓储","second_class":"快递"},
49 * "secondary_industry":{"first_class":"IT科技","second_class":"互联网|电子商务"}
50 * }
51 * ```
52 * @param {Function} callback 回调函数
53 */
54exports.getIndustry = function(callback){
55 this.preRequest(this._getIndustry, arguments);
56};
57
58exports._getIndustry = function (callback) {
59 var apiUrl = this.endpoint + '/cgi-bin/template/get_industry?access_token=' + this.token.accessToken;
60 this.request(apiUrl, {dataType: 'json'}, wrapper(callback));
61};
62
63/**
64 * 获得模板ID
65 * Examples:
66 * ```
67 * var templateIdShort = 'TM00015';
68 * api.addTemplate(templateIdShort, callback);
69 * ```
70 * Callback:
71 *
72 * - `err`, 调用失败时得到的异常
73 * - `result`, 调用正常时得到的对象
74 *
75 * @param {String} templateIdShort 模板库中模板的编号,有“TM**”和“OPENTMTM**”等形式
76 */
77exports.addTemplate = function(templateIdShort, callback){
78 this.preRequest(this._addTemplate, arguments);
79};
80
81exports._addTemplate = function (templateIdShort, callback) {
82 var apiUrl = this.endpoint + '/cgi-bin/template/api_add_template?access_token=' + this.token.accessToken;
83 var templateId = {
84 template_id_short: templateIdShort
85 };
86 this.request(apiUrl, postJSON(templateId), wrapper(callback));
87};
88
89/**
90 * 获取模板列表
91 * Examples:
92 * ```
93 * api.getAllPrivateTemplate(callback);
94 * ```
95 * Callback:
96 *
97 * - `err`, 调用失败时得到的异常
98 * - `result`, 调用正常时得到的对象
99 *
100 * Result:
101 * ```
102 * // 结果示例
103 * {
104 * "template_list": [{
105 * "template_id": "iPk5sOIt5X_flOVKn5GrTFpncEYTojx6ddbt8WYoV5s",
106 * "title": "领取奖金提醒",
107 * "primary_industry": "IT科技",
108 * "deputy_industry": "互联网|电子商务",
109 * "content": "{ {result.DATA} }\n\n领奖金额:{ {withdrawMoney.DATA} }\n领奖 时间:{ {withdrawTime.DATA} }\n银行信息:{ {cardInfo.DATA} }\n到账时间: { {arrivedTime.DATA} }\n{ {remark.DATA} }",
110 * "example": "您已提交领奖申请\n\n领奖金额:xxxx元\n领奖时间:2013-10-10 12:22:22\n银行信息:xx银行(尾号xxxx)\n到账时间:预计xxxxxxx\n\n预计将于xxxx到达您的银行卡"
111 * }]
112 * }
113 * ```
114 * @param {Function} callback 回调函数
115 */
116exports.getAllPrivateTemplate = function(callback){
117 this.preRequest(this._getAllPrivateTemplate, arguments);
118};
119
120exports._getAllPrivateTemplate = function (callback) {
121 var apiUrl = this.endpoint + '/cgi-bin/template/get_all_private_template?access_token=' + this.token.accessToken;
122 this.request(apiUrl, {dataType: 'json'}, wrapper(callback));
123};
124
125/**
126 * 删除模板
127 * Examples:
128 * ```
129 * var templateId = ”Dyvp3-Ff0cnail_CDSzk1fIc6-9lOkxsQE7exTJbwUE”
130 * api.delPrivateTemplate(templateId, callback);
131 * ```
132 * Callback:
133 *
134 * - `err`, 调用失败时得到的异常
135 * - `result`, 调用正常时得到的对象
136 *
137 * @param {String} templateId 公众帐号下模板消息ID
138 */
139exports.delPrivateTemplate = function(templateId, callback){
140 this.preRequest(this._delPrivateTemplate, arguments);
141};
142
143exports._delPrivateTemplate = function (templateId, callback) {
144 var apiUrl = this.endpoint + '/cgi-bin/template/del_private_template?access_token=' + this.token.accessToken;
145 var data = {
146 template_id: templateId
147 };
148 this.request(apiUrl, postJSON(data), wrapper(callback));
149};
150
151/**
152 * 发送模板消息
153 * 详细细节: http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html
154 * Examples:
155 * ```
156 * var templateId: '模板id';
157 * // URL置空,则在发送后,点击模板消息会进入一个空白页面(ios), 或无法点击(android)
158 * var url: 'http://weixin.qq.com/download';
159 * var data = {
160 * "first": {
161 * "value":"恭喜你购买成功!",
162 * "color":"#173177"
163 * },
164 * "keyword1":{
165 * "value":"巧克力",
166 * "color":"#173177"
167 * },
168 * "keyword2": {
169 * "value":"39.8元",
170 * "color":"#173177"
171 * },
172 * "keyword3": {
173 * "value":"2014年9月22日",
174 * "color":"#173177"
175 * },
176 * "remark":{
177 * "value":"欢迎再次购买!",
178 * "color":"#173177"
179 * }
180 * };
181 * api.sendTemplate('openid', templateId, url, data, callback);
182 * ```
183 * Callback:
184 *
185 * - `err`, 调用失败时得到的异常
186 * - `result`, 调用正常时得到的对象
187 *
188 * @param {String} openid 用户的openid
189 * @param {String} templateId 模板ID
190 * @param {String} url URL置空,则在发送后,点击模板消息会进入一个空白页面(ios),或无法点击(android)
191 * @param {Object} data 渲染模板的数据
192 * @param {Function} callback 回调函数
193 */
194exports.sendTemplate = function (openid, templateId, url, data, callback, callback2) {
195 this.preRequest(this._sendTemplate, arguments);
196};
197
198exports._sendTemplate = function (openid, templateId, url, data, callback, callback2) {
199 /*
200 duplicated interface `function (openid, templateId, url, topColor, data, callback)`
201 */
202 var apiUrl = this.endpoint + '/cgi-bin/message/template/send?access_token=' + this.token.accessToken;
203
204 if (typeof data === 'string') {
205 data = callback;
206 callback = callback2;
207 }
208
209 var template = {
210 touser: openid,
211 template_id: templateId,
212 url: url,
213 data: data
214 };
215 this.request(apiUrl, postJSON(template), wrapper(callback));
216};