|
|
'use strict';
|
|
|
/**
|
|
|
* Created by TaoHuang on 2016/10/21.
|
|
|
*/
|
|
|
|
|
|
const md5 = require('md5');
|
|
|
'use strict';
|
|
|
|
|
|
const aes = require('./aes-pwd');
|
|
|
const cache = global.yoho.cache;
|
|
|
const sign = global.yoho.sign;
|
|
|
const api = global.yoho.API;
|
|
|
const cookie = global.yoho.cookie;
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
|
|
const cartService = require('./cart-service');
|
|
|
|
|
|
const Auth = {
|
|
|
signin(type, area, profile, password, shoppingKey) {
|
|
|
let _that = this;
|
|
|
let loginBy = {
|
|
|
password: _that.signinByPasswordWithAes,
|
|
|
sms: _that.signinBySMS,
|
|
|
qrcode: _that.signinByQrCode
|
|
|
};
|
|
|
|
|
|
return loginBy[type](area, profile, password, shoppingKey);
|
|
|
},
|
|
|
signinByPasswordWithAes(area, profile, password, shoppingKey) {
|
|
|
const signinByPasswordWithAes = (area, profile, password, shoppingKey) => {
|
|
|
let param = {
|
|
|
method: 'app.passport.signinAES',
|
|
|
area: area,
|
...
|
...
|
@@ -37,8 +20,9 @@ const Auth = { |
|
|
}
|
|
|
|
|
|
return api.post('', param);
|
|
|
},
|
|
|
signinBySMS(area, mobile, token, shoppingKey) {
|
|
|
};
|
|
|
|
|
|
const signinBySMS = (area, mobile, token, shoppingKey) => {
|
|
|
let param = {
|
|
|
method: 'app.passport.autoSignin',
|
|
|
area: area,
|
...
|
...
|
@@ -51,8 +35,9 @@ const Auth = { |
|
|
}
|
|
|
|
|
|
return api.post('', param);
|
|
|
},
|
|
|
signinByQrCode(__1, __2, code, shoppingKey) { // eslint-disable-line
|
|
|
};
|
|
|
|
|
|
const signinByQrCode = (__1, __2, code, shoppingKey) => { // eslint-disable-line
|
|
|
let param = {
|
|
|
method: 'app.twoDimen.qrCodeLogin',
|
|
|
code: code.substring(code.indexOf('=') + 1)
|
...
|
...
|
@@ -63,23 +48,26 @@ const Auth = { |
|
|
}
|
|
|
|
|
|
return api.post('', param);
|
|
|
},
|
|
|
fetchByQrCode() {
|
|
|
};
|
|
|
|
|
|
const fetchByQrCode = () => {
|
|
|
let param = {
|
|
|
method: 'app.twoDimen.getCode'
|
|
|
};
|
|
|
|
|
|
return api.post('', param);
|
|
|
},
|
|
|
checkByQrCode(code) {
|
|
|
};
|
|
|
|
|
|
const checkByQrCode = (code) => {
|
|
|
let param = {
|
|
|
method: 'app.twoDimen.loginCheck',
|
|
|
code: code
|
|
|
};
|
|
|
|
|
|
return api.post('', param);
|
|
|
},
|
|
|
sendPasswordBySMS(area, mobile) {
|
|
|
};
|
|
|
|
|
|
const sendPasswordBySMS = (area, mobile) => {
|
|
|
let param = {
|
|
|
method: 'app.message.sendSms',
|
|
|
area: area,
|
...
|
...
|
@@ -88,15 +76,17 @@ const Auth = { |
|
|
};
|
|
|
|
|
|
return api.get('', param);
|
|
|
},
|
|
|
checkUserExitBySMS(area, mobile) {
|
|
|
};
|
|
|
|
|
|
const checkUserExitBySMS = (area, mobile) => {
|
|
|
return api.get('', {
|
|
|
method: 'app.passport.checkUserExist',
|
|
|
area: area,
|
|
|
mobile: mobile
|
|
|
});
|
|
|
},
|
|
|
verifyPasswordBySMS(area, mobile, code) {
|
|
|
};
|
|
|
|
|
|
const verifyPasswordBySMS = (area, mobile, code) => {
|
|
|
return api.get('', {
|
|
|
method: 'app.message.verifySmsCode',
|
|
|
area: area,
|
...
|
...
|
@@ -104,28 +94,14 @@ const Auth = { |
|
|
code: code,
|
|
|
type: 1 // 手机快捷登录短信验证码
|
|
|
});
|
|
|
},
|
|
|
signinByOpenID(nickname, openId, sourceType, shoppingKey) {
|
|
|
let param = {
|
|
|
nickname: nickname,
|
|
|
openId: openId,
|
|
|
source_type: sourceType,
|
|
|
method: 'app.passport.signinByOpenID'
|
|
|
};
|
|
|
|
|
|
if (shoppingKey) {
|
|
|
param.shopping_key = shoppingKey;
|
|
|
}
|
|
|
};
|
|
|
|
|
|
return api.get('', param);
|
|
|
},
|
|
|
signinByWechat(nickname, openId, unionId, sourceType, shoppingKey) {
|
|
|
const signinByOpenID = (nickname, openId, sourceType, shoppingKey) => {
|
|
|
let param = {
|
|
|
nickname: nickname,
|
|
|
openId: openId,
|
|
|
unionId: unionId,
|
|
|
source_type: sourceType,
|
|
|
method: 'app.passport.signinByWechat'
|
|
|
method: 'app.passport.signinByOpenID'
|
|
|
};
|
|
|
|
|
|
if (shoppingKey) {
|
...
|
...
|
@@ -133,62 +109,16 @@ const Auth = { |
|
|
}
|
|
|
|
|
|
return api.get('', param);
|
|
|
},
|
|
|
profile(uid) {
|
|
|
let param = {
|
|
|
uid: uid,
|
|
|
method: 'app.passport.profile'
|
|
|
};
|
|
|
|
|
|
return api.get('', param);
|
|
|
},
|
|
|
syncUserSession(uid, req, res) {
|
|
|
return Promise.all([Auth.profile(uid), cartService.goodsCount(uid)]).spread((userInfo, count) => {
|
|
|
let token = sign.makeToken(uid);
|
|
|
let data = userInfo.data;
|
|
|
let encryptionUid = aes.encryptionUid(data.uid);
|
|
|
|
|
|
if (data) {
|
|
|
let uidCookie = `${data.profile_name}::${encryptionUid}::${data.vip_info.title}::${token}`;
|
|
|
let isStudent = data.vip_info.is_student || 0;
|
|
|
|
|
|
req.session._TOKEN = token;
|
|
|
req.session._LOGIN_UID = uid;
|
|
|
|
|
|
res.cookie('_UID', uidCookie, {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
res.cookie('isStudent', isStudent, {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
|
|
|
// 购物车中商品的数量
|
|
|
res.cookie('_g', JSON.stringify({
|
|
|
_k: cookie.getShoppingKey(req),
|
|
|
_nac: count,
|
|
|
_ac: 0,
|
|
|
_c: 1
|
|
|
}), {
|
|
|
domain: 'yohobuy.com'
|
|
|
});
|
|
|
}
|
|
|
req.session._TOKEN = token; // esline-disable-line
|
|
|
req.session._LOGIN_UID = uid; // esline-disable-line
|
|
|
res.cookie('_TOKEN', token, {
|
|
|
domain: 'yohobuy.com'
|
|
|
}); // esline-disable-line
|
|
|
}).catch(console.log);
|
|
|
},
|
|
|
rememberAccount(accountInfo, req, res) {
|
|
|
let aWeek = (new Date()).getTime() / 1000 + 504000; // 504000-一周
|
|
|
let rememKey = md5(md5(accountInfo.account + accountInfo.password + accountInfo.area));
|
|
|
|
|
|
res.cookie('isRemember', true, aWeek);
|
|
|
res.cookie('remem', rememKey, aWeek);
|
|
|
if (!cache.get(rememKey)) {
|
|
|
cache.set(rememKey, accountInfo, aWeek);
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
module.exports = Auth; |
|
|
module.exports = {
|
|
|
signinByPasswordWithAes,
|
|
|
signinBySMS,
|
|
|
signinByQrCode,
|
|
|
fetchByQrCode,
|
|
|
checkByQrCode,
|
|
|
sendPasswordBySMS,
|
|
|
checkUserExitBySMS,
|
|
|
verifyPasswordBySMS,
|
|
|
signinByOpenID
|
|
|
}; |
...
|
...
|
|