mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 按钮级权限复用 /auth/codes,新增 PermissionGate/v-permission 与操作列无权限整列隐藏
This commit is contained in:
@@ -9,6 +9,7 @@ import '@vben/styles/antd';
|
||||
|
||||
import { useTitle } from '@vueuse/core';
|
||||
|
||||
import { registerPermissionDirective } from '#/directives/permission';
|
||||
import { $t, setupI18n } from '#/locales';
|
||||
|
||||
import { initComponentAdapter } from './adapter/component';
|
||||
@@ -46,8 +47,9 @@ async function bootstrap(namespace: string) {
|
||||
// 配置 pinia-tore
|
||||
await initStores(app, { namespace });
|
||||
|
||||
// 安装权限指令
|
||||
// 安装权限指令(业务页请用 v-permission / PermissionGate,与后端 API 码一致)
|
||||
registerAccessDirective(app);
|
||||
registerPermissionDirective(app);
|
||||
|
||||
// 初始化 tippy
|
||||
const { initTippy } = await import('@vben/common-ui/es/tippy');
|
||||
|
||||
@@ -9,7 +9,7 @@ import { useAccessStore, useUserStore } from '@vben/stores';
|
||||
|
||||
import { message, Radio, Spin } from 'ant-design-vue';
|
||||
|
||||
import { getAccessCodesApi, switchCompanyApi } from '#/api/core/auth';
|
||||
import { switchCompanyApi } from '#/api/core/auth';
|
||||
import { getAvailableCompaniesApi } from '#/api/sys/user';
|
||||
import { useAuthStore } from '#/store';
|
||||
|
||||
@@ -78,13 +78,7 @@ async function handleSwitchCompany() {
|
||||
result.refreshTokenExpiresAt,
|
||||
);
|
||||
|
||||
try {
|
||||
const accessCodes = await getAccessCodesApi();
|
||||
accessStore.setAccessCodes(accessCodes);
|
||||
} catch (error) {
|
||||
console.warn('获取用户权限码失败:', error);
|
||||
accessStore.setAccessCodes([]);
|
||||
}
|
||||
await authStore.fetchAccessCodes();
|
||||
|
||||
await authStore.fetchUserInfo();
|
||||
accessStore.setIsAccessChecked(false);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
<script lang="ts" setup>
|
||||
import type { PermissionMatchMode } from '#/utils/permission-match';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { Tooltip } from 'ant-design-vue';
|
||||
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
|
||||
defineOptions({ name: 'PermissionGate' });
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
/** 本操作所需权限码(与接口 @RequirePermission 相同;与用户 accessCodes 比对) */
|
||||
code: string | string[];
|
||||
/** disable 时无权限提示 */
|
||||
disabledTip?: string;
|
||||
match?: PermissionMatchMode;
|
||||
/** hide:无权限不渲染;disable:保留占位并阻断点击 */
|
||||
mode?: 'disable' | 'hide';
|
||||
}>(),
|
||||
{
|
||||
disabledTip: '无操作权限',
|
||||
match: 'any',
|
||||
mode: 'hide',
|
||||
},
|
||||
);
|
||||
|
||||
const { hasPermission } = usePermission();
|
||||
|
||||
const allowed = computed(() => hasPermission(props.code, props.match));
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<template v-if="allowed">
|
||||
<slot />
|
||||
</template>
|
||||
<template v-else-if="mode === 'disable'">
|
||||
<Tooltip :title="disabledTip">
|
||||
<span
|
||||
class="inline-flex cursor-not-allowed opacity-50 [&_*]:pointer-events-none"
|
||||
>
|
||||
<slot />
|
||||
</span>
|
||||
</Tooltip>
|
||||
</template>
|
||||
</template>
|
||||
@@ -0,0 +1 @@
|
||||
export { default as PermissionGate } from './PermissionGate.vue';
|
||||
@@ -0,0 +1,42 @@
|
||||
import type { PermissionMatchMode } from '#/utils/permission-match';
|
||||
|
||||
import { computed } from 'vue';
|
||||
|
||||
import { useAccessStore } from '@vben/stores';
|
||||
|
||||
import { matchPermissions } from '#/utils/permission-match';
|
||||
|
||||
/**
|
||||
* 按钮级权限:用户码来自 GET /auth/codes(accessStore.accessCodes);
|
||||
* 组件/指令传入的 code 为当前操作对应的后端 @RequirePermission 值。
|
||||
*/
|
||||
export function usePermission() {
|
||||
const accessStore = useAccessStore();
|
||||
|
||||
const accessCodes = computed(() => accessStore.accessCodes ?? []);
|
||||
|
||||
function hasPermission(
|
||||
code: string | string[],
|
||||
mode: PermissionMatchMode = 'any',
|
||||
): boolean {
|
||||
if (accessStore.isSuper) {
|
||||
return true;
|
||||
}
|
||||
return matchPermissions(accessCodes.value, code, mode);
|
||||
}
|
||||
|
||||
function hasSomePermission(codes: string[]): boolean {
|
||||
return hasPermission(codes, 'any');
|
||||
}
|
||||
|
||||
function hasEveryPermission(codes: string[]): boolean {
|
||||
return hasPermission(codes, 'all');
|
||||
}
|
||||
|
||||
return {
|
||||
accessCodes,
|
||||
hasEveryPermission,
|
||||
hasPermission,
|
||||
hasSomePermission,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { ComputedRef } from 'vue';
|
||||
|
||||
import { computed, watch } from 'vue';
|
||||
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
|
||||
/** 是否拥有操作列内任一按钮权限 */
|
||||
export function useActionColumnVisible(actionCodes: string[]) {
|
||||
const { hasPermission } = usePermission();
|
||||
return computed(() => hasPermission(actionCodes, 'any'));
|
||||
}
|
||||
|
||||
interface GridColumnsApi {
|
||||
setGridOptions: (options: Record<string, unknown>) => void;
|
||||
}
|
||||
|
||||
/** 权限变化时同步 VXE 列配置(无操作权限时去掉整列) */
|
||||
export function syncVxeGridColumns(
|
||||
gridApi: GridColumnsApi,
|
||||
columns: ComputedRef<unknown[]>,
|
||||
) {
|
||||
watch(
|
||||
columns,
|
||||
(cols) => {
|
||||
gridApi.setGridOptions({ columns: cols });
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import type { App, Directive, DirectiveBinding } from 'vue';
|
||||
|
||||
import { useAccessStore } from '@vben/stores';
|
||||
|
||||
import { matchPermissions } from '#/utils/permission-match';
|
||||
|
||||
function resolveAllowed(value: string | string[] | undefined): boolean {
|
||||
if (!value) {
|
||||
return true;
|
||||
}
|
||||
const accessStore = useAccessStore();
|
||||
if (accessStore.isSuper) {
|
||||
return true;
|
||||
}
|
||||
const codes = accessStore.accessCodes ?? [];
|
||||
const required = Array.isArray(value) ? value : [value];
|
||||
return matchPermissions(codes, required, 'any');
|
||||
}
|
||||
|
||||
function applyPermission(
|
||||
el: Element,
|
||||
binding: DirectiveBinding<string | string[]>,
|
||||
) {
|
||||
if (resolveAllowed(binding.value)) {
|
||||
return;
|
||||
}
|
||||
el.remove();
|
||||
}
|
||||
|
||||
/** 无权限时移除 DOM,用法:v-permission="'sys:menu:create'" */
|
||||
export function registerPermissionDirective(app: App) {
|
||||
const directive: Directive = {
|
||||
mounted: applyPermission,
|
||||
updated: applyPermission,
|
||||
};
|
||||
app.directive('permission', directive);
|
||||
}
|
||||
@@ -91,10 +91,12 @@ function setupAccessGuard(router: Router) {
|
||||
}
|
||||
|
||||
// 生成路由表
|
||||
// 当前登录用户拥有的角色标识列表
|
||||
const userInfo = userStore.userInfo || (await authStore.fetchUserInfo());
|
||||
const userRoles = userInfo.roles ?? [];
|
||||
|
||||
// 权限码(刷新页面时与菜单一并重拉,避免仅用 localStorage 旧数据)
|
||||
await authStore.fetchAccessCodes();
|
||||
|
||||
// 系统配置(刷新页面后补拉)
|
||||
try {
|
||||
const { useAppConfigStore } = await import('#/store/app-config');
|
||||
|
||||
@@ -73,14 +73,8 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
console.warn('获取系统配置失败:', error);
|
||||
}
|
||||
|
||||
// 获取用户权限码(可选,如果接口不存在则跳过)
|
||||
try {
|
||||
const accessCodes = await getAccessCodesApi();
|
||||
accessStore.setAccessCodes(accessCodes);
|
||||
} catch (error) {
|
||||
console.warn('获取用户权限码失败,使用默认权限:', error);
|
||||
accessStore.setAccessCodes([]);
|
||||
}
|
||||
// 用户权限码
|
||||
await fetchAccessCodes();
|
||||
|
||||
if (accessStore.loginExpired) {
|
||||
accessStore.setLoginExpired(false);
|
||||
@@ -154,14 +148,8 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
console.warn('获取系统配置失败:', error);
|
||||
}
|
||||
|
||||
// 获取用户权限码(可选,如果接口不存在则跳过)
|
||||
try {
|
||||
const accessCodes = await getAccessCodesApi();
|
||||
accessStore.setAccessCodes(accessCodes);
|
||||
} catch (error) {
|
||||
console.warn('获取用户权限码失败,使用默认权限:', error);
|
||||
accessStore.setAccessCodes([]);
|
||||
}
|
||||
// 用户权限码
|
||||
await fetchAccessCodes();
|
||||
|
||||
if (accessStore.loginExpired) {
|
||||
accessStore.setLoginExpired(false);
|
||||
@@ -227,6 +215,19 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
/** 拉取并写入当前用户 API 权限码(GET /auth/codes) */
|
||||
async function fetchAccessCodes() {
|
||||
try {
|
||||
const accessCodes = await getAccessCodesApi();
|
||||
accessStore.setAccessCodes(accessCodes);
|
||||
return accessCodes;
|
||||
} catch (error) {
|
||||
console.warn('获取用户权限码失败:', error);
|
||||
accessStore.setAccessCodes([]);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
function $reset() {
|
||||
loginLoading.value = false;
|
||||
}
|
||||
@@ -235,6 +236,7 @@ export const useAuthStore = defineStore('auth', () => {
|
||||
$reset,
|
||||
authLogin,
|
||||
authRegister,
|
||||
fetchAccessCodes,
|
||||
fetchUserInfo,
|
||||
loginLoading,
|
||||
logout,
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import {
|
||||
codeCoversRequired,
|
||||
matchPermission,
|
||||
matchPermissions,
|
||||
} from './permission-match';
|
||||
|
||||
describe('permission-match', () => {
|
||||
it('通配符放行', () => {
|
||||
expect(matchPermission(['*'], 'sys:menu:create')).toBe(true);
|
||||
expect(matchPermission(['company:*'], 'org:company:read')).toBe(true);
|
||||
});
|
||||
|
||||
it('精确匹配与父码前缀', () => {
|
||||
const codes = ['sys:menu', 'sys:user:read'];
|
||||
expect(matchPermission(codes, 'sys:menu:read')).toBe(true);
|
||||
expect(matchPermission(codes, 'sys:menu:create')).toBe(true);
|
||||
expect(matchPermission(codes, 'sys:user:read')).toBe(true);
|
||||
expect(matchPermission(codes, 'sys:user:update')).toBe(false);
|
||||
});
|
||||
|
||||
it('codeCoversRequired 边界', () => {
|
||||
expect(codeCoversRequired('sys', 'sys:menu')).toBe(true);
|
||||
expect(codeCoversRequired('sys:menu', 'sys:menu')).toBe(true);
|
||||
expect(codeCoversRequired('sys:menu', 'sys:menux')).toBe(false);
|
||||
});
|
||||
|
||||
it('多码 any/all', () => {
|
||||
const codes = ['sys:a:read'];
|
||||
expect(
|
||||
matchPermissions(codes, ['sys:a:read', 'sys:b:read'], 'any'),
|
||||
).toBe(true);
|
||||
expect(
|
||||
matchPermissions(codes, ['sys:a:read', 'sys:b:read'], 'all'),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
/** 与 GET /auth/codes 返回的全局通配一致 */
|
||||
export const PERMISSION_WILDCARD_ALL = '*';
|
||||
export const PERMISSION_WILDCARD_COMPANY = 'company:*';
|
||||
|
||||
export type PermissionMatchMode = 'all' | 'any';
|
||||
|
||||
/** 用户持有的权限码是否覆盖所需码(精确或父码前缀,对齐后端岗位/公司继承语义) */
|
||||
export function codeCoversRequired(userCode: string, required: string): boolean {
|
||||
if (userCode === required) {
|
||||
return true;
|
||||
}
|
||||
return required.startsWith(`${userCode}:`);
|
||||
}
|
||||
|
||||
export function isWildcardAll(userCodes: readonly string[]): boolean {
|
||||
return userCodes.includes(PERMISSION_WILDCARD_ALL);
|
||||
}
|
||||
|
||||
export function isWildcardCompany(userCodes: readonly string[]): boolean {
|
||||
return userCodes.includes(PERMISSION_WILDCARD_COMPANY);
|
||||
}
|
||||
|
||||
/** 单个 API 权限码是否满足 */
|
||||
export function matchPermission(
|
||||
userCodes: readonly string[],
|
||||
required: string,
|
||||
): boolean {
|
||||
if (!required) {
|
||||
return false;
|
||||
}
|
||||
if (isWildcardAll(userCodes) || isWildcardCompany(userCodes)) {
|
||||
return true;
|
||||
}
|
||||
return userCodes.some((code) => codeCoversRequired(code, required));
|
||||
}
|
||||
|
||||
/** 多码匹配:any=任一,all=全部 */
|
||||
export function matchPermissions(
|
||||
userCodes: readonly string[],
|
||||
required: string | string[],
|
||||
mode: PermissionMatchMode = 'any',
|
||||
): boolean {
|
||||
const list = (Array.isArray(required) ? required : [required]).filter(
|
||||
Boolean,
|
||||
);
|
||||
if (list.length === 0) {
|
||||
return false;
|
||||
}
|
||||
if (mode === 'all') {
|
||||
return list.every((code) => matchPermission(userCodes, code));
|
||||
}
|
||||
return list.some((code) => matchPermission(userCodes, code));
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { MenuDTO, MenuVO } from '#/api/sys/menu/types';
|
||||
|
||||
import { ref } from 'vue';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
|
||||
@@ -15,10 +15,17 @@ import {
|
||||
deleteMenuApi,
|
||||
getAllMenusApi,
|
||||
} from '#/api/sys/menu';
|
||||
import type { VxeRowContextMenuItem } from '#/components/vxe-row-context-menu/types';
|
||||
import {
|
||||
useVxeRowContextMenu,
|
||||
VxeRowContextMenu,
|
||||
} from '#/components/vxe-row-context-menu';
|
||||
import { PermissionGate } from '#/components/permission';
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
import {
|
||||
syncVxeGridColumns,
|
||||
useActionColumnVisible,
|
||||
} from '#/composables/use-vxe-action-column';
|
||||
|
||||
import MenuFormModal from './components/MenuFormModal.vue';
|
||||
|
||||
@@ -31,6 +38,15 @@ const PlusCircleIcon = createIconifyIcon('lucide:plus-circle');
|
||||
const ExpandIcon = createIconifyIcon('lucide:chevrons-down');
|
||||
const CollapseIcon = createIconifyIcon('lucide:chevrons-up');
|
||||
|
||||
const { hasPermission } = usePermission();
|
||||
|
||||
const menuActionCodes = [
|
||||
'sys:menu:create',
|
||||
'sys:menu:update',
|
||||
'sys:menu:delete',
|
||||
];
|
||||
const showActionColumn = useActionColumnVisible(menuActionCodes);
|
||||
|
||||
// 响应式数据
|
||||
const loading = ref(false);
|
||||
const menuFormModalRef = ref();
|
||||
@@ -50,82 +66,90 @@ function sameMenuParent(a: unknown, b: unknown) {
|
||||
return normalizeMenuParentId(a) === normalizeMenuParentId(b);
|
||||
}
|
||||
|
||||
const menuTableBaseColumns: VxeGridProps<any>['columns'] = [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '菜单名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'routeName',
|
||||
title: '路由名称',
|
||||
minWidth: 140,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'title',
|
||||
title: '菜单标题',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'icon',
|
||||
title: '图标',
|
||||
width: 72,
|
||||
align: 'center' as const,
|
||||
slots: { default: 'icon' },
|
||||
},
|
||||
{
|
||||
field: 'path',
|
||||
title: '路由路径',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'component',
|
||||
title: '页面组件',
|
||||
minWidth: 180,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '权限列表',
|
||||
minWidth: 200,
|
||||
slots: { default: 'permissions' },
|
||||
},
|
||||
{
|
||||
title: '类型',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuType' },
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuStatus' },
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
];
|
||||
|
||||
const menuActionColumn: NonNullable<VxeGridProps<any>['columns']>[number] = {
|
||||
title: '操作',
|
||||
minWidth: 280,
|
||||
fixed: 'right' as const,
|
||||
showOverflow: false,
|
||||
slots: { default: 'action' },
|
||||
};
|
||||
|
||||
const menuTableColumns = computed(() =>
|
||||
showActionColumn.value
|
||||
? [...(menuTableBaseColumns ?? []), menuActionColumn]
|
||||
: (menuTableBaseColumns ?? []),
|
||||
);
|
||||
|
||||
// VbenVxeGrid 表格配置
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '菜单名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true, // 启用树节点显示
|
||||
dragSort: true, // 启用拖拽排序
|
||||
// 移除自定义插槽,让拖拽控件正常显示
|
||||
},
|
||||
{
|
||||
field: 'routeName',
|
||||
title: '路由名称',
|
||||
minWidth: 140,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'title',
|
||||
title: '菜单标题',
|
||||
minWidth: 150,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'icon',
|
||||
title: '图标',
|
||||
width: 72,
|
||||
align: 'center' as const,
|
||||
slots: { default: 'icon' },
|
||||
},
|
||||
{
|
||||
field: 'path',
|
||||
title: '路由路径',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'component',
|
||||
title: '页面组件',
|
||||
minWidth: 180,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
title: '权限列表',
|
||||
minWidth: 200,
|
||||
slots: { default: 'permissions' },
|
||||
},
|
||||
{
|
||||
title: '类型',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuType' },
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
minWidth: 100,
|
||||
slots: { default: 'menuStatus' },
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 280,
|
||||
fixed: 'right' as const,
|
||||
showOverflow: false,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
columns: menuTableColumns.value,
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
@@ -274,24 +298,32 @@ const {
|
||||
row: ctxMenuRow,
|
||||
items: ctxMenuItems,
|
||||
onCellMenu,
|
||||
} = useVxeRowContextMenu<MenuVO.MenuInfo>((_row) => [
|
||||
{
|
||||
key: 'addChild',
|
||||
label: '新增',
|
||||
icon: PlusCircleIcon,
|
||||
},
|
||||
{
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
},
|
||||
]);
|
||||
} = useVxeRowContextMenu<MenuVO.MenuInfo>(() => {
|
||||
const items: VxeRowContextMenuItem[] = [];
|
||||
if (hasPermission('sys:menu:create')) {
|
||||
items.push({
|
||||
key: 'addChild',
|
||||
label: '新增',
|
||||
icon: PlusCircleIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:menu:update')) {
|
||||
items.push({
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:menu:delete')) {
|
||||
items.push({
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
|
||||
function handleCtxMenuSelect(key: string) {
|
||||
const row = ctxMenuRow.value;
|
||||
@@ -429,6 +461,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
cellMenu: onCellMenu,
|
||||
} as any,
|
||||
});
|
||||
|
||||
syncVxeGridColumns(gridApi, menuTableColumns);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -438,10 +472,12 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
<!-- 工具栏工具 -->
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加菜单
|
||||
</Button>
|
||||
<PermissionGate code="sys:menu:create">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加菜单
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<Button @click="expandAllTree">
|
||||
<ExpandIcon />
|
||||
全部展开
|
||||
@@ -506,25 +542,31 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
</template>
|
||||
|
||||
<!-- 操作列 -->
|
||||
<template #action="{ row }: { row: any }">
|
||||
<template v-if="showActionColumn" #action="{ row }: { row: any }">
|
||||
<Space size="small" :wrap="false">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定要删除这个菜单吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
<PermissionGate code="sys:menu:create">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:menu:update">
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:menu:delete">
|
||||
<Popconfirm
|
||||
title="确定要删除这个菜单吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { PermissionVO } from '#/api/sys/permission';
|
||||
|
||||
import { nextTick, ref } from 'vue';
|
||||
import { computed, nextTick, ref } from 'vue';
|
||||
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
@@ -15,10 +15,17 @@ import {
|
||||
deletePermissionApi,
|
||||
getSystemPermissionTreeApi,
|
||||
} from '#/api/sys/permission';
|
||||
import type { VxeRowContextMenuItem } from '#/components/vxe-row-context-menu/types';
|
||||
import {
|
||||
useVxeRowContextMenu,
|
||||
VxeRowContextMenu,
|
||||
} from '#/components/vxe-row-context-menu';
|
||||
import { PermissionGate } from '#/components/permission';
|
||||
import { usePermission } from '#/composables/use-permission';
|
||||
import {
|
||||
syncVxeGridColumns,
|
||||
useActionColumnVisible,
|
||||
} from '#/composables/use-vxe-action-column';
|
||||
|
||||
import PermissionFormModal from './components/PermissionFormModal.vue';
|
||||
|
||||
@@ -31,6 +38,15 @@ const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
const UnfoldIcon = createIconifyIcon('lucide:unfold-vertical');
|
||||
const FoldIcon = createIconifyIcon('lucide:fold-vertical');
|
||||
|
||||
const { hasPermission } = usePermission();
|
||||
|
||||
const permissionActionCodes = [
|
||||
'sys:permission:create',
|
||||
'sys:permission:update',
|
||||
'sys:permission:delete',
|
||||
];
|
||||
const showActionColumn = useActionColumnVisible(permissionActionCodes);
|
||||
|
||||
const loading = ref(false);
|
||||
const permissionFormModalRef = ref();
|
||||
|
||||
@@ -47,62 +63,72 @@ function flattenTree(
|
||||
return result;
|
||||
}
|
||||
|
||||
const permissionTableBaseColumns: VxeGridProps<any>['columns'] = [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'code',
|
||||
title: '权限码',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'type',
|
||||
title: '权限类型',
|
||||
minWidth: 120,
|
||||
slots: { default: 'type' },
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 80,
|
||||
slots: { default: 'status' },
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '备注',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
];
|
||||
|
||||
const permissionActionColumn: NonNullable<VxeGridProps<any>['columns']>[number] =
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 260,
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
};
|
||||
|
||||
const permissionTableColumns = computed(() =>
|
||||
showActionColumn.value
|
||||
? [...(permissionTableBaseColumns ?? []), permissionActionColumn]
|
||||
: (permissionTableBaseColumns ?? []),
|
||||
);
|
||||
|
||||
const gridOptions: VxeGridProps<any> = {
|
||||
columns: [
|
||||
{
|
||||
type: 'seq' as const,
|
||||
minWidth: 60,
|
||||
title: '序号',
|
||||
},
|
||||
{
|
||||
field: 'name',
|
||||
title: '权限名称',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
treeNode: true,
|
||||
dragSort: true,
|
||||
},
|
||||
{
|
||||
field: 'code',
|
||||
title: '权限码',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'type',
|
||||
title: '权限类型',
|
||||
minWidth: 120,
|
||||
slots: { default: 'type' },
|
||||
},
|
||||
{
|
||||
field: 'sortOrder',
|
||||
title: '排序',
|
||||
minWidth: 80,
|
||||
},
|
||||
{
|
||||
field: 'status',
|
||||
title: '状态',
|
||||
minWidth: 80,
|
||||
slots: { default: 'status' },
|
||||
},
|
||||
{
|
||||
field: 'remark',
|
||||
title: '备注',
|
||||
minWidth: 200,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'createTime',
|
||||
title: '创建时间',
|
||||
minWidth: 160,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
minWidth: 260,
|
||||
fixed: 'right' as const,
|
||||
slots: { default: 'action' },
|
||||
},
|
||||
],
|
||||
columns: permissionTableColumns.value,
|
||||
height: 'auto',
|
||||
border: true,
|
||||
stripe: true,
|
||||
@@ -308,24 +334,32 @@ const {
|
||||
row: ctxMenuRow,
|
||||
items: ctxMenuItems,
|
||||
onCellMenu,
|
||||
} = useVxeRowContextMenu<PermissionVO.PermissionInfoVO>(() => [
|
||||
{
|
||||
key: 'addChild',
|
||||
label: '增加下级',
|
||||
icon: PlusCircleIcon,
|
||||
},
|
||||
{
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
},
|
||||
{
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
},
|
||||
]);
|
||||
} = useVxeRowContextMenu<PermissionVO.PermissionInfoVO>(() => {
|
||||
const items: VxeRowContextMenuItem[] = [];
|
||||
if (hasPermission('sys:permission:create')) {
|
||||
items.push({
|
||||
key: 'addChild',
|
||||
label: '增加下级',
|
||||
icon: PlusCircleIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:permission:update')) {
|
||||
items.push({
|
||||
key: 'edit',
|
||||
label: '编辑',
|
||||
icon: EditIcon,
|
||||
});
|
||||
}
|
||||
if (hasPermission('sys:permission:delete')) {
|
||||
items.push({
|
||||
key: 'delete',
|
||||
label: '删除',
|
||||
icon: TrashIcon,
|
||||
danger: true,
|
||||
});
|
||||
}
|
||||
return items;
|
||||
});
|
||||
|
||||
function handleCtxMenuSelect(key: string) {
|
||||
const row = ctxMenuRow.value;
|
||||
@@ -355,6 +389,8 @@ const [Grid, gridApi] = useVbenVxeGrid({
|
||||
} as any,
|
||||
});
|
||||
|
||||
syncVxeGridColumns(gridApi, permissionTableColumns);
|
||||
|
||||
async function handleModalSuccess(payload?: { parentId?: null | number }) {
|
||||
const extra =
|
||||
payload?.parentId != null ? [payload.parentId] : ([] as number[]);
|
||||
@@ -387,10 +423,12 @@ const getTypeColor = (type: string) => {
|
||||
<Grid class="permission-grid">
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加权限
|
||||
</Button>
|
||||
<PermissionGate code="sys:permission:create">
|
||||
<Button type="primary" @click="handleAdd">
|
||||
<PlusIcon />
|
||||
添加权限
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<Button @click="expandAllTree">
|
||||
<UnfoldIcon class="h-4 w-4" />
|
||||
全部展开
|
||||
@@ -414,25 +452,31 @@ const getTypeColor = (type: string) => {
|
||||
</Tag>
|
||||
</template>
|
||||
|
||||
<template #action="{ row }">
|
||||
<template v-if="showActionColumn" #action="{ row }">
|
||||
<Space size="small" :wrap="false">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
<PermissionGate code="sys:permission:create">
|
||||
<Button type="link" size="small" @click="handleAddChild(row)">
|
||||
<PlusCircleIcon class="h-3 w-3" />
|
||||
增加下级
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:permission:update">
|
||||
<Button type="link" size="small" @click="handleEdit(row)">
|
||||
<EditIcon class="h-3 w-3" />
|
||||
编辑
|
||||
</Button>
|
||||
</PermissionGate>
|
||||
<PermissionGate code="sys:permission:delete">
|
||||
<Popconfirm
|
||||
title="确定要删除这个权限吗?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<Button type="link" size="small" danger>
|
||||
<TrashIcon class="h-3 w-3" />
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</PermissionGate>
|
||||
</Space>
|
||||
</template>
|
||||
</Grid>
|
||||
|
||||
Reference in New Issue
Block a user