UserRelevanceController.js
4.13 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
/**
* 账号绑定控制器
*
* @description :: Server-side logic for managing userrelevances
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/
module.exports = {
/**
* 账号绑定
*/
index: function(req, res, next) {
var open_id = req.query.open_id;
var target_url = req.query.target_url;
res.locals.open_id = open_id;
res.locals.target_url = target_url;
if (!open_id) {
return res.end("您不是使用微信操作,请使用微信操作,进行绑定");
}
User.findOne({open_id:open_id,active:0}).exec(function(err,record){
if(err){
return next(err);
}
// 检查是否已经关注,如果已经关注,帮其登陆,直接跳转出去
if(record){
if(!target_url){
target_url = "http://m.nong12.com/user/user_home.do?open_id="+open_id+"&user_id="+record['user_id'];
}
target_url = decodeURIComponent(target_url);
if(/user_id=[0-9]+/.test(target_url) == false){
if(target_url.indexOf("?") !== -1){
target_url += ("&user_id="+record['user_id']);
}else{
target_url += ("?user_id="+record['user_id']);
}
}
if(/open_id=\w+/.test(target_url) == false){
if(target_url.indexOf("?") !== -1){
target_url += ("&open_id="+open_id);
}else{
target_url += ("?open_id="+open_id);
}
}
// target_url = encodeURIComponent(target_url);
HttpClient.login(record)
.then(function(login_res){
return res.redirect(target_url);
},function(login_err){
sails.log.error("系统登陆错误!");
return res.end("系统登陆错误,请稍后再试!");
});
}else{
sails.wechat_api.getUser(open_id, function(err, result) {
if (err) {
return res.json(err);
} else {
res.locals.weixin_user = result;
return res.view();
}
});
}
});
},
/**
* 绑定成功
*/
succss: function(req, res, next) {
var user_id = req.query.user_id;
if (!user_id) {
return next(user_id);
}
res.locals.target_url = decodeURIComponent(req.target_url);
User.findOne({
user_id: user_id
}).exec(function(err, record) {
if (err) {
return next(err);
} else {
res.locals.user = record;
return res.view();
}
});
},
/**
* 登陆绑定
*/
login_bind: function(req, res, next) {
if (!req.xhr) {
return res.end("fail please use ajx request");
}
var post_data = req.body;
var open_id = post_data.open_id;
var permise = Promise
.resolve(User.login(post_data.username, post_data.password))
.then(function(res_body) {
return User.bind(open_id, res_body.accountId, post_data.username, post_data.password);
})
.then(function(bind_res) {
return res.json(bind_res);
}, function(err) {
if (err.msg == "server_http_erro") {
return res.json({
result: false,
msg: "请求网络错误!",
data: {}
});
} else if (err.msg == "server_login_fail") {
return res.json({
result: false,
msg: err.data.msg,
data: err.data
});
} else {
console.log("line:117",err);
res.json({
result: false,
msg: err.msg,
data: {}
});
}
});
},
/**
* 注册绑定
*/
register_bind: function(req, res, next) {
if (!req.xhr) {
return res.end("fail please use ajx request");
}
var post_data = req.body;
var open_id = post_data.open_id;
var permise = Promise
.resolve(User.register(post_data.username, post_data.code, "", post_data.password, 1))
.then(function(register_res) {
return User.login(post_data.username, post_data.password);
})
.then(function(login_res) {
return User.bind(open_id, login_res.accountId, post_data.username, post_data.password);
})
.then(function(bind_res) {
return res.json(bind_res);
}, function(err) {
if (err.msg == "server_http_erro") {
return res.json({
result: false,
msg: "请求网络错误!",
data: {}
});
} else if (err.msg == "server_login_fail") {
return res.json({
result: false,
msg: err.data.msg,
data: err.data
});
} else {
res.json({
result: false,
msg: err.msg,
data: {}
});
}
});
},
};