CNCEC_APP/uni_modules/uview-pro/components/u-toast/service.ts

41 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2026-03-25 14:54:15 +08:00
/**
* u-toast
* @description
* - useToast() uni.$emit
* - <u-toast /> uni.$on show/hide
*/
import type { ToastProps } from './types';
// 普通页面级toast 事件
export const U_TOAST_EVENT_SHOW = 'uview-pro:u-toast:show:';
export const U_TOAST_EVENT_HIDE = 'uview-pro:u-toast:hide:';
// 全局App 根部toast 事件,供 useToast() 使用
export const U_TOAST_GLOBAL_EVENT_SHOW = 'uview-pro:u-toast:global:show';
export const U_TOAST_GLOBAL_EVENT_HIDE = 'uview-pro:u-toast:global:hide';
// 根据当前页面获取事件名
export function getEventWithCurrentPage(event: string, page?: string | boolean) {
if (page && typeof page === 'string' && page !== '') {
return event + page;
}
return event + getCurrentPage();
}
// 获取当前页面路由
function getCurrentPage() {
try {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1].route as string;
return currentPage || '';
} catch (error) {
return '';
}
}
export type ToastPayload = Partial<ToastProps> & {
/** 文案(兼容 toast.show('xxx') */
title?: string;
};