forked from yxz1025/wxmsg-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
147 lines (139 loc) · 4.18 KB
/
index.js
File metadata and controls
147 lines (139 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
//微信模版消息API封装
var Q = require('q');
var request = require('request');
function WxMsgTpl(opts) {
this.opts = opts || {};
this.access_token = this.opts.access_token || "";
};
//设置所属行业
/**
* args object {"industry_id1": 1, "industry_id2": 4}
*https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN
*/
WxMsgTpl.prototype.setIndustry = function(args) {
var deferred = Q.defer();
if (typeof args === 'undefined' || args == "" || args == null) {
throw new Error('args is not illegal');
}
var options = {
url: "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" + this.access_token,
form: JSON.stringify(args)
};
request.post(options, function(err, response, data) {
if (err) {
deferred.reject(err);
} else {
data = JSON.parse(data);
deferred.resolve(data);
}
});
return deferred.promise;
};
/*
*获取设置的行业信息
*https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN
*/
WxMsgTpl.prototype.getIndustry = function () {
var deferred = Q.defer();
var url = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=" + this.access_token;
request(url, function(err, response, data){
if (err) {
deferred.reject(err);
} else {
data = JSON.parse(data);
deferred.resolve(data);
}
});
return deferred.promise;
};
/*
*从行业模板库选择模板到帐号后台,获得模板ID的过程可在MP中完成。为方便第三方开发者,提供通过接口调用的方式来获取模板ID
*args {"template_id_short": "TM00015"}
*/
WxMsgTpl.prototype.getTemplateId = function (args) {
var deferred = Q.defer();
if (typeof args === 'undefined' || args == "" || args == null) {
throw new Error('args is not illegal');
}
var options = {
url: "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" + this.access_token,
form: JSON.stringify(args)
};
request.post(options, function(err, response, data) {
if (err) {
deferred.reject(err);
} else {
data = JSON.parse(data);
deferred.resolve(data);
}
});
return deferred.promise;
};
/*
*发送模板消息
* body 数据:
*/
// {
// "touser":"OPENID",
// "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
// "url":"http://weixin.qq.com/download",
// "data":{
// "first": {
// "value":"恭喜你购买成功!",
// "color":"#173177"
// },
// "keynote1":{
// "value":"巧克力",
// "color":"#173177"
// },
// "keynote2": {
// "value":"39.8元",
// "color":"#173177"
// },
// "keynote3": {
// "value":"2014年9月22日",
// "color":"#173177"
// },
// "remark":{
// "value":"欢迎再次购买!",
// "color":"#173177"
// }
// }
// }
WxMsgTpl.prototype.sendMessage = function (args) {
var deferred = Q.defer();
if (typeof args === 'undefined' || args == "" || args == null) {
throw new Error('args is not illegal');
}
var options = {
url: "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + this.access_token,
form: JSON.stringify(args)
};
request.post(options, function(err, response, data) {
if (err) {
deferred.reject(err);
} else {
data = JSON.parse(data);
deferred.resolve(data);
}
});
return deferred.promise;
};
WxMsgTpl.prototype.getAccessToken = function (args) {
var deferred = Q.defer();
if (typeof args === 'undefined' || args == "" || args == null) {
throw new Error('args is not illegal');
}
var url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+args.appid+"&secret="+args.secret;
request(url, function(err, response, data){
if (err) {
deferred.reject(err);
} else {
data = JSON.parse(data);
//this.access_token=data.access_token;
deferred.resolve(data);
}
});
return deferred.promise;
};
module.exports = WxMsgTpl;