goview_vue/src/utils/plugin.ts

131 lines
3.2 KiB
TypeScript
Raw Normal View History

2021-12-20 14:29:29 +08:00
import { icon } from '@/plugins'
2021-12-20 17:38:44 +08:00
import { DialogEnum } from '@/enums/pluginEnum'
2022-01-15 14:56:48 +08:00
import { dialogIconSize } from '@/settings/designSetting'
2022-01-15 16:07:46 +08:00
import { maskClosable } from '@/settings/designSetting'
2021-12-20 17:38:44 +08:00
import { DialogReactive } from 'naive-ui'
2021-12-20 14:29:29 +08:00
const { InformationCircleIcon } = icon.ionicons5
import { renderIcon } from '@/utils'
2022-01-27 23:16:51 +08:00
// * 开启加载
export const loadingStart = () => {
window['$loading'].start()
}
// * 加载结束
export const loadingFinish = () => {
setTimeout(() => {
window['$loading'].finish()
})
}
// * 加载错误
export const loadingError = () => {
setTimeout(() => {
window['$loading'].error()
})
}
2021-12-20 14:29:29 +08:00
/**
2021-12-20 17:38:44 +08:00
* * render
2021-12-20 14:29:29 +08:00
* @param { Object} params
* @param { Function } dialogFn
2022-01-23 19:22:54 +08:00
* ```
2022-04-10 17:40:43 +08:00
* demo
2022-01-23 19:22:54 +08:00
* goDialog({
* onPositiveCallback: () => {}
* })
* ```
2021-12-20 14:29:29 +08:00
*/
2021-12-20 17:38:44 +08:00
export const goDialog = (
2021-12-20 14:29:29 +08:00
params: {
// 基本
type?: DialogEnum
2022-01-09 19:22:55 +08:00
// 提示
2021-12-20 14:29:29 +08:00
message?: string
2022-04-10 17:40:43 +08:00
// 取消提示词
negativeText?: string
// 取消按钮的属性
negativeButtonProps?: object,
// 确定提示词
positiveText?: string
// 确定按钮的属性
positiveButtonProps?: object,
2022-01-09 19:22:55 +08:00
// 点击遮罩是否关闭
isMaskClosable?: boolean
2021-12-20 17:38:44 +08:00
// 回调
onPositiveCallback: Function
2021-12-20 14:29:29 +08:00
onNegativeCallback?: Function
2021-12-20 17:38:44 +08:00
// 异步
promise?: boolean
promiseResCallback?: Function
promiseRejCallback?: Function
},
dialogFn?: Function
2021-12-20 14:29:29 +08:00
) => {
2021-12-20 17:38:44 +08:00
const {
type,
message,
2022-04-10 17:40:43 +08:00
negativeText,
negativeButtonProps,
positiveText,
positiveButtonProps,
2022-01-09 19:22:55 +08:00
isMaskClosable,
2021-12-20 17:38:44 +08:00
onPositiveCallback,
onNegativeCallback,
promise,
promiseResCallback,
2022-04-10 17:40:43 +08:00
promiseRejCallback
2021-12-20 17:38:44 +08:00
} = params
const typeObj = {
// 自定义
2022-02-09 20:12:54 +08:00
[DialogEnum.delete]: {
2021-12-20 17:38:44 +08:00
fn: dialogFn || window['$dialog'].warning,
2022-04-10 17:40:43 +08:00
message: message || '是否删除此数据?'
2021-12-20 17:38:44 +08:00
},
// 原有
2022-02-09 20:12:54 +08:00
[DialogEnum.warning]: {
2021-12-20 17:38:44 +08:00
fn: window['$dialog'].warning,
2022-04-10 17:40:43 +08:00
message: message || '是否执行此操作?'
2021-12-20 17:38:44 +08:00
},
2022-02-09 20:12:54 +08:00
[DialogEnum.error]: {
2021-12-20 17:38:44 +08:00
fn: window['$dialog'].error,
2022-04-10 17:40:43 +08:00
message: message || '是否执行此操作?'
2021-12-20 17:38:44 +08:00
},
2022-02-09 20:12:54 +08:00
[DialogEnum.success]: {
2021-12-20 17:38:44 +08:00
fn: window['$dialog'].success,
2022-04-10 17:40:43 +08:00
message: message || '是否执行此操作?'
}
2021-12-20 14:29:29 +08:00
}
2021-12-20 17:38:44 +08:00
2022-02-09 20:12:54 +08:00
const d: DialogReactive = typeObj[type || DialogEnum.warning]['fn']({
2021-12-20 14:29:29 +08:00
title: '提示',
icon: renderIcon(InformationCircleIcon, { size: dialogIconSize }),
2022-02-09 20:12:54 +08:00
content: typeObj[type || DialogEnum.warning]['message'],
2022-04-10 17:40:43 +08:00
positiveText: positiveText || '确定',
positiveButtonProps: positiveButtonProps,
negativeText: negativeText || '取消',
negativeButtonProps: negativeButtonProps,
// 是否通过遮罩关闭
2022-01-09 19:22:55 +08:00
maskClosable: isMaskClosable || maskClosable,
2021-12-20 17:38:44 +08:00
onPositiveClick: async () => {
// 执行异步
if (promise && onPositiveCallback) {
d.loading = true
try {
const res = await onPositiveCallback()
promiseResCallback && promiseResCallback(res)
} catch (err) {
promiseRejCallback && promiseRejCallback(err)
}
d.loading = false
return
}
onPositiveCallback && onPositiveCallback(d)
2021-12-20 14:29:29 +08:00
},
2021-12-20 17:38:44 +08:00
onNegativeClick: async () => {
onNegativeCallback && onNegativeCallback(d)
2022-04-10 17:40:43 +08:00
}
2021-12-20 14:29:29 +08:00
})
2021-12-20 17:38:44 +08:00
}