pageLinkList.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import devCache from "./devCache"
  2. export default {
  3. pageRouteMap: [],
  4. pageRouteKeyMap: {},
  5. /**
  6. * 安装路径分析插件
  7. */
  8. install() {
  9. let allRoutes = this.getAllRoutes();
  10. let pageRouteKeyMap = devCache.get("pageRouteKeyMap")
  11. if (!pageRouteKeyMap || typeof pageRouteKeyMap != "object") {
  12. pageRouteKeyMap = {}
  13. }
  14. let lastNo = 0;
  15. Object.keys(pageRouteKeyMap).forEach((key) => {
  16. let item = Number(pageRouteKeyMap[key])
  17. if (item > lastNo) {
  18. lastNo = item;
  19. }
  20. })
  21. allRoutes.forEach(item => {
  22. if (!pageRouteKeyMap[item.path]) {
  23. pageRouteKeyMap[item.path] = lastNo + 1;
  24. lastNo = lastNo + 1;
  25. }
  26. })
  27. devCache.set("pageRouteKeyMap", pageRouteKeyMap)
  28. this.pageRouteKeyMap = pageRouteKeyMap;
  29. let pageRouteMap = devCache.get("pageRouteMap");
  30. if (!pageRouteMap || typeof pageRouteMap != "object") {
  31. pageRouteMap = {}
  32. }
  33. Object.keys(pageRouteMap).forEach((key) => {
  34. try {
  35. let n = Number(pageRouteMap[key]);
  36. if (!Number.isInteger(n) || n < 0) {
  37. pageRouteMap[key] = 1;
  38. }
  39. } catch (error) { }
  40. })
  41. this.pageRouteMap = pageRouteMap;
  42. this.saveData()
  43. },
  44. /**
  45. * 获取APP注册的所有路由
  46. * @returns {{path: string}[]} 返回路由列表
  47. */
  48. getAllRoutes() {
  49. let pages = [];
  50. // #ifdef H5 || APP-PLUS
  51. try {
  52. __uniRoutes.map((item) => {
  53. let path = item.alias ? item.alias : item.path;
  54. pages.push({ path })
  55. });
  56. } catch (error) {
  57. pages = [];
  58. }
  59. // #endif
  60. // #ifdef MP-WEIXIN
  61. try {
  62. let wxPages = __wxConfig.pages;
  63. wxPages.map((item) => {
  64. pages.push({
  65. path: "/" + item,
  66. })
  67. });
  68. } catch (error) {
  69. pages = [];
  70. }
  71. // #endif
  72. return pages;
  73. },
  74. /**
  75. * 写入路由列表
  76. */
  77. pushPageRouteMap(list = []) {
  78. if (!list || list.length == 0) {
  79. list = getCurrentPages()
  80. }
  81. let key = ""
  82. list.forEach((item) => {
  83. let path = item.route.indexOf("/") == 0 ? item.route : "/" + item.route;
  84. let keyItem = this.pageRouteKeyMap[path];
  85. if (!keyItem) {
  86. keyItem = path;
  87. }
  88. if (key == "") {
  89. key = keyItem + ""
  90. } else {
  91. key = key + "," + keyItem
  92. }
  93. })
  94. if (this.pageRouteMap[key]) {
  95. this.pageRouteMap[key] = this.pageRouteMap[key] + 1;
  96. } else {
  97. this.pageRouteMap[key] = 1;
  98. }
  99. },
  100. /**
  101. * 保存路由缓存
  102. */
  103. saveData() {
  104. let that = this;
  105. setTimeout(() => {
  106. devCache.set("pageRouteMap", that.pageRouteMap)
  107. setTimeout(() => {
  108. that.saveData()
  109. }, 500);
  110. }, Math.round(Math.random() * 3 * 1000) + 2000);
  111. },
  112. }