HomeLease/utils/router.js

192 lines
3.8 KiB
JavaScript
Raw Normal View History

2025-08-12 15:38:25 +08:00
/**
* 路由仓库
* 统一管理页面路由配置和跳转逻辑
*/
// 页面路由配置
export const PAGE_ROUTES = {
// 主要功能页面
MONK: '/pages/monk/monk',
MONK_DETAIL: '/pages/monk/monkDetail',
WALK_INTO: '/pages/walkInto/walkInto',
INSTITUTIONAL_STRUCTURE: '/pages/institutionalStructure/institutionalStructure',
ACTIVITY: '/pages/activity/activity',
PRAY: '/pages/pray/pray',
2025-08-13 11:05:40 +08:00
2025-08-12 15:38:25 +08:00
// 待开发页面
NEWS: '/pages/news/news',
ABBOT: '/pages/abbot/abbot',
ANCIENT: '/pages/ancient/ancient',
FUTURE: '/pages/future/future',
2025-08-13 11:05:40 +08:00
2025-08-12 15:38:25 +08:00
// 其他页面
LOGIN: '/pages/login/login',
INDEX: '/pages/nearbystores/index',
MY: '/pages/my/my',
MY_ORDER: '/pages/myorder/index',
2025-08-13 11:05:40 +08:00
MY_ORDER_RETURNED: '/pages/myorder/returned/index',
}
2025-08-12 15:38:25 +08:00
// 页面类型映射
export const PAGE_TYPE_MAP = {
2025-08-13 11:05:40 +08:00
monk: PAGE_ROUTES.MONK,
monkDetail: PAGE_ROUTES.MONK_DETAIL,
walkInto: PAGE_ROUTES.WALK_INTO,
institutionalStructure: PAGE_ROUTES.INSTITUTIONAL_STRUCTURE,
activity: PAGE_ROUTES.ACTIVITY,
news: PAGE_ROUTES.NEWS,
abbot: PAGE_ROUTES.ABBOT,
ancient: PAGE_ROUTES.ANCIENT,
future: PAGE_ROUTES.FUTURE,
index: PAGE_ROUTES.INDEX,
pray: PAGE_ROUTES.PRAY,
}
2025-08-12 15:38:25 +08:00
/**
* 页面跳转方法
* @param {string} pageType - 页面类型
* @param {Object} options - 跳转选项
*/
export function navigateToPage(pageType, options = {}) {
// 清除loading状态
try {
uni.hideLoading()
} catch (error) {
console.warn('清除loading失败:', error)
}
2025-08-13 11:05:40 +08:00
const targetPage = PAGE_TYPE_MAP[pageType]
2025-08-12 15:38:25 +08:00
if (!targetPage) {
// 使用uni.showToast替代console减少文件系统访问
uni.showToast({
title: '页面配置错误',
2025-08-13 11:05:40 +08:00
icon: 'none',
})
return
2025-08-12 15:38:25 +08:00
}
const defaultOptions = {
url: targetPage,
2025-08-13 11:05:40 +08:00
fail: err => {
2025-08-12 15:38:25 +08:00
// 避免在真机调试时输出过多日志
uni.showToast({
title: '页面开发中',
2025-08-13 11:05:40 +08:00
icon: 'none',
})
},
}
2025-08-12 15:38:25 +08:00
uni.navigateTo({
...defaultOptions,
2025-08-13 11:05:40 +08:00
...options,
})
2025-08-12 15:38:25 +08:00
}
/**
* 跳转到TabBar页面
* @param {string} pageType - 页面类型
*/
export function switchToTab(pageType) {
// 清除loading状态
try {
uni.hideLoading()
} catch (error) {
console.warn('清除loading失败:', error)
}
2025-08-13 11:05:40 +08:00
2025-08-12 15:38:25 +08:00
const tabRoutes = {
2025-08-13 11:05:40 +08:00
index: PAGE_ROUTES.INDEX,
my: PAGE_ROUTES.MY,
}
const targetPage = tabRoutes[pageType]
2025-08-12 15:38:25 +08:00
if (!targetPage) {
uni.showToast({
title: '页面配置错误',
2025-08-13 11:05:40 +08:00
icon: 'none',
})
return
2025-08-12 15:38:25 +08:00
}
uni.switchTab({
url: targetPage,
2025-08-13 11:05:40 +08:00
fail: err => {
2025-08-12 15:38:25 +08:00
uni.showToast({
title: '跳转失败',
2025-08-13 11:05:40 +08:00
icon: 'none',
})
},
})
2025-08-12 15:38:25 +08:00
}
/**
* 返回上一页
* @param {number} delta - 返回的页面数默认为1
*/
export function navigateBack(delta = 1) {
uni.navigateBack({
delta,
fail: () => {
// 如果没有上一页,跳转到首页
2025-08-13 11:05:40 +08:00
switchToTab('index')
},
})
2025-08-12 15:38:25 +08:00
}
/**
* 重定向到页面
* @param {string} pageType - 页面类型
*/
export function redirectToPage(pageType) {
// 清除loading状态
try {
uni.hideLoading()
} catch (error) {
console.warn('清除loading失败:', error)
}
2025-08-13 11:05:40 +08:00
const targetPage = PAGE_TYPE_MAP[pageType]
2025-08-12 15:38:25 +08:00
if (!targetPage) {
2025-08-13 11:05:40 +08:00
console.error('未知的页面类型:', pageType)
return
2025-08-12 15:38:25 +08:00
}
uni.redirectTo({
url: targetPage,
2025-08-13 11:05:40 +08:00
fail: err => {
console.error('页面重定向失败:', err)
},
})
2025-08-12 15:38:25 +08:00
}
/**
* 重新启动到页面
* @param {string} pageType - 页面类型
*/
export function reLaunchToPage(pageType) {
// 清除loading状态
try {
uni.hideLoading()
} catch (error) {
console.warn('清除loading失败:', error)
}
2025-08-13 11:05:40 +08:00
const targetPage = PAGE_TYPE_MAP[pageType]
2025-08-12 15:38:25 +08:00
if (!targetPage) {
2025-08-13 11:05:40 +08:00
console.error('未知的页面类型:', pageType)
return
2025-08-12 15:38:25 +08:00
}
uni.reLaunch({
url: targetPage,
2025-08-13 11:05:40 +08:00
fail: err => {
console.error('页面重启失败:', err)
},
})
}