Files

121 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

2025-11-13 10:36:23 +08:00
import { router } from "@/cool";
import type { ClConfirmAction, ClConfirmOptions, ClToastOptions } from "../types";
import { t } from "@/locale";
/**
* UiInstance
* - showConfirm: 显示确认弹窗的方法
* - showTips: 显示提示弹窗的方法
*/
export type UiInstance = {
/**
*
* @param options ClConfirmOptions
*/
showConfirm: (options: ClConfirmOptions) => void;
/**
*
* @param message
* @param callback
*/
showTips: (message: string, callback: (action: ClConfirmAction) => void) => void;
/**
*
* @param options ClToastOptions
*/
showToast: (options: ClToastOptions) => void;
};
/**
* UiInstance
* key: 当前页面路由
* value: UiInstance
*/
const list = new Map<string, UiInstance>();
/**
* Ui
*/
class Ui {
/**
* UiInstance
* @returns UiInstance | undefined
*/
getInstance() {
return list.get(router.path());
}
/**
*
* @param options ClConfirmOptions
*/
showConfirm(options: ClConfirmOptions): void {
const instance = this.getInstance();
if (instance != null) {
instance.showConfirm(options);
}
}
/**
*
* @param message
* @param callback
*/
showTips(message: string, callback: (action: ClConfirmAction) => void): void {
const instance = this.getInstance();
if (instance != null) {
instance.showTips(message, callback);
}
}
/**
*
* @param options ClToastOptions
*/
showToast(options: ClToastOptions): void {
const instance = this.getInstance();
if (instance != null) {
instance.showToast(options);
}
}
/**
*
* @param title
* @param mask
*/
showLoading(title: string | null = null, mask: boolean | null = null): void {
uni.showLoading({
title: title ?? t("加载中"),
mask: mask ?? true
});
}
/**
*
*/
hideLoading(): void {
uni.hideLoading();
}
}
/**
* Ui Ui
* @returns Ui
*/
const ui = new Ui();
export function useUi() {
return ui;
}
/**
* UiInstance
* @param instance UiInstance
*/
export function createUi(instance: UiInstance): void {
list.set(router.path(), instance);
}