buddhism/utils/router.js

195 lines
4.0 KiB
JavaScript
Raw Normal View History

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