|
|
/* eslint no-unused-vars: ["error", { "args": "none" }] */
|
|
|
'use strict';
|
|
|
const _ = require('lodash');
|
|
|
const moment = require('moment');
|
|
|
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');
|
|
|
|
|
|
// constrant
|
|
|
const CODE_REQUIRED = '请输入校验码';
|
|
|
const PASSWORD_REQUIRED = '请输入密码';
|
|
|
const PASSWORD_LENGTH_ERROR = '密码6-20位,请重新输入';
|
|
|
const BAD_PASSWORD = '密码格式不正确';
|
|
|
const TOO_MANY = '请求太频繁';
|
|
|
const LOGIN_SUCCSS = '登录成功';
|
|
|
const VERIFY_ERROR = '校验失败';
|
|
|
|
...
|
...
|
@@ -44,7 +44,6 @@ const _step1 = (req, res, next) => { |
|
|
|
|
|
let template = 'sms/login';
|
|
|
let viewData = {
|
|
|
width750: true,
|
|
|
module: 'passport',
|
|
|
page: 'sms-login',
|
|
|
title: '手机短信登录',
|
...
|
...
|
@@ -56,8 +55,6 @@ const _step1 = (req, res, next) => { |
|
|
};
|
|
|
|
|
|
res.render(template, viewData);
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
// 短信登录 第二步: 输入 校验码
|
...
|
...
|
@@ -121,75 +118,44 @@ exports.loginPage = (req, res, next) => { |
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
/**
|
|
|
* step1 的表单提交验证
|
|
|
*/
|
|
|
exports.indexCheck = (req, res, next) => {
|
|
|
_.set(req.session, 'smsLogin.step', 1);
|
|
|
|
|
|
let area = req.body.area = (req.body.area || '').trim();
|
|
|
let mobile = req.body.mobile = (req.body.mobile || '').trim();
|
|
|
let captcode = (req.body.captcode || '').trim();
|
|
|
let captcodeValid = _.get(req.session, 'captcha');
|
|
|
|
|
|
let em = new EventEmitter();
|
|
|
|
|
|
// 校验 成功
|
|
|
em.on('resolve', () => {
|
|
|
// 1. 将信息放入 session
|
|
|
_.set(req.session, 'smsLogin.area', area);
|
|
|
_.set(req.session, 'smsLogin.mobile', mobile);
|
|
|
_.set(req.session, 'smsLogin.step', 2);
|
|
|
|
|
|
PhoneService.sendSMS(mobile, area, 1);
|
|
|
|
|
|
--req.session.smsLogin.count;
|
|
|
|
|
|
if (!req.session.smsLogin.count) {
|
|
|
_.set(req.session, 'smsLogin.interval', Date.now() + 5 * 60 * 1000);
|
|
|
} else {
|
|
|
_.set(req.session, 'smsLogin.interval', Date.now() + 60 * 1000);
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
code: 200,
|
|
|
redirect: '/passport/sms_login?step=2'
|
|
|
});
|
|
|
});
|
|
|
|
|
|
// 校验 失败
|
|
|
em.on('reject', error => {
|
|
|
_.set(req.session, 'smsLogin.step', 1);
|
|
|
|
|
|
res.json(error);
|
|
|
});
|
|
|
exports.tokenBefore = (req, res, next) => {
|
|
|
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();
|
|
|
|
|
|
|
|
|
req.session.captcha = void 0; // 验证码 用过就扔
|
|
|
if (!req.xhr) {
|
|
|
return next(404);
|
|
|
}
|
|
|
|
|
|
// 验证
|
|
|
if ([area, mobile].some(val => val === '')) {
|
|
|
return em.emit('reject', {code: 400, message: '请填写手机号'});
|
|
|
} else if (!captcode) {
|
|
|
return em.emit('reject', {code: 400, message: '请填写验证码'});
|
|
|
} else if (captcode !== captcodeValid) {
|
|
|
return em.emit('reject', {code: 400, message: '请将图片旋转到正确位置'});
|
|
|
return res.json({
|
|
|
code: 401,
|
|
|
message: '手机号 必填'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
// congratulation~~
|
|
|
em.emit('resolve');
|
|
|
};
|
|
|
|
|
|
exports.tokenBefore = (req, res, next) => {
|
|
|
|
|
|
let step = _.get(req.session, 'smsLogin.step');
|
|
|
let count = _.get(req.session, 'smsLogin.count');
|
|
|
let interval = _.get(req.session, 'smsLogin.interval');
|
|
|
delete req.session.smsLogin.captcha; // 图形验证码 一次性
|
|
|
|
|
|
// step1 要 校验图形验证码
|
|
|
if (step === 1) {
|
|
|
if (!captcha2) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: '请填写验证码'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (!req.xhr || step !== 2) {
|
|
|
return next(404);
|
|
|
if (captcha1 !== captcha2) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: VERIFY_ERROR
|
|
|
});
|
|
|
}
|
|
|
}
|
|
|
|
|
|
let now = Date.now();
|
...
|
...
|
@@ -198,14 +164,11 @@ exports.tokenBefore = (req, res, next) => { |
|
|
// 1. 过了冻结期, count 重设为 5次
|
|
|
// 2. 没过冻结期, end
|
|
|
// 没有用完, 判断是否请求太频繁
|
|
|
let during = moment.duration(interval - now, 'ms').minutes();
|
|
|
let message = `请${during}分钟后再试`;
|
|
|
|
|
|
if (!count) {
|
|
|
if (interval > now) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: message,
|
|
|
message: TOO_MANY,
|
|
|
during: Math.ceil((interval - now) / 1000)
|
|
|
});
|
|
|
} else {
|
...
|
...
|
@@ -214,19 +177,17 @@ exports.tokenBefore = (req, res, next) => { |
|
|
} else if (interval > now) {
|
|
|
return res.json({
|
|
|
code: 429,
|
|
|
message: message
|
|
|
message: TOO_MANY
|
|
|
});
|
|
|
}
|
|
|
|
|
|
next();
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// AJAX 获取验证码
|
|
|
exports.token = (req, res, next) => {
|
|
|
let area = _.get(req.session, 'smsLogin.area');
|
|
|
let mobile = _.get(req.session, 'smsLogin.mobile');
|
|
|
let area = req.query.area;
|
|
|
let mobile = req.query.mobile;
|
|
|
|
|
|
PhoneService.sendSMS(mobile, area, 1).then(result => {
|
|
|
if (result.code === 200) {
|
...
|
...
|
@@ -415,7 +376,6 @@ exports.password = (req, res, next) => { |
|
|
/**
|
|
|
* 生成 校验码
|
|
|
*/
|
|
|
/*
|
|
|
exports.genCaptcha = (req, res) => {
|
|
|
let captcha = captchaService.generateCaptcha(90, 52, 4);
|
|
|
|
...
|
...
|
@@ -426,4 +386,3 @@ exports.genCaptcha = (req, res) => { |
|
|
.status(200)
|
|
|
.send(captcha.image);
|
|
|
}; |
|
|
*/ |
...
|
...
|
|