login-service.js
3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
'use strict';
const md5 = require('md5');
const uuid = require('uuid');
const _ = require('lodash');
const aes = require('./aes-pwd');
const cache = global.yoho.cache;
const sign = global.yoho.sign;
const cookie = global.yoho.cookie;
const config = global.yoho.config;
const crypto = global.yoho.crypto;
const authcode = require(`${global.utils}/authcode`);
const AUTH_TIME = 2592000000;
const Promise = require('bluebird');
const cartService = require('./cart-service');
const userService = require('./user-service');
const api = require('./login-api');
const logger = global.yoho.logger;
const signin = (type, area, profile, password, shoppingKey) => {
let loginBy = {
password: api.signinByPasswordWithAes,
sms: api.signinBySMS,
qrcode: api.signinByQrCode
};
return loginBy[type](area, profile, password, shoppingKey);
};
const syncUserSession = (uid, req, res, sessionKey) => {
if (sessionKey) {
cache.set(`java_session_key:${uid}`, sessionKey).catch(() => {
logger.error('write session key fail');
});
}
return Promise.all([
userService.profile(uid),
cartService.goodsCount(uid),
userService.checkNoCertEmailUser(uid).catch(err => logger(`checkNoCertEmailUser【${uid}】err: ${err}`))
]).spread((userInfo, count, isTipCert) => {
let salt = uuid.v4().substr(0, 8);
let saltedUid = uid + salt;
let saltedToken = sign.makeToken(saltedUid);
let publicToken = saltedToken + salt;
let data = userInfo.data;
let encryptionUid = aes.encryptionUid(data.uid);
if (data) {
let uidCookie =
`${encodeURIComponent(data.profile_name)}::${encryptionUid}::${data.vip_info.title}::${saltedToken}`;
let isStudent = data.vip_info.is_student || 0;
res.cookie('_UID', uidCookie, {
domain: config.cookieDomain
});
res.cookie('isStudent', isStudent, {
domain: config.cookieDomain
});
res.cookie('_SESSION_KEY', authcode(sessionKey, '_SESSION_KEY', AUTH_TIME, 'encode'), {
domain: config.cookieDomain
});
// 购物车中商品的数量
res.cookie('_g', JSON.stringify({
_k: cookie.getShoppingKey(req),
_nac: count,
_ac: 0,
_c: 1
}), {
domain: config.cookieDomain
});
}
req.session.TOKEN_ = publicToken;
req.session.LOGIN_UID_ = uid;
res.cookie('_TOKEN', publicToken, {
domain: config.cookieDomain,
httpOnly: true
});
if (isTipCert && isTipCert.code === 200 && isTipCert.data) {
return isTipCert.data.isNeedPopupRelated === 'Y';
}
}).catch(console.log);
};
const 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, {
maxAge: aWeek,
domain: config.cookieDomain
});
res.cookie('remem', rememKey, {
maxAge: aWeek,
domain: config.cookieDomain
});
// if (!cache.get(rememKey)) {
// cache.set(rememKey, accountInfo, aWeek);
// }
};
const fetchByQrCode = () => {
let code = {
qrcode: uuid.v4(),
time: new Date().getTime()
};
const encodeStr = _.flow(JSON.stringify, _.partial(crypto.encryption, null), encodeURIComponent);
return Promise.resolve({
data: {
code: encodeStr(code)
},
code: 200
});
};
module.exports = {
signin,
syncUserSession,
rememberAccount,
fetchByQrCode,
checkByQrCode: api.checkByQrCode,
sendPasswordBySMS: api.sendPasswordBySMS,
checkUserExitBySMS: api.checkUserExitBySMS,
verifyPasswordBySMS: api.verifyPasswordBySMS,
signinByOpenID: api.signinByOpenID
};