...
|
...
|
@@ -3,11 +3,12 @@ |
|
|
const _ = require('lodash');
|
|
|
const helpers = global.yoho.helpers;
|
|
|
const cookie = global.yoho.cookie;
|
|
|
const EventEmitter = require('events');
|
|
|
const utils = require(global.utils);
|
|
|
const RegService = require('../models/reg-service');
|
|
|
const PhoneService = require('../models/phone-service');
|
|
|
const AuthHelper = require('../models/auth-helper');
|
|
|
const captchaService = require('../models/captcha-service');
|
|
|
const imgcheckAPI = require('../../serverAPI/imgcheck');
|
|
|
|
|
|
// constrant
|
|
|
const CODE_REQUIRED = '请输入校验码';
|
...
|
...
|
@@ -42,19 +43,28 @@ const _step1 = (req, res, next) => { |
|
|
req.session.smsLogin.count = 5;
|
|
|
}
|
|
|
|
|
|
let template = 'sms/login';
|
|
|
let viewData = {
|
|
|
module: 'passport',
|
|
|
page: 'sms-login',
|
|
|
title: '手机短信登录',
|
|
|
isPassportPage: true,
|
|
|
headerText: '手机号码快捷登录',
|
|
|
captchaUrl: helpers.urlFormat('/passport/sms_login/captcha.png', {t: Date.now()}),
|
|
|
areaCode: '+86', // 默认的区号
|
|
|
countrys: RegService.getAreaData() // 地区信息列表
|
|
|
};
|
|
|
imgcheckAPI.gen()
|
|
|
.then(captcha => {
|
|
|
_.set(req.session, 'captcha.code', captcha.data.code);
|
|
|
|
|
|
let template = 'sms/login';
|
|
|
let viewData = {
|
|
|
width750: true,
|
|
|
module: 'passport',
|
|
|
page: 'sms-login',
|
|
|
title: '手机短信登录',
|
|
|
isPassportPage: true,
|
|
|
headerText: '手机号码快捷登录',
|
|
|
captchaUrl: helpers.urlFormat('/passport/sms_login/captcha.png', {t: Date.now()}),
|
|
|
areaCode: '+86', // 默认的区号
|
|
|
countrys: RegService.getAreaData(), // 地区信息列表
|
|
|
captsrc: captcha.data.src
|
|
|
};
|
|
|
|
|
|
res.render(template, viewData);
|
|
|
})
|
|
|
.catch(next);
|
|
|
|
|
|
res.render(template, viewData);
|
|
|
};
|
|
|
|
|
|
// 短信登录 第二步: 输入 校验码
|
...
|
...
|
@@ -118,44 +128,59 @@ exports.loginPage = (req, res, next) => { |
|
|
}
|
|
|
};
|
|
|
|
|
|
exports.tokenBefore = (req, res, next) => {
|
|
|
|
|
|
/**
|
|
|
* step1 的表单提交验证
|
|
|
*/
|
|
|
exports.indexCheck = (req, res, next) => {
|
|
|
_.set(req.session, 'smsLogin.step', 1);
|
|
|
|
|
|
let area = req.query.area = (req.query.area || '').trim();
|
|
|
let mobile = req.query.mobile = (req.query.mobile || '').trim();
|
|
|
let step = _.get(req.session, 'smsLogin.step');
|
|
|
let count = _.get(req.session, 'smsLogin.count');
|
|
|
let interval = _.get(req.session, 'smsLogin.interval');
|
|
|
let captcha1 = _.get(req.session, 'smsLogin.captcha');
|
|
|
let captcha2 = (req.query.captcha || '').trim();
|
|
|
let captcode = (req.body.captcode || '').trim();
|
|
|
let captcodeValid = _.get(req.session, 'captcha.code');
|
|
|
|
|
|
let em = new EventEmitter();
|
|
|
|
|
|
if (!req.xhr) {
|
|
|
return next(404);
|
|
|
}
|
|
|
// 校验 成功
|
|
|
em.on('resolve', () => {
|
|
|
// 1. 将信息放入 session
|
|
|
_.set(req.session, 'smsLogin.area', area);
|
|
|
_.set(req.session, 'smsLogin.mobile', mobile);
|
|
|
_.set(req.session, 'smsLogin.step', 2);
|
|
|
|
|
|
if ([area, mobile].some(val => val === '')) {
|
|
|
return res.json({
|
|
|
code: 401,
|
|
|
message: '手机号 必填'
|
|
|
res.json({
|
|
|
redirect: '/passport/sms_login?step=2'
|
|
|
});
|
|
|
});
|
|
|
|
|
|
// 校验 失败
|
|
|
em.on('reject', error => {
|
|
|
_.set(req.session, 'smsLogin.step', 1);
|
|
|
|
|
|
res.status(400).json(error);
|
|
|
});
|
|
|
|
|
|
// 验证
|
|
|
if ([area, mobile].some(val => val === '')) {
|
|
|
em.emit('reject', {message: '请填写手机号'});
|
|
|
} else if (captcode !== captcodeValid) {
|
|
|
em.emit('reject', {message: '请将图片旋转到正确位置'});
|
|
|
}
|
|
|
|
|
|
delete req.session.smsLogin.captcha; // 图形验证码 一次性
|
|
|
// congratulation~~
|
|
|
em.emit('resolve');
|
|
|
};
|
|
|
|
|
|
// step1 要 校验图形验证码
|
|
|
if (step === 1) {
|
|
|
if (!captcha2) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: '请填写验证码'
|
|
|
});
|
|
|
}
|
|
|
exports.tokenBefore = (req, res, next) => {
|
|
|
|
|
|
if (captcha1 !== captcha2) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: VERIFY_ERROR
|
|
|
});
|
|
|
}
|
|
|
let step = _.get(req.session, 'smsLogin.step');
|
|
|
let count = _.get(req.session, 'smsLogin.count');
|
|
|
let interval = _.get(req.session, 'smsLogin.interval');
|
|
|
|
|
|
|
|
|
if (!req.xhr || step !== 2) {
|
|
|
return next(404);
|
|
|
}
|
|
|
|
|
|
let now = Date.now();
|
...
|
...
|
@@ -186,8 +211,8 @@ exports.tokenBefore = (req, res, next) => { |
|
|
|
|
|
// AJAX 获取验证码
|
|
|
exports.token = (req, res, next) => {
|
|
|
let area = req.query.area;
|
|
|
let mobile = req.query.mobile;
|
|
|
let area = _.get(req.session, 'smsLogin.area');
|
|
|
let mobile = _.get(req.session, 'smsLogin.mobile');
|
|
|
|
|
|
PhoneService.sendSMS(mobile, area, 1).then(result => {
|
|
|
if (result.code === 200) {
|
...
|
...
|
@@ -376,6 +401,7 @@ exports.password = (req, res, next) => { |
|
|
/**
|
|
|
* 生成 校验码
|
|
|
*/
|
|
|
/*
|
|
|
exports.genCaptcha = (req, res) => {
|
|
|
let captcha = captchaService.generateCaptcha(90, 52, 4);
|
|
|
|
...
|
...
|
@@ -386,3 +412,4 @@ exports.genCaptcha = (req, res) => { |
|
|
.status(200)
|
|
|
.send(captcha.image);
|
|
|
};
|
|
|
*/ |
...
|
...
|
|