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
107
108
109
110
111
112
113
114
115
116
export interface Params {
[key: string]: any;
}
class RouterService {
/**
* @description: 跨应用跳转
* @author ChenRui
* @date 2020/9/28 14:23
*/
push(subapp: string): void {
history.pushState(null, subapp, subapp);
}
/**
* @description: 获取纯路劲
* @author ChenRui
* @date 2020/10/12 21:06
*/
getPathFromUrl(url: string): string {
let path = url;
if (url.indexOf("?") !== -1) {
path = url.split("?")[0];
}
return path;
}
/**
* @description: 获取请求参数
* @author ChenRui
* @date 2020/10/12 21:06
*/
getParamsFromUrl(url: string): Params | undefined {
let parameter: Params | undefined;
if (url.indexOf("?") !== -1) {
parameter = this.urlParameterToParams(url.split("?")[1]);
}
return parameter;
}
/**
* @description: url地址参数转换成路由参数对象
* @author ChenRui
* @date 2020/10/12 21:07
*/
urlParameterToParams(parameter: string): Params | undefined {
const params: Params = {};
const arr: string[] = parameter.split("&");
arr.forEach((val, idx, array) => {
const kv: string[] = val.split("=");
if (kv.length > 1) {
params[kv[0]] = decodeURIComponent(kv[1]);
} else {
params[kv[0]] = "";
}
});
return params;
}
/**
* @description: 地址参数拼接
* @author ChenRui
* @date 2021/2/19 11:40
*/
combinationPathAndParameter(url: string, parameter?: { [key: string]: any }): string {
let connectiveSymbol = "";
if (url.indexOf("?") !== -1) {
connectiveSymbol = "&";
} else {
connectiveSymbol = "?";
}
if (parameter && Object.keys(parameter).length > 0) {
let mark = 0;
Object.keys(parameter).forEach((item) => {
if (mark === 0) {
if (parameter[item] != null && parameter[item] !== "null" && parameter[item] !== "undefined") {
url += connectiveSymbol + item + "=" + parameter[item];
} else {
url += connectiveSymbol + item + "=";
}
} else {
if (parameter[item] != null && parameter[item] !== "null" && parameter[item] !== "undefined") {
url += "&" + item + "=" + parameter[item];
} else {
url += "&" + item + "=";
}
}
mark++;
});
}
return url;
}
/**
* 切换路由动画
* @param to
* @param from
* @param component
*/
animateName: (to: any, from: any, component: any) => string = (to: any, from: any, component: any) => {
const toPath = to.path.split("/").join("_");
const frPath = from.path.split("/").join("_");
if (!component.pathMap[toPath]) {
component.pathMap[toPath] = +new Date() + 1;
}
if (!component.pathMap[frPath]) {
component.pathMap[frPath] = +new Date();
}
if (component.pathMap[toPath] > component.pathMap[frPath]) {
return "left";
} else {
return "right";
}
};
}
const routerService = new RouterService();
export default routerService;