Authored by htoooth

update captcha

... ... @@ -48,7 +48,7 @@ const validateInputAPI = (req, res, next) => {
/**
* 校验用户输入信息,是否是已经注册的用户
*/
const validateUserPage = (req, res, next) => {
const validateInputPage = (req, res, next) => {
let userInput = req.body.phoneNum || '';
let areaCode = (req.body.area || '86').replace('+', '');
... ... @@ -78,6 +78,7 @@ const sendCodePage = (req, res, next) => {
service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
.then(result => {
if (!(result.code && result.code === 200)) {
console.log(result);
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
... ... @@ -98,6 +99,7 @@ const saveInSession = (req, res) => {
{
req.session.mobile = req.inputInfo.phone;
req.session.area = req.inputInfo.area;
req.session.verifyCode = req.session.captcha;
res.redirect(helpers.urlFormat('/passport/back/verification'));
break;
}
... ... @@ -138,9 +140,9 @@ const validateEmailInSession = (req, res, next) => {
return res.redirect(helpers.urlFormat('/passport/back/index'));
}
let isp = email.split('@')[1];
let isp = email.split('@')[1].toLowerCase();
const mapperEmailISP = {
'yoho.cn': 'http://smail.yoho.cn'
'yoho.cn': 'http://exmail.qq.com/login'
};
req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
... ... @@ -254,9 +256,9 @@ const resetPwdSuccessPage = (req, res, next) => {
};
const verifyCodeByMobileAPI = (req, res) => {
let mobile = req.param('mobile', '');
let area = req.param('area', '86');
let mobileCode = req.param('code', '');
let mobile = req.body.mobile || '';
let area = req.body.area || '86';
let mobileCode = req.body.code || '';
const ERR = {
code: 400,
message: '验证码错误!',
... ... @@ -279,7 +281,6 @@ const verifyCodeByMobileAPI = (req, res) => {
};
const validateExistCodePage = (req, res, next) => {
let code = req.query.code || req.body.code;
if (!code) {
... ... @@ -343,7 +344,7 @@ const updatePwdAPI = (req, res, next) => {
const validateMobileInSession = (req, res, next) => {
req.body.mobile = req.session.mobile || '';
req.body.verifyCode = req.session.verifyCode || '';
req.body.verifyCode = req.session.captcha || '';
req.body.area = req.session.area || '';
if (!(req.body.mobile && req.body.verifyCode)) {
... ... @@ -371,7 +372,7 @@ module.exports = {
resetPwdSuccessPage, // 重设密码成功页面
validateInputAPI, // 验证用户输入的邮箱或者手机是否合法,返回是json
validateUserPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
validateInputPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
validateEmailInSession, // 验证邮箱是否在session中
validateMobileInSession, // 验证手机是否在session中
... ...
... ... @@ -4,13 +4,11 @@
'use strict';
const Captchapng = require('captchapng');
const _ = require('lodash');
const captchaService = require('../models/captcha-service');
const helpers = global.yoho.helpers;
const requiredAPI = (req, res, next) => {
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
let captchaToken = req.body.verifyCode || '';
if (captchaToken === req.session.captcha) {
return next();
... ... @@ -23,7 +21,7 @@ const requiredAPI = (req, res, next) => {
};
const requiredPage = (req, res, next) => {
let captchaToken = +(req.body.verifyCode || '').toLowerCase();
let captchaToken = req.body.verifyCode || '';
if (captchaToken === req.session.captcha) {
return next();
... ... @@ -32,28 +30,11 @@ const requiredPage = (req, res, next) => {
}
};
const _generateCaptcha = (width, height, length) => {
let min = Math.pow(10, (length - 1 || 1));
let max = Math.pow(10, (length - 1 || 1)) * 9;
let token = '' + _.random(min, max);
let png = new Captchapng(width, height, token);//
png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
return {
image: new Buffer(png.getBase64(), 'base64'),
text: token
};
};
const generate = (req, res) => {
let width = req.query.w || 150;
let height = req.query.h || 50;
let length = +(req.query.l || 4);
let captcha = _generateCaptcha(width, height, length);
let captcha = captchaService.generateCaptcha(width, height, length);
req.session.captcha = captcha.text;
res.writeHead(200, {
... ...
/**
* Created by TaoHuang on 2016/7/1.
*/
'use strict';
const _ = require('lodash');
const Captchapng = require('captchapng');
exports.generateCaptcha = (width, height, length) => {
let min = Math.pow(10, (length - 1 || 1));
let max = Math.pow(10, (length - 1 || 1)) * 9;
let token = '' + _.random(min, max);
let png = new Captchapng(width, height, token);//
png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
return {
image: new Buffer(png.getBase64(), 'base64'),
text: token
};
};
... ...
... ... @@ -84,14 +84,14 @@ router.post('/back/authcode',
// 提交按钮邮件API
router.post('/back/email',
captcha.requiredPage,
back.validateUserPage,
back.validateInputPage,
back.sendCodePage,
back.saveInSession);
// 提交按钮手机API
router.post('/back/mobile',
captcha.requiredPage,
back.validateUserPage,
back.validateInputPage,
back.sendCodePage,
back.saveInSession);
... ... @@ -108,7 +108,6 @@ router.get('/back/sendEmail',
*/
// 验证手机短信页面
router.get('/back/verification',
captcha.requiredPage,
back.validateMobileInSession,
back.verifyCodeByMobilePage);
... ...