feat(role): 添加角色管理相关常量并优化角色状态切换

refactor(role): 使用常量替换硬编码的角色ID和key
fix(role): 修正角色排序默认值为1
feat(component): 导出ApiSwitch全局组件
This commit is contained in:
dap 2026-01-15 10:03:39 +08:00
parent 07fa5626fd
commit 448856e547
4 changed files with 36 additions and 9 deletions

View File

@ -12,3 +12,5 @@ export function setupGlobalComponent(app: App) {
// 表格操作列专用按钮 // 表格操作列专用按钮
app.component('GhostButton', GhostButton); app.component('GhostButton', GhostButton);
} }
export { default as ApiSwitch } from './api-switch.vue';

View File

@ -131,7 +131,7 @@ export const drawerSchema: FormSchemaGetter = () => [
fieldName: 'roleSort', fieldName: 'roleSort',
label: '角色排序', label: '角色排序',
rules: 'required', rules: 'required',
defaultValue: 0, defaultValue: 1,
}, },
{ {
component: 'Select', component: 'Select',

View File

@ -9,6 +9,12 @@ import { useRouter } from 'vue-router';
import { useAccess } from '@vben/access'; import { useAccess } from '@vben/access';
import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui'; import { Page, useVbenDrawer, useVbenModal } from '@vben/common-ui';
import {
ADMIN_ROLE_KEY,
EnableStatus,
SUPERADMIN_ROLE_ID,
SUPERADMIN_ROLE_KEY,
} from '@vben/constants';
import { getVxePopupContainer } from '@vben/utils'; import { getVxePopupContainer } from '@vben/utils';
import { Modal, Popconfirm, Space } from 'antdv-next'; import { Modal, Popconfirm, Space } from 'antdv-next';
@ -20,7 +26,7 @@ import {
roleList, roleList,
roleRemove, roleRemove,
} from '#/api/system/role'; } from '#/api/system/role';
import { TableSwitch } from '#/components/table'; import { ApiSwitch } from '#/components/global';
import { commonDownloadExcel } from '#/utils/file/download'; import { commonDownloadExcel } from '#/utils/file/download';
import { columns, querySchema } from './data'; import { columns, querySchema } from './data';
@ -122,7 +128,7 @@ function handleDownloadExcel() {
const { hasAccessByCodes, hasAccessByRoles } = useAccess(); const { hasAccessByCodes, hasAccessByRoles } = useAccess();
const isSuperAdmin = computed(() => hasAccessByRoles(['superadmin'])); const isSuperAdmin = computed(() => hasAccessByRoles([SUPERADMIN_ROLE_KEY]));
const [RoleAuthModal, authModalApi] = useVbenModal({ const [RoleAuthModal, authModalApi] = useVbenModal({
connectedComponent: roleAuthModal, connectedComponent: roleAuthModal,
@ -137,6 +143,13 @@ const router = useRouter();
function handleAssignRole(record: Role) { function handleAssignRole(record: Role) {
router.push(`/system/role-auth/user/${record.roleId}`); router.push(`/system/role-auth/user/${record.roleId}`);
} }
async function handleChangeStatus(checked: boolean, row: Role) {
await roleChangeStatus({
roleId: row.roleId,
status: checked ? EnableStatus.Enable : EnableStatus.Disable,
});
}
</script> </script>
<template> <template>
@ -169,12 +182,12 @@ function handleAssignRole(record: Role) {
</Space> </Space>
</template> </template>
<template #status="{ row }"> <template #status="{ row }">
<TableSwitch <ApiSwitch
v-model:value="row.status" :value="row.status === EnableStatus.Enable"
:api="() => roleChangeStatus(row)" :api="(checked) => handleChangeStatus(checked, row)"
:disabled=" :disabled="
row.roleId === 1 || row.roleId === SUPERADMIN_ROLE_ID ||
row.roleKey === 'admin' || row.roleKey === ADMIN_ROLE_KEY ||
!hasAccessByCodes(['system:role:edit']) !hasAccessByCodes(['system:role:edit'])
" "
@reload="tableApi.query()" @reload="tableApi.query()"
@ -184,7 +197,9 @@ function handleAssignRole(record: Role) {
<!-- 租户管理员不可修改admin角色 防止误操作 --> <!-- 租户管理员不可修改admin角色 防止误操作 -->
<!-- 超级管理员可通过租户切换来操作租户管理员角色 --> <!-- 超级管理员可通过租户切换来操作租户管理员角色 -->
<template <template
v-if="!row.superAdmin && (row.roleKey !== 'admin' || isSuperAdmin)" v-if="
!row.superAdmin && (row.roleKey !== ADMIN_ROLE_KEY || isSuperAdmin)
"
> >
<Space> <Space>
<ghost-button <ghost-button

View File

@ -41,6 +41,16 @@ export const UNAUTHORIZED_CODE = 401;
* ID * ID
*/ */
export const SUPERADMIN_USER_ID = 1; export const SUPERADMIN_USER_ID = 1;
export const SUPERADMIN_ROLE_ID = 1;
/**
* key
*/
export const SUPERADMIN_ROLE_KEY = 'superadmin';
/**
* () key
*/
export const ADMIN_ROLE_KEY = 'admin';
/** /**
* -S系统开关的状态 * -S系统开关的状态