154 lines
4.7 KiB
TypeScript
Raw Normal View History

2024-12-31 10:05:55 +08:00
import { pages, subPackages, tabBar } from '@/pages.json'
import { isMp } from './platform'
const getLastPage = () => {
// getCurrentPages() 至少有1个元素所以不再额外判断
// const lastPage = getCurrentPages().at(-1)
// 上面那个在低版本安卓中打包回报错所以改用下面这个【虽然我加了src/interceptions/prototype.ts但依然报错】
const pages = getCurrentPages()
return pages[pages.length - 1]
}
/** 判断当前页面是否是tabbar页 */
export const getIsTabbar = () => {
if (!tabBar) {
return false
}
if (!tabBar.list.length) {
// 通常有tabBar的话list不能有空且至少有2个元素这里其实不用处理
return false
}
const lastPage = getLastPage()
const currPath = lastPage.route
2024-12-31 10:19:20 +08:00
return !!tabBar.list.find(e => e.pagePath === currPath)
2024-12-31 10:05:55 +08:00
}
/**
* path redirectPath
* path /pages/login/index
* redirectPath /pages/demo/base/route-interceptor
*/
export const currRoute = () => {
const lastPage = getLastPage()
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 }
// console.log(fullPath)
// 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('?')
// console.log(path, queryStr)
if (!queryStr) {
return {
path,
2024-12-31 10:19:20 +08:00
query: {}
2024-12-31 10:05:55 +08:00
}
}
const query: Record<string, string> = {}
2024-12-31 10:19:20 +08:00
queryStr.split('&').forEach(item => {
2024-12-31 10:05:55 +08:00
const [key, value] = item.split('=')
// console.log(key, value)
query[key] = ensureDecodeURIComponent(value) // 这里需要统一 decodeURIComponent 一下可以兼容h5和微信y
})
return { path, query }
}
/**
* pages
* key作为判断依据 needLogin, route-block 使
* keypages key, key
*/
export const getAllPages = (key = 'needLogin') => {
const mainPages = [
...pages
2025-01-03 15:01:22 +08:00
.filter(page => !key || page[key] !== false)
2024-12-31 10:19:20 +08:00
.map(page => ({
2024-12-31 10:05:55 +08:00
...page,
2024-12-31 10:19:20 +08:00
path: `/${page.path}`
}))
2024-12-31 10:05:55 +08:00
]
2025-01-03 15:01:22 +08:00
2024-12-31 10:05:55 +08:00
const subPages: any[] = []
2024-12-31 10:19:20 +08:00
subPackages.forEach(subPageObj => {
2024-12-31 10:05:55 +08:00
const { root } = subPageObj
subPageObj.pages
2025-01-03 15:01:22 +08:00
.filter(page => !key || page[key] !== false)
2024-12-31 10:05:55 +08:00
.forEach((page: { path: string } & Record<string, any>) => {
subPages.push({
...page,
2024-12-31 10:19:20 +08:00
path: `/${root}/${page.path}`
2024-12-31 10:05:55 +08:00
})
})
})
2025-01-03 15:01:22 +08:00
return [...mainPages, ...subPages]
2024-12-31 10:05:55 +08:00
}
/**
* pages
* path
*/
2024-12-31 10:19:20 +08:00
export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map(page => page.path)
2024-12-31 10:05:55 +08:00
/**
* pages
* path
*/
2024-12-31 10:19:20 +08:00
export const needLoginPages: string[] = getAllPages('needLogin').map(page => page.path)
2024-12-31 10:05:55 +08:00
/**
* BaseUrl
*/
export const getEnvBaseUrl = () => {
// 请求基准地址
2025-01-02 16:11:01 +08:00
return import.meta.env.VITE_SERVER_BASEURL
2024-12-31 10:05:55 +08:00
}
/**
* UPLOAD_BASEURL
*/
export const getEnvBaseUploadUrl = () => {
// 请求基准地址
let baseUploadUrl = import.meta.env.VITE_UPLOAD_BASEURL
// 小程序端环境区分
if (isMp) {
const {
2024-12-31 10:19:20 +08:00
miniProgram: { envVersion }
2024-12-31 10:05:55 +08:00
} = uni.getAccountInfoSync()
switch (envVersion) {
case 'develop':
baseUploadUrl = 'https://ukw0y1.laf.run/upload'
break
case 'trial':
baseUploadUrl = 'https://ukw0y1.laf.run/upload'
break
case 'release':
baseUploadUrl = 'https://ukw0y1.laf.run/upload'
break
}
}
return baseUploadUrl
}