"use strict"
* 插件
*/
import { LoadingPlugin, DialogPlugin, MessagePlugin } from "tdesign-vue-next"
* @typedef { object } MyPluginObj 全局对象
* @property { TDesign.LoadingInstance | null } loadingInstance Loading实例
* @property { TDesign.DialogInstance | null } dialogInstance Dialog实例
*/
* 全局对象
* 为防止内存泄漏,全局只有一个Loading实例、Dialog实例
* 通过import导入时,会全局共享同一个实例
* @type { MyPluginObj } 全局对象
*/
const myPluginObj = {
loadingInstance: null,
dialogInstance: null
}
* “加载中”
* @param { string | false } [text = "加载中..."] 文案
* @note 会读写全局对象myPluginObj.loadingInstance
*/
export function myLoading(text = "加载中...") {
myPluginObj.loadingInstance?.hide()
if (text !== false) {
myPluginObj.loadingInstance = LoadingPlugin({
delay: 0,
fullscreen: true,
indicator: true,
inheritColor: false,
loading: true,
preventScrollThrough: true,
showOverlay: true,
size: "large",
text: text,
zIndex: 3500
})
}
}
* 对话框
* @param { MyDialogParam | string } paramObj 参数对象
* @typedef { object } MyDialogParam
* @property { "default" | "info" | "warning" | "danger" | "success" } [MyDialogParam.theme = "info"] 对话框风格
* @property { string } [MyDialogParam.header] 标题
* @property { string } [MyDialogParam.body] 内容
* @property { string } [MyDialogParam.confirmBtn] 确认按钮文字
* @property { string } [MyDialogParam.cancelBtn = null] 取消按钮文字
* @property { Function } [MyDialogParam.onConfirmCallBack = () => { }] 确认回调
* @note 会读写全局对象myPluginObj.loadingInstance
*/
export function myDialog(paramObj) {
myPluginObj.dialogInstance?.destroy()
if (typeof paramObj === "string") {
paramObj = { body: paramObj }
}
const {
theme = "info",
header,
body,
confirmBtn = undefined,
onConfirmCallBack = () => { },
} = paramObj
const cancelBtn = paramObj.onConfirmCallBack
? paramObj.cancelBtn
: null
myPluginObj.dialogInstance = DialogPlugin({
mode: "modal",
placement: "center",
theme: theme,
closeBtn: false,
destroyOnClose: true,
header: header,
body: body,
footer: true,
confirmBtn: confirmBtn,
confirmLoading: false,
confirmOnEnter: true,
cancelBtn: cancelBtn,
onConfirm: () => {
onConfirmCallBack()
myPluginObj.dialogInstance?.destroy()
},
})
}
* 提示框
* @param { MyMessageParam | string } paramObj 参数对象
* @typedef { object } MyMessageParam
* @property { "info" | "success" | "warning" | "error" | "loading" } [MyMessageParam.theme = "info"] 对话框风格
* @property { boolean } [MyMessageParam.closeBtn = false] 是否显示关闭按钮
* @property { string } MyMessageParam.content 内容
* @property { number } [MyMessageParam.duration = 1500] 显示时长
*/
export function myMessage(paramObj) {
if (typeof paramObj === "string") {
paramObj = { content: paramObj }
}
const {
theme = "info",
closeBtn = false,
content,
duration = 1500
} = paramObj
MessagePlugin(
theme,
{
closeBtn: closeBtn,
content: content,
icon: true,
placement: "center",
},
duration
)
}
* 报错处理方法
* @param { string } errorText 报错文案
* @param { Error } errorObj 报错对象
* @param { Function } [callBack] 回调函数
*/
function myError(errorText = "程序报错", errorObj, callBack) {
console.log(errorText, errorObj)
if (callBack) {
callBack(errorObj)
} else {
throw new Error(errorText, { cause: errorObj })
}
}
* 声明一个等待方法
* @param { number } ms 等待时间,单位为毫秒
*/
async function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms)
})
}
* 默认导出。这里的全局导出会把“my”前缀去掉
* @prop { Function } loading 全局加载
* @prop { Function } dialog 全局对话框
* @prop { Function } message 全局提示
*/
export default {
loading: myLoading,
dialog: myDialog,
message: myMessage,
error: myError,
}