Showing
4 changed files
with
102 additions
and
24 deletions
@@ -73,8 +73,11 @@ try { | @@ -73,8 +73,11 @@ try { | ||
73 | // 路由分发 | 73 | // 路由分发 |
74 | require('./dispatch')(app); | 74 | require('./dispatch')(app); |
75 | 75 | ||
76 | + // 404处理 | ||
77 | + app.use(middleware.error.notFound); | ||
78 | + | ||
76 | // 错误处理 | 79 | // 错误处理 |
77 | - app.use(middleware.error); | 80 | + app.use(middleware.error.serverError); |
78 | 81 | ||
79 | } catch (err) { | 82 | } catch (err) { |
80 | logger.error(err); | 83 | logger.error(err); |
@@ -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,13 +47,18 @@ const smsController = { | @@ -48,13 +47,18 @@ 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', () => { | 55 | + if (inValid) { |
56 | + return res.json({ | ||
57 | + code: 400, | ||
58 | + message: '请填写手机号' | ||
59 | + }); | ||
60 | + } | ||
61 | + | ||
58 | req.ctx(SmsModel).sendSMS(mobile, area, 1) | 62 | req.ctx(SmsModel).sendSMS(mobile, area, 1) |
59 | .then(result => { | 63 | .then(result => { |
60 | if (result.code === 200) { | 64 | if (result.code === 200) { |
@@ -80,23 +84,8 @@ const smsController = { | @@ -80,23 +84,8 @@ const smsController = { | ||
80 | code: 500, | 84 | code: 500, |
81 | message: '验证码发送失败' | 85 | message: '验证码发送失败' |
82 | }); | 86 | }); |
83 | - }); | ||
84 | - }); | ||
85 | - | ||
86 | - em.on('reject', error => { | ||
87 | - res.json(error); | ||
88 | - }); | ||
89 | - | ||
90 | - | ||
91 | - if (!inValid) { | ||
92 | - em.emit('resolve'); | ||
93 | - return; | ||
94 | - } | ||
95 | - | ||
96 | - em.emit('reject', { | ||
97 | - code: 400, | ||
98 | - message: '请填写手机号' | ||
99 | - }); | 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 => { |
1 | 1 | ||
2 | ### 点赞【邱俊】 | 2 | ### 点赞【邱俊】 |
3 | +### 发送验证码 | ||
4 | +**请求路径** | ||
5 | +> {host}/passport/sms/sendCOde | ||
6 | + | ||
7 | +**请求方法** | ||
8 | +> POST | ||
9 | + | ||
10 | +**请求参数** | ||
11 | + | ||
12 | +| 名称 | 类型 | 描述 | 备注 | ||
13 | +| ---- | ----- |---- | --- | ||
14 | +| mobile|string|手机号| | ||
15 | +| area | string|区域码| 默认:86 | ||
16 | + | ||
17 | +**应答参数** | ||
18 | + | ||
19 | +| 名称 | 描述 | ||
20 | +| -------| ------- | ||
21 | +| name | 用户昵称 | ||
22 | +| avatar | 头像 | ||
23 | +| mobile | 手机号 | ||
24 | + | ||
25 | +### Request (JSON) | ||
26 | + { | ||
27 | + "mobile": 18551982891 | ||
28 | + } | ||
29 | +### Result | ||
30 | + { | ||
31 | + "code": 200, | ||
32 | + "message": "验证码发送成功" | ||
33 | + } | ||
34 | + | ||
35 | + | ||
36 | +### 验证并返回用户信息 | ||
37 | +**请求路径** | ||
38 | +> {host}/passport/sms/checkCOde | ||
39 | + | ||
40 | +**请求方法** | ||
41 | +> POST | ||
42 | + | ||
43 | +**请求参数** | ||
44 | + | ||
45 | +|名称 | 类型 |必填| 描述 | ||
46 | +|------|-------|-----|---- | ||
47 | +|mobile|string |是|手机号 | ||
48 | +|code | string |是|验证码 | ||
49 | + | ||
50 | +**应答参数** | ||
51 | + | ||
52 | +| 名称 | 描述 | ||
53 | +| -------| ------- | ||
54 | +| name | 用户昵称 | ||
55 | +| avatar | 头像 | ||
56 | +| mobile | 手机号 | ||
57 | + | ||
58 | +### Request (JSON) | ||
59 | + { | ||
60 | + "mobile": 18551933891 | ||
61 | + "code":1132 | ||
62 | + } | ||
63 | +### Result | ||
64 | + { | ||
65 | + "code": 200, | ||
66 | + "data": { | ||
67 | + "name": "用户_933891", | ||
68 | + "avatar": "http://img12.static.yhbimg.com/imserver/2016/11/17/11/02bc3c3de856432175c01d937342a1f2ce.jpg", | ||
69 | + "mobile": "18551933891" | ||
70 | + }, | ||
71 | + "message": "获取用户信息成功" | ||
72 | + } | ||
73 | + | ||
3 | ### 图片列表【李奇】 | 74 | ### 图片列表【李奇】 |
4 | ### 上传图片【李奇】 | 75 | ### 上传图片【李奇】 |
5 | ### 发布评论【李奇】 | 76 | ### 发布评论【李奇】 |
6 | ### 图片详情【邱俊】 | 77 | ### 图片详情【邱俊】 |
7 | ### 其他基础服务【李奇】 | 78 | ### 其他基础服务【李奇】 |
79 | + |
@@ -5,7 +5,7 @@ | @@ -5,7 +5,7 @@ | ||
5 | */ | 5 | */ |
6 | const logger = global.yoho.logger; | 6 | const logger = global.yoho.logger; |
7 | 7 | ||
8 | -module.exports = (err, req, res, next) => { // eslint-disable-line | 8 | +const serverError = (err, req, res, next) => { // eslint-disable-line |
9 | logger.error(err); | 9 | logger.error(err); |
10 | if (err.code === 401) { | 10 | if (err.code === 401) { |
11 | return res.status(401).json({ | 11 | return res.status(401).json({ |
@@ -24,3 +24,17 @@ module.exports = (err, req, res, next) => { // eslint-disable-line | @@ -24,3 +24,17 @@ module.exports = (err, req, res, next) => { // eslint-disable-line | ||
24 | message: '服务器错误!' | 24 | message: '服务器错误!' |
25 | }); | 25 | }); |
26 | }; | 26 | }; |
27 | + | ||
28 | + | ||
29 | +const notFound = (req, res) => { | ||
30 | + res.status(404); | ||
31 | + return res.json({ | ||
32 | + code: 404, | ||
33 | + message: '请求路径不存在' | ||
34 | + }); | ||
35 | +}; | ||
36 | + | ||
37 | +module.exports = { | ||
38 | + notFound, | ||
39 | + serverError | ||
40 | +}; |
-
Please register or login to post a comment