Authored by ccbikai

增加注册验证码页面

@@ -12,9 +12,6 @@ const helpers = require(`${library}/helpers`); @@ -12,9 +12,6 @@ const helpers = require(`${library}/helpers`);
12 12
13 const service = require('../models/back-service'); 13 const service = require('../models/back-service');
14 14
15 -// 本地地址 localhost  
16 -helpers.urlFormat = helpers.fakeUrlFormat;  
17 -  
18 const SIGN_IN_URL = helpers.urlFormat('/signin.html'); 15 const SIGN_IN_URL = helpers.urlFormat('/signin.html');
19 16
20 /** 17 /**
@@ -303,4 +300,3 @@ module.exports.setNewPasswordByMobileAPI = (req, res) => { @@ -303,4 +300,3 @@ module.exports.setNewPasswordByMobileAPI = (req, res) => {
303 res.json(result); 300 res.json(result);
304 }).catch(() => res.json({code: 400, message: '修改密码失败'})); 301 }).catch(() => res.json({code: 400, message: '修改密码失败'}));
305 }; 302 };
306 -  
@@ -9,10 +9,14 @@ @@ -9,10 +9,14 @@
9 9
10 const helpers = require(global.library + '/helpers'); 10 const helpers = require(global.library + '/helpers');
11 const sign = require(global.library + '/sign'); 11 const sign = require(global.library + '/sign');
  12 +const config = require('../../../config/common');
12 const RegService = require('../models/reg-service'); 13 const RegService = require('../models/reg-service');
13 const _ = require('lodash'); 14 const _ = require('lodash');
14 15
15 const reg = { 16 const reg = {
  17 + _verifyToken: (token) => {
  18 + return token;
  19 + },
16 index: (req, res) => { 20 index: (req, res) => {
17 // 设置注册有效时间30分钟, 防机器刷 21 // 设置注册有效时间30分钟, 防机器刷
18 req.session._REG_EXPIRE = Date.now() + 1800000; 22 req.session._REG_EXPIRE = Date.now() + 1800000;
@@ -25,7 +29,6 @@ const reg = { @@ -25,7 +29,6 @@ const reg = {
25 29
26 res.render('reg/index', { 30 res.render('reg/index', {
27 title: '注册', 31 title: '注册',
28 - regIndex: true, // 模板中使用JS的标识  
29 backUrl: 'javascript:history.go(-1)', // eslint-disable-line 32 backUrl: 'javascript:history.go(-1)', // eslint-disable-line
30 headerText: '注册', // 头部信息 33 headerText: '注册', // 头部信息
31 isPassportPage: true, // 模板中模块标识 34 isPassportPage: true, // 模板中模块标识
@@ -40,7 +43,6 @@ const reg = { @@ -40,7 +43,6 @@ const reg = {
40 data: '' 43 data: ''
41 }; 44 };
42 45
43 -  
44 let mobile = +req.body.phoneNum; 46 let mobile = +req.body.phoneNum;
45 let area = +(req.body.areaCode || 86); 47 let area = +(req.body.areaCode || 86);
46 48
@@ -78,6 +80,60 @@ const reg = { @@ -78,6 +80,60 @@ const reg = {
78 return res.json(result); 80 return res.json(result);
79 }).catch(next); 81 }).catch(next);
80 }, 82 },
  83 + code: (req, res, next) => {
  84 + let token = req.query.token;
  85 + let mobile = +req.query.phoneNum;
  86 + let area = +(req.query.areaCode || 86);
  87 +
  88 + // 判断是否允许访问, 不允许则跳转到错误页面
  89 + if (!_.isString(token) || !_.isNumber(mobile) || !sign.verifyToken(mobile, token)) {
  90 + return next({
  91 + code: 403,
  92 + message: 'error token or mobile'
  93 + });
  94 + }
  95 +
  96 + res.render('reg/code', {
  97 + page: 'code',
  98 + title: '注册-验证码',
  99 + backUrl: config.siteUrl + '/?go=1', // eslint-disable-line
  100 + headerText: '注册', // 头部信息
  101 + isPassportPage: true, // 模板中模块标识
  102 + areaCode: area, // 默认的区号
  103 + phoneNum: mobile, // 手机号
  104 + token: token, // 访问令牌
  105 + serviceUrl: 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409&info=' // 在线客服
  106 + });
  107 + },
  108 + sendcode: (req, res, next) => {
  109 + let data = {
  110 + code: 400,
  111 + message: '发送验证码失败',
  112 + data: ''
  113 + };
  114 +
  115 + let mobile = +req.body.phoneNum;
  116 + let area = +(req.body.areaCode || 86);
  117 +
  118 + /* 判断参数是否合法 */
  119 + if (!_.isNumber(mobile) || !_.isNumber(area)) {
  120 + data.message = '手机号错误';
  121 + return res.json(data);
  122 + }
  123 +
  124 + /* 设置注册有效时间30分钟, 防机器刷 */
  125 + let expire = req.session._REG_EXPIRE;
  126 +
  127 + if (!expire || expire < Date.now()) {
  128 + data.message = '非法请求';
  129 + return res.json(data);
  130 + }
  131 +
  132 + /* 向手机发送注册验证码 */
  133 + RegService.sendCodeToMobile(area, mobile).then((result) => {
  134 + return result.code ? res.json(result) : res.json(data);
  135 + }).catch(next);
  136 + },
81 verifycode: (req, res, next) => { 137 verifycode: (req, res, next) => {
82 let data = { 138 let data = {
83 code: 400, 139 code: 400,
@@ -85,12 +141,13 @@ const reg = { @@ -85,12 +141,13 @@ const reg = {
85 data: '' 141 data: ''
86 }; 142 };
87 143
88 - let mobile = req.body.phoneNum;  
89 - let area = req.body.areaCode;  
90 - let code = req.body.code; 144 + let mobile = +req.body.phoneNum;
  145 + let area = +(req.body.areaCode || 86);
  146 + let code = +req.body.code;
91 147
92 /* 判断参数是否合法 */ 148 /* 判断参数是否合法 */
93 - if (!_.isNumber(mobile) || !_.isNumber(area)) { 149 + if (!_.isNumber(mobile) || !_.isNumber(area) || !_.isNumber(code)) {
  150 + data.message = '手机号错误';
94 return res.json(data); 151 return res.json(data);
95 } 152 }
96 153
@@ -98,6 +155,7 @@ const reg = { @@ -98,6 +155,7 @@ const reg = {
98 let expire = req.session._REG_EXPIRE; 155 let expire = req.session._REG_EXPIRE;
99 156
100 if (!expire || expire < Date.now()) { 157 if (!expire || expire < Date.now()) {
  158 + data.message = '非法请求';
101 return res.json(data); 159 return res.json(data);
102 } 160 }
103 161
@@ -54,6 +54,9 @@ router.post('/bind/changeMobile', bind.changeMobile); @@ -54,6 +54,9 @@ router.post('/bind/changeMobile', bind.changeMobile);
54 */ 54 */
55 router.get('/reg/index', reg.index); 55 router.get('/reg/index', reg.index);
56 router.post('/reg/verifymobile', reg.verifymobile); 56 router.post('/reg/verifymobile', reg.verifymobile);
  57 +router.get('/reg/code', reg.code);
  58 +router.post('/reg/sendcode', reg.sendcode);
  59 +router.post('/reg/verifycode', reg.verifycode);
57 60
58 /** 61 /**
59 * 邮箱 62 * 邮箱
1 -{{> layout/header}}  
2 <div class="reg-code-page passport-page yoho-page"> 1 <div class="reg-code-page passport-page yoho-page">
3 {{> passport/code}} 2 {{> passport/code}}
4 </div> 3 </div>
5 -{{> layout/footer}}  
1 -{{> layout/header}}  
2 <div class="reg-password-page passport-page yoho-page"> 1 <div class="reg-password-page passport-page yoho-page">
3 {{> passport/header}} 2 {{> passport/header}}
4 <div class="content"> 3 <div class="content">
@@ -11,13 +10,3 @@ @@ -11,13 +10,3 @@
11 <input id="area-code" type="hidden" value="{{areaCode}}"> 10 <input id="area-code" type="hidden" value="{{areaCode}}">
12 <input id="token" type="hidden" value="{{token}}"> 11 <input id="token" type="hidden" value="{{token}}">
13 </div> 12 </div>
14 -<script type='text/javascript'>  
15 -(function(a,b,c,d){  
16 -a[c]=function(){a[c]['ar']=a[c]['ar']||[];a[c]['ar'].push(arguments);};  
17 -var s=b.createElement('script');s.async = 1;s.src='//t.agrantsem.com/js/agt.js';  
18 -var r=b.getElementsByTagName('script')[0];r.parentNode.insertBefore(s,r);  
19 -})(window,document,'_agtjs','script');  
20 -_agtjs('init','AG_052186_PHCW','yohobuy.com');  
21 -_agtjs('loadEvent',{atsev:101,'atsrmv':""});  
22 -</script>  
23 -{{> layout/footer}}  
@@ -76,41 +76,6 @@ exports.urlFormat = (uri, qs, module) => { @@ -76,41 +76,6 @@ exports.urlFormat = (uri, qs, module) => {
76 }; 76 };
77 77
78 /** 78 /**
79 - * 站内地址格式化  
80 - * @param {[string]} uri 路径  
81 - * @param {[object]} qs 查询字符串  
82 - * @param {[string]} module 模块  
83 - * @return {[string]}  
84 - */  
85 -exports.fakeUrlFormat = (uri, qs, module) => {  
86 - const subDomain = 'http://localhost:6001';  
87 - const subName = {  
88 - default: subDomain,  
89 - guang: `${subDomain}`,  
90 - list: `${subDomain}`,  
91 - search: `${subDomain}`,  
92 - huodong: `${subDomain}`,  
93 - index: subDomain  
94 - };  
95 - let url;  
96 -  
97 - module = module || 'default';  
98 - if (subName[module]) {  
99 - url = subName[module];  
100 - } else {  
101 - url = `//${module}${subDomain}`; // 规则没匹配到就把模块当作子域名  
102 - }  
103 -  
104 - url += uri;  
105 - if (qs) {  
106 - url += `?${querystring.stringify(qs)}`;  
107 - }  
108 -  
109 - return url;  
110 -};  
111 -  
112 -  
113 -/**  
114 * 大写转小写处理 79 * 大写转小写处理
115 * @param {[string]} str 转换字符 80 * @param {[string]} str 转换字符
116 */ 81 */
@@ -95,6 +95,12 @@ exports.webSign = (params) => { @@ -95,6 +95,12 @@ exports.webSign = (params) => {
95 return params.key === md5(md5(webPrivateKey) + params.uid); 95 return params.key === md5(md5(webPrivateKey) + params.uid);
96 }; 96 };
97 97
  98 +// 生成 token
98 exports.makeToken = (string) => { 99 exports.makeToken = (string) => {
99 return md5(md5(string + '#@!@#')); 100 return md5(md5(string + '#@!@#'));
100 }; 101 };
  102 +
  103 +// 校验 token
  104 +exports.verifyToken = (string, token) => {
  105 + return exports.makeToken(string) === token;
  106 +};
@@ -23,7 +23,7 @@ module.exports = function(useInRegister, useForBind, useForRelate) { @@ -23,7 +23,7 @@ module.exports = function(useInRegister, useForBind, useForRelate) {
23 23
24 var urlMid = useInRegister ? 'reg' : 'back'; 24 var urlMid = useInRegister ? 'reg' : 'back';
25 25
26 - var isReg = parseInt($('#isReg').val()); 26 + var isReg = parseInt($('#isReg').val(), 10);
27 27
28 function startBind() { 28 function startBind() {
29 $.ajax({ 29 $.ajax({
  1 +require('./register/code');