{"version":3,"file":"index.js","sources":["../../../../src/utils/index.ts"],"sourcesContent":["import { pages, subPackages, tabBar } from '@/pages.json'\r\nconst getLastPage = () => {\r\n // getCurrentPages() 至少有1个元素,所以不再额外判断\r\n // const lastPage = getCurrentPages().at(-1)\r\n // 上面那个在低版本安卓中打包回报错,所以改用下面这个【虽然我加了src/interceptions/prototype.ts,但依然报错】\r\n const pages = getCurrentPages()\r\n return pages[pages.length - 1]\r\n}\r\n\r\n/** 判断当前页面是否是tabbar页 */\r\nexport const getIsTabbar = () => {\r\n if (!tabBar) {\r\n return false\r\n }\r\n if (!tabBar.list.length) {\r\n // 通常有tabBar的话,list不能有空,且至少有2个元素,这里其实不用处理\r\n return false\r\n }\r\n const lastPage = getLastPage()\r\n const currPath = lastPage.route\r\n return !!tabBar.list.find((e) => e.pagePath === currPath)\r\n}\r\n\r\n/**\r\n * 获取当前页面路由的 path 路劲和 redirectPath 路径\r\n * path 如 ‘/pages/login/index’\r\n * redirectPath 如 ‘/pages/demo/base/route-interceptor’\r\n */\r\nexport const currRoute = () => {\r\n const lastPage = getLastPage()\r\n const currRoute = (lastPage as any).$page\r\n // console.log('lastPage.$page:', currRoute)\r\n // console.log('lastPage.$page.fullpath:', currRoute.fullPath)\r\n // console.log('lastPage.$page.options:', currRoute.options)\r\n // console.log('lastPage.options:', (lastPage as any).options)\r\n // 经过多端测试,只有 fullPath 靠谱,其他都不靠谱\r\n const { fullPath } = currRoute as { fullPath: string }\r\n // console.log(fullPath)\r\n // eg: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)\r\n // eg: /pages/login/index?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)\r\n return getUrlObj(fullPath)\r\n}\r\n\r\nconst ensureDecodeURIComponent = (url: string) => {\r\n if (url.startsWith('%')) {\r\n return ensureDecodeURIComponent(decodeURIComponent(url))\r\n }\r\n return url\r\n}\r\n/**\r\n * 解析 url 得到 path 和 query\r\n * 比如输入url: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor\r\n * 输出: {path: /pages/login/index, query: {redirect: /pages/demo/base/route-interceptor}}\r\n */\r\nexport const getUrlObj = (url: string) => {\r\n const [path, queryStr] = url.split('?')\r\n // console.log(path, queryStr)\r\n\r\n if (!queryStr) {\r\n return {\r\n path,\r\n query: {},\r\n }\r\n }\r\n const query: Record = {}\r\n queryStr.split('&').forEach((item) => {\r\n const [key, value] = item.split('=')\r\n // console.log(key, value)\r\n query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下,可以兼容h5和微信y\r\n })\r\n return { path, query }\r\n}\r\n/**\r\n * 得到所有的需要登录的pages,包括主包和分包的\r\n * 这里设计得通用一点,可以传递key作为判断依据,默认是 needLogin, 与 route-block 配对使用\r\n * 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤\r\n */\r\nexport const getAllPages = (key = 'needLogin') => {\r\n // 这里处理主包\r\n const mainPages = [\r\n ...pages\r\n .filter((page) => !key || page[key])\r\n .map((page) => ({\r\n ...page,\r\n path: `/${page.path}`,\r\n })),\r\n ]\r\n // 这里处理分包\r\n const subPages: any[] = []\r\n subPackages.forEach((subPageObj) => {\r\n // console.log(subPageObj)\r\n const { root } = subPageObj\r\n\r\n subPageObj.pages\r\n .filter((page) => !key || page[key])\r\n .forEach((page: { path: string } & Record) => {\r\n subPages.push({\r\n ...page,\r\n path: `/${root}/${page.path}`,\r\n })\r\n })\r\n })\r\n const result = [...mainPages, ...subPages]\r\n // console.log(`getAllPages by ${key} result: `, result)\r\n return result\r\n}\r\n\r\n/**\r\n * 得到所有的需要登录的pages,包括主包和分包的\r\n * 只得到 path 数组\r\n */\r\nexport const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((page) => page.path)\r\n\r\n/**\r\n * 得到所有的需要登录的pages,包括主包和分包的\r\n * 只得到 path 数组\r\n */\r\nexport const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path)\r\n"],"names":["pages","subPackages"],"mappings":";;;;;;;;;;;;;;;;;;;;;AA6Ea,MAAA,cAAc,CAAC,MAAM,gBAAgB;AAEhD,QAAM,YAAY;AAAA,IAChB,GAAGA,oBACA,OAAO,CAAC,SAAS,CAAC,OAAO,KAAK,GAAG,CAAC,EAClC,IAAI,CAAC,SAAU,iCACX,OADW;AAAA,MAEd,MAAM,IAAI,KAAK,IAAI;AAAA,IAAA,EACnB;AAAA,EAAA;AAGN,QAAM,WAAkB,CAAA;AACZC,4BAAA,QAAQ,CAAC,eAAe;AAE5B,UAAA,EAAE,KAAS,IAAA;AAEjB,eAAW,MACR,OAAO,CAAC,SAAS,CAAC,OAAO,KAAK,GAAG,CAAC,EAClC,QAAQ,CAAC,SAAiD;AACzD,eAAS,KAAK,iCACT,OADS;AAAA,QAEZ,MAAM,IAAI,IAAI,IAAI,KAAK,IAAI;AAAA,MAAA,EAC5B;AAAA,IAAA,CACF;AAAA,EAAA,CACJ;AACD,QAAM,SAAS,CAAC,GAAG,WAAW,GAAG,QAAQ;AAElC,SAAA;AACT;AAMa,MAAA,oBAAoB,MAAgB,YAAY,WAAW,EAAE,IAAI,CAAC,SAAS,KAAK,IAAI;AAMzD,YAAY,WAAW,EAAE,IAAI,CAAC,SAAS,KAAK,IAAI;;"}