bind.js
3.69 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
/**
* 第三方登录后绑定
*
* @author: jiangfeng<jeff.jiang@yoho.cn>
*/
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const passportHelper = require('../models/passport-helper');
const BindService = require('../models/bind-service');
const LoginService = require('../models/login-service');
// const UserService = require('../models/user-service');
const simpleHeaderModel = require('../../../doraemon/models/simple-header');
const Sources = {
qq: 'QQ',
sina: '微博',
alipay: '支付宝',
wechat: '微信',
renren: '人人',
douban: '豆瓣'
};
const bind = {
indexPage: (req, res) => {
let openId = req.query.openId;
let sourceType = req.query.sourceType;
let nickName = req.query.nickName;
res.render('bind/index', {
thirdLogin: true,
openId: openId,
sourceType: sourceType,
nickName: nickName,
region: passportHelper.getCountry(),
serviceUrl: helpers.urlFormat('/help', {
category_id: 9
}),
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
module: 'passport',
page: 'bind',
title: '联合登录补全信息'
});
},
bindSuccess: (req, res) => {
let sourceType = _.trim(req.query.sourceType);
let sourceInfo = sourceType.split('_');
let sourceName = Sources[sourceInfo[0]];
let isRelate = (sourceInfo[1] === 'relate');
let data = {
thirdLogin: true,
goShopping: helpers.urlFormat('/'),
sourceName: sourceName,
relate: isRelate,
module: 'passport',
page: 'bind-success',
simpleHeader: simpleHeaderModel.setSimpleHeaderData(),
title: '绑定手机号'
};
req.session.type = '';
res.render('bind/success', data);
},
sendBindMsg: (req, res, next) => {
let mobile = req.body.mobile;
let area = req.body.area;
req.ctx(BindService).sendBindMsg(area, mobile).then(result => {
if (result && result.code) {
return res.json(result);
} else {
return res.json({code: 400, message: '', data: ''});
}
}).catch(next);
},
bindMobile: (req, res, next) => {
let mobile = _.trim(req.body.mobile);
let area = _.trim(req.body.area) || '86';
let openId = _.trim(req.body.openId);
let sourceType = _.trim(req.body.sourceType);
let nickName = _.trim(req.body.nickName);
let code = _.trim(req.body.code);
try {
nickName = decodeURIComponent(nickName);
} catch (e) {
nickName = '';
}
req.ctx(BindService).bindMobile(openId, sourceType, mobile, area, nickName, code).then(result => {
if (result && result.code) {
if (result.code === 200 && result.data && result.data.uid) {
let refer = helpers.urlFormat('/passport/thirdlogin/bindsuccess', {
sourceType: sourceType + '_bind'
});
return req.ctx(LoginService).syncUserSession(result.data, req, res).then(() => {
return {code: 200, message: result.message, data: {refer: refer}};
});
} else {
return {code: result.code, message: result.message, data: {refer: ''}};
}
} else {
return {code: 400, message: '', data: ''};
}
}).then(result => {
return res.json(result);
}).catch(next);
},
};
module.exports = bind;