This commit is contained in:
张成
2025-11-14 22:54:36 +08:00
parent 6b560da897
commit 0adab95d34
16 changed files with 171 additions and 42 deletions

115
src/utils/navigation.ts Normal file
View File

@@ -0,0 +1,115 @@
import Taro from "@tarojs/taro";
/**
* 导航工具函数 - 封装页面跳转,提供流畅的过渡效果
*/
interface NavigateOptions {
url: string;
success?: (res: any) => void;
fail?: (err: any) => void;
complete?: (res: any) => void;
}
/**
* 导航到新页面(带过渡动画)
* 使用 Taro.navigateTo系统会自动提供过渡动画
*/
export const navigateTo = (options: NavigateOptions): Promise<TaroGeneral.NavigateToSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).navigateTo({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};
/**
* 重定向到新页面(带过渡动画)
* 使用 Taro.redirectTo系统会自动提供过渡动画
*/
export const redirectTo = (options: NavigateOptions): Promise<TaroGeneral.NavigateToSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).redirectTo({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};
/**
* 返回上一页(带过渡动画)
*/
export const navigateBack = (options?: { delta?: number }): Promise<TaroGeneral.NavigateBackSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).navigateBack({
delta: options?.delta || 1,
success: (res: any) => {
resolve(res);
},
fail: (err: any) => {
reject(err);
},
});
});
};
/**
* 切换到 tabBar 页面(无过渡动画,这是 tabBar 的特性)
*/
export const switchTab = (options: { url: string }): Promise<TaroGeneral.SwitchTabSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
Taro.switchTab({
...options,
success: (res: any) => {
resolve(res);
},
fail: (err: any) => {
reject(err);
},
});
});
};
/**
* 重新加载当前页面
*/
export const reLaunch = (options: NavigateOptions): Promise<TaroGeneral.ReLaunchSuccessCallbackResult> => {
return new Promise((resolve, reject) => {
(Taro as any).reLaunch({
...options,
success: (res: any) => {
options.success?.(res);
resolve(res);
},
fail: (err: any) => {
options.fail?.(err);
reject(err);
},
complete: (res: any) => {
options.complete?.(res);
},
});
});
};