import store from '@/store' import { App } from 'vue' export default { install(app: App): any { /** * a标签重定向地址下载 * @param params { * url: '地址', * name: '名字' * } */ app.config.globalProperties.aFileDownLoad = function (params: any) { if (params.type === 'blob') { const x = new XMLHttpRequest() x.open('GET', params.url, true) x.responseType = 'blob' x.onload = function () { const url = window.URL.createObjectURL(x.response) const a = document.createElement('a') a.href = url a.download = params.name || '默认' a.click() } x.send() } if (params.type === 'more') { const iframe = document.createElement('iframe') !params.name && (params.name = 'default') iframe.style.display = 'none' iframe.src = params.url + `&download=true&downloadName=${encodeURIComponent( params.name )}` document.body.appendChild(iframe) iframe.click() } else { const a = document.createElement('a') !params.name && (params.name = 'default') a.href = params.url + `&download=true&downloadName=${encodeURIComponent( params.name )}` a.click() } } // 权限相关 start const _has = function (value) { let flag = false const btnRoles = store.state.userInfo.permissions if (btnRoles == undefined || btnRoles == null) { return false } if (btnRoles.indexOf(value) > -1) { flag = true } return flag } app.directive('btn', { mounted(el, binding: any) { const route:any = app.config.globalProperties.$route const value = (route.meta && route.meta.roles && route.meta.roles.btnRoles && route.meta.roles.btnRoles[ binding.value /* eslint-disable-line */ ]) || '' if (value) { if (!_has(value)) { el.style.opacity = '0.4' } } }, }) app.config.globalProperties.checkRole = function ( role, noMessage = false ) { const route:any = app.config.globalProperties.$route const btnRole = route.meta.roles && route.meta.roles.btnRoles && route.meta.roles.btnRoles[role] if (!_has(btnRole) && btnRole) { if (!noMessage) { window.$message.error('暂无权限') } return false } else { return true } } // 权限相关 end }, }