...
|
...
|
@@ -6,7 +6,6 @@ |
|
|
'use strict';
|
|
|
const _ = require('lodash');
|
|
|
const moment = require('moment');
|
|
|
const EventEmitter = require('events');
|
|
|
const SmsModel = require('../models/sms');
|
|
|
|
|
|
const smsController = {
|
...
|
...
|
@@ -48,55 +47,45 @@ const smsController = { |
|
|
* @param res
|
|
|
* @returns {*|boolean}
|
|
|
*/
|
|
|
sendCode(req, res) {
|
|
|
const em = new EventEmitter();
|
|
|
sendCode(req, res, next) {
|
|
|
const area = (req.body.area || '86').trim();
|
|
|
const mobile = (req.body.mobile || '').trim();
|
|
|
const inValid = [area, mobile].some(v => v === '');
|
|
|
|
|
|
em.on('resolve', () => {
|
|
|
req.ctx(SmsModel).sendSMS(mobile, area, 1)
|
|
|
.then(result => {
|
|
|
if (result.code === 200) {
|
|
|
_.set(req.session, 'smsSend.area', area);
|
|
|
_.set(req.session, 'smsSend.mobile', mobile);
|
|
|
|
|
|
// 剩余次数
|
|
|
--req.session.smsSend.count;
|
|
|
|
|
|
if (!req.session.smsSend.count) {
|
|
|
_.set(req.session, 'smsSend.interval', Date.now() + 5 * 60 * 1000);
|
|
|
} else {
|
|
|
_.set(req.session, 'smsSend.interval', Date.now() + 60 * 1000);
|
|
|
}
|
|
|
|
|
|
return res.json({
|
|
|
code: 200,
|
|
|
message: '验证码发送成功'
|
|
|
});
|
|
|
}
|
|
|
if (inValid) {
|
|
|
return res.json({
|
|
|
code: 400,
|
|
|
message: '请填写手机号'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
res.json({
|
|
|
code: 500,
|
|
|
message: '验证码发送失败'
|
|
|
});
|
|
|
});
|
|
|
});
|
|
|
req.ctx(SmsModel).sendSMS(mobile, area, 1)
|
|
|
.then(result => {
|
|
|
if (result.code === 200) {
|
|
|
_.set(req.session, 'smsSend.area', area);
|
|
|
_.set(req.session, 'smsSend.mobile', mobile);
|
|
|
|
|
|
em.on('reject', error => {
|
|
|
res.json(error);
|
|
|
});
|
|
|
// 剩余次数
|
|
|
--req.session.smsSend.count;
|
|
|
|
|
|
if (!req.session.smsSend.count) {
|
|
|
_.set(req.session, 'smsSend.interval', Date.now() + 5 * 60 * 1000);
|
|
|
} else {
|
|
|
_.set(req.session, 'smsSend.interval', Date.now() + 60 * 1000);
|
|
|
}
|
|
|
|
|
|
if (!inValid) {
|
|
|
em.emit('resolve');
|
|
|
return;
|
|
|
}
|
|
|
return res.json({
|
|
|
code: 200,
|
|
|
message: '验证码发送成功'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
em.emit('reject', {
|
|
|
code: 400,
|
|
|
message: '请填写手机号'
|
|
|
});
|
|
|
res.json({
|
|
|
code: 500,
|
|
|
message: '验证码发送失败'
|
|
|
});
|
|
|
})
|
|
|
.catch(next);
|
|
|
},
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -106,8 +95,8 @@ const smsController = { |
|
|
*/
|
|
|
checkCode(req, res, next) {
|
|
|
const code = (req.body.code || '').trim();
|
|
|
const mobile = (req.body.mobile || '').trim();
|
|
|
const area = _.get(req.session, 'smsSend.area', '');
|
|
|
const mobile = _.get(req.session, 'smsSend.mobile', '');
|
|
|
|
|
|
req.ctx(SmsModel).verifySMS(mobile, area, code, 1)
|
|
|
.then(result => {
|
...
|
...
|
|