Authored by 毕凯

PC 注册基本逻辑

... ... @@ -6,10 +6,12 @@ const _ = require('lodash');
const passportHelper = require('../models/passport-helper');
const regService = require('../models/reg-service');
const userService = require('../models/user-service');
const authHelper = require('../models/auth-helper');
const config = require('../../../config/common');
let helpers = global.yoho.helpers;
let cache = global.yoho.cache;
let cookie = global.yoho.cookie;
let index = (req, res, next) => {
// 设置注册有效时间30分钟, 防机器刷
... ... @@ -76,6 +78,220 @@ let checkMobile = (req, res, next) => {
}).catch(next);
};
let picCaptcha = (req, res, next) => {
let verifyCode = _.trim(req.body.verifyCode);
let picFlag = true; // TODO: 图形验证码校验
if (picFlag) {
return res.json({
code: 200,
message: '验证码正确'
});
}
return res.json({
code: 400,
message: '验证码错误'
});
};
let sendBindMsg = (req, res, next) => {
let data = {
code: 400,
message: '',
data: ''
};
let mobile = +req.body.mobile;
let area = +req.body.area;
let verifyCode = +req.body.verifyCode;
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
// 检查是否检查过
// let makeMobile = passportHelper.makeAreaMobile(area, mobile);
//
// if (req.session[`checkmobile_${makeMobile}`] !== makeMobile) {
// data.message = '发送失败';
// return res.json(data);
// }
// 校验是否发送过多
let sendCodeKey = `send_code_${area}_${mobile}`;
cache.get(sendCodeKey).then((sendCodeTimes) => {
if (!sendCodeTimes) {
sendCodeTimes = 0;
} else {
sendCodeTimes = +sendCodeTimes;
}
if (sendCodeTimes >= 10) {
data.message = '您已多次提交验证码,请尽快联系客服解决';
return res.json(data);
}
if (sendCodeTimes >= 5) {
data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
return res.json(data);
}
// TODO: 检测验证码不正确
// if (!PassportModel::verifyCode($verifyCode)) {
// $data['code'] = 400;
// $data['message'] = '图形验证码不正确';
// break;
// }
/* 向手机发送注册验证码 */
return regService.sendCodeToMobile(area, mobile).then((result) => {
return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
if (result.code) {
return res.json(result);
} else {
data.message = '发送失败';
return res.json(data);
}
});
});
}).catch(next);
};
let msgCaptcha = (req, res, next) => {
let data = {
code: 400,
message: '',
data: ''
};
let area = +req.body.area;
let mobile = +req.body.mobile;
let code = +req.body.code; // 短信验证码
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
regService.validMobileCode(area, mobile, code).then((result) => {
if (result.code) {
return res.json(result);
} else {
data.message = '验证码错误';
return res.json(data);
}
}).catch(next);
};
let mobileRegister = (req, res, next) => {
let data = {
code: 400,
message: '',
data: ''
};
let area = +req.body.area;
let mobile = +req.body.mobile;
// 判断参数是否合法
if (!_.isNumber(mobile) || !_.isNumber(area)) {
data.message = '手机号码格式不正确';
return res.json(data);
}
/* 判断是否是有效的注册方式,防注册机刷 */
let regExpireTime = req.session._REG_EXPIRE;
if (!regExpireTime || regExpireTime < Date.now()) {
data.message = '注册超时';
return res.json(data);
}
// TODO: 检测验证码不正确
// $verifyCode = strtolower(trim($this->post('verifyCode'))); //图形验证码
// if (!PassportModel::verifyCode($verifyCode)) {
// $data['message'] = '验证码不正确';
// break;
// }
/* 判断密码是否符合规则 */
let code = +req.body.code; // 短信验证码
let password = req.body.password;
if (!helpers.verifyPassword(password)) {
data.message = '密码不正确';
return res.json(data);
}
/* IP仅允许点击注册500次/时 */
let ip = req.ip;
let ipKey = 'ip_register_' + ip;
cache.get(ipKey).then((ipTimes) => {
if (!ipTimes) {
ipTimes = 0;
} else {
ipTimes = +ipTimes;
}
if (ipTimes >= 500) {
data.message = '由于你IP受限无法注册';
return res.json(data);
}
return cache.set(ipKey, ipTimes + 1, 3600).then(() => {
/* 验证注册的标识码是否有效 */
return regService.validMobileCode(area, mobile, code).then((result) => {
if (!result.code || result.code !== 200) {
data.message = '验证码错误';
return res.json(data);
}
let shoppingKey = cookie.getShoppingKey(req);
/* 手机注册: 调用注册接口,ip限制计数 */
return regService.regMobile(area, mobile, password, shoppingKey).then((regResult) => {
if (!regResult.code || regResult.code !== 200) {
data.message = '注册失败';
return res.json(data);
}
// 返回跳转到来源页面
let refer = req.cookies.refer;
if (refer) {
refer = decodeURI(req.cookies.refer);
} else {
refer = '/?go=1';
}
if (/sign|login/.test(refer)) {
refer = '/?go=1';
}
return authHelper.syncUserSession(regResult.data.uid).then(() => {
return res.json({
code: 200,
message: '注册成功',
data: {
href: helpers.urlFormat('/passport/register/success', {
next: refer,
goShoppingUrl: config.siteUrl
})
}
});
});
});
});
});
}).catch(next);
};
let success = (req, res, next) => {
let goUrl = req.query.next || config.siteUrl;
let goShoppingUrl = req.query.goShoppingUrl || config.siteUrl;
... ... @@ -96,5 +312,9 @@ let success = (req, res, next) => {
module.exports = {
index,
success,
checkMobile
checkMobile,
picCaptcha,
sendBindMsg,
msgCaptcha,
mobileRegister
};
... ...
... ... @@ -7,10 +7,10 @@ class Auth {
static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
let param = {
nickname : nickname,
openId : openId,
nickname: nickname,
openId: openId,
source_type: sourceType,
method : 'app.passport.signinByOpenID'
method: 'app.passport.signinByOpenID'
};
if (shoppingKey) {
... ... @@ -22,11 +22,11 @@ class Auth {
static signinByWechat(nickname, openId, unionId, sourceType, shoppingKey) {
let param = {
nickname : nickname,
openId : openId,
unionId : unionId,
nickname: nickname,
openId: openId,
unionId: unionId,
source_type: sourceType,
method : 'app.passport.signinByWechat'
method: 'app.passport.signinByWechat'
};
if (shoppingKey) {
... ... @@ -38,7 +38,7 @@ class Auth {
static profile(uid) {
let param = {
uid : uid,
uid: uid,
method: 'app.passport.profile'
};
... ... @@ -65,7 +65,7 @@ class Auth {
res.cookie('_TOKEN', token, {
domain: 'yohobuy.com'
}); // esline-disable-line
}).catch(console.log);
});
}
}
... ...
... ... @@ -5,11 +5,51 @@
const passportHelper = require('./passport-helper');
const REGISTER_LEFT_BANNER_CODE = 'c479ec90120cae7f96e52922b4917064'; // 注册左边的banner
const api = global.yoho.API;
let getRegData = () => {
return passportHelper.getLeftBannerAsync(REGISTER_LEFT_BANNER_CODE);
};
let sendCodeToMobile = (area, mobile) => {
let params = {
method: 'app.register.sendRegCodeToMobile',
area: area,
mobile: mobile
};
return api.post('', params);
};
let validMobileCode = (area, mobile, code) => {
let params = {
method: 'app.register.validRegCode',
area: area,
mobile: mobile,
code: code
};
return api.post('', params);
};
let regMobile = (area, mobile, password, shoppingKey)=> {
let params = {
method: 'app.register.register',
area: area,
profile: mobile,
password: password
};
if (shoppingKey) {
params.shopping_key = shoppingKey;
}
return api.post('', params);
};
module.exports = {
getRegData
getRegData,
sendCodeToMobile,
validMobileCode,
regMobile
};
... ...
... ... @@ -24,6 +24,10 @@ router.get('/login/wechat/callback', login.wechat.callback);
*/
router.get('/reg/index', reg.index);
router.post('/reg/checkmobile', reg.checkMobile);
router.post('/reg/piccaptcha', reg.picCaptcha);
router.post('/reg/msgcaptcha', reg.msgCaptcha);
router.post('/reg/sendBindMsg', reg.sendBindMsg);
router.post('/reg/mobileregister', reg.mobileRegister);
router.get('/reg/success', reg.success);
/**
... ...
... ... @@ -14,8 +14,8 @@ module.exports = {
port: 6002,
siteUrl: 'http://www.yohobuy.com',
domains: {
api: 'http://devapi.yoho.cn:58078/', // devapi.yoho.cn:58078 testapi.yoho.cn:28078 devapi.yoho.cn:58078
service: 'http://devservice.yoho.cn:58077/', // testservice.yoho.cn:28077 devservice.yoho.cn:58077
api: 'http://devapi.yoho.cn:58078/',
service: 'http://devservice.yoho.cn:58077/',
search: 'http://192.168.102.216:8080/yohosearch/'
},
useOneapm: false,
... ...
... ... @@ -178,7 +178,7 @@ function picCaptchaAjaxFn(page, callback) {
var url;
if (page === 'reg') {
url = '/passport/register/piccaptcha';
url = '/passport/reg/piccaptcha';
} else if (page === 'third') {
url = '/passport/autouserinfo/checkPicCode';
}
... ... @@ -218,7 +218,7 @@ function msgCaptchaAjaxFn(page, callback) {
var url;
if (page === 'reg') {
url = '/passport/register/msgcaptcha';
url = '/passport/reg/msgcaptcha';
} else if (page === 'third') {
url = '/passport/autouserinfo/checkBindMsg';
}
... ... @@ -617,7 +617,7 @@ exports.init = function(page) {
}, 1000);
if (page === 'reg') {
url = '/passport/register/sendBindMsg';
url = '/passport/reg/sendBindMsg';
} else if (page === 'third') {
url = '/passport/autouserinfo/sendBindMsg';
}
... ... @@ -646,7 +646,7 @@ exports.init = function(page) {
var url;
if (page === 'reg') {
url = '/passport/register/mobileregister';
url = '/passport/reg/mobileregister';
} else if (page === 'third') {
url = '/passport/autouserinfo/bindMobile';
}
... ...