|
@@ -6,7 +6,6 @@ |
|
@@ -6,7 +6,6 @@ |
6
|
'use strict';
|
6
|
'use strict';
|
7
|
const _ = require('lodash');
|
7
|
const _ = require('lodash');
|
8
|
const moment = require('moment');
|
8
|
const moment = require('moment');
|
9
|
-const EventEmitter = require('events');
|
|
|
10
|
const SmsModel = require('../models/sms');
|
9
|
const SmsModel = require('../models/sms');
|
11
|
|
10
|
|
12
|
const smsController = {
|
11
|
const smsController = {
|
|
@@ -48,55 +47,45 @@ const smsController = { |
|
@@ -48,55 +47,45 @@ const smsController = { |
48
|
* @param res
|
47
|
* @param res
|
49
|
* @returns {*|boolean}
|
48
|
* @returns {*|boolean}
|
50
|
*/
|
49
|
*/
|
51
|
- sendCode(req, res) {
|
|
|
52
|
- const em = new EventEmitter();
|
50
|
+ sendCode(req, res, next) {
|
53
|
const area = (req.body.area || '86').trim();
|
51
|
const area = (req.body.area || '86').trim();
|
54
|
const mobile = (req.body.mobile || '').trim();
|
52
|
const mobile = (req.body.mobile || '').trim();
|
55
|
const inValid = [area, mobile].some(v => v === '');
|
53
|
const inValid = [area, mobile].some(v => v === '');
|
56
|
|
54
|
|
57
|
- em.on('resolve', () => {
|
|
|
58
|
- req.ctx(SmsModel).sendSMS(mobile, area, 1)
|
|
|
59
|
- .then(result => {
|
|
|
60
|
- if (result.code === 200) {
|
|
|
61
|
- _.set(req.session, 'smsSend.area', area);
|
|
|
62
|
- _.set(req.session, 'smsSend.mobile', mobile);
|
|
|
63
|
-
|
|
|
64
|
- // 剩余次数
|
|
|
65
|
- --req.session.smsSend.count;
|
|
|
66
|
-
|
|
|
67
|
- if (!req.session.smsSend.count) {
|
|
|
68
|
- _.set(req.session, 'smsSend.interval', Date.now() + 5 * 60 * 1000);
|
|
|
69
|
- } else {
|
|
|
70
|
- _.set(req.session, 'smsSend.interval', Date.now() + 60 * 1000);
|
|
|
71
|
- }
|
|
|
72
|
-
|
|
|
73
|
- return res.json({
|
|
|
74
|
- code: 200,
|
|
|
75
|
- message: '验证码发送成功'
|
|
|
76
|
- });
|
|
|
77
|
- }
|
55
|
+ if (inValid) {
|
|
|
56
|
+ return res.json({
|
|
|
57
|
+ code: 400,
|
|
|
58
|
+ message: '请填写手机号'
|
|
|
59
|
+ });
|
|
|
60
|
+ }
|
78
|
|
61
|
|
79
|
- res.json({
|
|
|
80
|
- code: 500,
|
|
|
81
|
- message: '验证码发送失败'
|
|
|
82
|
- });
|
|
|
83
|
- });
|
|
|
84
|
- });
|
62
|
+ req.ctx(SmsModel).sendSMS(mobile, area, 1)
|
|
|
63
|
+ .then(result => {
|
|
|
64
|
+ if (result.code === 200) {
|
|
|
65
|
+ _.set(req.session, 'smsSend.area', area);
|
|
|
66
|
+ _.set(req.session, 'smsSend.mobile', mobile);
|
85
|
|
67
|
|
86
|
- em.on('reject', error => {
|
|
|
87
|
- res.json(error);
|
|
|
88
|
- });
|
68
|
+ // 剩余次数
|
|
|
69
|
+ --req.session.smsSend.count;
|
89
|
|
70
|
|
|
|
71
|
+ if (!req.session.smsSend.count) {
|
|
|
72
|
+ _.set(req.session, 'smsSend.interval', Date.now() + 5 * 60 * 1000);
|
|
|
73
|
+ } else {
|
|
|
74
|
+ _.set(req.session, 'smsSend.interval', Date.now() + 60 * 1000);
|
|
|
75
|
+ }
|
90
|
|
76
|
|
91
|
- if (!inValid) {
|
|
|
92
|
- em.emit('resolve');
|
|
|
93
|
- return;
|
|
|
94
|
- }
|
77
|
+ return res.json({
|
|
|
78
|
+ code: 200,
|
|
|
79
|
+ message: '验证码发送成功'
|
|
|
80
|
+ });
|
|
|
81
|
+ }
|
95
|
|
82
|
|
96
|
- em.emit('reject', {
|
|
|
97
|
- code: 400,
|
|
|
98
|
- message: '请填写手机号'
|
|
|
99
|
- });
|
83
|
+ res.json({
|
|
|
84
|
+ code: 500,
|
|
|
85
|
+ message: '验证码发送失败'
|
|
|
86
|
+ });
|
|
|
87
|
+ })
|
|
|
88
|
+ .catch(next);
|
100
|
},
|
89
|
},
|
101
|
|
90
|
|
102
|
/**
|
91
|
/**
|
|
@@ -106,8 +95,8 @@ const smsController = { |
|
@@ -106,8 +95,8 @@ const smsController = { |
106
|
*/
|
95
|
*/
|
107
|
checkCode(req, res, next) {
|
96
|
checkCode(req, res, next) {
|
108
|
const code = (req.body.code || '').trim();
|
97
|
const code = (req.body.code || '').trim();
|
|
|
98
|
+ const mobile = (req.body.mobile || '').trim();
|
109
|
const area = _.get(req.session, 'smsSend.area', '');
|
99
|
const area = _.get(req.session, 'smsSend.area', '');
|
110
|
- const mobile = _.get(req.session, 'smsSend.mobile', '');
|
|
|
111
|
|
100
|
|
112
|
req.ctx(SmsModel).verifySMS(mobile, area, code, 1)
|
101
|
req.ctx(SmsModel).verifySMS(mobile, area, code, 1)
|
113
|
.then(result => {
|
102
|
.then(result => {
|