url-to-path.js
2.64 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
/**
* url解析到path
*/
import { stringify, parse } from '../vendors/query-stringify';
const GO_DETAIL = 'go.productDetail';
const GO_H5 = 'go.h5';
const GO_MINIAPP = 'go.miniapp';
import Action from './actions'
function urlToPath(jumpUrl) {
if (!jumpUrl) {
return {};
}
let pathName = '';
let openBy = {}
if (typeof jumpUrl === 'string') { // 处理普通url
let tempArr = jumpUrl.split('?');
tempArr.splice(0, 1);
let search = tempArr.join('?');
// 排除search内rom_page_param参数的url参数
search = search.replace(/"from_page_param":"[^,]+"/, '"from_page_param":""');
search = search.replace(/'from_page_param':'[^,]+'/, "'from_page_param':''");
const qs = parse(search);
if (!qs['openby:yohobuy'])
return {};
openBy = JSON.parse(qs['openby:yohobuy']);
} else if (typeof jumpUrl === 'object') { // 处理公共跳转页面path参数
openBy = JSON.parse(jumpUrl['openby:yohobuy']);
}
let action = openBy.action;
console.log(action)
let query = openBy.params;
let appId = openBy.appId || '';
if (query && query.appId) {
appId = query.appId;
}
let miniApp = openBy.miniApp;
let pagePath = '';
if (Action[action]) {
pathName = Action[action]
if (openBy.params) {
if (openBy.params.actiontype) {
let actionType = openBy.params.actiontype
pathName = Action[action][actionType]
}
if (openBy.params.type) {
let actionType = openBy.params.type
pathName = Action[action][actionType]
}
}
}
if (GO_H5 === action) {
//处理go.h5 url参数
if (query && query.param) {
if (query.url) {
let urltemp = query.url;
let urlparams = stringify(query.param, { encode: false });
if (urltemp.indexOf('?') > -1) {
urltemp = urltemp + "&" + encodeURIComponent(urlparams);
} else {
urltemp = urltemp + "?" + encodeURIComponent(urlparams);
}
query.url = encodeURIComponent(urltemp);
}
}
} else if (GO_DETAIL === action) {
// 参数兼容
query.productSkn = query.product_skn || query.productSkn;
query.activityId = query.activity_id || query.activityId;
} else if (GO_MINIAPP === action) {
//跳转到小程序必须有appid和path
if (!query || !query.appId || query.appId.length === 0) { return }
if (!query.path || query.path.length === 0) {
pagePath = '';
} else {
pagePath = query.path;
}
if (pagePath) {
if (pagePath.indexOf(0) != '/') {
pagePath = '/' + pagePath;
}
}
}
return {
appId,
miniApp,
pagePath,
pathName,
query
};
}
export default urlToPath;