list-params-process.js
2.15 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
const _ = require('lodash');
const config = global.yoho.config;
/**
* 参数列表
*/
const PARAMMAP = {
ag: 'age_level',
bd: 'brand',
cc: 'coupon_code',
cd: 'coupon_id',
ci: 'category_id',
cl: 'color',
cn: 'channel',
fp: 'filter_poolId',
gd: 'gender',
ld: 'limited',
lt: 'limit',
mi: 'misort',
ms: 'msort',
nw: 'new',
od: 'order',
ol: 'outlets',
pc: 'price',
pd: 'p_d',
pg: 'page',
pm: 'promotion',
pp: 'productPool',
qr: 'query',
sd: 'standard',
se: 'shelveTime',
sf: 'specialoffer',
sh: 'shop_id',
si: 'specialsale_id',
so: 'sort',
st: 'style',
sz: 'size',
tp: 'type'
};
/**
* 获取标准参数
*/
const getParams = (url) => {
let params = [];
let paramStringRaw = _.last(_.split(url, '/'));
if (paramStringRaw) {
let paramsRaw = _.split(paramStringRaw, '-');
_.forEach(paramsRaw, paramRaw => {
let keyRaw = paramRaw.substr(0, 2);
let valueRaw = _.chain(paramRaw)
.replace(keyRaw, '')
.replace('__', '-')
.value();
if (PARAMMAP[keyRaw]) {
params[PARAMMAP[keyRaw]] = valueRaw;
}
});
}
return _.toPlainObject(params);
};
/**
* 生成链接
* @param {参数} queryParams
*/
const generatePathUrl = (queryParams) => {
let pathUrlParams = []; // 可以找到对应关系的参数
let noFindParams = []; // 没有找到对应关系的
let pathUrl = '';
_.forEach(queryParams, (value, key) => {
let paramKey = _.findKey(PARAMMAP, o => {
return o === key;
});
if (paramKey) {
pathUrlParams.push(paramKey + value + '');
} else {
noFindParams.push(key + '=' + value);
}
});
pathUrl = pathUrlParams.length ? pathUrlParams.join('-') : '';
pathUrl += noFindParams.length ? '?' + noFindParams.join('&') : '';
pathUrl = (pathUrl && !_.startsWith(pathUrl, '?')) ? `/${pathUrl}` : pathUrl;
return `${config.subDomains.default}/list${pathUrl}`;
};
module.exports = {
getParams,
generatePathUrl
};