Authored by 李奇

接口文档修改

... ... @@ -73,8 +73,11 @@ try {
// 路由分发
require('./dispatch')(app);
// 404处理
app.use(middleware.error.notFound);
// 错误处理
app.use(middleware.error);
app.use(middleware.error.serverError);
} catch (err) {
logger.error(err);
... ...
... ... @@ -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 => {
... ...
### 点赞【邱俊】
### 发送验证码
**请求路径**
> {host}/passport/sms/sendCOde
**请求方法**
> POST
**请求参数**
| 名称 | 类型 | 描述 | 备注
| ---- | ----- |---- | ---
| mobile|string|手机号|
| area | string|区域码| 默认:86
**应答参数**
| 名称 | 描述
| -------| -------
| name | 用户昵称
| avatar | 头像
| mobile | 手机号
### Request (JSON)
{
"mobile": 18551982891
}
### Result
{
"code": 200,
"message": "验证码发送成功"
}
### 验证并返回用户信息
**请求路径**
> {host}/passport/sms/checkCOde
**请求方法**
> POST
**请求参数**
|名称 | 类型 |必填| 描述
|------|-------|-----|----
|mobile|string |是|手机号
|code | string |是|验证码
**应答参数**
| 名称 | 描述
| -------| -------
| name | 用户昵称
| avatar | 头像
| mobile | 手机号
### Request (JSON)
{
"mobile": 18551933891
"code":1132
}
### Result
{
"code": 200,
"data": {
"name": "用户_933891",
"avatar": "http://img12.static.yhbimg.com/imserver/2016/11/17/11/02bc3c3de856432175c01d937342a1f2ce.jpg",
"mobile": "18551933891"
},
"message": "获取用户信息成功"
}
### 图片列表【李奇】
### 上传图片【李奇】
### 发布评论【李奇】
### 图片详情【邱俊】
### 其他基础服务【李奇】
... ...
... ... @@ -5,7 +5,7 @@
*/
const logger = global.yoho.logger;
module.exports = (err, req, res, next) => { // eslint-disable-line
const serverError = (err, req, res, next) => { // eslint-disable-line
logger.error(err);
if (err.code === 401) {
return res.status(401).json({
... ... @@ -24,3 +24,17 @@ module.exports = (err, req, res, next) => { // eslint-disable-line
message: '服务器错误!'
});
};
const notFound = (req, res) => {
res.status(404);
return res.json({
code: 404,
message: '请求路径不存在'
});
};
module.exports = {
notFound,
serverError
};
... ...