ai-manus/chat-client/library/plugins/vab.ts

175 lines
5.4 KiB
TypeScript
Raw Normal View History

2025-07-18 16:38:18 +08:00
import { App } from 'vue'
import { loadingText, messageDuration } from '@/config'
import mitt from 'mitt'
import _ from 'lodash'
import { globalPropertiesType } from '/#/library'
export let gp: globalPropertiesType
export default {
install(app: App<Element>) {
gp = {
/**
* @description
* @param {number} index ID
* @param {string} text
*/
$baseLoading: (index = undefined, text = loadingText) => {
return ElLoading.service({
lock: true,
text,
spinner: index ? `vab-loading-type${index}` : index,
background: 'hsla(0,0%,100%,.8)',
})
},
/**
* @description Message
* @param {string} message
* @param {'success'|'warning'|'info'|'error'} type
* @param {string} customClass
* @param {boolean} dangerouslyUseHTMLString message属性作为HTML片段处理
*/
$baseMessage: (
message,
type = 'info',
customClass,
dangerouslyUseHTMLString
) => {
ElMessage({
message,
type,
customClass,
duration: messageDuration,
dangerouslyUseHTMLString,
showClose: true,
})
},
/**
* @description Alert
* @param {string|VNode} content
* @param {string} title
* @param {function} callback 使Promise,使MessageBox关闭后的回调
*/
$baseAlert: (content, title = '温馨提示', callback = undefined) => {
if (title && typeof title == 'function') {
callback = title
title = '温馨提示'
}
ElMessageBox.alert(content, title, {
confirmButtonText: '确定',
dangerouslyUseHTMLString: true, // 此处可能引起跨站攻击建议配置为false
callback: () => {
if (callback) callback()
},
}).then(() => {})
},
/**
* @description Confirm
* @param {string|VNode} content
* @param {string} title
* @param {function} callback1
* @param {function} callback2
* @param {string} confirmButtonText
* @param {string} cancelButtonText
*/
$baseConfirm: (
content,
title,
callback1,
callback2,
confirmButtonText = '确定',
cancelButtonText = '取消'
) => {
ElMessageBox.confirm(content, title || '温馨提示', {
confirmButtonText,
cancelButtonText,
closeOnClickModal: false,
type: 'warning',
lockScroll: false,
})
.then(() => {
if (callback1) {
callback1()
}
})
.catch(() => {
if (callback2) {
callback2()
}
})
},
/**
* @description Notification
* @param {string} message
* @param {string} title
* @param {'success'|'warning'|'info'|'error'} type ,
* @param {'top-right'|'top-left'|'bottom-right'|'bottom-left'} position
* @param duration ,
*/
$baseNotify: (
message,
title,
type = 'success',
position = 'top-right',
duration = messageDuration
) => {
ElNotification({
title,
message,
type,
duration,
position,
})
},
/**
* @description
* @param {*} formType
*/
$baseTableHeight: (formType) => {
let height = window.innerHeight
const paddingHeight = 291
const formHeight = 60
if ('number' === typeof formType) {
height = height - paddingHeight - formHeight * formType
} else {
height = height - paddingHeight
}
return height
},
$pub: (...args: any[]) => {
_emitter.emit(_.head(args), args[1])
},
$sub: function () {
// eslint-disable-next-line prefer-rest-params
Reflect.apply(_emitter.on, _emitter, _.toArray(arguments))
},
$unsub: function () {
// eslint-disable-next-line prefer-rest-params
Reflect.apply(_emitter.off, _emitter, _.toArray(arguments))
},
}
const _emitter = mitt()
Object.keys(gp).forEach((key) => {
app.provide(key, gp[key as keyof typeof gp])
// 允许vue3下继续使用vue2中的this调用vab方法
app.config.globalProperties[key] = gp[key as keyof typeof gp]
})
if (process.env['NODE_' + 'ENV'] !== `${'deve' + 'lopme' + 'nt'}`) {
const key = 'vab-' + 'icons'
if (!__APP_INFO__['dependencies'][key]) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app.config.globalProperties = null
}
if (!process.env['VUE_' + 'APP_' + 'SECRET_' + 'KEY']) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
app.config.globalProperties = null
}
}
},
}