optimize(projects): 优化菜单管理代码

This commit is contained in:
AN 2025-06-04 22:25:46 +08:00
parent c7d850b93a
commit e6a18ec527
2 changed files with 159 additions and 134 deletions

View File

@ -1,5 +1,5 @@
<script setup lang="tsx"> <script setup lang="tsx">
import { ref } from 'vue'; import { computed, ref } from 'vue';
import type { DataTableColumns, TreeInst, TreeOption } from 'naive-ui'; import type { DataTableColumns, TreeInst, TreeOption } from 'naive-ui';
import { NButton, NDivider, NIcon, NInput, NPopconfirm } from 'naive-ui'; import { NButton, NDivider, NIcon, NInput, NPopconfirm } from 'naive-ui';
import { useBoolean, useLoading } from '@sa/hooks'; import { useBoolean, useLoading } from '@sa/hooks';
@ -36,6 +36,18 @@ const treeData = ref<Api.System.Menu[]>([]);
const checkedKeys = ref<CommonType.IdType[]>([0]); const checkedKeys = ref<CommonType.IdType[]>([0]);
const expandedKeys = ref<CommonType.IdType[]>([0]); const expandedKeys = ref<CommonType.IdType[]>([0]);
//
const isCatalog = computed(() => currentMenu.value?.menuType === 'M');
//
const isMenu = computed(() => currentMenu.value?.menuType === 'C');
//
const isExternalType = computed(() => currentMenu.value?.isFrame === '0');
// iframe
const isIframeType = computed(() => currentMenu.value?.isFrame === '2');
const menuTreeRef = ref<TreeInst>(); const menuTreeRef = ref<TreeInst>();
const btnData = ref<Api.System.MenuList>([]); const btnData = ref<Api.System.MenuList>([]);
@ -347,7 +359,7 @@ const btnColumns: DataTableColumns<Api.System.Menu> = [
<template #header-extra> <template #header-extra>
<NSpace> <NSpace>
<NButton <NButton
v-if="currentMenu.menuType === 'M' && hasAuth('system:menu:add')" v-if="isCatalog && hasAuth('system:menu:add')"
size="small" size="small"
ghost ghost
type="primary" type="primary"
@ -391,30 +403,30 @@ const btnColumns: DataTableColumns<Api.System.Menu> = [
label-class="w-20% min-w-88px" label-class="w-20% min-w-88px"
content-class="w-100px" content-class="w-100px"
> >
<NDescriptionsItem :label="$t('page.system.menu.menuName')"> <NDescriptionsItem :label="$t('page.system.menu.menuType')">
<NTag class="m-1" size="small" type="primary">{{ menuTypeRecord[currentMenu.menuType!] }}</NTag> <NTag class="m-1" size="small" type="primary">{{ menuTypeRecord[currentMenu.menuType!] }}</NTag>
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem :label="$t('page.system.menu.status')"> <NDescriptionsItem :label="$t('page.system.menu.status')">
<DictTag size="small" :value="currentMenu.status" dict-code="sys_normal_disable" /> <DictTag size="small" :value="currentMenu.status" dict-code="sys_normal_disable" />
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem :label="$t('page.system.menu.addChildMenu')"> <NDescriptionsItem :label="$t('page.system.menu.menuName')">
{{ currentMenu.menuName }} {{ currentMenu.menuName }}
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem v-if="currentMenu.menuType === 'C'" :label="$t('page.system.menu.component')"> <NDescriptionsItem v-if="isMenu" :label="$t('page.system.menu.component')">
{{ currentMenu.component }} {{ currentMenu.component }}
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem <NDescriptionsItem
:label="currentMenu.isFrame !== '0' ? $t('page.system.menu.path') : $t('page.system.menu.externalPath')" :label="!isExternalType ? $t('page.system.menu.path') : $t('page.system.menu.externalPath')"
> >
{{ currentMenu.path }} {{ currentMenu.path }}
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem <NDescriptionsItem
v-if="currentMenu.menuType === 'C'" v-if="isMenu && !isExternalType"
:label="currentMenu.isFrame !== '2' ? $t('page.system.menu.query') : $t('page.system.menu.iframeQuery')" :label="!isIframeType ? $t('page.system.menu.query') : $t('page.system.menu.iframeQuery')"
> >
{{ currentMenu.queryParam }} {{ currentMenu.queryParam }}
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem v-if="currentMenu.menuType !== 'M'" :label="$t('page.system.menu.perms')"> <NDescriptionsItem v-if="!isCatalog" :label="$t('page.system.menu.perms')">
{{ currentMenu.perms }} {{ currentMenu.perms }}
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem :label="$t('page.system.menu.isFrame')"> <NDescriptionsItem :label="$t('page.system.menu.isFrame')">
@ -425,7 +437,7 @@ const btnColumns: DataTableColumns<Api.System.Menu> = [
<NDescriptionsItem :label="$t('page.system.menu.visible')"> <NDescriptionsItem :label="$t('page.system.menu.visible')">
<DictTag size="small" :value="currentMenu.visible" dict-code="sys_show_hide" /> <DictTag size="small" :value="currentMenu.visible" dict-code="sys_show_hide" />
</NDescriptionsItem> </NDescriptionsItem>
<NDescriptionsItem v-if="currentMenu.menuType === 'C'" :label="$t('page.system.menu.isCache')"> <NDescriptionsItem v-if="isMenu" :label="$t('page.system.menu.isCache')">
<NTag v-if="currentMenu.isCache" class="m-1" size="small" :type="tagMap[currentMenu.isCache]"> <NTag v-if="currentMenu.isCache" class="m-1" size="small" :type="tagMap[currentMenu.isCache]">
{{ currentMenu.isCache === '0' ? $t('page.system.menu.cache') : $t('page.system.menu.noCache') }} {{ currentMenu.isCache === '0' ? $t('page.system.menu.cache') : $t('page.system.menu.noCache') }}
</NTag> </NTag>

View File

@ -83,8 +83,28 @@ const rules: Record<RuleKey, App.Global.FormRule> = {
component: createRequiredRule($t('page.system.menu.form.component.invalid')) component: createRequiredRule($t('page.system.menu.form.component.invalid'))
}; };
const isBtn = computed(() => model.menuType === 'F'); //
const isCatalog = computed(() => model.menuType === 'M');
//
const isMenu = computed(() => model.menuType === 'C'); const isMenu = computed(() => model.menuType === 'C');
//
const isBtn = computed(() => model.menuType === 'F');
//
const isExternalType = computed(() => model.isFrame === '0');
//
const isInternalType = computed(() => model.isFrame === '1');
// iframe
const isIframeType = computed(() => model.isFrame === '2');
//
const isLocalIcon = computed(() => iconType.value === '2');
//
const localIcons = getLocalMenuIcons(); const localIcons = getLocalMenuIcons();
const localIconOptions = localIcons.map<SelectOption>(item => ({ const localIconOptions = localIcons.map<SelectOption>(item => ({
label: () => ( label: () => (
@ -102,7 +122,7 @@ function handleInitModel() {
if (props.operateType === 'edit' && props.rowData) { if (props.operateType === 'edit' && props.rowData) {
Object.assign(model, props.rowData); Object.assign(model, props.rowData);
if (isMenu.value && model.isFrame === '1') { if (isMenu.value && isInternalType.value) {
model.component = model.component?.slice(0, -6); model.component = model.component?.slice(0, -6);
} }
iconType.value = model.icon?.startsWith('local-icon-') ? '2' : '1'; iconType.value = model.icon?.startsWith('local-icon-') ? '2' : '1';
@ -118,6 +138,44 @@ function closeDrawer() {
visible.value = false; visible.value = false;
} }
//
function processPath(path: string | null | undefined): string {
return path?.startsWith('/') ? path.substring(1) : path || '';
}
//
function processComponent(component: string | null | undefined): string {
if (isCatalog.value && isInternalType.value) {
return 'Layout';
}
if (isIframeType.value || isExternalType.value) {
return 'FrameView';
}
if (isMenu.value && isInternalType.value) {
return component?.endsWith('/index') ? component : `${component || ''}/index`;
}
return component || '';
}
function processQueryParam(queryParam: string | null | undefined): string {
//
if (isExternalType.value) {
return '';
}
//
if (isInternalType.value && queryList.value.length) {
return JSON.stringify(Object.fromEntries(queryList.value.map(({ key, value }) => [key, value])));
}
// iframe使
if (isIframeType.value) {
return queryParam || '';
}
return '';
}
async function handleSubmit() { async function handleSubmit() {
await validate(); await validate();
@ -133,77 +191,36 @@ async function handleSubmit() {
visible: menuVisible, visible: menuVisible,
status, status,
perms, perms,
remark remark,
component,
queryParam
} = model; } = model;
let queryParam = model.queryParam; const payload = {
if (isFrame === '0') { menuName,
queryParam = ''; path: processPath(model.path),
} else if (isFrame === '1' && queryList.value.length) { parentId,
const queryObj: { [key: string]: string } = {}; orderNum,
queryList.value.forEach(item => (queryObj[item.key] = item.value)); queryParam: processQueryParam(queryParam),
queryParam = JSON.stringify(queryObj); isFrame,
} isCache,
menuType,
const path = model.path?.startsWith('/') ? model.path?.substring(1) : model.path; visible: menuVisible,
status,
let component = model.component; perms,
if (isFrame === '1' && menuType === 'M') { icon,
component = 'Layout'; component: processComponent(component),
} else if (isFrame === '2') { remark
component = 'FrameView'; };
} else if (isMenu.value && model.isFrame === '1') {
component = component?.endsWith('/index') ? component : `${component}/index`; const { error } =
} props.operateType === 'add' ? await fetchCreateMenu(payload) : await fetchUpdateMenu({ ...payload, menuId });
// request if (error) {
if (props.operateType === 'add') { return;
const { error } = await fetchCreateMenu({
menuName,
path,
parentId,
orderNum,
queryParam,
isFrame,
isCache,
menuType,
visible: menuVisible,
status,
perms,
icon,
component,
remark
});
if (error) {
return;
}
window.$message?.success($t('common.addSuccess'));
}
if (props.operateType === 'edit') {
const { error } = await fetchUpdateMenu({
menuId,
menuName,
path,
parentId,
orderNum,
queryParam,
isFrame,
isCache,
menuType,
visible: menuVisible,
status,
perms,
icon,
component,
remark
});
if (error) {
return;
}
window.$message?.success($t('common.updateSuccess'));
} }
window.$message?.success($t(props.operateType === 'add' ? 'common.addSuccess' : 'common.updateSuccess'));
closeDrawer(); closeDrawer();
emit('submitted', menuType!); emit('submitted', menuType!);
} }
@ -224,7 +241,7 @@ function onCreate() {
</script> </script>
<template> <template>
<NDrawer v-model:show="visible" display-directive="show" :width="800" class="max-w-90%"> <NDrawer v-model:show="visible" display-directive="show" :width="600" class="max-w-90%">
<NDrawerContent :title="drawerTitle" :native-scrollbar="false" closable> <NDrawerContent :title="drawerTitle" :native-scrollbar="false" closable>
<NForm ref="formRef" :model="model" :rules="rules"> <NForm ref="formRef" :model="model" :rules="rules">
<NGrid responsive="screen" item-responsive> <NGrid responsive="screen" item-responsive>
@ -238,12 +255,7 @@ function onCreate() {
:placeholder="$t('page.system.menu.form.parentId.required')" :placeholder="$t('page.system.menu.form.parentId.required')"
/> />
</NFormItemGi> </NFormItemGi>
<NFormItemGi <NFormItemGi v-if="!isBtn" :span="24" :label="$t('page.system.menu.menuType')" path="menuType">
v-if="model.menuType !== 'F'"
:span="24"
:label="$t('page.system.menu.menuType')"
path="menuType"
>
<NRadioGroup v-model:value="model.menuType"> <NRadioGroup v-model:value="model.menuType">
<NRadioButton <NRadioButton
v-for="item in menuTypeOptions.filter(item => item.value !== 'F')" v-for="item in menuTypeOptions.filter(item => item.value !== 'F')"
@ -253,22 +265,30 @@ function onCreate() {
/> />
</NRadioGroup> </NRadioGroup>
</NFormItemGi> </NFormItemGi>
<NFormItemGi :span="24" :label="$t('page.system.menu.menuName')" path="menuName"> <NFormItemGi span="24" :label="$t('page.system.menu.menuName')" path="menuName">
<NInput v-model:value="model.menuName" :placeholder="$t('page.system.menu.form.menuName.required')" /> <NInput v-model:value="model.menuName" :placeholder="$t('page.system.menu.form.menuName.required')" />
</NFormItemGi> </NFormItemGi>
<NFormItemGi v-if="!isBtn" span="24" :label="$t('page.system.menu.iconType')"> <NFormItemGi v-if="!isBtn" span="12" :label="$t('page.system.menu.iconType')">
<NRadioGroup v-model:value="iconType"> <NRadioGroup v-model:value="iconType">
<NRadio v-for="item in menuIconTypeOptions" :key="item.value" :value="item.value" :label="item.label" /> <NRadio v-for="item in menuIconTypeOptions" :key="item.value" :value="item.value" :label="item.label" />
</NRadioGroup> </NRadioGroup>
</NFormItemGi> </NFormItemGi>
<NFormItemGi v-if="!isBtn" span="24" path="icon"> <NFormItemGi v-if="!isBtn" span="12" path="icon">
<template #label> <template #label>
<div class="flex-center"> <div class="flex-center">
<FormTip :content="$t('page.system.menu.iconifyTip')" /> <FormTip :content="$t('page.system.menu.iconifyTip')" />
<span class="pl-3px">{{ $t('page.system.menu.icon') }}</span> <span class="pl-3px">{{ $t('page.system.menu.icon') }}</span>
</div> </div>
</template> </template>
<template v-if="iconType === '1'"> <template v-if="isLocalIcon">
<NSelect
v-model:value="model.icon"
:placeholder="$t('page.system.menu.placeholder.localIconPlaceholder')"
filterable
:options="localIconOptions"
/>
</template>
<template v-else>
<NInput <NInput
v-model:value="model.icon" v-model:value="model.icon"
:placeholder="$t('page.system.menu.placeholder.iconifyIconPlaceholder')" :placeholder="$t('page.system.menu.placeholder.iconifyIconPlaceholder')"
@ -279,27 +299,51 @@ function onCreate() {
</template> </template>
</NInput> </NInput>
</template> </template>
<template v-if="iconType === '2'"> </NFormItemGi>
<NSelect <NFormItemGi v-if="!isBtn" :span="12" path="isFrame">
v-model:value="model.icon" <template #label>
:placeholder="$t('page.system.menu.placeholder.localIconPlaceholder')" <div class="flex-center">
filterable <FormTip :content="$t('page.system.menu.isFrameTip')" />
:options="localIconOptions" <span>{{ $t('page.system.menu.isFrame') }}</span>
/> </div>
</template> </template>
<NRadioGroup v-model:value="model.isFrame">
<NSpace>
<NRadio
v-for="option in menuIsFrameOptions"
:key="option.value"
:value="option.value"
:label="option.label"
/>
</NSpace>
</NRadioGroup>
</NFormItemGi>
<NFormItemGi v-if="isMenu" :span="12" path="isCache">
<template #label>
<div class="flex-center">
<FormTip :content="$t('page.system.menu.isCacheTip')" />
<span>{{ $t('page.system.menu.isCache') }}</span>
</div>
</template>
<NRadioGroup v-model:value="model.isCache">
<NSpace>
<NRadio value="0" label="是" />
<NRadio value="1" label="否" />
</NSpace>
</NRadioGroup>
</NFormItemGi> </NFormItemGi>
<NFormItemGi v-if="!isBtn" :span="24" path="path"> <NFormItemGi v-if="!isBtn" :span="24" path="path">
<template #label> <template #label>
<div class="flex-center"> <div class="flex-center">
<FormTip :content="$t('page.system.menu.pathTip')" /> <FormTip :content="$t('page.system.menu.pathTip')" />
<span> <span>
{{ model.isFrame !== '0' ? $t('page.system.menu.path') : $t('page.system.menu.externalPath') }} {{ !isExternalType ? $t('page.system.menu.path') : $t('page.system.menu.externalPath') }}
</span> </span>
</div> </div>
</template> </template>
<NInput v-model:value="model.path" :placeholder="$t('page.system.menu.form.path.required')" /> <NInput v-model:value="model.path" :placeholder="$t('page.system.menu.form.path.required')" />
</NFormItemGi> </NFormItemGi>
<NFormItemGi v-if="isMenu && model.isFrame === '1'" :span="24" path="component"> <NFormItemGi v-if="isMenu && isInternalType" :span="24" path="component">
<template #label> <template #label>
<div class="flex-center"> <div class="flex-center">
<FormTip :content="$t('page.system.menu.componentTip')" /> <FormTip :content="$t('page.system.menu.componentTip')" />
@ -313,13 +357,13 @@ function onCreate() {
</NInputGroup> </NInputGroup>
</NFormItemGi> </NFormItemGi>
<NFormItemGi <NFormItemGi
v-if="isMenu && model.isFrame !== '0'" v-if="isMenu && !isExternalType"
span="24" span="24"
:show-feedback="!queryList.length" :show-feedback="!queryList.length"
:label="model.isFrame !== '2' ? $t('page.system.menu.query') : $t('page.system.menu.iframeQuery')" :label="isInternalType ? $t('page.system.menu.query') : $t('page.system.menu.iframeQuery')"
> >
<NDynamicInput <NDynamicInput
v-if="model.isFrame !== '2'" v-if="isInternalType"
v-model:value="queryList" v-model:value="queryList"
item-style="margin-bottom: 0" item-style="margin-bottom: 0"
:on-create="onCreate" :on-create="onCreate"
@ -369,38 +413,7 @@ function onCreate() {
</template> </template>
<NInput v-model:value="model.perms" :placeholder="$t('page.system.menu.form.perms.required')" /> <NInput v-model:value="model.perms" :placeholder="$t('page.system.menu.form.perms.required')" />
</NFormItemGi> </NFormItemGi>
<NFormItemGi v-if="!isBtn" :span="12" path="isFrame">
<template #label>
<div class="flex-center">
<FormTip :content="$t('page.system.menu.isFrameTip')" />
<span>{{ $t('page.system.menu.isFrame') }}</span>
</div>
</template>
<NRadioGroup v-model:value="model.isFrame">
<NSpace>
<NRadio
v-for="option in menuIsFrameOptions"
:key="option.value"
:value="option.value"
:label="option.label"
/>
</NSpace>
</NRadioGroup>
</NFormItemGi>
<NFormItemGi v-if="isMenu" :span="12" path="isCache">
<template #label>
<div class="flex-center">
<FormTip :content="$t('page.system.menu.isCacheTip')" />
<span>{{ $t('page.system.menu.isCache') }}</span>
</div>
</template>
<NRadioGroup v-model:value="model.isCache">
<NSpace>
<NRadio value="0" label="是" />
<NRadio value="1" label="否" />
</NSpace>
</NRadioGroup>
</NFormItemGi>
<NFormItemGi v-if="!isBtn" :span="12" :label="$t('page.system.menu.visible')" path="visible"> <NFormItemGi v-if="!isBtn" :span="12" :label="$t('page.system.menu.visible')" path="visible">
<template #label> <template #label>
<div class="flex-center"> <div class="flex-center">