Authored by 姜枫

完成第三方登录绑定

... ... @@ -10,6 +10,7 @@
const helpers = require(global.library + '/helpers');
const RegService = require('../models/reg-service');
const BindService = require('../models/bind-service');
const AuthHelper = require('../models/auth-helper');
const _ = require('lodash');
const Sources = {
... ... @@ -90,14 +91,14 @@ const bind = {
if (result.code === 200) {
let nextUrl = helpers.urlFormat('/passport/bind/code', {
isReg: res.data.is_register, // esline-disable-line
isReg: result.data.is_register, // esline-disable-line
openId: openId,
sourceType: sourceType,
areaCode: areaCode,
phoneNum: phoneNum
});
data.data.isReg = res.data.is_register;
data.data.isReg = result.data.is_register;
data.data.next = nextUrl;
} else {
data.data = result.data;
... ... @@ -112,6 +113,142 @@ const bind = {
data: ''
});
}
},
sendBindMsg: (req, res) => {
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode;
if (req.xhr && _.isNumber(parseInt(phoneNum, 0)) && areaCode) {
BindService.sendBindMsg(areaCode, phoneNum).then(result => {
if (result && result.code) {
res.json(result);
} else {
res.json({ code: 400, message: '', data: '' });
}
});
} else {
res.json({ code: 400, message: '', data: '' });
}
},
checkBindMsg: (req, res) => {
let phoneNum = req.body.phoneNum;
let code = req.body.code;
let areaCode = req.body.areaCode;
if (_.isNumber(parseInt(phoneNum, 0)) && code && areaCode) {
BindService.checkBindMsg(areaCode, phoneNum, code).then(result => {
if (result && result.code) {
res.json(result);
} else {
res.json({ code: 400, message: '', data: '' });
}
});
} else {
res.json({ code: 400, message: '', data: '' });
}
},
bindMobile: (req, res) => {
let phoneNum = req.body.phoneNum;
let openId = req.body.openId;
let areaCode = req.body.areaCode || '86';
let sourceType = req.body.sourceType;
let code = req.body.code;
let password = req.body.password || '';
if (_.isNumber(parseInt(phoneNum)) && openId && sourceType && areaCode && code) {
BindService.checkBindCode(areaCode, phoneNum, code).then(result => {
if (result && result.code && result.code === 200) {
return BindService.bindMobile(openId, sourceType, phoneNum, areaCode, password);
} else {
return { code: 400, message: '短信验证码错误', data: '' };
}
}).then(result => {
let refer = req.cookies.refer;
refer = refer ? decodeURI(refer) : helpers.urlFormat();
if (result && result.code && result.code === 200 && result.data.uid) {
AuthHelper.syncUserSession(result.data.uid, req, res);
result.data.refer = refer;
}
return result;
}).then(result => {
res.json(result);
});
} else {
res.json({ code: 400, message: '', data: '' });
}
},
relateMobile: (req, res) => {
let phoneNum = req.body.phoneNum;
let openId = req.body.openId;
let areaCode = req.body.areaCode || '86';
let sourceType = req.body.sourceType;
let code = req.body.code;
if (_.isNumber(parseInt(phoneNum, 0)) && openId && areaCode && sourceType && code) {
BindService.checkBindCode(areaCode, phoneNum, code).then(result => {
if (result && result.code && result.code === 200) {
return BindService.relateMobile(openId, sourceType, phoneNum, code);
} else {
return { code: 400, message: '短信验证码错误', data: '' };
}
}).then(result => {
let refer = helpers.urlFormat('/passport/bind/success', { sourceType: sourceType });
if (result && result.code && result.code === 200 && result.data.uid) {
AuthHelper.syncUserSession(result.data.uid, req, res);
result.data.refer = refer;
}
return result;
}).then(result => {
res.json(result);
});
} else {
res.json({ code: 400, message: '', data: '' });
}
},
successPage: (req, res) => {
res.render('bind/success', {
isPassportPage: true,
successTip: '恭喜您,第三方账号关联手机号码成功!',
goUrl: helpers.urlFormat(),
module: 'passport',
page: 'bind-success',
title: '绑定手机号'
});
},
changeCheck: (req, res) => {
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode;
if (_.isNumber(parseInt(phoneNum, 0)) && areaCode) {
BindService.changeCheck(phoneNum, areaCode).then(result => {
res.json(result);
});
} else {
res.json({ code: 400, message: '', data: '' });
}
},
changeMobile: (req, res) => {
let uid = req.user.uid;
let phoneNum = req.body.phoneNum;
let areaCode = req.body.areaCode;
let code = req.body.code;
if (_.isNumber(parseInt(phoneNum)) && uid && areaCode && code) {
BindService.changeMobile(uid, phoneNum, areaCode, code).then(result => {
res.json(result);
});
} else {
res.json({ code: 400, message: '', data: '' });
}
}
};
... ...
... ... @@ -23,6 +23,73 @@ class RegService {
return api.get('', params);
}
static sendBindMsg(area, mobile) {
let params = {
method: 'app.passport.smsbind',
mobile: mobile,
area: area
};
return api.get('', params);
}
static checkBindCode(area, mobile, code) {
return api.get('', {
method: 'app.register.validRegCode',
mobile: mobile,
area: area,
code: code
});
}
static bindMobile(openId, sourceType, mobile, area, password, nickname) {
let params = {
method: 'app.passport.bind',
mobile: mobile,
open_id: openId,
source_type: sourceType,
area: area
};
if (password) {
params.password = password;
}
if (nickname) {
params.nickname = nickname;
}
return api.get('', params);
}
static relateMobile(openId, sourceType, mobile, area) {
return api.get('', {
method: 'app.passport.relateMobile',
mobile: mobile,
openId: openId,
source_type: sourceType,
area: area
});
}
static changeCheck(mobile, area) {
return api.get('', {
method: 'app.passport.changeCheck',
mobile: mobile,
area: area
});
}
static changeMobile(uid, mobile, area, code) {
return api.get('', {
method: 'app.passport.changeMobile',
mobile: mobile,
uid: uid,
code: code,
area: area
});
}
}
module.exports = RegService;
... ...
... ... @@ -10,6 +10,7 @@ const express = require('express');
const cRoot = './controllers';
const login = require(cRoot + '/login');
const back = require(cRoot + '/back');
const bind = require(cRoot + '/bind');
const router = express.Router(); // eslint-disable-line
... ... @@ -34,6 +35,19 @@ router.get('/login/sina/callback', login.sina.callback);
router.get('/login/qq', login.common.beforeLogin, login.qq.login);
router.get('/login/qq/callback', login.qq.callback);
// 登录绑定
router.get('/bind/index', bind.indexPage);
router.post('/bind/bindCheck', bind.bindCheck);
router.get('/bind/code', bind.codePage);
router.post('/bind/sendBindMsg', bind.sendBindMsg);
router.post('/bind/bindMobile', bind.bindMobile);
router.post('/bind/relateMobile', bind.relateMobile);
router.get('/bind/success', bind.successPage);
router.post('/bind/changeCheck', bind.changeCheck);
router.post('/bind/changeMobile', bind.changeMobile);
/**
* 邮箱
*/
... ...
... ... @@ -8,44 +8,47 @@ var $ = require('yoho-jquery'),
Handlebars = require('yoho-handlebars'),
Hammer = require('yoho-hammer');
var $dialogWrapper,
dialogTpl,
dialogTemplate;
function getInstance() {
if (dialogTpl === null) {
dialogTpl = '<div id="dialog-wrapper" class="dialog-wrapper">' +
'<div class="dialog-box">' +
'{{# hasHeader}}' +
'{{/ hasHeader}}' +
'<div class="dialog-content">{{dialogText}}</div>' +
'{{# hasFooter}}' +
'<div class="dialog-footer">' +
'{{# leftBtnText}}' +
'<span class="dialog-left-btn tap-hightlight">{{.}}</span>' +
'{{/ leftBtnText}}' +
'{{# rightBtnText}}' +
'<span class="dialog-right-btn tap-hightlight">{{.}}</span>' +
'{{/ rightBtnText}}' +
'</div>' +
'{{/ hasFooter}}' +
'</div>' +
'</div>';
dialogTemplate = Handlebars.compile(dialogTpl);
}
return dialogTemplate;
}
// fullWithBtn是供详情页获取限购码使用的特殊参数
exports.showDialog = function(data, callback, callbackForLeft, fullWithBtn) {
var dialogStr = dialogTemplate(data),
var dialogStr,
$dialogBox,
defaultHideDuraton,
$dialogWrapper,
dialogWrapperHammer;
dialogTemplate = getInstance();
function getInstance() {
var dialogTpl = null,
dialogTemplate;
if (dialogTpl === null) {
dialogTpl = '<div id="dialog-wrapper" class="dialog-wrapper">' +
'<div class="dialog-box">' +
'{{# hasHeader}}' +
'{{/ hasHeader}}' +
'<div class="dialog-content">{{dialogText}}</div>' +
'{{# hasFooter}}' +
'<div class="dialog-footer">' +
'{{# leftBtnText}}' +
'<span class="dialog-left-btn tap-hightlight">{{.}}</span>' +
'{{/ leftBtnText}}' +
'{{# rightBtnText}}' +
'<span class="dialog-right-btn tap-hightlight">{{.}}</span>' +
'{{/ rightBtnText}}' +
'</div>' +
'{{/ hasFooter}}' +
'</div>' +
'</div>';
dialogTemplate = Handlebars.compile(dialogTpl);
}
return dialogTemplate;
}
dialogStr = getInstance()(data);
$('.dialog-wrapper').remove();
$('body').append($(dialogStr));
... ...
... ... @@ -11,6 +11,29 @@ body.passport-body {
text-align: center;
padding: 0 6%;
.bind-tip {
margin-top: 10PX;
margin-bottom: 10PX;
color: #fff;
font-size: 13PX;
.title {
font-size: 28PX;
line-height: 1.5;
}
}
.tip {
margin-top: 30PX;
color: #fff;
font-size: 16PX;
line-height: 2;
a {
color: #3FB274;
}
}
.header {
position: relative;
height: 40PX;
... ...