miniapp-path-rules.js
2.98 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
const _isPlainObject = (obj) => {
if (typeof(obj) !== 'object' || obj.nodeType || obj !== null && obj !== undefined && obj === obj.window) { // eslint-disable-line
return false;
}
if (obj.constructor &&
!Object.prototype.hasOwnProperty.call(obj.constructor.prototype, 'isPrototypeOf')) {
return false;
}
return true;
};
const _qsStringfy = (params) => {
let paramsArr = [];
if (params && _isPlainObject(params)) {
for (let key in params) {
if (params.hasOwnProperty(key)) {
paramsArr.push(`${key}=${params[key]}`);
}
}
} else {
console.info('params is:', params);
}
return paramsArr.length ? `?${paramsArr.join('&')}` : '';
};
// 有货跳转规则转化为有货精选路由
const transToMiniappPath = (href) => {
let path = '';
let openbyParamsArr = href.split(/openby:yohobuy(=|=)/);
let paramsStr = openbyParamsArr[openbyParamsArr.length - 1];
let paramsObj = {};
try {
paramsObj = JSON.parse(paramsStr);
} catch (error) {
console.error('paramsStr is: ', paramsStr);
}
switch (paramsObj.action) {
case 'go.productDetail': // 商品详情页
if (paramsObj.params && paramsObj.params.product_skn &&
paramsObj.params.activity_type !== 'groupPurchase') {
path = `/pages/goodsDetail/goodsDetail?productSkn=${paramsObj.params.product_skn}&page_name=home`;
} else if (paramsObj.params.activity_type === 'groupPurchase') {
paramsObj.action = 'go.groupProductDetail';
}
break;
case 'go.list': // 列表页、专区
path = `/pages/goodsList/goodsList${_qsStringfy(paramsObj.params)}`;
break;
case 'go.poollist': // 商品池
path = `/pages/goodsList/productPool${_qsStringfy(paramsObj.params)}`;
break;
case 'go.h5':
if (paramsObj.params && paramsObj.params.url) {
let regHome = /yoluck\/index/;
let regDetail = /yoluck\/(\d+)\.html/;
let detailMatchResult = paramsObj.params.url.match(regDetail);
if (regHome.test(paramsObj.params.url)) {
paramsObj.action = 'go.yoluckHome';
} else if (detailMatchResult && detailMatchResult[1]) {
paramsObj.action = 'go.yoluckDetail';
paramsObj.params.param.id = detailMatchResult[1];
} else {
path = `/pages/webview/webview?page_name=home&url=${paramsObj.params.url}${_qsStringfy(paramsObj.params.param)}`; // eslint-disable-line
}
}
break;
case 'go.shop':
path = `/pages/goodsList/brandStore?shopId=${paramsObj.params.shop_id}`;
break;
default:
break;
}
return {
path: path,
action: paramsObj.action
};
};
module.exports = {
transToMiniappPath
};