Authored by 毕凯

Merge branch 'feature/passport' of git.yoho.cn:fe/yohobuy-node into feature/passport

... ... @@ -10,20 +10,20 @@ const service = require('../models/back-service');
const passportHelper = require('../models/passport-helper');
const _ = require('lodash');
//helpers.urlFormat = (url, qs) => {
// let localhost = 'http://localhost:6002';
//
// if (_.isEmpty(qs)) {
// return localhost + url;
// }
//
// const queryString = require('queryString');
//
// let str = queryString.stringify(qs);
//
// return localhost + url + '?' + str;
//
//};
helpers.urlFormat = (url, qs) => {
let localhost = 'http://localhost:6002';
if (_.isEmpty(qs)) {
return localhost + url;
}
const queryString = require('queryString');
let str = queryString.stringify(qs);
return localhost + url + '?' + str;
};
/**
* 找回密码主页面
... ... @@ -33,8 +33,8 @@ const index = (req, res, next) => {
.then(result => {
res.render('back/index', Object.assign({
module: 'passport',
page : 'back-index',
title : '找回密码'
page: 'back-index',
title: '找回密码'
}, result));
})
.catch(next);
... ... @@ -54,7 +54,7 @@ const validateInputAPI = (req, res, next) => {
})
.catch(err => {
res.json({
code : 400,
code: 400,
message: err
});
});
... ... @@ -93,11 +93,11 @@ const sendCodePage = (req, res, next) => {
service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
if (result.code && result.code === 200) {
return next();
} else {
if (!(result.code && result.code === 200)) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
return next();
})
.catch(next);
};
... ... @@ -105,22 +105,22 @@ const sendCodePage = (req, res, next) => {
const saveInSession = (req, res) => {
switch (req.inputInfo.type) {
case 'email':
{
req.session.email = req.inputInfo.phone;
res.redirect(helpers.urlFormat('/passport/back/sendEmail'));
break;
}
{
req.session.email = req.inputInfo.phone;
res.redirect(helpers.urlFormat('/passport/back/sendEmail'));
break;
}
case 'mobile':
{
req.session.mobile = req.inputInfo.phone;
req.session.area = req.inputInfo.area;
res.redirect(helpers.urlFormat('/passport/back/verification'));
break;
}
{
req.session.mobile = req.inputInfo.phone;
req.session.area = req.inputInfo.area;
res.redirect(helpers.urlFormat('/passport/back/verification'));
break;
}
default:
{
res.redirect(helpers.urlFormat('/passport/back/index'));
}
{
res.redirect(helpers.urlFormat('/passport/back/index'));
}
}
};
... ... @@ -142,24 +142,23 @@ const validateMobileAPI = (req, res, next) => {
if (!passportHelper.validator.isAreaMobile(passportHelper.makeAreaMobile(area, mobile))) {
return res.json(ERR);
} else {
return next();
}
next();
};
const validateEmailInSession = (req, res, next) => {
let email = req.session.email || '';
if (!email) {
res.redirect(helpers.urlFormat('/passport/back/index'));
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
let isp = email.split('@')[1];
const mapperEmailISP = {
'yoho.cn': 'http://smail.yoho.cn'
};
let isp = email.split('@')[1];
req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
next();
};
... ... @@ -169,13 +168,13 @@ const sendEmailPage = (req, res, next) => {
.then(result => {
res.render('back/send-email', Object.assign({
module: 'passport',
page : 'back-send-email-ok',
title : '邮件发送成功'
page: 'back-send-email-ok',
title: '邮件发送成功'
}, {
sendEmail: {
coverHref: result.url,
coverImg : result.img,
email : req.body.emailUrl
coverImg: result.img,
email: req.body.emailUrl
}
}));
})
... ... @@ -187,8 +186,7 @@ const validateCodeByEmailPage = (req, res, next) => {
let code = req.query.code || '';
if (!_.isEmpty(req.mobileAuth)) {
next();
return;
return next();
}
service.checkEmailCodeAsync(code)
... ... @@ -209,13 +207,13 @@ const resetPasswordPage = (req, res, next) => {
.then(result => {
res.render('back/reset-pwd', Object.assign({
module: 'passport',
page : 'back-reset-pwd',
title : '重置密码'
page: 'back-reset-pwd',
title: '重置密码'
}, {
resetPwd: Object.assign({
coverHref: result.url,
coverImg : result.img,
code : code
coverImg: result.img,
code: code
}, req.mobileAuth)
}));
})
... ... @@ -235,14 +233,14 @@ const verifyCodeByMobilePage = (req, res, next) => {
.then(result => {
res.render('back/verification', Object.assign({
module: 'passport',
page : 'back-verify-mobile-code',
title : '手机验证'
page: 'back-verify-mobile-code',
title: '手机验证'
}, {
verification: {
coverHref : result.url,
coverImg : result.img,
mobile : req.body.mobile,
area : req.body.area,
coverHref: result.url,
coverImg: result.img,
mobile: req.body.mobile,
area: req.body.area,
verifyCode: req.body.verifyCode
}
}));
... ... @@ -253,12 +251,12 @@ const verifyCodeByMobilePage = (req, res, next) => {
const checkSuccessStatusPage = (req, res, next) => {
let successType = req.session.successType || '';
if (successType) {
delete req.session.successType;
next();
} else {
res.redirect(helpers.urlFormat('/passport/back/index'));
if (!successType) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
delete req.session.successType;
next();
};
const resetPwdSuccessPage = (req, res, next) => {
... ... @@ -266,12 +264,12 @@ const resetPwdSuccessPage = (req, res, next) => {
.then(result => {
res.render('back/reset-success', Object.assign({
module: 'passport',
page : 'back-index',
title : '重置密码成功'
page: 'back-index',
title: '重置密码成功'
}, {
resetSuccess: {
coverHref: result.url,
coverImg : result.img
coverImg: result.img
}
}));
})
... ... @@ -282,12 +280,13 @@ const verifyCodeByMobileAPI = (req, res) => {
let mobile = req.param('mobile', '');
let area = req.param('area', '86');
let mobileCode = req.param('code', '');
const session = req.session;
// const session = req.session;
const ERR = {
code : 400,
code: 400,
message: '验证码错误!',
data : helpers.urlFormat('/passport/back/index')
data: helpers.urlFormat('/passport/back/index')
};
// if (!code || mobile !== session.mobile || area !== session.area) {
... ... @@ -304,7 +303,7 @@ const verifyCodeByMobileAPI = (req, res) => {
};
const validateExistCodePage = (req, res, next) => {
let code = req.param('code', '');
let code = req.query.code || req.body.code;
if (!code) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
... ... @@ -314,24 +313,25 @@ const validateExistCodePage = (req, res, next) => {
};
const validateCodeByMobilePage = (req, res, next) => {
let code = req.param('code', '');
let mobile = req.param('mobile', '');
let code = req.query.code || req.body.code;
let mobile = req.query.mobile || req.body.mobile;
let area = req.query.area || req.body.area;
let token = req.query.token || req.body.token;
let createdAt = req.query.createdAt || req.body.createdAt;
if (!mobile) {
req.mobileAuth = {};
next();
return;
return next();
}
let data = {
mobile : mobile,
area : req.param('area', ''),
token : req.param('token', ''),
create_time: req.param('create_time', 0)
mobile: mobile,
area: area,
token: token,
createdAt: createdAt
};
code = new Buffer(code, 'base64').toString();
req.mobileAuth = service.authRequest(data, code);
next();
... ... @@ -340,11 +340,11 @@ const validateCodeByMobilePage = (req, res, next) => {
const validatePwdPage = (req, res, next) => {
let pwd = req.body.pwd || '';
if (helpers.isPassword(pwd)) {
next();
} else {
res.redirect(helpers.urlFormat('/passport/back/index'));
if (!passportHelper.validator.isPassword(pwd)) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
next();
};
const updatePwdAPI = (req, res, next) => {
... ... @@ -365,20 +365,15 @@ const updatePwdAPI = (req, res, next) => {
};
const validateMobileInSession = (req, res, next) => {
console.log(req.session.mobile);
console.log(req.session.area);
req.body.mobile = req.session.mobile || '';
req.body.verifyCode = req.session.verifyCode || '';
req.body.area = req.session.area || '';
if (req.body.mobile && req.body.verifyCode) {
return next();
} else {
if (!(req.body.mobile && req.body.verifyCode)) {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
next();
};
module.exports = {
... ...
... ... @@ -29,7 +29,6 @@ const requiredPage = (req, res, next) => {
}
};
module.exports = {
requiredAPI,
requiredPage
... ...
... ... @@ -4,6 +4,7 @@
'use strict';
const passportHelper = require('../models/passport-helper');
const regService = require('../models/reg-service');
const config = require('../../../config/common');
let helpers = global.yoho.helpers;
... ... @@ -35,6 +36,25 @@ let index = (req, res, next) => {
}).catch(next);
};
let success = (req, res, next) => {
let goUrl = req.query.next || config.siteUrl;
// $goShoppingUrl = $this->get('goShoppingUrl',SITE_MAIN);
// $cover = PassportModel::getLeftBanner(PassportModel::REGISTER_LEFT_BANNER_CODE);
// $data = array(
// 'registerPage' => true,
// 'passport' => array(
// 'goUrl' => $goUrl,
// 'goShoppong' => $goShoppingUrl,
// 'coverHref' => $cover['url'],
// 'coverImg' => $cover['img'],
// ),
// 'uid' => $this->getUid(false),
// );
// $this->_view->display('success', $data);
};
module.exports = {
index
index,
success
};
... ...
... ... @@ -123,13 +123,11 @@ const verifyCodyByMobileAsync = (area, mobile, mobileCode) => {
mobile: mobile,
area: area,
token: result.data.token,
create_time: moment().unix()
createdAt: moment().unix()
};
data.code = new Buffer(backHelper.makeToken(data)).toString('base64');
console.log(data.code);
return {
code: 200,
message: '验证成功',
... ... @@ -145,7 +143,7 @@ const authRequest = (data, token) => {
}
let existTime = moment.duration(1, 'hours').seconds();
let isExpired = (moment().unix() - data.create_time) > existTime;
let isExpired = (moment().unix() - data.createdAt) > existTime;
if (isExpired) {
return {};
... ...
... ... @@ -230,12 +230,23 @@ const makeAreaMobile = (area, mobile) => {
return `${area}-${mobile}`;
};
const isPassword = pwd => {
if (!pwd) {
return false;
}
let pwdRegexp = /^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/;
return pwdRegexp.test(_.trim(pwd));
};
module.exports = {
validator: {
verifyPassword,
verifyMobile,
isAreaMobile,
verifyEmail
verifyEmail,
isPassword
},
makeAreaMobile,
getCountry,
... ...
... ... @@ -10,8 +10,8 @@ const express = require('express');
const cRoot = './controllers';
const login = require(cRoot + '/login');
const Captcha = require(cRoot + '/captcha');
const Back = require(cRoot + '/back');
const captcha = require(cRoot + '/captcha');
const back = require(cRoot + '/back');
const reg = require(cRoot + '/reg');
const router = express.Router(); // eslint-disable-line
... ... @@ -28,37 +28,38 @@ router.get('/reg/index', reg.index);
* 找回密码首页信息
*/
// 找回密码首页
router.get('/back/index', Back.index);
router.get('/back/index', back.index);
// 实时验证输入是否正确
router.post('/back/authcode',
Captcha.requiredAPI,
Back.validateInputAPI,
Back.getUserInfoAPI);
// captcha.requiredAPI,
back.validateInputAPI,
back.getUserInfoAPI);
// 提交按钮邮件API
router.post('/back/email',
// Captcha.requiredPage,
Back.validateUserPage,
Back.sendCodePage,
Back.saveInSession);
back.validateUserPage,
back.sendCodePage,
back.saveInSession);
// 提交按钮手机API
router.post('/back/mobile',
Captcha.requiredPage,
Back.validateUserPage,
Back.sendCodePage,
Back.saveInSession);
captcha.requiredPage,
back.validateUserPage,
back.sendCodePage,
back.saveInSession);
/**
* 邮件找回密码
*/
// 发送邮件成功页面
router.get('/back/sendEmail',
Back.validateEmailInSession,
Back.sendEmailPage);
back.validateEmailInSession,
back.sendEmailPage);
/**
* 短信找回密码
... ... @@ -68,42 +69,43 @@ router.get('/back/verification',
// Back.validateMobileInSession,
// Captcha.requiredPage,
Back.verifyCodeByMobilePage);
back.verifyCodeByMobilePage);
// 重新发送短信接口
router.post('/back/sendBackMobile',
// Captcha.requiredAPI,
Back.validateMobileAPI,
Back.sendBackMobileAPI);
back.validateMobileAPI,
back.sendBackMobileAPI);
// 验证手机验证码接口
router.post('/back/backMobile',
// Captcha.requiredAPI,
Back.verifyCodeByMobileAPI);
back.verifyCodeByMobileAPI);
/**
* 重置密码
*/
// 重置密码页面
router.get('/back/backcode',
Back.validateExistCodePage,
Back.validateCodeByMobilePage,
Back.validateCodeByEmailPage,
Back.resetPasswordPage);
back.validateExistCodePage,
back.validateCodeByMobilePage,
back.validateCodeByEmailPage,
back.resetPasswordPage);
// 重置密码接口
router.post('/back/update',
Back.validateExistCodePage,
Back.validateCodeByMobilePage,
Back.validatePwdPage,
Back.updatePwdAPI);
back.validateExistCodePage,
back.validateCodeByMobilePage,
// Back.validatePwdPage,
back.updatePwdAPI);
// 重置密码成功页面
router.get('/back/resetSuccess',
// Back.checkSuccessStatusPage,
Back.resetPwdSuccessPage);
back.resetPwdSuccessPage);
module.exports = router;
... ...
... ... @@ -14,7 +14,7 @@ module.exports = {
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:28077/', // testservice.yoho.cn:28077 devservice.yoho.cn:58077
service: 'http://devservice.yoho.cn:58077/', // testservice.yoho.cn:28077 devservice.yoho.cn:58077
search: 'http://192.168.102.216:8080/yohosearch/'
},
useOneapm: false,
... ... @@ -76,12 +76,11 @@ if (isProduction) {
} else if (isTest) {
Object.assign(module.exports, {
appName: 'www.yohobuy.com for test',
// domains: {
// api: 'http://testapi.yoho.cn:28078/',
// service: 'http://testservice.yoho.cn:28077/',
// search: 'http://192.168.102.216:8080/yohosearch/'
// },
domains: {
api: 'http://testapi.yoho.cn:28078/',
service: 'http://testservice.yoho.cn:28077/',
search: 'http://192.168.102.216:8080/yohosearch/'
},
useOneapm: true,
useCache: true,
memcache: {
... ...
... ... @@ -325,7 +325,6 @@ exports.isPassword = (pwd) => {
return false;
}
// TODO
let pwdRegexp = /^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/;
return pwdRegexp.test(_.trim(pwd));
... ...
... ... @@ -52,7 +52,7 @@
"uuid": "^2.0.2",
"winston": "^2.2.0",
"winston-daily-rotate-file": "^1.1.4",
"yoho-node-lib": "0.0.5"
"yoho-node-lib": "0.0.7"
},
"devDependencies": {
"autoprefixer": "^6.3.6",
... ...
... ... @@ -41,6 +41,7 @@ $sc.click(function() {
$sc.val(seconds-- + '秒后可重新操作');
}
}, 1000);
} else {
$(this).addClass('error');
$errTip.removeClass('hide').text('发送失败');
... ...