Authored by htoooth

增加代码

@@ -168,10 +168,7 @@ const local = { @@ -168,10 +168,7 @@ const local = {
168 } 168 }
169 }); 169 });
170 } else { 170 } else {
171 - let isRemember = req.body.isRemember;  
172 - let refer = req.cookies.refer;  
173 -  
174 - if (isRemember) { 171 + if (_.get(req, 'body.isRemember', false)) {
175 AuthHelper.rememberAccount({ 172 AuthHelper.rememberAccount({
176 area: req.body.areaCode || '86', 173 area: req.body.areaCode || '86',
177 account: req.body.account, 174 account: req.body.account,
@@ -179,21 +176,23 @@ const local = { @@ -179,21 +176,23 @@ const local = {
179 }, req, res); 176 }, req, res);
180 } 177 }
181 178
182 - if (refer) {  
183 - refer = decodeURI(req.cookies.refer);  
184 - } else {  
185 - refer = `${config.siteUrl}/home`;  
186 - } 179 + let refer = (function() {
  180 + if (_.has(req, 'cookies.refer')) {
  181 + return decodeURI(req.cookies.refer);
  182 + } else if (/sign|login|passport/.test(_.get(req, 'cookies.refer', ''))) {
  183 + return `${config.siteUrl}/home`;
  184 + } else {
  185 + return `${config.siteUrl}/home`;
  186 + }
  187 + }());
187 188
188 - if (/sign|login/.test(refer)) {  
189 - refer = `${config.siteUrl}/home`;  
190 - }  
191 - user.session = refer;  
192 - user.href = refer;  
193 AuthHelper.syncUserSession(user.uid, req, res).then(() => { 189 AuthHelper.syncUserSession(user.uid, req, res).then(() => {
194 res.json({ 190 res.json({
195 code: 200, 191 code: 200,
196 - data: user 192 + data: {
  193 + session: refer,
  194 + href: refer
  195 + }
197 }); 196 });
198 }); 197 });
199 } 198 }
@@ -228,7 +227,9 @@ const local = { @@ -228,7 +227,9 @@ const local = {
228 }); 227 });
229 } 228 }
230 229
231 - AuthHelper.sendSmsBySMS(area, mobile).then(res.json).catch(next); 230 + AuthHelper.sendPasswordBySMS(area, mobile).then((result) => {
  231 + return res.json(result);
  232 + }).catch(next);
232 }, 233 },
233 auth: (req, res, next) => { 234 auth: (req, res, next) => {
234 let area = req.body.area || '86'; 235 let area = req.body.area || '86';
@@ -242,7 +243,8 @@ const local = { @@ -242,7 +243,8 @@ const local = {
242 }); 243 });
243 } 244 }
244 245
245 - AuthHelper.verifySmsCodeBySMS(area, mobile, code).then((result) => { 246 + AuthHelper.verifyPasswordBySMS(area, mobile, code).then((result) => {
  247 + console.log(result);
246 if (_.get(result, 'code', 200) !== 200) { 248 if (_.get(result, 'code', 200) !== 200) {
247 return Promise.reject('error'); 249 return Promise.reject('error');
248 } 250 }
@@ -44,7 +44,7 @@ const Auth = { @@ -44,7 +44,7 @@ const Auth = {
44 44
45 return api.post('', param); 45 return api.post('', param);
46 }, 46 },
47 - sendSmsBySMS(area, mobile) { 47 + sendPasswordBySMS(area, mobile) {
48 let param = { 48 let param = {
49 method: 'app.message.sendSms', 49 method: 'app.message.sendSms',
50 area: area, 50 area: area,
@@ -61,7 +61,7 @@ const Auth = { @@ -61,7 +61,7 @@ const Auth = {
61 mobile: mobile 61 mobile: mobile
62 }); 62 });
63 }, 63 },
64 - verifySmsCodeBySMS(area, mobile, code) { 64 + verifyPasswordBySMS(area, mobile, code) {
65 return api.get('', { 65 return api.get('', {
66 method: 'app.message.verifySmsCode', 66 method: 'app.message.verifySmsCode',
67 area: area, 67 area: area,
@@ -60,7 +60,7 @@ var $accountInput = $('#account'), @@ -60,7 +60,7 @@ var $accountInput = $('#account'),
60 return $.trim($captchaSmsInput.val()); 60 return $.trim($captchaSmsInput.val());
61 }, 61 },
62 $captchaSmsBtn = $('.change-captcha-sms'), 62 $captchaSmsBtn = $('.change-captcha-sms'),
63 - $captchaSMSToken = $('#captcha-sms-token-hide'), 63 + $captchaSmsTokenHideInput = $('#captcha-sms-token-hide'),
64 64
65 // 区域选择 65 // 区域选择
66 $countryCodeInput = $('#country-code-hide'), 66 $countryCodeInput = $('#country-code-hide'),
@@ -126,12 +126,14 @@ var LoginType = { @@ -126,12 +126,14 @@ var LoginType = {
126 return defer.promise(); 126 return defer.promise();
127 }); 127 });
128 }, 128 },
  129 + validateAccountLocal: validateAccountPasswordLocal, // eslint-disable-line
129 creditableToken: function() { 130 creditableToken: function() {
130 return getPasswordVal(); 131 return getPasswordVal();
131 }, 132 },
132 type: function() { 133 type: function() {
133 return 'password'; 134 return 'password';
134 }, 135 },
  136 + name: 'PasswordLogin',
135 validate: prePasswordLoginWithValidate // eslint-disable-line 137 validate: prePasswordLoginWithValidate // eslint-disable-line
136 }, 138 },
137 SMSLogin: { 139 SMSLogin: {
@@ -158,12 +160,14 @@ var LoginType = { @@ -158,12 +160,14 @@ var LoginType = {
158 return defer.promise(); 160 return defer.promise();
159 }); 161 });
160 }, 162 },
  163 + validateAccountLocal: validateAccountSmsLocal, // eslint-disable-line
161 creditableToken: function() { 164 creditableToken: function() {
162 - return getCaptchaSmsVal(); 165 + return $captchaSmsTokenHideInput.val();
163 }, 166 },
164 type: function() { 167 type: function() {
165 return 'sms'; 168 return 'sms';
166 }, 169 },
  170 + name: 'SMSLogin',
167 validate: preSmsLoginWithValidate // eslint-disable-line 171 validate: preSmsLoginWithValidate // eslint-disable-line
168 } 172 }
169 }; 173 };
@@ -173,18 +177,27 @@ changeLoginTypeEvent.add(function(type) { @@ -173,18 +177,27 @@ changeLoginTypeEvent.add(function(type) {
173 currentLogin = LoginType[type]; 177 currentLogin = LoginType[type];
174 }); 178 });
175 179
176 -// 清除输入状态 180 +// 重置状态
177 changeLoginTypeEvent.add(function() { 181 changeLoginTypeEvent.add(function() {
178 hideAccountTip(); // eslint-disable-line 182 hideAccountTip(); // eslint-disable-line
179 hideCaptchaImgTip(); // eslint-disable-line 183 hideCaptchaImgTip(); // eslint-disable-line
180 hideCaptchaSmsTip(); // eslint-disable-line 184 hideCaptchaSmsTip(); // eslint-disable-line
181 hidePasswordTip(); // eslint-disable-line 185 hidePasswordTip(); // eslint-disable-line
182 186
183 - // clear input state  
184 $accountInput.val(''); 187 $accountInput.val('');
185 $passwordInput.val(''); 188 $passwordInput.val('');
186 $captchaImgInput.val(''); 189 $captchaImgInput.val('');
187 $captchaSmsInput.val(''); 190 $captchaSmsInput.val('');
  191 + $captchaSmsTokenHideInput.val('');
  192 +});
  193 +
  194 +// placeholder 变化
  195 +changeLoginTypeEvent.add(function(type) {
  196 + if (type === LoginType.SMSLogin.name) {
  197 + $accountInput.attr('placeholder', '手机号码');
  198 + } else {
  199 + $accountInput.attr('placeholder', '邮箱/手机号码');
  200 + }
188 }); 201 });
189 202
190 /**************************************************************************/ 203 /**************************************************************************/
@@ -254,7 +267,7 @@ function hideCaptchaSmsTip() { @@ -254,7 +267,7 @@ function hideCaptchaSmsTip() {
254 /**************************************************************************/ 267 /**************************************************************************/
255 268
256 // 本地验证用户输入格式 269 // 本地验证用户输入格式
257 -function validateAccountLocal() { 270 +function validateAccountPasswordLocal() {
258 var account = getAccountVal(), 271 var account = getAccountVal(),
259 countryCode = getAreaCodeVal(), 272 countryCode = getAreaCodeVal(),
260 err; 273 err;
@@ -292,9 +305,35 @@ function validateAccountLocal() { @@ -292,9 +305,35 @@ function validateAccountLocal() {
292 return defer.promise(); 305 return defer.promise();
293 } 306 }
294 307
  308 +function validateAccountSmsLocal() {
  309 + var account = getAccountVal(),
  310 + err;
  311 +
  312 + var defer = $.Deferred(); // eslint-disable-line
  313 +
  314 + if (account !== '') {
  315 + if (/^[0-9]+$/.test(account)) {
  316 + defer.resolve();
  317 + } else {
  318 + defer.reject();
  319 + err = '手机号码不正确,请重新输入';
  320 + }
  321 + } else {
  322 + err = '请输入手机号';
  323 + }
  324 +
  325 + if (defer.state() === 'resolved') {
  326 + hideAccountTip();
  327 + } else {
  328 + showAccountTip(err);
  329 + }
  330 + return defer.promise();
  331 +}
  332 +
  333 +
295 // 本地验证和网络验证 334 // 本地验证和网络验证
296 function validateAccount() { 335 function validateAccount() {
297 - return validateAccountLocal() 336 + return currentLogin.validateAccountLocal()
298 .then(currentLogin.validateAccountAsync) 337 .then(currentLogin.validateAccountAsync)
299 .then(hideAccountTip); 338 .then(hideAccountTip);
300 } 339 }
@@ -434,7 +473,14 @@ function validateCaptchaSmsLocal() { @@ -434,7 +473,14 @@ function validateCaptchaSmsLocal() {
434 } 473 }
435 474
436 // 网络验证短信验证码 475 // 网络验证短信验证码
  476 +// Note: 这里的验证码只能成功验证一次,十分钟内有效
437 function validateCaptchaSmsAsync() { 477 function validateCaptchaSmsAsync() {
  478 + var smsToken = $.trim($captchaSmsTokenHideInput.val());
  479 +
  480 + if (!smsToken) {
  481 + return $.Deferred().resolve().promise; // eslint-disable-line
  482 + }
  483 +
438 return $.ajax({ 484 return $.ajax({
439 url: '/passport/login/sms/auth', 485 url: '/passport/login/sms/auth',
440 type: 'POST', 486 type: 'POST',
@@ -448,10 +494,11 @@ function validateCaptchaSmsAsync() { @@ -448,10 +494,11 @@ function validateCaptchaSmsAsync() {
448 494
449 if (result.code === 200) { 495 if (result.code === 200) {
450 hideCaptchaSmsTip(); 496 hideCaptchaSmsTip();
451 - $captchaSMSToken.val(result.token); 497 + $captchaSmsTokenHideInput.val(result.token);
452 defer.resolve(); 498 defer.resolve();
453 } else { 499 } else {
454 showCaptchaSmsTip('验证码不正确'); 500 showCaptchaSmsTip('验证码不正确');
  501 + $captchaSmsTokenHideInput.val('');
455 defer.reject(); 502 defer.reject();
456 } 503 }
457 504
@@ -609,10 +656,9 @@ function login() { @@ -609,10 +656,9 @@ function login() {
609 } 656 }
610 }); 657 });
611 }).then(function(res) { 658 }).then(function(res) {
  659 + console.log(res);
612 if (res.code === 200) { 660 if (res.code === 200) {
613 if (res.data) { 661 if (res.data) {
614 -  
615 - // 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing  
616 location.href = res.data.session; 662 location.href = res.data.session;
617 } 663 }
618 } else { 664 } else {
@@ -670,7 +716,7 @@ $(document).on('click', function(e) { @@ -670,7 +716,7 @@ $(document).on('click', function(e) {
670 716
671 // 邮箱自动完成后失去焦点:1. 本地验证格式;2.服务器检查用户是否注册;3.检查验证用户次数 717 // 邮箱自动完成后失去焦点:1. 本地验证格式;2.服务器检查用户是否注册;3.检查验证用户次数
672 mailAc($accountInput, function() { 718 mailAc($accountInput, function() {
673 - return validateAccountLocal().then(function() { 719 + return validateAccountPasswordLocal().then(function() {
674 return currentLogin.validateAccountAsync().always(checkUserAuthCountAsync); 720 return currentLogin.validateAccountAsync().always(checkUserAuthCountAsync);
675 }); 721 });
676 }); 722 });
@@ -710,7 +756,7 @@ $captchaSmsBtn.on('click', function() { @@ -710,7 +756,7 @@ $captchaSmsBtn.on('click', function() {
710 var val = getAccountVal(); 756 var val = getAccountVal();
711 757
712 if (val === '') { 758 if (val === '') {
713 - showAccountTip('请输入账户名'); 759 + showAccountTip('请输入手机号');
714 return; 760 return;
715 } 761 }
716 762