feat: 完成对接
This commit is contained in:
+26
-49
@@ -9,8 +9,15 @@ export const getLastPage = () => {
|
||||
return pages[pages.length - 1]
|
||||
}
|
||||
|
||||
export const getPath = (url: string) => {
|
||||
if (typeof url === 'string') {
|
||||
return url.split('?')[0].replace(/^pages/, '/pages')
|
||||
}
|
||||
throw new Error('please input string type')
|
||||
}
|
||||
|
||||
/** 判断当前页面是否是tabbar页 */
|
||||
export const getIsTabbar = () => {
|
||||
export const getIsTabbar = (url?: string) => {
|
||||
if (!tabBar) {
|
||||
return false
|
||||
}
|
||||
@@ -18,29 +25,14 @@ export const getIsTabbar = () => {
|
||||
// 通常有tabBar的话,list不能有空,且至少有2个元素,这里其实不用处理
|
||||
return false
|
||||
}
|
||||
const lastPage = getLastPage()
|
||||
const currPath = lastPage.route
|
||||
return !!tabBar.list.find((e) => e.pagePath === currPath)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前页面路由的 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)
|
||||
if (url) {
|
||||
console.log(tabBar.list, getPath(url))
|
||||
return !!tabBar.list.some((e) => getPath(e.pagePath) === getPath(url))
|
||||
} else {
|
||||
const lastPage = getLastPage()
|
||||
const currPath = lastPage.route
|
||||
return !!tabBar.list.some((e) => e.pagePath === currPath)
|
||||
}
|
||||
}
|
||||
|
||||
const ensureDecodeURIComponent = (url: string) => {
|
||||
@@ -74,14 +66,17 @@ export const getUrlObj = (url: string) => {
|
||||
}
|
||||
/**
|
||||
* 得到所有的需要登录的pages,包括主包和分包的
|
||||
* 这里设计得通用一点,可以传递key作为判断依据,默认是 needLogin, 与 route-block 配对使用
|
||||
* 这里设计得通用一点,可以传递key作为判断依据,默认是 notNeedLogin, 与 route-block 配对使用
|
||||
* 如果没有传 key,则表示所有的pages,如果传递了 key, 则表示通过 key 过滤
|
||||
*/
|
||||
export const getAllPages = (key = 'needLogin') => {
|
||||
export const getAllPages = (key = 'notNeedLogin') => {
|
||||
// 这里处理主包
|
||||
const mainPages = [
|
||||
...pages
|
||||
.filter((page) => !key || page[key])
|
||||
.filter((page) => {
|
||||
if (!key) return false
|
||||
return page[key] === false
|
||||
})
|
||||
.map((page) => ({
|
||||
...page,
|
||||
path: `/${page.path}`,
|
||||
@@ -111,39 +106,21 @@ export const getAllPages = (key = 'needLogin') => {
|
||||
* 得到所有的需要登录的pages,包括主包和分包的
|
||||
* 只得到 path 数组
|
||||
*/
|
||||
export const getNeedLoginPages = (): string[] => getAllPages('needLogin').map((page) => page.path)
|
||||
export const getNotNeedLoginPages = (): string[] =>
|
||||
getAllPages('notNeedLogin').map((page) => page.path)
|
||||
|
||||
/**
|
||||
* 得到所有的需要登录的pages,包括主包和分包的
|
||||
* 只得到 path 数组
|
||||
*/
|
||||
export const needLoginPages: string[] = getAllPages('needLogin').map((page) => page.path)
|
||||
export const notNeedLoginPages: string[] = getAllPages('notNeedLogin').map((page) => page.path)
|
||||
|
||||
/**
|
||||
* 根据微信小程序当前环境,判断应该获取的BaseUrl
|
||||
*/
|
||||
export const getEnvBaseUrl = () => {
|
||||
// 请求基准地址
|
||||
let baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
||||
|
||||
// 小程序端环境区分
|
||||
if (isMp) {
|
||||
const {
|
||||
miniProgram: { envVersion },
|
||||
} = uni.getAccountInfoSync()
|
||||
|
||||
switch (envVersion) {
|
||||
case 'develop':
|
||||
baseUrl = 'https://ukw0y1.laf.run'
|
||||
break
|
||||
case 'trial':
|
||||
baseUrl = 'https://ukw0y1.laf.run'
|
||||
break
|
||||
case 'release':
|
||||
baseUrl = 'https://ukw0y1.laf.run'
|
||||
break
|
||||
}
|
||||
}
|
||||
const baseUrl = import.meta.env.VITE_SERVER_BASEURL
|
||||
|
||||
return baseUrl
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user