unibest/src/utils/index.ts

119 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-05-08 15:33:31 +08:00
import { pages, subPackages, tabBar } from '@/pages.json'
const getLastPage = () => {
// getCurrentPages() 至少有1个元素所以不再额外判断
// const lastPage = getCurrentPages().at(-1)
// 上面那个在低版本安卓中打包回报错所以改用下面这个【虽然我加了src/interceptions/prototype.ts但依然报错】
const pages = getCurrentPages()
return pages[pages.length - 1]
}
2024-02-04 16:00:27 +08:00
/** 判断当前页面是否是tabbar页 */
export const getIsTabbar = () => {
2024-05-08 15:33:31 +08:00
if (!tabBar) {
return false
}
if (!tabBar.list.length) {
// 通常有tabBar的话list不能有空且至少有2个元素这里其实不用处理
2024-02-04 16:00:27 +08:00
return false
}
const lastPage = getLastPage()
const currPath = lastPage.route
2024-05-08 15:33:31 +08:00
return !!tabBar.list.find((e) => e.pagePath === currPath)
2024-02-04 16:00:27 +08:00
}
2024-03-27 17:20:05 +08:00
/**
* path redirectPath
* path /pages/login/index
* redirectPath /pages/demo/base/route-interceptor
*/
export const currRoute = () => {
const lastPage = getLastPage()
2024-03-27 17:20:05 +08:00
const currRoute = (lastPage as any).$page
// console.log('lastPage.$page:', currRoute)
// console.log('lastPage.$page.fullpath:', currRoute.fullPath)
// console.log('lastPage.$page.options:', currRoute.options)
// console.log('lastPage.options:', (lastPage as any).options)
// 经过多端测试,只有 fullPath 靠谱,其他都不靠谱
const { fullPath } = currRoute as { fullPath: string }
2024-06-16 16:46:58 +08:00
// console.log(fullPath)
2024-03-27 17:20:05 +08:00
// eg: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor (小程序)
// eg: /pages/login/index?redirect=%2Fpages%2Froute-interceptor%2Findex%3Fname%3Dfeige%26age%3D30(h5)
return getUrlObj(fullPath)
}
const ensureDecodeURIComponent = (url: string) => {
if (url.startsWith('%')) {
return ensureDecodeURIComponent(decodeURIComponent(url))
}
return url
}
/**
* url path query
* url: /pages/login/index?redirect=%2Fpages%2Fdemo%2Fbase%2Froute-interceptor
* : {path: /pages/login/index, query: {redirect: /pages/demo/base/route-interceptor}}
*/
export const getUrlObj = (url: string) => {
const [path, queryStr] = url.split('?')
2024-06-11 13:29:18 +08:00
// console.log(path, queryStr)
2024-03-27 17:20:05 +08:00
2024-06-11 13:29:18 +08:00
if (!queryStr) {
return {
path,
query: {},
}
}
2024-03-27 17:20:05 +08:00
const query: Record<string, string> = {}
queryStr.split('&').forEach((item) => {
const [key, value] = item.split('=')
2024-06-11 13:29:18 +08:00
// console.log(key, value)
2024-03-27 17:20:05 +08:00
query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下可以兼容h5和微信y
})
return { path, query }
}
/**
* pages
* key作为判断依据 needLogin, route-block 使
* keypages key, key
*/
export const getAllPages = (key = 'needLogin') => {
// 这里处理主包
2024-05-08 15:33:31 +08:00
const mainPages = [
...pages
2024-03-27 17:20:05 +08:00
.filter((page) => !key || page[key])
.map((page) => ({
...page,
path: `/${page.path}`,
})),
]
// 这里处理分包
const subPages: any[] = []
2024-05-08 15:33:31 +08:00
subPackages.forEach((subPageObj) => {
2024-03-27 17:20:05 +08:00
// console.log(subPageObj)
const { root } = subPageObj
subPageObj.pages
.filter((page) => !key || page[key])
.forEach((page: { path: string } & Record<string, any>) => {
subPages.push({
...page,
path: `/${root}/${page.path}`,
})
})
})
2024-05-08 15:33:31 +08:00
const result = [...mainPages, ...subPages]
2024-06-16 16:46:58 +08:00
// console.log(`getAllPages by ${key} result: `, result)
2024-03-27 17:20:05 +08:00
return result
}
/**
* pages
* path
*/
export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((page) => page.path)
/**
* pages
* path
*/
export const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path)