This commit is contained in:
weicw
2021-07-29 16:17:26 +08:00
commit a6eb6f83d1
127 changed files with 60792 additions and 0 deletions

View File

@@ -0,0 +1,92 @@
/**
* 页签操作封装
*/
import {unref} from 'vue';
import store from '../store';
import router from '../router';
/**
* 刷新当前页签
*/
export function reloadPageTab() {
const {path, query, matched} = unref(router.currentRoute);
const components = new Set();
matched.forEach((m) => {
if (m.components && m.components.default && m.components.default.name) {
if (!['EleEmptyLayout', 'EleLayout'].includes(m.components.default.name)) {
components.add(m.components.default.name);
}
}
});
return store.dispatch('theme/setKeepAliveExclude', Array.from(components)).then(() => {
return router.replace({
query: query,
path: '/redirect' + path
});
});
}
/**
* 关闭当前tab
*/
export function finishPageTab() {
return store.dispatch('theme/tabRemove', router.currentRoute.fullPath).then(({lastPath}) => {
return router.push(lastPath || '/');
});
}
/**
* 移除指定tab
* @param key {String}
* @returns {Promise<Object>}
*/
export function removePageTab(key) {
return store.dispatch('theme/tabRemove', key);
}
/**
* 移除所有tab
*/
export function removeAllPageTab() {
return store.dispatch('theme/tabRemoveAll');
}
/**
* 移除左侧tab
* @param key {String}
*/
export function removeLeftPageTab(key) {
return store.dispatch('theme/tabRemoveLeft', key);
}
/**
* 移除右侧tab
* @param key {String}
*/
export function removeRightPageTab(key) {
return store.dispatch('theme/tabRemoveRight', key);
}
/**
* 移除其他tab
* @param key {String}
*/
export function removeOtherPageTab(key) {
return store.dispatch('theme/tabRemoveOther', key);
}
/**
* 添加tab
* @param obj
*/
export function addPageTab(obj) {
return store.dispatch('theme/tabAdd', obj);
}
/**
* 修改指定tab
* @param obj
*/
export function setPageTab(obj) {
return store.dispatch('theme/tabSetTitle', obj);
}

128
src/utils/permission.js Normal file
View File

@@ -0,0 +1,128 @@
/**
* 权限、角色控制组件
*/
import store from '@/store';
export default {
install(app) {
// 添加全局方法
app.config.globalProperties.$hasRole = this.hasRole;
app.config.globalProperties.$hasAnyRole = this.hasAnyRole;
app.config.globalProperties.$hasPermission = this.hasPermission;
app.config.globalProperties.$hasAnyPermission = this.hasAnyPermission;
// 添加自定义指令
app.directive('role', {
mounted: (el, binding) => {
if (!this.hasRole(binding.value)) {
el.parentNode && el.parentNode.removeChild(el);
}
}
});
app.directive('any-role', {
mounted: (el, binding) => {
if (!this.hasAnyRole(binding.value)) {
el.parentNode && el.parentNode.removeChild(el);
}
}
});
app.directive('permission', {
mounted: (el, binding) => {
if (!this.hasPermission(binding.value)) {
el.parentNode && el.parentNode.removeChild(el);
}
}
});
app.directive('any-permission', {
mounted: (el, binding) => {
if (!this.hasAnyPermission(binding.value)) {
el.parentNode && el.parentNode.removeChild(el);
}
}
});
},
/**
* 是否有某些角色
* @param role {String, Array<String>} 角色字符或字符数组
* @returns {boolean}
*/
hasRole(role) {
const data = store.state.user ? store.state.user.roles : null;
return arrayHas(data, role);
},
/**
* 是否有任意角色
* @param role {String, Array<String>} 角色字符或字符数组
* @returns {boolean}
*/
hasAnyRole(role) {
const data = store.state.user ? store.state.user.roles : null;
return arrayHasAny(data, role);
},
/**
* 是否有某些权限
* @param auth {String, Array<String>} 权限字符或字符数组
* @returns {boolean}
*/
hasPermission(auth) {
const data = store.state.user ? store.state.user.authorities : null;
return arrayHas(data, auth);
},
/**
* 是否有任意权限
* @param auth {String, Array<String>} 权限字符或字符数组
* @returns {boolean}
*/
hasAnyPermission(auth) {
const data = store.state.user ? store.state.user.authorities : null;
return arrayHasAny(data, auth);
}
}
/**
* 数组是否有某些值
* @param array {Array<String>} 数组
* @param obj {String, Array<String>} 值
* @returns {boolean}
*/
function arrayHas(array, obj) {
if (!obj) {
return true;
}
if (!array) {
return false;
}
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (array.indexOf(obj[i]) === -1) {
return false;
}
}
return true;
}
return array.indexOf(obj) !== -1;
}
/**
* 数组是否有任意值
* @param array {Array<String>} 数组
* @param obj {String, Array<String>} 值
* @returns {boolean}
*/
function arrayHasAny(array, obj) {
if (!obj) {
return true;
}
if (!array) {
return false;
}
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
if (array.indexOf(obj[i]) !== -1) {
return true;
}
}
return false;
}
return array.indexOf(obj) !== -1;
}