140 lines
3.3 KiB
JavaScript
140 lines
3.3 KiB
JavaScript
|
|
/**
|
|||
|
|
* 统一的页面跳转工具
|
|||
|
|
* 自动判断是否为 tabBar 页面,使用正确的跳转方法
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
// tabBar 页面列表
|
|||
|
|
const TAB_BAR_PAGES = [
|
|||
|
|
'/pages/index/index',
|
|||
|
|
'/pages/service/list',
|
|||
|
|
'/pages/booking/quick-booking',
|
|||
|
|
'/pages/order/list',
|
|||
|
|
'/pages/user/index'
|
|||
|
|
]
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 智能跳转页面
|
|||
|
|
* @param {String} url - 页面路径(可以包含参数)
|
|||
|
|
* @param {Object} options - 额外选项
|
|||
|
|
* @param {String} options.method - 强制使用的跳转方法:navigateTo, redirectTo, switchTab, reLaunch
|
|||
|
|
*/
|
|||
|
|
export function navigateTo(url, options = {}) {
|
|||
|
|
// 提取路径和参数
|
|||
|
|
const [path, queryString] = url.split('?')
|
|||
|
|
|
|||
|
|
// 判断是否为 tabBar 页面
|
|||
|
|
const isTabBar = TAB_BAR_PAGES.includes(path)
|
|||
|
|
|
|||
|
|
if (isTabBar) {
|
|||
|
|
// tabBar 页面不支持传参,需要使用本地存储
|
|||
|
|
if (queryString) {
|
|||
|
|
console.warn(`[Navigation] tabBar 页面不支持传参: ${url}`)
|
|||
|
|
console.warn('[Navigation] 请使用 navigateToTabBar 方法并传递 params 对象')
|
|||
|
|
|
|||
|
|
// 解析参数
|
|||
|
|
const params = {}
|
|||
|
|
queryString.split('&').forEach(item => {
|
|||
|
|
const [key, value] = item.split('=')
|
|||
|
|
params[decodeURIComponent(key)] = decodeURIComponent(value)
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
// 保存到本地存储
|
|||
|
|
const storageKey = getStorageKey(path)
|
|||
|
|
uni.setStorageSync(storageKey, params)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 使用 switchTab 跳转
|
|||
|
|
uni.switchTab({ url: path })
|
|||
|
|
} else {
|
|||
|
|
// 普通页面使用 navigateTo
|
|||
|
|
const method = options.method || 'navigateTo'
|
|||
|
|
uni[method]({ url })
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 跳转到 tabBar 页面(支持传参)
|
|||
|
|
* @param {String} path - tabBar 页面路径
|
|||
|
|
* @param {Object} params - 参数对象
|
|||
|
|
*/
|
|||
|
|
export function navigateToTabBar(path, params = {}) {
|
|||
|
|
if (!TAB_BAR_PAGES.includes(path)) {
|
|||
|
|
console.error(`[Navigation] ${path} 不是 tabBar 页面`)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 保存参数到本地存储
|
|||
|
|
if (Object.keys(params).length > 0) {
|
|||
|
|
const storageKey = getStorageKey(path)
|
|||
|
|
uni.setStorageSync(storageKey, params)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 跳转
|
|||
|
|
uni.switchTab({ url: path })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 在 tabBar 页面中读取参数
|
|||
|
|
* @param {String} path - 当前页面路径
|
|||
|
|
* @returns {Object} 参数对象
|
|||
|
|
*/
|
|||
|
|
export function getTabBarParams(path) {
|
|||
|
|
const storageKey = getStorageKey(path)
|
|||
|
|
const params = uni.getStorageSync(storageKey)
|
|||
|
|
|
|||
|
|
// 读取后清除
|
|||
|
|
if (params) {
|
|||
|
|
uni.removeStorageSync(storageKey)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return params || {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 获取存储键名
|
|||
|
|
* @param {String} path - 页面路径
|
|||
|
|
* @returns {String} 存储键名
|
|||
|
|
*/
|
|||
|
|
function getStorageKey(path) {
|
|||
|
|
// 将路径转换为存储键名
|
|||
|
|
// /pages/service/list -> serviceListParams
|
|||
|
|
// /pages/booking/quick-booking -> bookingQuickBookingParams
|
|||
|
|
const parts = path.split('/').filter(p => p && p !== 'pages')
|
|||
|
|
const key = parts.map((p, i) => {
|
|||
|
|
if (i === 0) return p
|
|||
|
|
return p.charAt(0).toUpperCase() + p.slice(1).replace(/-([a-z])/g, (m, c) => c.toUpperCase())
|
|||
|
|
}).join('') + 'Params'
|
|||
|
|
|
|||
|
|
return key
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 重定向
|
|||
|
|
*/
|
|||
|
|
export function redirectTo(url) {
|
|||
|
|
navigateTo(url, { method: 'redirectTo' })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 重启应用
|
|||
|
|
*/
|
|||
|
|
export function reLaunch(url) {
|
|||
|
|
uni.reLaunch({ url })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 返回
|
|||
|
|
*/
|
|||
|
|
export function navigateBack(delta = 1) {
|
|||
|
|
uni.navigateBack({ delta })
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
export default {
|
|||
|
|
navigateTo,
|
|||
|
|
navigateToTabBar,
|
|||
|
|
getTabBarParams,
|
|||
|
|
redirectTo,
|
|||
|
|
reLaunch,
|
|||
|
|
navigateBack
|
|||
|
|
}
|