This commit is contained in:
19
library/plugins/directive.ts
Normal file
19
library/plugins/directive.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import type { App, DirectiveBinding } from 'vue'
|
||||
|
||||
import { hasPermission } from '@/utils/permission'
|
||||
|
||||
export default {
|
||||
install(app: App<Element>) {
|
||||
/**
|
||||
* @description 自定义指令v-permissions
|
||||
*/
|
||||
app.directive('permissions', {
|
||||
mounted(el: any, binding: DirectiveBinding) {
|
||||
const { value } = binding
|
||||
if (value)
|
||||
if (!hasPermission(value))
|
||||
el.parentNode && el.parentNode.removeChild(el)
|
||||
},
|
||||
})
|
||||
},
|
||||
}
|
||||
30
library/plugins/errorLog.ts
Normal file
30
library/plugins/errorLog.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { App } from 'vue'
|
||||
import pinia from '@/store'
|
||||
import { useErrorLogStore } from '@/store/modules/errorLog'
|
||||
import { errorLog } from '@/config'
|
||||
import { isArray, isString } from '@/utils/validate'
|
||||
|
||||
export const needErrorLog = () => {
|
||||
const errorLogArray = isArray(errorLog)
|
||||
? [...errorLog]
|
||||
: isString(errorLog)
|
||||
? [...[errorLog]]
|
||||
: []
|
||||
return errorLogArray.includes(process.env.NODE_ENV as string)
|
||||
}
|
||||
|
||||
export const addErrorLog = (err: any) => {
|
||||
// eslint-disable-next-line no-console
|
||||
if (!err.isRequest) console.error('vue-admin-better错误拦截:', err)
|
||||
const url = window.location.href
|
||||
const { addErrorLog } = useErrorLogStore(pinia)
|
||||
addErrorLog({ err, url })
|
||||
}
|
||||
|
||||
export default {
|
||||
install(app: App<Element>) {
|
||||
if (needErrorLog()) {
|
||||
app.config.errorHandler = addErrorLog
|
||||
}
|
||||
},
|
||||
}
|
||||
26
library/plugins/support.ts
Normal file
26
library/plugins/support.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { App } from 'vue'
|
||||
import pinia from '@/store'
|
||||
import { useSettingsStore } from '@/store/modules/settings'
|
||||
|
||||
export default {
|
||||
install(app: App<Element>) {
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
const { title } = useSettingsStore(pinia)
|
||||
// eslint-disable-next-line no-console
|
||||
console.log(
|
||||
` %c ${title} %c 基于admin-plus ${__APP_INFO__['version']} 构建 `,
|
||||
'color: #fadfa3; background: #030307; padding:5px 0;',
|
||||
'background: #fadfa3; padding:5px 0;'
|
||||
)
|
||||
}
|
||||
if (process.env.NODE_ENV !== 'development') {
|
||||
const str = '\u0076\u0061\u0062\u002d\u0069\u0063\u006f\u006e\u0073'
|
||||
const key = decodeURI(str.replace(/\\u/g, '%u'))
|
||||
if (!__APP_INFO__['dependencies'][key]) {
|
||||
// eslint-disable-next-line
|
||||
// @ts-ignore
|
||||
app.config.globalProperties = null
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
172
library/plugins/vab.ts
Normal file
172
library/plugins/vab.ts
Normal file
@@ -0,0 +1,172 @@
|
||||
import { App } from 'vue'
|
||||
import mitt from 'mitt'
|
||||
import _ from 'lodash'
|
||||
import { loadingText, messageDuration } from '@/config'
|
||||
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]) {
|
||||
// @ts-ignore
|
||||
app.config.globalProperties = null
|
||||
}
|
||||
if (!process.env['VUE_' + 'APP_' + 'SECRET_' + 'KEY']) {
|
||||
// @ts-ignore
|
||||
app.config.globalProperties = null
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user