ServiceController.js
4.43 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
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* 内部服务,禁止外部访问
*/
if (!sails.config.custom) {
throw new Error('token not config');
}
var https = require('https');
var url = require('url');
var fs = require('fs');
var custom = sails.config.custom;
module.exports = {
/**
* 模板消息发送
* 1、检测用户是否在数据中
* 2、使用 template_id_short 获取模板ID
* 3、发送消息
*
* @api {post} /service/msg 发送微信模板消息
* @apiName 发送微信模板
* @apiGroup service
*
* @apiParam {json} data 消息体数据
* @apiParam {string} touser 用户ID
* @apiParam {string} templateId 后台设置的模板ID
* @apiParam {string} url 消息跳转链接
*
* @apiParamExample {form-data} 请求示例
data={
"productType": {
"value": "商品名称:",
"color": "#333333"
},
"name": {
"value": "商品名称:",
"color": "#333333"
},
"time": {
"value": "商品名称:",
"color": "#333333"
},
"remark": {
"value": "备注:",
"color": "#333333"
}
}
touser=18913
templateId:S5TkyGdQeJHongw1lOiJX2aNEJbJrHTyCKJTZDehBT0
url:http://nong12.com
* @apiSampleRequest /service/msg
*/
msg: function(req, res, next) {
try {
var data = JSON.parse(req.body.data);
} catch (e) {
return res.json({
errcode: "-1001",
errmsg: "invaild json"
});
}
var touser = req.body.touser;
var templateId = req.body.templateId;
var url = req.body.url;
// 查询用户
User.findOne({
user_id: touser
}).exec(function(err, row) {
if (err) {
return res.json(err);
}
if (!row) {
return res.json({
errcode: "-1002",
errmsg: "no found user info"
});
}
touser = row.open_id;
sails.wechat_api.sendTemplate(touser, templateId, url, "#333333", data, function(err, return_data) {
if (err) {
return res.json({
errcode: err.code,
errmsg: err.name
});
}
return res.json(return_data);
});
});
},
/**
* 获取token
* @api {get} /service/accessToken 获取token
* @apiName 获取token
* @apiGroup service
*
* @apiSampleRequest /service/accessToken
*/
accessToken: function(req, res, next) {
sails.wechat_api.getLatestToken(function(err, token) {
if (err) {
return res.json({
errcode: err.code,
errmsg: err.name
});
}
token.code = 0;
return res.json(token);
});
},
/**
* 对URL签名
* @api {get} /service/getJsConfig 对URL签名
* @apiName 对URL签名
* @apiGroup service
* @apiParam {string} url url
* @apiParam {string} callback callback
* @apiSampleRequest /service/getJsConfig
*/
getJsConfig: function(req, res, next) {
var url = decodeURIComponent(req.query.url);
var callback = req.query.callback;
var param = {
debug: false,
jsApiList: [
'checkJsApi',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone'
],
url: url
};
console.log(param);
sails.wechat_api.getJsConfig(param, function(err, rs) {
var out = "";
if (err) {
out = JSON.stringify({
errcode: err.code,
errmsg: err.name
});
}else{
out = JSON.stringify(rs);
}
if (callback) {
out = callback + '(' + out + ')';
}
return res.send(out);
});
}
};