Authored by 毕凯

PC 注册基本逻辑

@@ -6,10 +6,12 @@ const _ = require('lodash'); @@ -6,10 +6,12 @@ const _ = require('lodash');
6 const passportHelper = require('../models/passport-helper'); 6 const passportHelper = require('../models/passport-helper');
7 const regService = require('../models/reg-service'); 7 const regService = require('../models/reg-service');
8 const userService = require('../models/user-service'); 8 const userService = require('../models/user-service');
  9 +const authHelper = require('../models/auth-helper');
9 const config = require('../../../config/common'); 10 const config = require('../../../config/common');
10 11
11 let helpers = global.yoho.helpers; 12 let helpers = global.yoho.helpers;
12 let cache = global.yoho.cache; 13 let cache = global.yoho.cache;
  14 +let cookie = global.yoho.cookie;
13 15
14 let index = (req, res, next) => { 16 let index = (req, res, next) => {
15 // 设置注册有效时间30分钟, 防机器刷 17 // 设置注册有效时间30分钟, 防机器刷
@@ -76,6 +78,220 @@ let checkMobile = (req, res, next) => { @@ -76,6 +78,220 @@ let checkMobile = (req, res, next) => {
76 }).catch(next); 78 }).catch(next);
77 }; 79 };
78 80
  81 +let picCaptcha = (req, res, next) => {
  82 + let verifyCode = _.trim(req.body.verifyCode);
  83 + let picFlag = true; // TODO: 图形验证码校验
  84 +
  85 + if (picFlag) {
  86 + return res.json({
  87 + code: 200,
  88 + message: '验证码正确'
  89 + });
  90 + }
  91 +
  92 + return res.json({
  93 + code: 400,
  94 + message: '验证码错误'
  95 + });
  96 +};
  97 +
  98 +let sendBindMsg = (req, res, next) => {
  99 + let data = {
  100 + code: 400,
  101 + message: '',
  102 + data: ''
  103 + };
  104 +
  105 + let mobile = +req.body.mobile;
  106 + let area = +req.body.area;
  107 + let verifyCode = +req.body.verifyCode;
  108 +
  109 + // 判断参数是否合法
  110 + if (!_.isNumber(mobile) || !_.isNumber(area)) {
  111 + data.message = '手机号码格式不正确';
  112 + return res.json(data);
  113 + }
  114 +
  115 + // 检查是否检查过
  116 + // let makeMobile = passportHelper.makeAreaMobile(area, mobile);
  117 + //
  118 + // if (req.session[`checkmobile_${makeMobile}`] !== makeMobile) {
  119 + // data.message = '发送失败';
  120 + // return res.json(data);
  121 + // }
  122 +
  123 + // 校验是否发送过多
  124 + let sendCodeKey = `send_code_${area}_${mobile}`;
  125 +
  126 + cache.get(sendCodeKey).then((sendCodeTimes) => {
  127 + if (!sendCodeTimes) {
  128 + sendCodeTimes = 0;
  129 + } else {
  130 + sendCodeTimes = +sendCodeTimes;
  131 + }
  132 +
  133 + if (sendCodeTimes >= 10) {
  134 + data.message = '您已多次提交验证码,请尽快联系客服解决';
  135 + return res.json(data);
  136 + }
  137 +
  138 + if (sendCodeTimes >= 5) {
  139 + data.message = '您收到的验证码短信已超过本日限定最多次数,请您耐心等待';
  140 + return res.json(data);
  141 + }
  142 +
  143 + // TODO: 检测验证码不正确
  144 + // if (!PassportModel::verifyCode($verifyCode)) {
  145 + // $data['code'] = 400;
  146 + // $data['message'] = '图形验证码不正确';
  147 + // break;
  148 + // }
  149 +
  150 + /* 向手机发送注册验证码 */
  151 + return regService.sendCodeToMobile(area, mobile).then((result) => {
  152 + return cache.set(sendCodeKey, sendCodeTimes + 1, 3600).then(() => {
  153 + if (result.code) {
  154 + return res.json(result);
  155 + } else {
  156 + data.message = '发送失败';
  157 + return res.json(data);
  158 + }
  159 + });
  160 + });
  161 + }).catch(next);
  162 +};
  163 +
  164 +let msgCaptcha = (req, res, next) => {
  165 + let data = {
  166 + code: 400,
  167 + message: '',
  168 + data: ''
  169 + };
  170 +
  171 + let area = +req.body.area;
  172 + let mobile = +req.body.mobile;
  173 + let code = +req.body.code; // 短信验证码
  174 +
  175 + // 判断参数是否合法
  176 + if (!_.isNumber(mobile) || !_.isNumber(area)) {
  177 + data.message = '手机号码格式不正确';
  178 + return res.json(data);
  179 + }
  180 +
  181 + regService.validMobileCode(area, mobile, code).then((result) => {
  182 + if (result.code) {
  183 + return res.json(result);
  184 + } else {
  185 + data.message = '验证码错误';
  186 + return res.json(data);
  187 + }
  188 + }).catch(next);
  189 +};
  190 +
  191 +let mobileRegister = (req, res, next) => {
  192 + let data = {
  193 + code: 400,
  194 + message: '',
  195 + data: ''
  196 + };
  197 +
  198 + let area = +req.body.area;
  199 + let mobile = +req.body.mobile;
  200 +
  201 + // 判断参数是否合法
  202 + if (!_.isNumber(mobile) || !_.isNumber(area)) {
  203 + data.message = '手机号码格式不正确';
  204 + return res.json(data);
  205 + }
  206 +
  207 + /* 判断是否是有效的注册方式,防注册机刷 */
  208 + let regExpireTime = req.session._REG_EXPIRE;
  209 +
  210 + if (!regExpireTime || regExpireTime < Date.now()) {
  211 + data.message = '注册超时';
  212 + return res.json(data);
  213 + }
  214 +
  215 + // TODO: 检测验证码不正确
  216 + // $verifyCode = strtolower(trim($this->post('verifyCode'))); //图形验证码
  217 + // if (!PassportModel::verifyCode($verifyCode)) {
  218 + // $data['message'] = '验证码不正确';
  219 + // break;
  220 + // }
  221 +
  222 + /* 判断密码是否符合规则 */
  223 + let code = +req.body.code; // 短信验证码
  224 + let password = req.body.password;
  225 +
  226 + if (!helpers.verifyPassword(password)) {
  227 + data.message = '密码不正确';
  228 + return res.json(data);
  229 + }
  230 +
  231 + /* IP仅允许点击注册500次/时 */
  232 + let ip = req.ip;
  233 + let ipKey = 'ip_register_' + ip;
  234 +
  235 + cache.get(ipKey).then((ipTimes) => {
  236 + if (!ipTimes) {
  237 + ipTimes = 0;
  238 + } else {
  239 + ipTimes = +ipTimes;
  240 + }
  241 +
  242 + if (ipTimes >= 500) {
  243 + data.message = '由于你IP受限无法注册';
  244 + return res.json(data);
  245 + }
  246 +
  247 + return cache.set(ipKey, ipTimes + 1, 3600).then(() => {
  248 + /* 验证注册的标识码是否有效 */
  249 + return regService.validMobileCode(area, mobile, code).then((result) => {
  250 + if (!result.code || result.code !== 200) {
  251 + data.message = '验证码错误';
  252 + return res.json(data);
  253 + }
  254 +
  255 + let shoppingKey = cookie.getShoppingKey(req);
  256 +
  257 + /* 手机注册: 调用注册接口,ip限制计数 */
  258 + return regService.regMobile(area, mobile, password, shoppingKey).then((regResult) => {
  259 + if (!regResult.code || regResult.code !== 200) {
  260 + data.message = '注册失败';
  261 + return res.json(data);
  262 + }
  263 +
  264 + // 返回跳转到来源页面
  265 + let refer = req.cookies.refer;
  266 +
  267 + if (refer) {
  268 + refer = decodeURI(req.cookies.refer);
  269 + } else {
  270 + refer = '/?go=1';
  271 + }
  272 +
  273 + if (/sign|login/.test(refer)) {
  274 + refer = '/?go=1';
  275 + }
  276 +
  277 + return authHelper.syncUserSession(regResult.data.uid).then(() => {
  278 + return res.json({
  279 + code: 200,
  280 + message: '注册成功',
  281 + data: {
  282 + href: helpers.urlFormat('/passport/register/success', {
  283 + next: refer,
  284 + goShoppingUrl: config.siteUrl
  285 + })
  286 + }
  287 + });
  288 + });
  289 + });
  290 + });
  291 + });
  292 + }).catch(next);
  293 +};
  294 +
79 let success = (req, res, next) => { 295 let success = (req, res, next) => {
80 let goUrl = req.query.next || config.siteUrl; 296 let goUrl = req.query.next || config.siteUrl;
81 let goShoppingUrl = req.query.goShoppingUrl || config.siteUrl; 297 let goShoppingUrl = req.query.goShoppingUrl || config.siteUrl;
@@ -96,5 +312,9 @@ let success = (req, res, next) => { @@ -96,5 +312,9 @@ let success = (req, res, next) => {
96 module.exports = { 312 module.exports = {
97 index, 313 index,
98 success, 314 success,
99 - checkMobile 315 + checkMobile,
  316 + picCaptcha,
  317 + sendBindMsg,
  318 + msgCaptcha,
  319 + mobileRegister
100 }; 320 };
1 'use strict'; 1 'use strict';
2 2
3 const sign = global.yoho.sign; 3 const sign = global.yoho.sign;
4 -const api = global.yoho.API; 4 +const api = global.yoho.API;
5 5
6 class Auth { 6 class Auth {
7 7
8 static signinByOpenID(nickname, openId, sourceType, shoppingKey) { 8 static signinByOpenID(nickname, openId, sourceType, shoppingKey) {
9 let param = { 9 let param = {
10 - nickname : nickname,  
11 - openId : openId, 10 + nickname: nickname,
  11 + openId: openId,
12 source_type: sourceType, 12 source_type: sourceType,
13 - method : 'app.passport.signinByOpenID' 13 + method: 'app.passport.signinByOpenID'
14 }; 14 };
15 15
16 if (shoppingKey) { 16 if (shoppingKey) {
@@ -22,11 +22,11 @@ class Auth { @@ -22,11 +22,11 @@ class Auth {
22 22
23 static signinByWechat(nickname, openId, unionId, sourceType, shoppingKey) { 23 static signinByWechat(nickname, openId, unionId, sourceType, shoppingKey) {
24 let param = { 24 let param = {
25 - nickname : nickname,  
26 - openId : openId,  
27 - unionId : unionId, 25 + nickname: nickname,
  26 + openId: openId,
  27 + unionId: unionId,
28 source_type: sourceType, 28 source_type: sourceType,
29 - method : 'app.passport.signinByWechat' 29 + method: 'app.passport.signinByWechat'
30 }; 30 };
31 31
32 if (shoppingKey) { 32 if (shoppingKey) {
@@ -38,7 +38,7 @@ class Auth { @@ -38,7 +38,7 @@ class Auth {
38 38
39 static profile(uid) { 39 static profile(uid) {
40 let param = { 40 let param = {
41 - uid : uid, 41 + uid: uid,
42 method: 'app.passport.profile' 42 method: 'app.passport.profile'
43 }; 43 };
44 44
@@ -48,24 +48,24 @@ class Auth { @@ -48,24 +48,24 @@ class Auth {
48 static syncUserSession(uid, req, res) { 48 static syncUserSession(uid, req, res) {
49 return Auth.profile(uid).then((userInfo) => { 49 return Auth.profile(uid).then((userInfo) => {
50 let token = sign.makeToken(uid); 50 let token = sign.makeToken(uid);
51 - let data = userInfo.data; 51 + let data = userInfo.data;
52 52
53 if (data) { 53 if (data) {
54 let uidCookie = `${data.profile_name}::${data.uid}::${data.vip_info.title}::${token}`; 54 let uidCookie = `${data.profile_name}::${data.uid}::${data.vip_info.title}::${token}`;
55 55
56 - req.session._TOKEN = token; 56 + req.session._TOKEN = token;
57 req.session._LOGIN_UID = uid; 57 req.session._LOGIN_UID = uid;
58 58
59 res.cookie('_UID', uidCookie, { 59 res.cookie('_UID', uidCookie, {
60 domain: 'yohobuy.com' 60 domain: 'yohobuy.com'
61 }); 61 });
62 } 62 }
63 - req.session._TOKEN = token; // esline-disable-line 63 + req.session._TOKEN = token; // esline-disable-line
64 req.session._LOGIN_UID = uid; // esline-disable-line 64 req.session._LOGIN_UID = uid; // esline-disable-line
65 res.cookie('_TOKEN', token, { 65 res.cookie('_TOKEN', token, {
66 domain: 'yohobuy.com' 66 domain: 'yohobuy.com'
67 }); // esline-disable-line 67 }); // esline-disable-line
68 - }).catch(console.log); 68 + });
69 } 69 }
70 } 70 }
71 71
@@ -5,11 +5,51 @@ @@ -5,11 +5,51 @@
5 const passportHelper = require('./passport-helper'); 5 const passportHelper = require('./passport-helper');
6 6
7 const REGISTER_LEFT_BANNER_CODE = 'c479ec90120cae7f96e52922b4917064'; // 注册左边的banner 7 const REGISTER_LEFT_BANNER_CODE = 'c479ec90120cae7f96e52922b4917064'; // 注册左边的banner
  8 +const api = global.yoho.API;
8 9
9 let getRegData = () => { 10 let getRegData = () => {
10 return passportHelper.getLeftBannerAsync(REGISTER_LEFT_BANNER_CODE); 11 return passportHelper.getLeftBannerAsync(REGISTER_LEFT_BANNER_CODE);
11 }; 12 };
12 13
  14 +let sendCodeToMobile = (area, mobile) => {
  15 + let params = {
  16 + method: 'app.register.sendRegCodeToMobile',
  17 + area: area,
  18 + mobile: mobile
  19 + };
  20 +
  21 + return api.post('', params);
  22 +};
  23 +
  24 +let validMobileCode = (area, mobile, code) => {
  25 + let params = {
  26 + method: 'app.register.validRegCode',
  27 + area: area,
  28 + mobile: mobile,
  29 + code: code
  30 + };
  31 +
  32 + return api.post('', params);
  33 +};
  34 +
  35 +let regMobile = (area, mobile, password, shoppingKey)=> {
  36 + let params = {
  37 + method: 'app.register.register',
  38 + area: area,
  39 + profile: mobile,
  40 + password: password
  41 + };
  42 +
  43 + if (shoppingKey) {
  44 + params.shopping_key = shoppingKey;
  45 + }
  46 +
  47 + return api.post('', params);
  48 +};
  49 +
13 module.exports = { 50 module.exports = {
14 - getRegData 51 + getRegData,
  52 + sendCodeToMobile,
  53 + validMobileCode,
  54 + regMobile
15 }; 55 };
@@ -24,6 +24,10 @@ router.get('/login/wechat/callback', login.wechat.callback); @@ -24,6 +24,10 @@ router.get('/login/wechat/callback', login.wechat.callback);
24 */ 24 */
25 router.get('/reg/index', reg.index); 25 router.get('/reg/index', reg.index);
26 router.post('/reg/checkmobile', reg.checkMobile); 26 router.post('/reg/checkmobile', reg.checkMobile);
  27 +router.post('/reg/piccaptcha', reg.picCaptcha);
  28 +router.post('/reg/msgcaptcha', reg.msgCaptcha);
  29 +router.post('/reg/sendBindMsg', reg.sendBindMsg);
  30 +router.post('/reg/mobileregister', reg.mobileRegister);
27 router.get('/reg/success', reg.success); 31 router.get('/reg/success', reg.success);
28 32
29 /** 33 /**
@@ -14,8 +14,8 @@ module.exports = { @@ -14,8 +14,8 @@ module.exports = {
14 port: 6002, 14 port: 6002,
15 siteUrl: 'http://www.yohobuy.com', 15 siteUrl: 'http://www.yohobuy.com',
16 domains: { 16 domains: {
17 - api: 'http://devapi.yoho.cn:58078/', // devapi.yoho.cn:58078 testapi.yoho.cn:28078 devapi.yoho.cn:58078  
18 - service: 'http://devservice.yoho.cn:58077/', // testservice.yoho.cn:28077 devservice.yoho.cn:58077 17 + api: 'http://devapi.yoho.cn:58078/',
  18 + service: 'http://devservice.yoho.cn:58077/',
19 search: 'http://192.168.102.216:8080/yohosearch/' 19 search: 'http://192.168.102.216:8080/yohosearch/'
20 }, 20 },
21 useOneapm: false, 21 useOneapm: false,
@@ -178,7 +178,7 @@ function picCaptchaAjaxFn(page, callback) { @@ -178,7 +178,7 @@ function picCaptchaAjaxFn(page, callback) {
178 var url; 178 var url;
179 179
180 if (page === 'reg') { 180 if (page === 'reg') {
181 - url = '/passport/register/piccaptcha'; 181 + url = '/passport/reg/piccaptcha';
182 } else if (page === 'third') { 182 } else if (page === 'third') {
183 url = '/passport/autouserinfo/checkPicCode'; 183 url = '/passport/autouserinfo/checkPicCode';
184 } 184 }
@@ -218,7 +218,7 @@ function msgCaptchaAjaxFn(page, callback) { @@ -218,7 +218,7 @@ function msgCaptchaAjaxFn(page, callback) {
218 var url; 218 var url;
219 219
220 if (page === 'reg') { 220 if (page === 'reg') {
221 - url = '/passport/register/msgcaptcha'; 221 + url = '/passport/reg/msgcaptcha';
222 } else if (page === 'third') { 222 } else if (page === 'third') {
223 url = '/passport/autouserinfo/checkBindMsg'; 223 url = '/passport/autouserinfo/checkBindMsg';
224 } 224 }
@@ -617,7 +617,7 @@ exports.init = function(page) { @@ -617,7 +617,7 @@ exports.init = function(page) {
617 }, 1000); 617 }, 1000);
618 618
619 if (page === 'reg') { 619 if (page === 'reg') {
620 - url = '/passport/register/sendBindMsg'; 620 + url = '/passport/reg/sendBindMsg';
621 } else if (page === 'third') { 621 } else if (page === 'third') {
622 url = '/passport/autouserinfo/sendBindMsg'; 622 url = '/passport/autouserinfo/sendBindMsg';
623 } 623 }
@@ -646,7 +646,7 @@ exports.init = function(page) { @@ -646,7 +646,7 @@ exports.init = function(page) {
646 var url; 646 var url;
647 647
648 if (page === 'reg') { 648 if (page === 'reg') {
649 - url = '/passport/register/mobileregister'; 649 + url = '/passport/reg/mobileregister';
650 } else if (page === 'third') { 650 } else if (page === 'third') {
651 url = '/passport/autouserinfo/bindMobile'; 651 url = '/passport/autouserinfo/bindMobile';
652 } 652 }