ApiController.js
5.52 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* 微信接入控制器,和微信对接API 使用
*/
if (!sails.config.custom) {
throw new Error('token not config');
}
var https = require('https');
var url = require('url');
var custom = sails.config.custom;
// 跳转map
var btnMaps = {
"btn_my_purchase":"http://m.nong12.com/user/my_purchase.do",
"btn_my_sale":"http://m.nong12.com/user/my_sale.do"
};
module.exports = {
/**
* 微信通知入口方法
* @param {[type]} req [description]
*/
index: function(req, res, next) {
// 认证检查
if (req.method !== "POST") {
if (!WechatUtils.checkSignature(req.query, custom.token)) {
console.log(req.query);
res.send("Bad Token!");
} else {
res.send(req.query.echostr);
}
}
if (req.method == "POST") {
WechatDispatch.postMsg(custom, req, function(err, data) {
if (err) {
return next(err);
}
if (typeof data === "object") {
// 渲染输出
res.set('Content-Type', 'text/xml; charset=utf-8');
var view = "api/" + data.MsgType + ".ejs";
return res.view(view, data);
} else {
return res.end(data);
}
});
}
},
/**
* 发起菜单更新
* 使用第三方模块
*/
updateMenu: function(req, res, next) {
var menu = Menu.data;
sails.wechat_api.createMenu(menu, function(err, result) {
if (err) {
return next(err);
}
return res.json(result);
});
},
/**
* 发起授权请求,匿名认证code,获取openid
*/
auth: function(req, res, next) {
var code = req.query.code;
var EventKey = req.query.event_key;
var OpenID = "";
if (!code) {
return res.end("访问非法,请在微信中访问");
}
var p_getopenid = new Promise(function(resolve, reject) {
var request_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + custom.appid + "&secret=" + custom.appsecret + "&code=" + code + "&grant_type=authorization_code"
var opt = {
method: "GET",
host: url.parse(request_url).host,
port: 443,
path: url.parse(request_url).path,
};
var body = '';
var req = https.request(opt, function(http_res) {
http_res.on('data', function(d) {
body += d;
}).on('end', function() {
body = JSON.parse(body);
return resolve(body);
});
}).on('error', function(e) {
return reject(e);
})
req.end();
});
Promise.resolve(p_getopenid).then(function(auth_res) {
OpenID = auth_res.openid;
if (!OpenID) {
return res.end("参数错误");
}
return User.findOne({
open_id: OpenID,
active: 0
});
})
.then(function(user_record) {
if (!user_record) {
return Promise.reject({
name: 'no_bind',
"msg": ""
});
}
return user_record;
})
.then(function(user_record) {
return HttpClient.login(user_record);
})
.then(function resolve(login_res) {
// TODO 需要重构,建立btn 和 url的映射
var target_url = "http://m.nong12.com/user/user_home.do?open_id=" + OpenID;
if (EventKey == "btn_ordersearch") {
target_url = 'http://m.nong12.com/order/shopped_list.do?orderState=-1&user_id=' + login_res.accountId + '&open_id=' + OpenID;
}
if (EventKey == "btn_usercenter") {
target_url = 'http://m.nong12.com/user/user_home.do?user_id=' + login_res.accountId + '&open_id=' + OpenID;
}
if (EventKey == "supply_list") {
target_url = 'http://m.nong12.com/supply/supply_list.do?user_id=' + login_res.accountId + '&open_id=' + OpenID;
}
if (EventKey == "release_supply") {
target_url = 'http://m.nong12.com/supply/release_supply.do?user_id=' + login_res.accountId + '&open_id=' + OpenID;
}
if (EventKey == "btn_category") {
target_url = "http://m.nong12.com/features/category.do?id=0&open_id=" + OpenID + "&user_id=" + login_res.accountId;
}
if(btnMaps[EventKey]){
target_url = btnMaps[EventKey]+'?user_id=' + login_res.accountId + '&open_id=' + OpenID;
}
res.redirect(target_url);
}, function reject(err) {
var target_url = "http://m.nong12.com/user/user_home.do" + '?open_id=' + OpenID;
if (EventKey == "btn_usercenter") {
target_url = encodeURIComponent("http://m.nong12.com/user/user_home.do" + '?open_id=' + OpenID);
}
if (EventKey == "btn_ordersearch") {
target_url = encodeURIComponent("http://m.nong12.com/order/shopped_list.do?orderState=-1" + '&open_id=' + OpenID);
}
if (EventKey == "supply_list") {
target_url = 'http://m.nong12.com/supply/supply_list.do?open_id=' + OpenID;
}
if (EventKey == "release_supply") {
target_url = encodeURIComponent('http://m.nong12.com/supply/release_supply.do?open_id=' + OpenID);
}
if(btnMaps[EventKey]){
target_url = encodeURIComponent(btnMaps[EventKey] + '?open_id=' + OpenID);
}
console.log(err,EventKey,target_url);
// 跳转绑定
if (err.name == "no_bind") {
// 部分链接允许先不跳转
if (['btn_category','supply_list'].indexOf(EventKey) !== -1) {
if(EventKey == "btn_category"){
target_url = "http://m.nong12.com/features/category.do?id=0&open_id=" + OpenID;
}
if(EventKey == "supply_list"){
target_url = "http://m.nong12.com/supply/supply_list.do?open_id=" + OpenID;
}
return res.redirect(target_url);
} else {
return res.redirect('http://wx.nong12.com/UserRelevance/index?target_url=' + target_url + '&open_id=' + OpenID);
}
} else {
console.log(err);
// 先解除关联
User.destroy({
open_id: OpenID
}).exec(function(destroy_err, records) {
if (destroy_err) {
err = destroy_err;
}
res.locals.err = err;
res.locals.open_id = OpenID;
return res.view();
});
}
});
}
};