From 14aa108c393a5c25ab2e16c7cc72115908e197a1 Mon Sep 17 00:00:00 2001 From: lqc <15342622+aq982@user.noreply.gitee.com> Date: Sat, 8 Mar 2025 10:04:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=9F=BA=E4=BA=8EElement=20P?= =?UTF-8?q?lus=20ElLoading=E7=9A=84=E6=B5=81=E7=A8=8B=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=99=A8=EF=BC=8C=E6=94=AF=E6=8C=81=E9=93=BE?= =?UTF-8?q?=E5=BC=8F=E6=93=8D=E4=BD=9C=E5=92=8C=E5=8A=A8=E6=80=81=E6=96=87?= =?UTF-8?q?=E6=9C=AC=E6=9B=B4=E6=96=B0=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=A4=8D?= =?UTF-8?q?=E6=9D=82=E8=BF=9E=E7=BB=AD=E7=9A=84=E6=93=8D=E4=BD=9C=E6=B5=81?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Web/src/utils/flowLoading.ts | 105 +++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 Web/src/utils/flowLoading.ts diff --git a/Web/src/utils/flowLoading.ts b/Web/src/utils/flowLoading.ts new file mode 100644 index 00000000..7b967eea --- /dev/null +++ b/Web/src/utils/flowLoading.ts @@ -0,0 +1,105 @@ +// utils/flowLoading.ts +/** + * @author easycreatecode.com + * @created 2025-03-05 + * @updated 2025-03-08 + * @version 1.2.0 + * @description 基于Element Plus的流程加载控制器,支持链式操作和动态文本更新,如果使用请保留作者信息 + */ +import { ElLoading } from "element-plus" +import type { Component, Ref } from "vue" +import { isRef, unref } from "vue" +import type { LoadingOptions } from 'element-plus/es/components/loading/src/types' + +// 1. 明确控制器类型接口 +interface LoadingController { + step: (text: string | Ref) => LoadingController + end: () => void +} + +type CustomLoadingOptions = Omit & { + text?: string | Ref + spinner?: string | Component +} + +const DEFAULT_CONFIG: CustomLoadingOptions = { + lock: true, + text: '', + //background: 'rgba(0, 199, 255, 0.3)', + background: 'rgba(94, 158, 214, 0.12)', // 景德镇青花色 + //spinner: 'circle-check' + //spinner: 'el-icon-potato', + spinner: 'el-icon-loading', +} + +class FlowLoadingManager { + private instance: ReturnType | null = null + + // 2. 统一返回类型 + start(options?: string | Ref | CustomLoadingOptions): LoadingController { + try { + if (this.instance) { + console.warn('Existing loading instance detected') + return this.createController() + } + + const resolved = this.normalizeOptions(options) + this.validateConfig(resolved) + + this.instance = ElLoading.service(resolved as LoadingOptions) + return this.createController() + } catch (e) { + console.error('Loading initialization failed:', e) + return this.createController(true) // 传入错误标记 + } + } + + private normalizeOptions(options?: string | Ref | CustomLoadingOptions): CustomLoadingOptions { + const baseConfig = { ...DEFAULT_CONFIG } + + if (!options) return baseConfig + + if (typeof options === 'string' || isRef(options)) { + return { + ...baseConfig, + text: unref(options) + } + } + + return { + ...baseConfig, + ...options, + text: options.text ? unref(options.text) : baseConfig.text + } + } + + private validateConfig(config: CustomLoadingOptions) { + if (config.spinner && typeof config.spinner !== 'string') { + console.warn('Custom spinner components require proper registration') + } + + if (typeof config.text !== 'string') { + throw new Error('Loading text must be a string') + } + } + + // 3. 增强控制器创建方法 + private createController(isError = false): LoadingController { + return { + step: (text: string | Ref) => { + if (!isError && this.instance) { + this.instance.setText(unref(text)) + } + return this.createController(isError) // 保持链式调用 + }, + end: () => { + if (!isError) { + this.instance?.close() + } + this.instance = null + } + } + } +} + +export const flowLoading = new FlowLoadingManager() \ No newline at end of file