Authored by 郭成尧

register-success

const _ = require('lodash');
const url = require('url');
const RegService = require('../models/reg-service');
const utils = require(global.utils);
const sign = global.yoho.sign;
const co = require('bluebird').coroutine;
const cookie = global.yoho.cookie;
const AuthHelper = require('../models/auth-helper');
class Reg {
/**
* 注册页面
*/
regPage(req, res) {
if (req.user.uid) {
return res.redirect(req.get('refer') || '/');
}
// 判断是否 来自 个人中心
if (!_.get(req.session, 'phoneReg.isFromMy')) {
let referer = req.get('Referer') || '';
let urlObj = url.parse(referer, true, true);
referer = _.get(urlObj, 'query.refer', '');
urlObj = url.parse(referer, true, true);
urlObj.path === '/home' && _.set(req.session, 'phoneReg.isFromMy', '1');
}
// 设置注册有效时间30分钟, 防机器刷
// req.session.REG_EXPIRE = Date.now() + 1800000;
let refer = req.query.refer;
refer = utils.refererLimit(refer);
refer && res.cookie('refer', encodeURI(refer), {
domain: 'yohobuy.com'
});
// session init
_.set(req.session, 'phoneReg.step', 1);
if (req.session.captchaValidCount == null) { // eslint-disable-line
req.session.captchaValidCount = 5;
}
if (req.session.phoneReg.count == null) { // eslint-disable-line
req.session.phoneReg.count = 5;
}
res.render('reg/reg-new', {
module: 'passport',
page: 'reg-new',
... ... @@ -9,6 +54,99 @@ class Reg {
countrys: RegService.getAreaData() // 地区信息列表
});
}
/**
* 注册请求处理
*/
register(req, res, next) {
let postResult = {
code: 400
};
let mobile = +req.body.phoneNum;
let area = +(req.body.areaCode || 86);
let password = req.body.password;
let smsCode = +req.body.smsCode;
let inviteCode = req.body.inviteCode || '';
let isFromMy = _.get(req.session, 'phoneReg.isFromMy', '0');
let token = req.body.token;
// 购物车key
let shoppingKey = cookie.getShoppingKey(req);
// 判断参数是否合法
if (!smsCode || !_.isString(token) || !_.isNumber(mobile) || !_.isNumber(area) || !password) {
postResult.message = '请求参数不合法';
return res.json(postResult);
}
// 判断是否允许访问
if (!sign.verifyToken(mobile, token)) {
postResult.message = '非法 token';
return res.json(postResult);
}
// 判断密码是否符合规则
if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/.test(password)) {
postResult.message = '密码格式不正确';
return res.json(postResult);
}
co(function* () {
let validMobileCodeResult = yield RegService.validMobileCode(area, mobile, smsCode);
if (!validMobileCodeResult.code) {
return res.json(validMobileCodeResult);
}
let resultCode = _.get(validMobileCodeResult, 'code');
if (resultCode !== 200) {
return res.json(validMobileCodeResult);
}
let regMobileAesResult = yield RegService.regMobileAes(area, mobile, password,
shoppingKey, smsCode, inviteCode, isFromMy);
if (regMobileAesResult.code !== 200 || !regMobileAesResult.data ||
!regMobileAesResult.data.uid) {
return res.json(regMobileAesResult);
}
AuthHelper.syncUserSession(regMobileAesResult.data.uid, req, res, regMobileAesResult.data.session_key);
// 返回跳转到来源页面
let refer = req.cookies.refer;
if (regMobileAesResult.data.newUserPage) {
refer = regMobileAesResult.data.msgDelivery; // 来自个人中心,跳新人会场
} else {
if (refer) {
refer = decodeURI(req.cookies.refer);
} else {
refer = '/home';
}
if (/sign|login/.test(refer)) {
refer = '/home';
}
refer = utils.refererLimit(refer);
}
delete req.session.phoneNum;
return res.json({
code: 200,
message: '注册成功',
data: {
session: refer,
href: '//m.yohobuy.com/activity/invite-reg',
msgDelivery: _.get(regMobileAesResult, 'data.msgDelivery', '')
}
});
})().catch(next);
}
}
module.exports = Reg;
... ...
... ... @@ -180,7 +180,10 @@ router.get('/passport/login-new',
login.loginShowCaptchaByIp,
loginNew.loginPage); // 普通登录
router.get('/passport/international-new', loginNew.international); // 国际账号登录
router.get('/passport/reg-new', validateCode.load, regNew.regPage); // 注册
router.get('/passport/reg-new', validateCode.load, regNew.regPage); // 注册页面
router.post('/passport/register-new',
reg.sendCodeBusyBoy,
regNew.register); // 注册请求
router.get('/passport/sms-login-new', smsNew.smsLoginPage); // 短信验证码登录
router.get('/passport/back/mobile-new', backNew.backByMobile); // 通过手机找回密码
router.get('/passport/back/email-new', backNew.backByEmail); // 通过邮箱找回密码
... ...
... ... @@ -5,7 +5,7 @@
<div class="banner-info">
<div class="top-operation-bar">
<button class="close iconfont">&#xe72d;</button>
<a href="registerUrl" class="register">注册</a>
<a href="{{registerUrl}}" class="register">注册</a>
</div>
<div class="yoho-family-logo"></div>
<div class="tip">Yoho!Family账号可登录Yoho!Buy有货 <i class="iconfont">&#xe639;</i></div>
... ...
... ... @@ -49,6 +49,35 @@ class RegisterNew {
}
/**
* 验证码校验
*/
postVerifyCode() {
$.ajax({
type: 'POST',
url: '/passport/reg/verifycode',
data: {
phoneNum: this.view.mobileInput.val(),
areaCode: this.view.countryCodeSelector.val(),
code: this.view.verifyCodeInput.val(),
token: this.view.tokenInput.val(),
inviteCode: this.view.inviteCodeInput.val()
},
success: data => {
if (data.code === 200) {
location.href = data.data;
} else {
// 验证码不正确,显示提示
showErrTip(data.message);
if (data.refer) {
location.href = data.refer;
}
}
}
});
}
/**
* 注册动作处理
*/
register() {
... ... @@ -67,7 +96,7 @@ class RegisterNew {
}
/**
* 提交注册
* 提交注册请求
*/
postRegister() {
if (this.view.regBtn.hasClass('active')) {
... ... @@ -83,7 +112,7 @@ class RegisterNew {
return $.ajax({
type: 'POST',
url: '/passport/reg/setpassword',
url: '/passport/register-new',
data: postData,
success: (data) => {
let res = data.data;
... ...