Authored by htoooth

update captcha

@@ -48,7 +48,7 @@ const validateInputAPI = (req, res, next) => { @@ -48,7 +48,7 @@ const validateInputAPI = (req, res, next) => {
48 /** 48 /**
49 * 校验用户输入信息,是否是已经注册的用户 49 * 校验用户输入信息,是否是已经注册的用户
50 */ 50 */
51 -const validateUserPage = (req, res, next) => { 51 +const validateInputPage = (req, res, next) => {
52 let userInput = req.body.phoneNum || ''; 52 let userInput = req.body.phoneNum || '';
53 let areaCode = (req.body.area || '86').replace('+', ''); 53 let areaCode = (req.body.area || '86').replace('+', '');
54 54
@@ -78,6 +78,7 @@ const sendCodePage = (req, res, next) => { @@ -78,6 +78,7 @@ const sendCodePage = (req, res, next) => {
78 service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area) 78 service.sendCodeToUserAsync(inputInfo.type, inputInfo.phone, inputInfo.area)
79 .then(result => { 79 .then(result => {
80 if (!(result.code && result.code === 200)) { 80 if (!(result.code && result.code === 200)) {
  81 + console.log(result);
81 return res.redirect(helpers.urlFormat('/passport/back/index')); 82 return res.redirect(helpers.urlFormat('/passport/back/index'));
82 } 83 }
83 84
@@ -98,6 +99,7 @@ const saveInSession = (req, res) => { @@ -98,6 +99,7 @@ const saveInSession = (req, res) => {
98 { 99 {
99 req.session.mobile = req.inputInfo.phone; 100 req.session.mobile = req.inputInfo.phone;
100 req.session.area = req.inputInfo.area; 101 req.session.area = req.inputInfo.area;
  102 + req.session.verifyCode = req.session.captcha;
101 res.redirect(helpers.urlFormat('/passport/back/verification')); 103 res.redirect(helpers.urlFormat('/passport/back/verification'));
102 break; 104 break;
103 } 105 }
@@ -138,9 +140,9 @@ const validateEmailInSession = (req, res, next) => { @@ -138,9 +140,9 @@ const validateEmailInSession = (req, res, next) => {
138 return res.redirect(helpers.urlFormat('/passport/back/index')); 140 return res.redirect(helpers.urlFormat('/passport/back/index'));
139 } 141 }
140 142
141 - let isp = email.split('@')[1]; 143 + let isp = email.split('@')[1].toLowerCase();
142 const mapperEmailISP = { 144 const mapperEmailISP = {
143 - 'yoho.cn': 'http://smail.yoho.cn' 145 + 'yoho.cn': 'http://exmail.qq.com/login'
144 }; 146 };
145 147
146 req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`; 148 req.body.emailUrl = mapperEmailISP[isp] || `http://mail.${isp}`;
@@ -254,9 +256,9 @@ const resetPwdSuccessPage = (req, res, next) => { @@ -254,9 +256,9 @@ const resetPwdSuccessPage = (req, res, next) => {
254 }; 256 };
255 257
256 const verifyCodeByMobileAPI = (req, res) => { 258 const verifyCodeByMobileAPI = (req, res) => {
257 - let mobile = req.param('mobile', '');  
258 - let area = req.param('area', '86');  
259 - let mobileCode = req.param('code', ''); 259 + let mobile = req.body.mobile || '';
  260 + let area = req.body.area || '86';
  261 + let mobileCode = req.body.code || '';
260 const ERR = { 262 const ERR = {
261 code: 400, 263 code: 400,
262 message: '验证码错误!', 264 message: '验证码错误!',
@@ -279,7 +281,6 @@ const verifyCodeByMobileAPI = (req, res) => { @@ -279,7 +281,6 @@ const verifyCodeByMobileAPI = (req, res) => {
279 }; 281 };
280 282
281 const validateExistCodePage = (req, res, next) => { 283 const validateExistCodePage = (req, res, next) => {
282 -  
283 let code = req.query.code || req.body.code; 284 let code = req.query.code || req.body.code;
284 285
285 if (!code) { 286 if (!code) {
@@ -343,7 +344,7 @@ const updatePwdAPI = (req, res, next) => { @@ -343,7 +344,7 @@ const updatePwdAPI = (req, res, next) => {
343 344
344 const validateMobileInSession = (req, res, next) => { 345 const validateMobileInSession = (req, res, next) => {
345 req.body.mobile = req.session.mobile || ''; 346 req.body.mobile = req.session.mobile || '';
346 - req.body.verifyCode = req.session.verifyCode || ''; 347 + req.body.verifyCode = req.session.captcha || '';
347 req.body.area = req.session.area || ''; 348 req.body.area = req.session.area || '';
348 349
349 if (!(req.body.mobile && req.body.verifyCode)) { 350 if (!(req.body.mobile && req.body.verifyCode)) {
@@ -371,7 +372,7 @@ module.exports = { @@ -371,7 +372,7 @@ module.exports = {
371 resetPwdSuccessPage, // 重设密码成功页面 372 resetPwdSuccessPage, // 重设密码成功页面
372 373
373 validateInputAPI, // 验证用户输入的邮箱或者手机是否合法,返回是json 374 validateInputAPI, // 验证用户输入的邮箱或者手机是否合法,返回是json
374 - validateUserPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面 375 + validateInputPage, // 验证用户输入的邮箱或者手机是否合法,跳转是页面
375 376
376 validateEmailInSession, // 验证邮箱是否在session中 377 validateEmailInSession, // 验证邮箱是否在session中
377 validateMobileInSession, // 验证手机是否在session中 378 validateMobileInSession, // 验证手机是否在session中
@@ -4,13 +4,11 @@ @@ -4,13 +4,11 @@
4 4
5 'use strict'; 5 'use strict';
6 6
7 -const Captchapng = require('captchapng');  
8 -const _ = require('lodash');  
9 - 7 +const captchaService = require('../models/captcha-service');
10 const helpers = global.yoho.helpers; 8 const helpers = global.yoho.helpers;
11 9
12 const requiredAPI = (req, res, next) => { 10 const requiredAPI = (req, res, next) => {
13 - let captchaToken = +(req.body.verifyCode || '').toLowerCase(); 11 + let captchaToken = req.body.verifyCode || '';
14 12
15 if (captchaToken === req.session.captcha) { 13 if (captchaToken === req.session.captcha) {
16 return next(); 14 return next();
@@ -23,7 +21,7 @@ const requiredAPI = (req, res, next) => { @@ -23,7 +21,7 @@ const requiredAPI = (req, res, next) => {
23 }; 21 };
24 22
25 const requiredPage = (req, res, next) => { 23 const requiredPage = (req, res, next) => {
26 - let captchaToken = +(req.body.verifyCode || '').toLowerCase(); 24 + let captchaToken = req.body.verifyCode || '';
27 25
28 if (captchaToken === req.session.captcha) { 26 if (captchaToken === req.session.captcha) {
29 return next(); 27 return next();
@@ -32,28 +30,11 @@ const requiredPage = (req, res, next) => { @@ -32,28 +30,11 @@ const requiredPage = (req, res, next) => {
32 } 30 }
33 }; 31 };
34 32
35 -  
36 -const _generateCaptcha = (width, height, length) => {  
37 - let min = Math.pow(10, (length - 1 || 1));  
38 - let max = Math.pow(10, (length - 1 || 1)) * 9;  
39 - let token = '' + _.random(min, max);  
40 -  
41 - let png = new Captchapng(width, height, token);//  
42 -  
43 - png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)  
44 - png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)  
45 -  
46 - return {  
47 - image: new Buffer(png.getBase64(), 'base64'),  
48 - text: token  
49 - };  
50 -};  
51 -  
52 const generate = (req, res) => { 33 const generate = (req, res) => {
53 let width = req.query.w || 150; 34 let width = req.query.w || 150;
54 let height = req.query.h || 50; 35 let height = req.query.h || 50;
55 let length = +(req.query.l || 4); 36 let length = +(req.query.l || 4);
56 - let captcha = _generateCaptcha(width, height, length); 37 + let captcha = captchaService.generateCaptcha(width, height, length);
57 38
58 req.session.captcha = captcha.text; 39 req.session.captcha = captcha.text;
59 res.writeHead(200, { 40 res.writeHead(200, {
  1 +/**
  2 + * Created by TaoHuang on 2016/7/1.
  3 + */
  4 +
  5 +'use strict';
  6 +
  7 +const _ = require('lodash');
  8 +const Captchapng = require('captchapng');
  9 +
  10 +exports.generateCaptcha = (width, height, length) => {
  11 + let min = Math.pow(10, (length - 1 || 1));
  12 + let max = Math.pow(10, (length - 1 || 1)) * 9;
  13 + let token = '' + _.random(min, max);
  14 +
  15 + let png = new Captchapng(width, height, token);//
  16 +
  17 + png.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
  18 + png.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)
  19 +
  20 + return {
  21 + image: new Buffer(png.getBase64(), 'base64'),
  22 + text: token
  23 + };
  24 +};
@@ -84,14 +84,14 @@ router.post('/back/authcode', @@ -84,14 +84,14 @@ router.post('/back/authcode',
84 // 提交按钮邮件API 84 // 提交按钮邮件API
85 router.post('/back/email', 85 router.post('/back/email',
86 captcha.requiredPage, 86 captcha.requiredPage,
87 - back.validateUserPage, 87 + back.validateInputPage,
88 back.sendCodePage, 88 back.sendCodePage,
89 back.saveInSession); 89 back.saveInSession);
90 90
91 // 提交按钮手机API 91 // 提交按钮手机API
92 router.post('/back/mobile', 92 router.post('/back/mobile',
93 captcha.requiredPage, 93 captcha.requiredPage,
94 - back.validateUserPage, 94 + back.validateInputPage,
95 back.sendCodePage, 95 back.sendCodePage,
96 back.saveInSession); 96 back.saveInSession);
97 97
@@ -108,7 +108,6 @@ router.get('/back/sendEmail', @@ -108,7 +108,6 @@ router.get('/back/sendEmail',
108 */ 108 */
109 // 验证手机短信页面 109 // 验证手机短信页面
110 router.get('/back/verification', 110 router.get('/back/verification',
111 - captcha.requiredPage,  
112 back.validateMobileInSession, 111 back.validateMobileInSession,
113 back.verifyCodeByMobilePage); 112 back.verifyCodeByMobilePage);
114 113