...
|
...
|
@@ -2,99 +2,64 @@ |
|
|
* Created by TaoHuang on 2016/6/18.
|
|
|
*/
|
|
|
|
|
|
'use strict';
|
|
|
/**
|
|
|
* 验证码的形式: xxxx|page
|
|
|
*/
|
|
|
|
|
|
const captchaService = require('../models/captcha-img-service');
|
|
|
const CAPTCHA = global.yoho.config.UNIVERSAL_CAPTCHA;
|
|
|
const CaptchaServiceModel = require('../models/captcha-img-service');
|
|
|
const request = require('request');
|
|
|
const logger = global.yoho.logger;
|
|
|
const CAPTCHATIME = 1 * 60000; // 默认1分钟
|
|
|
|
|
|
// 对比函数
|
|
|
const _mustEqual = (req) => {
|
|
|
let result = req.session.captcha && (req.body.verifyCode === req.session.captcha ||
|
|
|
req.body.verifyCode === CAPTCHA);
|
|
|
const _mustEqualAsync = (req) => {
|
|
|
let id = req.session.id;
|
|
|
let verifyCode = req.body.verifyCode;
|
|
|
|
|
|
logger.info(`img captcha auth [${result ? 'success' : 'fail'}]`,
|
|
|
`CLIENT [${req.body.verifyCode}] SERVER [${req.session.captcha}]`);
|
|
|
if (!id || !verifyCode) {
|
|
|
return Promise.resolve({
|
|
|
code: 405,
|
|
|
message: '请将所有图片点击翻转至正向朝上'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
return req.ctx(CaptchaServiceModel).check(id, verifyCode);
|
|
|
};
|
|
|
|
|
|
// 中间件
|
|
|
const requiredAPI = (req, res, next) => {
|
|
|
let count = req.session.captchaCount;
|
|
|
let time = new Date().getTime();
|
|
|
let firstTime = req.session.captchaTime;
|
|
|
|
|
|
if (count >= 4 || firstTime && (time - firstTime > CAPTCHATIME)) {
|
|
|
req.session.captcha = CAPTCHA;
|
|
|
|
|
|
return res.json({
|
|
|
code: 405,
|
|
|
message: '该验证码已失效,请刷新验证码',
|
|
|
data: {
|
|
|
needCaptcha: true
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
req.session.captchaCount = count + 1;
|
|
|
if (_mustEqual(req)) {
|
|
|
return next();
|
|
|
} else {
|
|
|
return res.json({
|
|
|
code: 405,
|
|
|
message: '请将图形验证码翻转至正确方向',
|
|
|
data: {
|
|
|
needCaptcha: true
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
_mustEqualAsync(req).then((result) => {
|
|
|
if (result.code === 200) {
|
|
|
return next();
|
|
|
} else {
|
|
|
return res.json({
|
|
|
code: 405,
|
|
|
message: result.message,
|
|
|
data: {
|
|
|
needCaptcha: true
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
}).catch(next);
|
|
|
};
|
|
|
|
|
|
// 七牛验证码
|
|
|
const generate = (req, res, next) => {
|
|
|
captchaService.generateCaptcha().then((result) => {
|
|
|
req.session.captcha = result.data.text;
|
|
|
req.ctx(CaptchaServiceModel).generateCaptcha(req.session.id).then((result) => {
|
|
|
req.session.captchaCount = 0;
|
|
|
req.session.captchaTime = new Date().getTime();
|
|
|
|
|
|
res.type('png');
|
|
|
if (result.code === 200) {
|
|
|
request(result.data.images).pipe(res);
|
|
|
request(result.data.url).pipe(res);
|
|
|
}
|
|
|
|
|
|
}).catch(next);
|
|
|
};
|
|
|
|
|
|
// 端到端检查
|
|
|
const checkAPI = (req, res) => {
|
|
|
let count = req.session.captchaCount;
|
|
|
let time = new Date().getTime();
|
|
|
let firstTime = req.session.captchaTime;
|
|
|
|
|
|
if (count >= 4 || firstTime && (time - firstTime > CAPTCHATIME)) {
|
|
|
req.session.captcha = CAPTCHA;
|
|
|
|
|
|
return res.json({
|
|
|
code: 405,
|
|
|
message: '该验证码已失效,请刷新验证码'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
req.session.captchaCount = count + 1;
|
|
|
if (_mustEqual(req)) {
|
|
|
return res.json({
|
|
|
code: 200,
|
|
|
message: '验证成功'
|
|
|
});
|
|
|
} else {
|
|
|
return res.json({
|
|
|
code: 405,
|
|
|
message: '请将图形验证码翻转至正确方向'
|
|
|
});
|
|
|
}
|
|
|
const checkAPI = (req, res, next) => {
|
|
|
_mustEqualAsync(req).then(result => {
|
|
|
res.json(result);
|
|
|
}).catch(next);
|
|
|
};
|
|
|
|
|
|
module.exports = {
|
...
|
...
|
|