Authored by htoooth

Merge branch 'feature/back' into feature/passport

@@ -77,8 +77,6 @@ const sendCodePage = (req, res, next) => { @@ -77,8 +77,6 @@ const sendCodePage = (req, res, next) => {
77 77
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 - console.log(result);  
81 -  
82 if (!(result.code && result.code === 200)) { 80 if (!(result.code && result.code === 200)) {
83 return res.redirect(helpers.urlFormat('/passport/back/index')); 81 return res.redirect(helpers.urlFormat('/passport/back/index'));
84 } 82 }
@@ -4,10 +4,13 @@ @@ -4,10 +4,13 @@
4 4
5 'use strict'; 5 'use strict';
6 6
  7 +const Captchapng = require('captchapng');
  8 +const _ = require('lodash');
  9 +
7 const helpers = global.yoho.helpers; 10 const helpers = global.yoho.helpers;
8 11
9 const requiredAPI = (req, res, next) => { 12 const requiredAPI = (req, res, next) => {
10 - let captchaToken = (req.body.verifyCode || '').toLowerCase(); 13 + let captchaToken = +(req.body.verifyCode || '').toLowerCase();
11 14
12 if (captchaToken === req.session.captcha) { 15 if (captchaToken === req.session.captcha) {
13 return next(); 16 return next();
@@ -20,16 +23,48 @@ const requiredAPI = (req, res, next) => { @@ -20,16 +23,48 @@ const requiredAPI = (req, res, next) => {
20 }; 23 };
21 24
22 const requiredPage = (req, res, next) => { 25 const requiredPage = (req, res, next) => {
23 - let captchaToken = (req.body.verifyCode || '').toLowerCase(); 26 + let captchaToken = +(req.body.verifyCode || '').toLowerCase();
24 27
25 if (captchaToken === req.session.captcha) { 28 if (captchaToken === req.session.captcha) {
26 return next(); 29 return next();
27 } else { 30 } else {
28 - return res.redirect(helpers.urlFormat('/passport/back/index.html')); 31 + return res.redirect(helpers.urlFormat('/passport/back/index'));
29 } 32 }
30 }; 33 };
31 34
  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) => {
  53 + let width = req.query.w || 150;
  54 + let height = req.query.h || 50;
  55 + let length = +(req.query.l || 4);
  56 + let captcha = _generateCaptcha(width, height, length);
  57 +
  58 + req.session.captcha = captcha.text;
  59 + res.writeHead(200, {
  60 + 'Content-Type': 'image/png'
  61 + });
  62 +
  63 + res.end(captcha.image);
  64 +};
  65 +
32 module.exports = { 66 module.exports = {
33 requiredAPI, 67 requiredAPI,
34 - requiredPage 68 + requiredPage,
  69 + generate
35 }; 70 };
@@ -10,7 +10,7 @@ const express = require('express'); @@ -10,7 +10,7 @@ const express = require('express');
10 const cRoot = './controllers'; 10 const cRoot = './controllers';
11 const login = require(cRoot + '/login'); 11 const login = require(cRoot + '/login');
12 12
13 -// const captcha = require(cRoot + '/captcha'); 13 +const captcha = require(cRoot + '/captcha');
14 const back = require(cRoot + '/back'); 14 const back = require(cRoot + '/back');
15 const reg = require(cRoot + '/reg'); 15 const reg = require(cRoot + '/reg');
16 const bind = require(cRoot + '/bind'); 16 const bind = require(cRoot + '/bind');
@@ -70,23 +70,20 @@ router.get('/back/index', back.index); @@ -70,23 +70,20 @@ router.get('/back/index', back.index);
70 70
71 // 实时验证输入是否正确 71 // 实时验证输入是否正确
72 router.post('/back/authcode', 72 router.post('/back/authcode',
73 -  
74 - // captcha.requiredAPI, 73 + captcha.requiredAPI,
75 back.validateInputAPI, 74 back.validateInputAPI,
76 back.getUserInfoAPI); 75 back.getUserInfoAPI);
77 76
78 // 提交按钮邮件API 77 // 提交按钮邮件API
79 router.post('/back/email', 78 router.post('/back/email',
80 -  
81 - // captcha.requiredPage, 79 + captcha.requiredPage,
82 back.validateUserPage, 80 back.validateUserPage,
83 back.sendCodePage, 81 back.sendCodePage,
84 back.saveInSession); 82 back.saveInSession);
85 83
86 // 提交按钮手机API 84 // 提交按钮手机API
87 router.post('/back/mobile', 85 router.post('/back/mobile',
88 -  
89 - // captcha.requiredPage, 86 + captcha.requiredPage,
90 back.validateUserPage, 87 back.validateUserPage,
91 back.sendCodePage, 88 back.sendCodePage,
92 back.saveInSession); 89 back.saveInSession);
@@ -104,29 +101,26 @@ router.get('/back/sendEmail', @@ -104,29 +101,26 @@ router.get('/back/sendEmail',
104 */ 101 */
105 // 验证手机短信页面 102 // 验证手机短信页面
106 router.get('/back/verification', 103 router.get('/back/verification',
  104 + captcha.requiredPage,
107 back.validateMobileInSession, 105 back.validateMobileInSession,
108 -  
109 - // captcha.requiredPage,  
110 back.verifyCodeByMobilePage); 106 back.verifyCodeByMobilePage);
111 107
112 // 重新发送短信接口 108 // 重新发送短信接口
113 router.post('/back/sendBackMobile', 109 router.post('/back/sendBackMobile',
114 -  
115 - // captcha.requiredAPI, 110 + captcha.requiredAPI,
116 back.validateMobileAPI, 111 back.validateMobileAPI,
117 back.sendBackMobileAPI); 112 back.sendBackMobileAPI);
118 113
119 // 验证手机验证码接口 114 // 验证手机验证码接口
120 router.post('/back/backMobile', 115 router.post('/back/backMobile',
121 -  
122 - // captcha.requiredAPI, 116 + captcha.requiredAPI,
123 back.verifyCodeByMobileAPI); 117 back.verifyCodeByMobileAPI);
124 118
125 /** 119 /**
126 * 重置密码 120 * 重置密码
127 */ 121 */
128 122
129 -// 重置密码页面 123 + // 重置密码页面
130 router.get('/back/backcode', 124 router.get('/back/backcode',
131 back.validateExistCodePage, 125 back.validateExistCodePage,
132 back.validateCodeByMobilePage, 126 back.validateCodeByMobilePage,
@@ -145,4 +139,6 @@ router.get('/back/resetSuccess', @@ -145,4 +139,6 @@ router.get('/back/resetSuccess',
145 back.validateSuccessStatusPage, 139 back.validateSuccessStatusPage,
146 back.resetPwdSuccessPage); 140 back.resetPwdSuccessPage);
147 141
  142 +router.get('/images', captcha.generate);
  143 +
148 module.exports = router; 144 module.exports = router;