Authored by 郭成尧

validata-change-api

'use strict';
const _ = require('lodash');
const logger = global.yoho.logger;
let imgCheckAPI = require('../models/imgcheck');
const imgCheckServiceModel = require('../models/imgcheck');
const request = require('request');
const uuid = require('uuid');
exports.get = (req, res, next) => {
let data = {
src: ''
};
let udid = req.session.id;
return imgCheckAPI.gen().then(result => {
return req.ctx(imgCheckServiceModel).gen(udid).then(result => {
if (result.code === 200 && result.data) {
let codeStr = result.data.degrees.reduce((str, rotate) => {
return str.concat((4 - rotate / 90 % 4) % 4);
}, '');
req.session.captcha = codeStr;
req.session.captchaSrc = result.data.verifiedGraphicCode;
data.src = `/passport/img-check.jpg?t=${Date.now()}`;
return res.json(data);
}
next();
}).catch(next);
};
exports.imgCheck = (req, res, next) => {
return imgCheckAPI.gen().then(result => {
if (result.code === 200 && result.data) {
let codeStr = result.data.degrees.reduce((str, rotate) => {
return str.concat((4 - rotate / 90 % 4) % 4);
}, '');
let udid = req.session.id;
req.session.captcha = codeStr;
req.session.captchaTimeout = new Date().getTime() + 1000 * 60;
req.session.captchaSrc = result.data.verifiedGraphicCode;
return request(`${result.data.verifiedGraphicCode}?imageView2/0/format/jpg/q/99|watermark/2/text/${uuid.v4()}/fontsize/120/dissolve/10`).pipe(res); // eslint-disable-line
return req.ctx(imgCheckServiceModel).gen(udid).then(result => {
if (result.code === 200 && result.data) {
return request(result.data.verifiedGraphicCode).pipe(res); // eslint-disable-line
}
next();
}).catch(next);
... ... @@ -51,44 +34,14 @@ exports.imgCheck = (req, res, next) => {
* 验证img-check验证码
*/
exports.validate = (req, res, next) => {
let udid = req.session.id;
let captchaInput = req.body.captcha;
let captchaCode = _.get(req.session, 'captcha');
let captchaTimeout = _.get(req.session, 'captchaTimeout');
if (new Date().getTime() > captchaTimeout) {
_.set(req.session, 'captchaValidCount', 5);
req.session.captcha = null;
return res.json({
code: 400,
message: '验证码超时,请重试',
changeCaptcha: true,
captchaShow: true
});
}
let errorCount = _.get(req.session, 'captchaValidCount'); // 初始1次 + 后续4次, 同一个验证码 共5次
let jsonData = {
code: 400,
message: '请将所有图片点击翻转至正向朝上',
captchaShow: true
};
logger.info(`captcha validate result${(captchaInput && captchaInput.toString()) === captchaCode},user:${captchaInput};server:${captchaCode}`); // eslint-disable-line
_.set(req.session, 'captchaValidCount', errorCount - 1);
if (!errorCount) {
_.set(req.session, 'captchaValidCount', 5);
req.session.captcha = null;
jsonData.changeCaptcha = true;
}
if (!(captchaInput && captchaCode && captchaInput === captchaCode)) {
return res.json(jsonData);
}
req.session.captcha = null;
req.session.captchaValidCount = null;
req.session.useYohoCaptcha = null;
return req.ctx(imgCheckServiceModel).check(udid, captchaInput).then(result => {
if (result.code === 200) {
return next();
} else {
return res.json(result);
}
});
};
... ...
'use strict';
const _ = require('lodash');
let captchaData = require('../data/captcha.json');
const PAGE = 'H5';
const serviceAPI = global.yoho.ServiceAPI.ApiUrl;
// let api = global.yoho.API;
/**
* 获取图形旋转验证码
* @return Promise
* {
* "alg": "SALT_MD5",
* "code": 200,
* "data":
* {
* "degrees":
* [
* 90,
* 270,
* 90,
* 0
* ],
* "verifiedGraphicCode": "http://img11.static.yhbimg.com/yhb-img01/2016/12/09/09/011c1ef761ab6f0cdb9fbd116f409a6e52.png"
* },
* "md5": "25d3e6b030a142c32d246322e6884080",
* "message": "操作成功"
*}
*/
exports.gen = () => {
// let params = {
// method: 'web.register.getVerifiedGraphicCode'
// };
// return api.get('', params);
let random = _.random(0, captchaData.length);
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
/**
* 获取图片
*/
gen(udid) {
return Promise.resolve({
code: 200,
data: captchaData[random]
data: {
verifiedGraphicCode: `${serviceAPI}passport/img-check.jpg?udid=${udid}&fromPage=${PAGE}`
}
});
}
/**
* 校验
*/
check(udid, captcha) {
return this.get({
data: {
method: 'app.verified.graphic',
udid: udid,
fromPage: PAGE,
degrees: captcha
}
});
}
};
... ...
... ... @@ -184,7 +184,7 @@ ImgCheck.prototype = {
result.push(val % 4);
});
return result.join('');
return result.join(',');
}
};
... ...
... ... @@ -91,7 +91,7 @@ class Validate {
} else if (this.type === validType.IMG_CHECK) {
let captcha = this.imgCheck.getResults();
if (captcha === '0000') {
if (captcha === '0,0,0,0') {
tip.show('请将所有图片点击旋转至正向朝上');
return Promise.reject();
}
... ...