pageLinkList.js
2.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import devCache from "./devCache"
export default {
pageRouteMap: [],
pageRouteKeyMap: {},
/**
* 安装路径分析插件
*/
install() {
let allRoutes = this.getAllRoutes();
let pageRouteKeyMap = devCache.get("pageRouteKeyMap")
if (!pageRouteKeyMap || typeof pageRouteKeyMap != "object") {
pageRouteKeyMap = {}
}
let lastNo = 0;
Object.keys(pageRouteKeyMap).forEach((key) => {
let item = Number(pageRouteKeyMap[key])
if (item > lastNo) {
lastNo = item;
}
})
allRoutes.forEach(item => {
if (!pageRouteKeyMap[item.path]) {
pageRouteKeyMap[item.path] = lastNo + 1;
lastNo = lastNo + 1;
}
})
devCache.set("pageRouteKeyMap", pageRouteKeyMap)
this.pageRouteKeyMap = pageRouteKeyMap;
let pageRouteMap = devCache.get("pageRouteMap");
if (!pageRouteMap || typeof pageRouteMap != "object") {
pageRouteMap = {}
}
Object.keys(pageRouteMap).forEach((key) => {
try {
let n = Number(pageRouteMap[key]);
if (!Number.isInteger(n) || n < 0) {
pageRouteMap[key] = 1;
}
} catch (error) { }
})
this.pageRouteMap = pageRouteMap;
this.saveData()
},
/**
* 获取APP注册的所有路由
* @returns {{path: string}[]} 返回路由列表
*/
getAllRoutes() {
let pages = [];
// #ifdef H5 || APP-PLUS
try {
__uniRoutes.map((item) => {
let path = item.alias ? item.alias : item.path;
pages.push({ path })
});
} catch (error) {
pages = [];
}
// #endif
// #ifdef MP-WEIXIN
try {
let wxPages = __wxConfig.pages;
wxPages.map((item) => {
pages.push({
path: "/" + item,
})
});
} catch (error) {
pages = [];
}
// #endif
return pages;
},
/**
* 写入路由列表
*/
pushPageRouteMap(list = []) {
if (!list || list.length == 0) {
list = getCurrentPages()
}
let key = ""
list.forEach((item) => {
let path = item.route.indexOf("/") == 0 ? item.route : "/" + item.route;
let keyItem = this.pageRouteKeyMap[path];
if (!keyItem) {
keyItem = path;
}
if (key == "") {
key = keyItem + ""
} else {
key = key + "," + keyItem
}
})
if (this.pageRouteMap[key]) {
this.pageRouteMap[key] = this.pageRouteMap[key] + 1;
} else {
this.pageRouteMap[key] = 1;
}
},
/**
* 保存路由缓存
*/
saveData() {
let that = this;
setTimeout(() => {
devCache.set("pageRouteMap", that.pageRouteMap)
setTimeout(() => {
that.saveData()
}, 500);
}, Math.round(Math.random() * 3 * 1000) + 2000);
},
}