|
|
/* eslint no-unused-vars: ["error", { "args": "none" }] */
|
|
|
'use strict';
|
|
|
const RegService = require('../models/reg-service');
|
|
|
const PhoneService = require('../models/phone-service');
|
|
|
|
|
|
// constrant
|
|
|
const CODE_REQUIRED = '请输入校验码';
|
|
|
|
|
|
// 短信登录 第一步: 展现页面
|
|
|
const step1 = (req, res, next) => {
|
|
|
let template = 'sms/login';
|
|
|
let viewData = {
|
|
|
module: 'passport',
|
|
|
page: 'sms-login',
|
|
|
isPassportPage: true,
|
|
|
headerText: '手机号码快捷登录',
|
|
|
areaCode: '+86', // 默认的区号
|
|
|
countrys: RegService.getAreaData() // 地区信息列表
|
|
|
};
|
|
|
|
|
|
res.render(template, viewData);
|
|
|
};
|
|
|
|
|
|
// 短信登录 第二步: 输入 校验码
|
|
|
const step2 = (req, res, next) => {
|
|
|
const mobile = req.session.sms_login.mobile;
|
|
|
const area = req.session.sms_login.area;
|
|
|
|
|
|
const template = 'sms/check';
|
|
|
const viewData = {
|
|
|
module: 'passport',
|
|
|
page: 'sms-check',
|
|
|
isPassportPage: true,
|
|
|
headerText: '手机号码快捷登录',
|
|
|
interval: 60,
|
|
|
mobile,
|
|
|
area
|
|
|
};
|
|
|
|
|
|
res.render(template, viewData);
|
|
|
};
|
|
|
|
|
|
// 短信登录 第二步: 设置密码 (针对 改手机未注册用户)
|
|
|
const step3 = (req, res, next) => {
|
|
|
|
|
|
};
|
|
|
|
|
|
// 短信 登录
|
|
|
exports.loginPage = (req, res, next) => {
|
|
|
let step = Number(req.query.step) || 1;
|
|
|
|
|
|
switch (step) {
|
|
|
case 2: step2(req, res, next); break;
|
|
|
case 3: step3(req, res, next); break;
|
|
|
case 1:
|
|
|
default:
|
|
|
step1(req, res, next);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
exports.token = (req, res, next) => {
|
|
|
if (!req.xhr) {
|
|
|
return next(404);
|
|
|
}
|
|
|
|
|
|
let area = req.query.area;
|
|
|
let mobile = req.query.mobile;
|
|
|
|
|
|
PhoneService.sendSMS({ mobile, area }).then(result => {
|
|
|
if (result.code === 200) {
|
|
|
req.session.sms_login = {
|
|
|
area,
|
|
|
mobile
|
|
|
};
|
|
|
|
|
|
result.redirect = '/passport/sms_login?step=2';
|
|
|
res.json(result);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
res.json(result);
|
|
|
});
|
|
|
};
|
|
|
|
|
|
exports.check = (req, res, next) => {
|
|
|
if (!req.xhr) {
|
|
|
return next(404);
|
|
|
}
|
|
|
// TODO: 防御性
|
|
|
|
|
|
let code = req.query.code || '';
|
|
|
const mobile = req.session.sms_login.mobile;
|
|
|
const area = req.session.sms_login.mobile;
|
|
|
|
|
|
code = code.trim();
|
|
|
if (!code) {
|
|
|
res.json({
|
|
|
code: 404,
|
|
|
message: CODE_REQUIRED
|
|
|
});
|
|
|
}
|
|
|
|
|
|
Promise.all([
|
|
|
PhoneService.checkUserPhoneExist(mobile, area),
|
|
|
PhoneService.verifySMS(mobile, area, code)
|
|
|
])
|
|
|
.then(result => {
|
|
|
let r1 = result[0] || {};
|
|
|
let r2 = result[1] || {};
|
|
|
let redirect;
|
|
|
|
|
|
// 验证码 校验失败
|
|
|
if (r2.code !== 200) {
|
|
|
res.json(r2);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 检测 手机号 是否注册 异常
|
|
|
if (r1.code !== 200) {
|
|
|
res.json(r1);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (r1.data.is_register === 'Y') {
|
|
|
redirect = '';
|
|
|
} else {
|
|
|
redirect = '/passport/sms_login?step=3';
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
code: 200,
|
|
|
redirect
|
|
|
});
|
|
|
})
|
|
|
.catch();
|
|
|
};
|
|
|
|
|
|
exports.password = (req, res, next) => {
|
|
|
res.render('sms/password', {
|
|
|
module: 'passport',
|
|
|
page: 'sms-login',
|
|
|
isPassportPage: true,
|
|
|
headerText: '设置密码',
|
|
|
});
|
|
|
};
|
|
|
|
...
|
...
|
|