user-service.js
2.81 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
/**
* Created by TaoHuang on 2017/4/17.
*/
'use strict';
const _ = require('lodash');
const Context = require('../framework/context');
const rp = require('request-promise');
const Api = require('../common/api');
const config = global.yoho.config;
const logger = global.yoho.logger;
const regSession = '${0}=([^;]+);';
class UserService extends Context {
constructor() {
super();
this.api = this.instance(Api);
}
login(account, password) {
return this.api.post(config.apiDomain.erp.login, {
account,
password,
platform: config.platform
}).then(userInfo => {
if (userInfo.code !== 200 || !userInfo.data.pid) {
logger.error('[apiDomain.erp.login]', userInfo);
return Promise.reject({code: 500, message: '用户名密码错误'});
}
return userInfo.data;
});
}
updatePwd(pid, password) {
return this.api.post(config.apiDomain.erp.update, {
pid,
password,
});
}
shopLogin(account, password) {
return rp.get({
url: config.apiDomain.shop.login,
resolveWithFullResponse: true,
qs: {
user: account,
password: password
}
}).then(response => {
return this._getShopsSession(response.rawHeaders);
});
}
_getShopsSession(rawHeaders) {
let sessId = '', sid = '';
_.each(rawHeaders, header => {
let sessIdMatched = header.match(new RegExp(regSession.replace('${0}', 'PHPSESSID')));
let sidMatched = header.match(new RegExp(regSession.replace('${0}', 'connect.sid')));
if (sessIdMatched) {
sessId = sessIdMatched[1];
}
if (sidMatched) {
sid = sidMatched[1];
}
});
return {
PHPSESSID: sessId,
'connect.sid': sid
};
}
getShops(pid) {
return this.api.get(config.apiDomain.platform.queryShopsByAdminPid, {
userId: pid,
platform: 4
}).then(result => {
return result;
});
}
switchShop(opts) {
let options = {
url: `${config.apiDomain.shop.switchShop}?shops_id=${opts.shopId}`,
resolveWithFullResponse: true,
};
let jar = rp.jar();
_.each(opts.cookies, (val, key) => {
jar.setCookie(rp.cookie(`${key}=${val}`), options.url);
});
options.jar = jar;
return rp.get(options).then(response => {
return this._getShopsSession(response.rawHeaders);
});
}
profile(pid) {
return this.instance(Api).get(config.apiDomain.shop.profile.url, {userId: pid});
}
}
module.exports = UserService;