yas-process.js
2.24 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
const _ = require('lodash');
const logger = global.yoho.logger;
const getPname = (req) => {
let userAgent = req.get('User-Agent');
let isiOS = /\(i[^;]+;( U;)? CPU.+Mac OS X/i.test(userAgent);
let isAndroid = /Android/i.test(userAgent);
let pName = '';
if (isiOS) {
pName = 'iFP_SalesPromotion';
}
if (isAndroid) {
pName = 'aFP_SalesPromotion';
}
return pName;
};
/**
* openbyStr 处理
*/
const _handleOpenbyParams = (params) => {
const paramsKeyStr = 'openby:yohobuy=';
let openByStrNew = '';
let openByParamObj = {};
if (_.isString(params.openbyStr)) {
openByStrNew = _.replace(params.openbyStr, paramsKeyStr, '');
} else {
return params.openbyStr;
}
if (_.isString(openByStrNew)) {
try {
openByParamObj = JSON.parse(openByStrNew);
} catch (error) {
logger.info(`openbyStr json parse err, raw string is ${openByStrNew}`);
}
} else {
return params.openbyStr;
}
if (!_.isEmpty(openByParamObj) && _.has(openByParamObj, 'params')) {
openByParamObj.params.from_page_name = params.fromPageName;
openByParamObj.params.from_page_param = params.fromPageParam;
openByStrNew = `${paramsKeyStr}${JSON.stringify(openByParamObj)}`;
}
return openByStrNew;
};
/**
* 配置的商品添加参数
*/
const addParamsToGoodsHref = (params) => {
let finalHref = '';
let paramsSplitArr = params.href.split('?') || [];
let paramsArr = _.has(paramsSplitArr, '1') ? paramsSplitArr[1].split('&') : [];
let paramsArrNew = [];
let paramsStrNew = '';
paramsArrNew = _.map(paramsArr, param => {
if (_.isString(param) && param.indexOf('openby:yohobuy=') !== -1) {
param = _handleOpenbyParams({
openbyStr: param,
fromPageName: params.fromPageName,
fromPageParam: params.fromPageParam
});
}
return param;
});
if (paramsArrNew.length) {
paramsStrNew = paramsArrNew.join('&');
finalHref = `${paramsSplitArr[0]}?${paramsStrNew}`;
} else {
finalHref = params.href;
}
return finalHref;
};
module.exports = {
getPname,
addParamsToGoodsHref
};