Authored by 毕凯

注册代码整理

@@ -14,6 +14,37 @@ let cache = global.yoho.cache; @@ -14,6 +14,37 @@ let cache = global.yoho.cache;
14 let cookie = global.yoho.cookie; 14 let cookie = global.yoho.cookie;
15 15
16 /** 16 /**
  17 + * 检查图形验证码
  18 + */
  19 +let checkCode = (req, res, next) => {
  20 + let verifyCode = _.trim(req.body.verifyCode);
  21 +
  22 + if (verifyCode !== req.session.captcha) {
  23 + return res.json({
  24 + code: 400,
  25 + message: '验证码错误'
  26 + });
  27 + }
  28 + next();
  29 +};
  30 +
  31 +/**
  32 + * 检查手机格式
  33 + */
  34 +let checkMobileMiddleware = (req, res, next) => {
  35 + let mobile = +req.body.mobile;
  36 + let area = +req.body.area;
  37 +
  38 + if (!_.isNumber(mobile) || !_.isNumber(area)) {
  39 + return res.json({
  40 + code: 400,
  41 + message: '手机号码格式不正确'
  42 + });
  43 + }
  44 + next();
  45 +};
  46 +
  47 +/**
17 * 注册首页 48 * 注册首页
18 */ 49 */
19 let index = (req, res, next) => { 50 let index = (req, res, next) => {
@@ -54,33 +85,31 @@ let checkMobile = (req, res, next) => { @@ -54,33 +85,31 @@ let checkMobile = (req, res, next) => {
54 let mobile = +req.body.mobile; 85 let mobile = +req.body.mobile;
55 let area = +req.body.area; 86 let area = +req.body.area;
56 87
57 - if (!_.isNumber(mobile) || !_.isNumber(area)) {  
58 - data.message = '手机号码格式不正确';  
59 - return res.json(data);  
60 - }  
61 -  
62 // 判断手机号是否检查超过指定次数 88 // 判断手机号是否检查超过指定次数
63 let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile); 89 let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile);
64 - let checkNum = req.session[regCheckKey];  
65 -  
66 - checkNum = +(checkNum || 0);  
67 - req.session[regCheckKey] = ++checkNum;  
68 -  
69 - if (checkNum > 500) {  
70 - data.message = '检查次数太多';  
71 - return res.json(data);  
72 - }  
73 90
  91 + cache.get(regCheckKey).then((checkNum) => {
  92 + checkNum = +(checkNum || 0);
74 93
75 - // 判断用户是否存在  
76 - return userService.findByMobileAsync(area, mobile).then((user) => {  
77 - if (!_.isEmpty(user)) {  
78 - data.message = '手机号码已经存在'; 94 + return cache.set(regCheckKey, ++checkNum).then(() => {
  95 + return checkNum;
  96 + });
  97 + }).then((checkNum) => {
  98 + if (checkNum > 500) {
  99 + data.message = '检查次数太多';
79 return res.json(data); 100 return res.json(data);
80 } 101 }
81 102
82 - data.code = 200;  
83 - return res.json(data); 103 + // 判断用户是否存在
  104 + return userService.findByMobileAsync(area, mobile).then((user) => {
  105 + if (!_.isEmpty(user)) {
  106 + data.message = '手机号码已经存在';
  107 + return res.json(data);
  108 + }
  109 +
  110 + data.code = 200;
  111 + return res.json(data);
  112 + });
84 }).catch(next); 113 }).catch(next);
85 }; 114 };
86 115
@@ -88,15 +117,7 @@ let checkMobile = (req, res, next) => { @@ -88,15 +117,7 @@ let checkMobile = (req, res, next) => {
88 * 图形验证码校验 117 * 图形验证码校验
89 */ 118 */
90 let picCaptcha = (req, res) => { 119 let picCaptcha = (req, res) => {
91 - let verifyCode = _.trim(req.body.verifyCode);  
92 -  
93 - if (verifyCode !== req.session.captcha) {  
94 - return res.json({  
95 - code: 400,  
96 - message: '验证码错误'  
97 - });  
98 - }  
99 - 120 + // 图形验证码已经在中间件校验,如果走到这儿,说明通过校验
100 return res.json({ 121 return res.json({
101 code: 200, 122 code: 200,
102 message: '验证码正确' 123 message: '验证码正确'
@@ -116,57 +137,45 @@ let sendBindMsg = (req, res, next) => { @@ -116,57 +137,45 @@ let sendBindMsg = (req, res, next) => {
116 let mobile = +req.body.mobile; 137 let mobile = +req.body.mobile;
117 let area = +req.body.area; 138 let area = +req.body.area;
118 139
119 - // 判断参数是否合法  
120 - if (!_.isNumber(mobile) || !_.isNumber(area)) {  
121 - data.message = '手机号码格式不正确';  
122 - return res.json(data);  
123 - }  
124 -  
125 - let verifyCode = _.trim(req.body.verifyCode);  
126 -  
127 - if (verifyCode !== req.session.captcha) {  
128 - data.message = '验证码不正确';  
129 - return res.json(data);  
130 - }  
131 -  
132 // 检查是否检查过 140 // 检查是否检查过
133 let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile); 141 let regCheckKey = 'regCheckMobileNum_' + passportHelper.makeAreaMobile(area, mobile);
134 - let checkNum = req.session[regCheckKey];  
135 -  
136 - if (!checkNum) {  
137 - data.message = '发送失败';  
138 - return res.json(data);  
139 - }  
140 142
141 // 校验是否发送过多 143 // 校验是否发送过多
142 let sendCodeKey = `send_code_${area}_${mobile}`; 144 let sendCodeKey = `send_code_${area}_${mobile}`;
143 145
144 - cache.get(sendCodeKey).then((sendCodeTimes) => {  
145 - if (!sendCodeTimes) {  
146 - sendCodeTimes = 0;  
147 - } else {  
148 - sendCodeTimes = +sendCodeTimes;  
149 - }  
150 -  
151 - if (sendCodeTimes >= 10) {  
152 - data.message = '您已多次提交验证码,请尽快联系客服解决'; 146 + cache.get(regCheckKey).then((checkNum) => {
  147 + if (!checkNum) {
  148 + data.message = '发送失败';
153 return res.json(data); 149 return res.json(data);
154 } 150 }
155 151
156 - if (sendCodeTimes >= 5) {  
157 - data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';  
158 - return res.json(data);  
159 - } 152 + return cache.get(sendCodeKey).then((sendCodeTimes) => {
  153 + if (!sendCodeTimes) {
  154 + sendCodeTimes = 0;
  155 + } else {
  156 + sendCodeTimes = +sendCodeTimes;
  157 + }
160 158
161 - /* 向手机发送注册验证码 */  
162 - return regService.sendCodeToMobile(area, mobile).then((result) => {  
163 - return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {  
164 - if (result.code) {  
165 - return res.json(result);  
166 - } else {  
167 - data.message = '发送失败';  
168 - return res.json(data);  
169 - } 159 + if (sendCodeTimes >= 10) {
  160 + data.message = '您已多次提交验证码,请尽快联系客服解决';
  161 + return res.json(data);
  162 + }
  163 +
  164 + if (sendCodeTimes >= 5) {
  165 + data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
  166 + return res.json(data);
  167 + }
  168 +
  169 + /* 向手机发送注册验证码 */
  170 + return regService.sendCodeToMobile(area, mobile).then((result) => {
  171 + return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
  172 + if (result.code) {
  173 + return res.json(result);
  174 + } else {
  175 + data.message = '发送失败';
  176 + return res.json(data);
  177 + }
  178 + });
170 }); 179 });
171 }); 180 });
172 }).catch(next); 181 }).catch(next);
@@ -181,17 +190,10 @@ let msgCaptcha = (req, res, next) => { @@ -181,17 +190,10 @@ let msgCaptcha = (req, res, next) => {
181 message: '', 190 message: '',
182 data: '' 191 data: ''
183 }; 192 };
184 -  
185 let area = +req.body.area; 193 let area = +req.body.area;
186 let mobile = +req.body.mobile; 194 let mobile = +req.body.mobile;
187 let code = +req.body.code; // 短信验证码 195 let code = +req.body.code; // 短信验证码
188 196
189 - // 判断参数是否合法  
190 - if (!_.isNumber(mobile) || !_.isNumber(area)) {  
191 - data.message = '手机号码格式不正确';  
192 - return res.json(data);  
193 - }  
194 -  
195 regService.validMobileCode(area, mobile, code).then((result) => { 197 regService.validMobileCode(area, mobile, code).then((result) => {
196 if (result.code) { 198 if (result.code) {
197 return res.json(result); 199 return res.json(result);
@@ -211,16 +213,9 @@ let mobileRegister = (req, res, next) => { @@ -211,16 +213,9 @@ let mobileRegister = (req, res, next) => {
211 message: '', 213 message: '',
212 data: '' 214 data: ''
213 }; 215 };
214 -  
215 let area = +req.body.area; 216 let area = +req.body.area;
216 let mobile = +req.body.mobile; 217 let mobile = +req.body.mobile;
217 218
218 - // 判断参数是否合法  
219 - if (!_.isNumber(mobile) || !_.isNumber(area)) {  
220 - data.message = '手机号码格式不正确';  
221 - return res.json(data);  
222 - }  
223 -  
224 /* 判断是否是有效的注册方式,防注册机刷 */ 219 /* 判断是否是有效的注册方式,防注册机刷 */
225 let regExpireTime = req.session._REG_EXPIRE; 220 let regExpireTime = req.session._REG_EXPIRE;
226 221
@@ -229,13 +224,6 @@ let mobileRegister = (req, res, next) => { @@ -229,13 +224,6 @@ let mobileRegister = (req, res, next) => {
229 return res.json(data); 224 return res.json(data);
230 } 225 }
231 226
232 - let verifyCode = _.trim(req.body.verifyCode);  
233 -  
234 - if (verifyCode !== req.session.captcha) {  
235 - data.message = '验证码不正确';  
236 - return res.json(data);  
237 - }  
238 -  
239 /* 判断密码是否符合规则 */ 227 /* 判断密码是否符合规则 */
240 let code = +req.body.code; // 短信验证码 228 let code = +req.body.code; // 短信验证码
241 let password = req.body.password; 229 let password = req.body.password;
@@ -271,33 +259,20 @@ let mobileRegister = (req, res, next) => { @@ -271,33 +259,20 @@ let mobileRegister = (req, res, next) => {
271 259
272 let shoppingKey = cookie.getShoppingKey(req); 260 let shoppingKey = cookie.getShoppingKey(req);
273 261
274 - /* 手机注册: 调用注册接口,ip限制计数 */ 262 + /* 手机注册: 调用注册接口*/
275 return regService.regMobile(area, mobile, password, shoppingKey).then((regResult) => { 263 return regService.regMobile(area, mobile, password, shoppingKey).then((regResult) => {
276 if (!regResult.code || regResult.code !== 200) { 264 if (!regResult.code || regResult.code !== 200) {
277 data.message = '注册失败'; 265 data.message = '注册失败';
278 return res.json(data); 266 return res.json(data);
279 } 267 }
280 268
281 - // 返回跳转到来源页面  
282 - let refer = req.cookies.refer;  
283 -  
284 - if (refer) {  
285 - refer = decodeURI(req.cookies.refer);  
286 - } else {  
287 - refer = '/?go=1';  
288 - }  
289 -  
290 - if (/signin|login/.test(refer)) {  
291 - refer = '/?go=1';  
292 - }  
293 -  
294 return authHelper.syncUserSession(regResult.data.uid, req, res).then(() => { 269 return authHelper.syncUserSession(regResult.data.uid, req, res).then(() => {
295 return res.json({ 270 return res.json({
296 code: 200, 271 code: 200,
297 message: '注册成功', 272 message: '注册成功',
298 data: { 273 data: {
299 href: helpers.urlFormat('/passport/reg/success', { 274 href: helpers.urlFormat('/passport/reg/success', {
300 - next: refer, 275 + next: cookie.getRefer(req, '/?go=1'),
301 goShoppingUrl: config.siteUrl 276 goShoppingUrl: config.siteUrl
302 }) 277 })
303 } 278 }
@@ -327,6 +302,8 @@ let success = (req, res, next) => { @@ -327,6 +302,8 @@ let success = (req, res, next) => {
327 }; 302 };
328 303
329 module.exports = { 304 module.exports = {
  305 + checkCode,
  306 + checkMobileMiddleware,
330 index, 307 index,
331 success, 308 success,
332 checkMobile, 309 checkMobile,
@@ -62,11 +62,11 @@ router.post('/autouserinfo/bindMobile', bind.bindMobile); @@ -62,11 +62,11 @@ router.post('/autouserinfo/bindMobile', bind.bindMobile);
62 * 注册页面路由 62 * 注册页面路由
63 */ 63 */
64 router.get('/reg/index', reg.index); 64 router.get('/reg/index', reg.index);
65 -router.post('/reg/checkmobile', reg.checkMobile);  
66 -router.post('/reg/piccaptcha', reg.picCaptcha);  
67 -router.post('/reg/msgcaptcha', reg.msgCaptcha);  
68 -router.post('/reg/sendBindMsg', reg.sendBindMsg);  
69 -router.post('/reg/mobileregister', reg.mobileRegister); 65 +router.post('/reg/checkmobile', reg.checkMobileMiddleware, reg.checkMobile);
  66 +router.post('/reg/piccaptcha', reg.checkCode, reg.picCaptcha);
  67 +router.post('/reg/msgcaptcha', reg.checkMobileMiddleware, reg.msgCaptcha);
  68 +router.post('/reg/sendBindMsg', reg.checkMobileMiddleware, reg.checkCode, reg.sendBindMsg);
  69 +router.post('/reg/mobileregister', reg.checkMobileMiddleware, reg.checkCode, reg.mobileRegister);
70 router.get('/reg/success', reg.success); 70 router.get('/reg/success', reg.success);
71 71
72 /** 72 /**