shuishen
2025-10-11 3fc27febccd04e2fcffbd2cdd16d2daafd0b3ca3
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
/*
 * @Author       : yuan
 * @Date         : 2025-09-28 09:31:16
 * @LastEditors  : yuan
 * @LastEditTime : 2025-09-28 09:55:05
 * @FilePath     : \src\router\index.js
 * @Description  :
 * Copyright 2025 OBKoro1, All Rights Reserved.
 * 2025-09-28 09:31:16
 */
import pagesJson from "@/pages.json"
 
// 路径常量
export const HOME_PATH = "/pages/map/index"
export const LOGIN_PATH = "/pages/login/index"
export const ERROR404_PATH = "/pages/common/404/index"
 
/**
 * 解析路由地址
 * @param {object} pagesJson
 * @returns [{"path": "/pages/tab/map/index","needLogin": false},...]
 */
function parseRoutes (pagesJson = {}) {
  if (!pagesJson.pages) {
    pagesJson.pages = []
  }
  if (!pagesJson.subPackages) {
    pagesJson.subPackages = []
  }
 
  function parsePages (pages = [], rootPath = "") {
    const routes = []
    for (let i = 0; i < pages.length; i++) {
      routes.push({
        path: rootPath ? `/${rootPath}/${pages[i].path}` : `/${pages[i].path}`,
        needLogin: pages[i].needLogin === true
      })
    }
    return routes
  }
 
  function parseSubPackages (subPackages = []) {
    const routes = []
    for (let i = 0; i < subPackages.length; i++) {
      routes.push(...parsePages(subPackages[i].pages, subPackages[i].root))
    }
    return routes
  }
 
  return [
    ...parsePages(pagesJson.pages),
    ...parseSubPackages(pagesJson.subPackages)
  ]
}
export const routes = parseRoutes(pagesJson)
 
/**
 * 当前路由
 * @returns {string} 当前路由
 */
export function currentRoute () {
  // getCurrentPages() 至少有1个元素,所以不再额外判断
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  return currentPage?.$page?.fullPath || currentPage.route
}
 
/**
 * 去除查询字符串
 * @param {string} path
 * @returns {string} 去除查询字符串后的路径
 */
export function removeQueryString (path = "") {
  return path.split("?")[0]
}
 
/**
 * 路径是否存在
 * @param {string} path
 * @returns {boolean} 路径是否存在
 */
export function isPathExists (path = "") {
  const cleanPath = removeQueryString(path)
  return routes.some(item => item.path === cleanPath)
}
 
/**
 * 是否是tabbar页面路径
 * @param {string} path
 * @returns {boolean} 是否是tabbar页面
 */
export function isTabBarPath (path = "") {
  const cleanPath = removeQueryString(path)
  return (
    pagesJson.tabBar?.list?.some(item => `/${item.pagePath}` === cleanPath) ===
    true
  )
}