frontend: 系统菜单/字段/权限页面 UI 优化

- IconSelector.vue: 重构,精简代码
- MenuFormModal / index: 表单和页面优化
- field / system-permission: 小幅调整
This commit is contained in:
小蟑螂
2026-05-04 02:22:48 +08:00
parent 845ef8bb62
commit eefe9244cb
5 changed files with 349 additions and 559 deletions
@@ -98,13 +98,12 @@ const gridOptions: VxeGridProps<any> = {
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
resizable: true,
rowConfig: {
isHover: true,
drag: true, // 启用行拖拽
},
showOverflow: true,
size: 'small',
rowDragConfig: {
isPeerDrag: true, // 启用同层级拖拽限制
},
@@ -1,9 +1,10 @@
<script setup lang="ts">
import { computed, ref, watch } from 'vue';
import { computed, onMounted, ref, watch } from 'vue';
import { createIconifyIcon } from '@vben/icons';
import { IconifyIcon } from '@vben/icons';
import { useDebounceFn } from '@vueuse/core';
import { Input } from 'ant-design-vue';
import { Input, Select, Spin } from 'ant-design-vue';
interface Props {
modelValue?: string;
@@ -16,435 +17,143 @@ interface Emits {
const props = defineProps<Props>();
const emit = defineEmits<Emits>();
const searchKeyword = ref('');
const selectedIcon = ref(props.modelValue || '');
const currentPage = ref(1);
const pageSize = 36; // 6列 x 6行
const ICONIFY = 'https://api.iconify.design';
// 常用图标列表
const iconList = [
{ name: 'lucide:plus', component: createIconifyIcon('lucide:plus') },
{ name: 'lucide:home', component: createIconifyIcon('lucide:home') },
{ name: 'lucide:user', component: createIconifyIcon('lucide:user') },
{ name: 'lucide:settings', component: createIconifyIcon('lucide:settings') },
{ name: 'lucide:menu', component: createIconifyIcon('lucide:menu') },
{ name: 'lucide:search', component: createIconifyIcon('lucide:search') },
{ name: 'lucide:edit', component: createIconifyIcon('lucide:edit') },
{ name: 'lucide:trash-2', component: createIconifyIcon('lucide:trash-2') },
{ name: 'lucide:save', component: createIconifyIcon('lucide:save') },
{ name: 'lucide:download', component: createIconifyIcon('lucide:download') },
{ name: 'lucide:upload', component: createIconifyIcon('lucide:upload') },
{ name: 'lucide:folder', component: createIconifyIcon('lucide:folder') },
{ name: 'lucide:file', component: createIconifyIcon('lucide:file') },
{ name: 'lucide:image', component: createIconifyIcon('lucide:image') },
{ name: 'lucide:video', component: createIconifyIcon('lucide:video') },
{ name: 'lucide:music', component: createIconifyIcon('lucide:music') },
{ name: 'lucide:mail', component: createIconifyIcon('lucide:mail') },
{ name: 'lucide:phone', component: createIconifyIcon('lucide:phone') },
{ name: 'lucide:calendar', component: createIconifyIcon('lucide:calendar') },
{ name: 'lucide:clock', component: createIconifyIcon('lucide:clock') },
{ name: 'lucide:star', component: createIconifyIcon('lucide:star') },
{ name: 'lucide:heart', component: createIconifyIcon('lucide:heart') },
const GLOBAL_PREFIX = '__global__';
/** 常用图标集(Iconify prefix),可继续扩充 */
const COLLECTION_OPTIONS = [
{
name: 'lucide:thumbs-up',
component: createIconifyIcon('lucide:thumbs-up'),
label: '全局搜索(不限图标集,仅关键词)',
value: GLOBAL_PREFIX,
},
{
name: 'lucide:thumbs-down',
component: createIconifyIcon('lucide:thumbs-down'),
},
{ name: 'lucide:eye', component: createIconifyIcon('lucide:eye') },
{ name: 'lucide:eye-off', component: createIconifyIcon('lucide:eye-off') },
{ name: 'lucide:lock', component: createIconifyIcon('lucide:lock') },
{ name: 'lucide:unlock', component: createIconifyIcon('lucide:unlock') },
{ name: 'lucide:shield', component: createIconifyIcon('lucide:shield') },
{ name: 'lucide:key', component: createIconifyIcon('lucide:key') },
{ name: 'lucide:bell', component: createIconifyIcon('lucide:bell') },
{
name: 'lucide:message-circle',
component: createIconifyIcon('lucide:message-circle'),
},
{ name: 'lucide:users', component: createIconifyIcon('lucide:users') },
{
name: 'lucide:user-plus',
component: createIconifyIcon('lucide:user-plus'),
},
{
name: 'lucide:user-minus',
component: createIconifyIcon('lucide:user-minus'),
},
{
name: 'lucide:user-check',
component: createIconifyIcon('lucide:user-check'),
},
{ name: 'lucide:user-x', component: createIconifyIcon('lucide:user-x') },
{ name: 'lucide:grid', component: createIconifyIcon('lucide:grid') },
{ name: 'lucide:list', component: createIconifyIcon('lucide:list') },
{ name: 'lucide:layout', component: createIconifyIcon('lucide:layout') },
{ name: 'lucide:sidebar', component: createIconifyIcon('lucide:sidebar') },
{
name: 'lucide:panel-left',
component: createIconifyIcon('lucide:panel-left'),
},
{
name: 'lucide:panel-right',
component: createIconifyIcon('lucide:panel-right'),
},
{
name: 'lucide:panel-top',
component: createIconifyIcon('lucide:panel-top'),
},
{
name: 'lucide:panel-bottom',
component: createIconifyIcon('lucide:panel-bottom'),
},
{
name: 'lucide:chevron-up',
component: createIconifyIcon('lucide:chevron-up'),
},
{
name: 'lucide:chevron-down',
component: createIconifyIcon('lucide:chevron-down'),
},
{
name: 'lucide:chevron-left',
component: createIconifyIcon('lucide:chevron-left'),
},
{
name: 'lucide:chevron-right',
component: createIconifyIcon('lucide:chevron-right'),
},
{ name: 'lucide:arrow-up', component: createIconifyIcon('lucide:arrow-up') },
{
name: 'lucide:arrow-down',
component: createIconifyIcon('lucide:arrow-down'),
},
{
name: 'lucide:arrow-left',
component: createIconifyIcon('lucide:arrow-left'),
},
{
name: 'lucide:arrow-right',
component: createIconifyIcon('lucide:arrow-right'),
},
{
name: 'lucide:external-link',
component: createIconifyIcon('lucide:external-link'),
},
{ name: 'lucide:link', component: createIconifyIcon('lucide:link') },
{ name: 'lucide:unlink', component: createIconifyIcon('lucide:unlink') },
{ name: 'lucide:copy', component: createIconifyIcon('lucide:copy') },
{ name: 'lucide:cut', component: createIconifyIcon('lucide:cut') },
{ name: 'lucide:paste', component: createIconifyIcon('lucide:paste') },
{ name: 'lucide:undo', component: createIconifyIcon('lucide:undo') },
{ name: 'lucide:redo', component: createIconifyIcon('lucide:redo') },
{
name: 'lucide:refresh-cw',
component: createIconifyIcon('lucide:refresh-cw'),
},
{
name: 'lucide:rotate-cw',
component: createIconifyIcon('lucide:rotate-cw'),
},
{
name: 'lucide:rotate-ccw',
component: createIconifyIcon('lucide:rotate-ccw'),
},
{ name: 'lucide:zoom-in', component: createIconifyIcon('lucide:zoom-in') },
{ name: 'lucide:zoom-out', component: createIconifyIcon('lucide:zoom-out') },
{ name: 'lucide:maximize', component: createIconifyIcon('lucide:maximize') },
{ name: 'lucide:minimize', component: createIconifyIcon('lucide:minimize') },
{ name: 'lucide:monitor', component: createIconifyIcon('lucide:monitor') },
{
name: 'lucide:smartphone',
component: createIconifyIcon('lucide:smartphone'),
},
{ name: 'lucide:tablet', component: createIconifyIcon('lucide:tablet') },
{ name: 'lucide:laptop', component: createIconifyIcon('lucide:laptop') },
{ name: 'lucide:desktop', component: createIconifyIcon('lucide:desktop') },
{ name: 'lucide:server', component: createIconifyIcon('lucide:server') },
{ name: 'lucide:database', component: createIconifyIcon('lucide:database') },
{
name: 'lucide:hard-drive',
component: createIconifyIcon('lucide:hard-drive'),
},
{ name: 'lucide:wifi', component: createIconifyIcon('lucide:wifi') },
{
name: 'lucide:bluetooth',
component: createIconifyIcon('lucide:bluetooth'),
},
{ name: 'lucide:usb', component: createIconifyIcon('lucide:usb') },
{ name: 'lucide:plug', component: createIconifyIcon('lucide:plug') },
{ name: 'lucide:battery', component: createIconifyIcon('lucide:battery') },
{
name: 'lucide:battery-charging',
component: createIconifyIcon('lucide:battery-charging'),
},
{ name: 'lucide:power', component: createIconifyIcon('lucide:power') },
{ name: 'lucide:volume-2', component: createIconifyIcon('lucide:volume-2') },
{ name: 'lucide:volume-x', component: createIconifyIcon('lucide:volume-x') },
{ name: 'lucide:play', component: createIconifyIcon('lucide:play') },
{ name: 'lucide:pause', component: createIconifyIcon('lucide:pause') },
{ name: 'lucide:stop', component: createIconifyIcon('lucide:stop') },
{
name: 'lucide:skip-back',
component: createIconifyIcon('lucide:skip-back'),
},
{
name: 'lucide:skip-forward',
component: createIconifyIcon('lucide:skip-forward'),
},
{ name: 'lucide:repeat', component: createIconifyIcon('lucide:repeat') },
{ name: 'lucide:shuffle', component: createIconifyIcon('lucide:shuffle') },
{ name: 'lucide:mic', component: createIconifyIcon('lucide:mic') },
{ name: 'lucide:mic-off', component: createIconifyIcon('lucide:mic-off') },
{ name: 'lucide:camera', component: createIconifyIcon('lucide:camera') },
{
name: 'lucide:camera-off',
component: createIconifyIcon('lucide:camera-off'),
},
{
name: 'lucide:video-off',
component: createIconifyIcon('lucide:video-off'),
},
{ name: 'lucide:cast', component: createIconifyIcon('lucide:cast') },
{ name: 'lucide:airplay', component: createIconifyIcon('lucide:airplay') },
{
name: 'lucide:headphones',
component: createIconifyIcon('lucide:headphones'),
},
{ name: 'lucide:speaker', component: createIconifyIcon('lucide:speaker') },
{ name: 'lucide:radio', component: createIconifyIcon('lucide:radio') },
{ name: 'lucide:tv', component: createIconifyIcon('lucide:tv') },
{
name: 'lucide:gamepad-2',
component: createIconifyIcon('lucide:gamepad-2'),
},
{ name: 'lucide:joystick', component: createIconifyIcon('lucide:joystick') },
{
name: 'lucide:controller',
component: createIconifyIcon('lucide:controller'),
},
{ name: 'lucide:mouse', component: createIconifyIcon('lucide:mouse') },
{ name: 'lucide:keyboard', component: createIconifyIcon('lucide:keyboard') },
{ name: 'lucide:printer', component: createIconifyIcon('lucide:printer') },
{ name: 'lucide:scanner', component: createIconifyIcon('lucide:scanner') },
{ name: 'lucide:fax', component: createIconifyIcon('lucide:fax') },
{
name: 'lucide:calculator',
component: createIconifyIcon('lucide:calculator'),
},
{ name: 'lucide:watch', component: createIconifyIcon('lucide:watch') },
{
name: 'lucide:alarm-clock',
component: createIconifyIcon('lucide:alarm-clock'),
},
{ name: 'lucide:timer', component: createIconifyIcon('lucide:timer') },
{
name: 'lucide:stopwatch',
component: createIconifyIcon('lucide:stopwatch'),
},
{
name: 'lucide:hourglass',
component: createIconifyIcon('lucide:hourglass'),
},
{
name: 'lucide:thermometer',
component: createIconifyIcon('lucide:thermometer'),
},
{ name: 'lucide:gauge', component: createIconifyIcon('lucide:gauge') },
{ name: 'lucide:activity', component: createIconifyIcon('lucide:activity') },
{
name: 'lucide:trending-up',
component: createIconifyIcon('lucide:trending-up'),
},
{
name: 'lucide:trending-down',
component: createIconifyIcon('lucide:trending-down'),
},
{
name: 'lucide:bar-chart',
component: createIconifyIcon('lucide:bar-chart'),
},
{
name: 'lucide:line-chart',
component: createIconifyIcon('lucide:line-chart'),
},
{
name: 'lucide:pie-chart',
component: createIconifyIcon('lucide:pie-chart'),
},
{
name: 'lucide:credit-card',
component: createIconifyIcon('lucide:credit-card'),
},
{ name: 'lucide:wallet', component: createIconifyIcon('lucide:wallet') },
{ name: 'lucide:banknote', component: createIconifyIcon('lucide:banknote') },
{ name: 'lucide:coins', component: createIconifyIcon('lucide:coins') },
{ name: 'lucide:receipt', component: createIconifyIcon('lucide:receipt') },
{
name: 'lucide:shopping-cart',
component: createIconifyIcon('lucide:shopping-cart'),
},
{
name: 'lucide:shopping-bag',
component: createIconifyIcon('lucide:shopping-bag'),
},
{ name: 'lucide:store', component: createIconifyIcon('lucide:store') },
{ name: 'lucide:building', component: createIconifyIcon('lucide:building') },
{
name: 'lucide:building-2',
component: createIconifyIcon('lucide:building-2'),
},
{ name: 'lucide:map', component: createIconifyIcon('lucide:map') },
{ name: 'lucide:map-pin', component: createIconifyIcon('lucide:map-pin') },
{
name: 'lucide:navigation',
component: createIconifyIcon('lucide:navigation'),
},
{ name: 'lucide:compass', component: createIconifyIcon('lucide:compass') },
{ name: 'lucide:globe', component: createIconifyIcon('lucide:globe') },
{ name: 'lucide:earth', component: createIconifyIcon('lucide:earth') },
{ name: 'lucide:flag', component: createIconifyIcon('lucide:flag') },
{ name: 'lucide:bookmark', component: createIconifyIcon('lucide:bookmark') },
{ name: 'lucide:tag', component: createIconifyIcon('lucide:tag') },
{ name: 'lucide:tags', component: createIconifyIcon('lucide:tags') },
{ name: 'lucide:archive', component: createIconifyIcon('lucide:archive') },
{ name: 'lucide:inbox', component: createIconifyIcon('lucide:inbox') },
{ name: 'lucide:outbox', component: createIconifyIcon('lucide:outbox') },
{ name: 'lucide:send', component: createIconifyIcon('lucide:send') },
{ name: 'lucide:reply', component: createIconifyIcon('lucide:reply') },
{ name: 'lucide:forward', component: createIconifyIcon('lucide:forward') },
{ name: 'lucide:share', component: createIconifyIcon('lucide:share') },
{ name: 'lucide:share-2', component: createIconifyIcon('lucide:share-2') },
{
name: 'lucide:git-branch',
component: createIconifyIcon('lucide:git-branch'),
},
{
name: 'lucide:git-commit',
component: createIconifyIcon('lucide:git-commit'),
},
{
name: 'lucide:git-merge',
component: createIconifyIcon('lucide:git-merge'),
},
{
name: 'lucide:git-pull-request',
component: createIconifyIcon('lucide:git-pull-request'),
},
{ name: 'lucide:github', component: createIconifyIcon('lucide:github') },
{ name: 'lucide:gitlab', component: createIconifyIcon('lucide:gitlab') },
{
name: 'lucide:bitbucket',
component: createIconifyIcon('lucide:bitbucket'),
},
{ name: 'lucide:code', component: createIconifyIcon('lucide:code') },
{ name: 'lucide:code-2', component: createIconifyIcon('lucide:code-2') },
{ name: 'lucide:terminal', component: createIconifyIcon('lucide:terminal') },
{ name: 'lucide:command', component: createIconifyIcon('lucide:command') },
{ name: 'lucide:hash', component: createIconifyIcon('lucide:hash') },
{ name: 'lucide:brackets', component: createIconifyIcon('lucide:brackets') },
{ name: 'lucide:braces', component: createIconifyIcon('lucide:braces') },
{
name: 'lucide:parentheses',
component: createIconifyIcon('lucide:parentheses'),
},
{ name: 'lucide:slash', component: createIconifyIcon('lucide:slash') },
{
name: 'lucide:backslash',
component: createIconifyIcon('lucide:backslash'),
},
{ name: 'lucide:at-sign', component: createIconifyIcon('lucide:at-sign') },
{ name: 'lucide:percent', component: createIconifyIcon('lucide:percent') },
{ name: 'lucide:asterisk', component: createIconifyIcon('lucide:asterisk') },
{
name: 'lucide:ampersand',
component: createIconifyIcon('lucide:ampersand'),
},
{ name: 'lucide:equal', component: createIconifyIcon('lucide:equal') },
{
name: 'lucide:not-equal',
component: createIconifyIcon('lucide:not-equal'),
},
{
name: 'lucide:greater-than',
component: createIconifyIcon('lucide:greater-than'),
},
{
name: 'lucide:less-than',
component: createIconifyIcon('lucide:less-than'),
},
{
name: 'lucide:greater-than-equal',
component: createIconifyIcon('lucide:greater-than-equal'),
},
{
name: 'lucide:less-than-equal',
component: createIconifyIcon('lucide:less-than-equal'),
},
{ name: 'lucide:minus', component: createIconifyIcon('lucide:minus') },
{ name: 'lucide:divide', component: createIconifyIcon('lucide:divide') },
{ name: 'lucide:multiply', component: createIconifyIcon('lucide:multiply') },
{ name: 'lucide:infinity', component: createIconifyIcon('lucide:infinity') },
{ name: 'lucide:pi', component: createIconifyIcon('lucide:pi') },
{ name: 'lucide:sigma', component: createIconifyIcon('lucide:sigma') },
{ name: 'lucide:alpha', component: createIconifyIcon('lucide:alpha') },
{ name: 'lucide:beta', component: createIconifyIcon('lucide:beta') },
{ name: 'lucide:gamma', component: createIconifyIcon('lucide:gamma') },
{ name: 'lucide:delta', component: createIconifyIcon('lucide:delta') },
{ name: 'lucide:epsilon', component: createIconifyIcon('lucide:epsilon') },
{ name: 'lucide:zeta', component: createIconifyIcon('lucide:zeta') },
{ name: 'lucide:eta', component: createIconifyIcon('lucide:eta') },
{ name: 'lucide:theta', component: createIconifyIcon('lucide:theta') },
{ name: 'lucide:iota', component: createIconifyIcon('lucide:iota') },
{ name: 'lucide:kappa', component: createIconifyIcon('lucide:kappa') },
{ name: 'lucide:lambda', component: createIconifyIcon('lucide:lambda') },
{ name: 'lucide:mu', component: createIconifyIcon('lucide:mu') },
{ name: 'lucide:nu', component: createIconifyIcon('lucide:nu') },
{ name: 'lucide:xi', component: createIconifyIcon('lucide:xi') },
{ name: 'lucide:omicron', component: createIconifyIcon('lucide:omicron') },
{ name: 'lucide:rho', component: createIconifyIcon('lucide:rho') },
{ name: 'lucide:tau', component: createIconifyIcon('lucide:tau') },
{ name: 'lucide:upsilon', component: createIconifyIcon('lucide:upsilon') },
{ name: 'lucide:phi', component: createIconifyIcon('lucide:phi') },
{ name: 'lucide:chi', component: createIconifyIcon('lucide:chi') },
{ name: 'lucide:psi', component: createIconifyIcon('lucide:psi') },
{ name: 'lucide:omega', component: createIconifyIcon('lucide:omega') },
{ label: 'Lucide', value: 'lucide' },
{ label: 'Material Design Icons', value: 'mdi' },
{ label: 'Carbon', value: 'carbon' },
{ label: 'Tabler', value: 'tabler' },
{ label: 'Phosphor', value: 'ph' },
{ label: 'Remix Icon', value: 'ri' },
{ label: 'Heroicons', value: 'heroicons' },
{ label: 'Material Symbols', value: 'material-symbols' },
{ label: 'Ant Design Icons', value: 'ant-design' },
{ label: 'Bootstrap Icons', value: 'bi' },
{ label: 'MingCute', value: 'mingcute' },
{ label: 'Logos', value: 'logos' },
{ label: 'Simple Icons', value: 'simple-icons' },
{ label: 'Font Awesome 6', value: 'fa6-solid' },
{ label: 'Iconoir', value: 'iconoir' },
];
// 过滤后的图标
const filteredIcons = computed(() => {
let icons = iconList;
const searchKeyword = ref('');
const selectedCollection = ref('lucide');
const selectedIcon = ref(props.modelValue || '');
const currentPage = ref(1);
const pageSize = 36;
if (searchKeyword.value) {
icons = icons.filter((icon) =>
icon.name.toLowerCase().includes(searchKeyword.value.toLowerCase()),
);
const browseFlat = ref<string[]>([]);
const searchResultIcons = ref<string[]>([]);
const loading = ref(false);
const loadError = ref('');
const collectionCache = new Map<string, string[]>();
function flattenCollectionBody(
prefix: string,
data: {
uncategorized?: string[];
categories?: Record<string, string[]>;
},
): string[] {
const set = new Set<string>();
for (const n of data.uncategorized || []) {
set.add(`${prefix}:${n}`);
}
if (data.categories) {
for (const arr of Object.values(data.categories)) {
for (const n of arr) {
set.add(`${prefix}:${n}`);
}
}
}
return [...set].sort((a, b) => a.localeCompare(b));
}
async function fetchCollectionIcons(prefix: string): Promise<string[]> {
const hit = collectionCache.get(prefix);
if (hit) {
return hit;
}
const res = await fetch(
`${ICONIFY}/collection?prefix=${encodeURIComponent(prefix)}`,
);
if (!res.ok) {
throw new Error(`Iconify collection ${res.status}`);
}
const data = await res.json();
const list = flattenCollectionBody(prefix, data);
collectionCache.set(prefix, list);
return list;
}
const isGlobalMode = computed(
() => selectedCollection.value === GLOBAL_PREFIX,
);
async function refreshIcons() {
const q = searchKeyword.value.trim();
const prefix = selectedCollection.value;
loading.value = true;
loadError.value = '';
try {
if (q) {
let url = `${ICONIFY}/search?query=${encodeURIComponent(q)}&limit=400`;
if (!isGlobalMode.value) {
url += `&prefix=${encodeURIComponent(prefix)}`;
}
const res = await fetch(url);
if (!res.ok) {
throw new Error(`Iconify search ${res.status}`);
}
const data = await res.json();
searchResultIcons.value = Array.isArray(data.icons) ? data.icons : [];
} else {
searchResultIcons.value = [];
if (isGlobalMode.value) {
browseFlat.value = [];
} else {
browseFlat.value = await fetchCollectionIcons(prefix);
}
}
} catch (e: unknown) {
loadError.value = e instanceof Error ? e.message : '加载失败';
browseFlat.value = [];
searchResultIcons.value = [];
} finally {
loading.value = false;
}
}
const debouncedRefresh = useDebounceFn(refreshIcons, 320);
const activeSource = computed(() => {
return searchKeyword.value.trim()
? searchResultIcons.value
: browseFlat.value;
});
const displayedIconNames = computed(() => {
const source = activeSource.value;
const start = (currentPage.value - 1) * pageSize;
const end = start + pageSize;
return icons.slice(start, end);
return source.slice(start, start + pageSize);
});
// 总页数
const totalPages = computed(() => {
let total = iconList.length;
if (searchKeyword.value) {
total = iconList.filter((icon) =>
icon.name.toLowerCase().includes(searchKeyword.value.toLowerCase()),
).length;
}
return Math.ceil(total / pageSize);
const n = activeSource.value.length;
return Math.max(1, Math.ceil(n / pageSize));
});
// 可见页码
const visiblePages = computed(() => {
const pages = [];
const pages: number[] = [];
const total = totalPages.value;
const current = currentPage.value;
@@ -452,26 +161,23 @@ const visiblePages = computed(() => {
for (let i = 1; i <= total; i++) {
pages.push(i);
}
} else if (current <= 4) {
for (let i = 1; i <= 5; i++) {
pages.push(i);
}
} else if (current >= total - 3) {
for (let i = total - 4; i <= total; i++) {
pages.push(i);
}
} else {
if (current <= 4) {
for (let i = 1; i <= 5; i++) {
pages.push(i);
}
} else if (current >= total - 3) {
for (let i = total - 4; i <= total; i++) {
pages.push(i);
}
} else {
for (let i = current - 2; i <= current + 2; i++) {
pages.push(i);
}
for (let i = current - 2; i <= current + 2; i++) {
pages.push(i);
}
}
return pages;
});
// 是否显示省略号
const showEllipsis = computed(() => {
return (
totalPages.value > 7 &&
@@ -480,70 +186,101 @@ const showEllipsis = computed(() => {
);
});
// 选择图标
const selectIcon = (iconName: string) => {
selectedIcon.value = iconName;
emit('update:modelValue', iconName);
};
// 搜索处理
const handleSearch = () => {
currentPage.value = 1;
};
// 跳转页面
const goToPage = (page: number) => {
if (page >= 1 && page <= totalPages.value) {
currentPage.value = page;
}
};
// 监听props变化
watch(
() => props.modelValue,
(newVal) => {
selectedIcon.value = newVal || '';
},
);
watch(selectedCollection, () => {
currentPage.value = 1;
debouncedRefresh();
});
watch(searchKeyword, () => {
currentPage.value = 1;
debouncedRefresh();
});
onMounted(() => {
void refreshIcons();
});
</script>
<template>
<div class="icon-selector">
<!-- 搜索框 -->
<div class="mb-2 flex w-full">
<div class="mb-2 flex flex-col gap-2 px-2">
<Select
v-model:value="selectedCollection"
:options="COLLECTION_OPTIONS"
class="w-full"
placeholder="图标集"
/>
<Input
v-model:value="searchKeyword"
placeholder="搜索图标..."
class="mx-2"
@input="handleSearch"
placeholder="搜索图标(留空则浏览当前图标集)"
allow-clear
/>
</div>
<!-- 图标网格 -->
<div
class="grid max-h-[360px] w-full grid-cols-6 justify-items-center overflow-y-auto"
>
<button
v-for="icon in filteredIcons"
:key="icon.name"
class="focus-visible:ring-ring hover:bg-accent hover:text-accent-foreground text-foreground/80 inline-flex h-8 w-8 items-center justify-center whitespace-nowrap rounded-full px-1 text-lg font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:class="{
'bg-primary text-primary-foreground': selectedIcon === icon.name,
}"
@click="selectIcon(icon.name)"
:title="icon.name"
<Spin :spinning="loading">
<div
v-if="loadError"
class="text-destructive px-2 py-4 text-center text-sm"
>
<component :is="icon.component" />
</button>
</div>
{{ loadError }}
</div>
<div
v-else-if="
!searchKeyword.trim() &&
isGlobalMode &&
displayedIconNames.length === 0
"
class="text-muted-foreground px-2 py-8 text-center text-sm"
>
「全局搜索」模式下请在上框输入关键词;或切换到具体图标集以分页浏览。
</div>
<div
v-else
class="grid max-h-[360px] w-full grid-cols-6 justify-items-center overflow-y-auto"
>
<button
v-for="name in displayedIconNames"
:key="name"
type="button"
class="focus-visible:ring-ring hover:bg-accent hover:text-accent-foreground text-foreground/80 inline-flex h-8 w-8 items-center justify-center whitespace-nowrap rounded-full px-1 text-lg font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:class="{
'bg-primary text-primary-foreground': selectedIcon === name,
}"
:title="name"
@click="selectIcon(name)"
>
<IconifyIcon :icon="name" class="size-4" />
</button>
</div>
</Spin>
<!-- 分页 -->
<div
class="flex-center flex justify-end overflow-hidden border-t py-2 pr-3"
>
<nav size="small">
<div class="flex w-full items-center gap-1">
<button
type="button"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-sm font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:disabled="currentPage === 1"
@click="goToPage(1)"
@@ -566,6 +303,7 @@ watch(
</button>
<button
type="button"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-sm font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:disabled="currentPage === 1"
@click="goToPage(currentPage - 1)"
@@ -586,17 +324,18 @@ watch(
</svg>
</button>
<button
v-for="page in visiblePages"
:key="page"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-sm font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:class="{
'bg-primary text-primary-foreground shadow': page === currentPage,
}"
@click="goToPage(page)"
>
{{ page }}
</button>
<template v-for="page in visiblePages" :key="page">
<button
type="button"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-xs font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:class="{
'bg-primary text-primary-foreground': currentPage === page,
}"
@click="goToPage(page)"
>
{{ page }}
</button>
</template>
<div
v-if="showEllipsis"
@@ -621,6 +360,7 @@ watch(
</div>
<button
type="button"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-sm font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:disabled="currentPage === totalPages"
@click="goToPage(currentPage + 1)"
@@ -642,6 +382,7 @@ watch(
</button>
<button
type="button"
class="focus-visible:ring-ring border-input bg-background hover:bg-accent hover:text-accent-foreground inline-flex size-5 items-center justify-center whitespace-nowrap rounded-md border p-0 text-sm font-medium shadow-sm transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50"
:disabled="currentPage === totalPages"
@click="goToPage(totalPages)"
@@ -674,13 +415,6 @@ watch(
max-width: 400px;
}
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
gap: 4px;
padding: 8px;
}
button {
transition: all 0.2s ease;
}
@@ -1,10 +1,10 @@
<script setup lang="ts">
import type { MenuVO } from '#/api/system/menu/types';
import { h, ref } from 'vue';
import { ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
import {
Form,
@@ -20,6 +20,7 @@ import {
import {
createMenuApi,
getAllMenusApi,
getMenuDetailApi,
updateMenuApi,
} from '#/api/system/menu';
import {
@@ -30,9 +31,13 @@ import { getAllPermissionsApi } from '#/api/system/permission';
import IconSelector from './IconSelector.vue';
type MenuFormSuccessPayload =
| { mode: 'update'; menu: MenuVO.MenuInfo }
| { mode: 'create' };
const emit = defineEmits<{
cancel: [];
success: [];
success: [payload?: MenuFormSuccessPayload];
}>();
// 创建Vben Modal实例
@@ -95,17 +100,9 @@ const permissionOptions = ref<{ label: string; value: number }[]>([]);
const selectedPermissions = ref<number[]>([]);
const originalPermissions = ref<number[]>([]);
// 创建图标组件
/** 无图标时占位,点击仍打开选择器 */
const GridIcon = createIconifyIcon('lucide:grid-3x3');
// 图标选择器内容
const iconSelectorContent = h(IconSelector, {
modelValue: formData.value.icon,
'onUpdate:modelValue': (value: string) => {
formData.value.icon = value;
},
});
// 获取父菜单选项
async function fetchParentMenuOptions() {
try {
@@ -301,31 +298,36 @@ async function handleSubmit() {
};
if (isEdit.value && currentRecord.value) {
// 更新菜单
await updateMenuApi(currentRecord.value.id, submitData);
const menuId = currentRecord.value.id;
await updateMenuApi(menuId, submitData);
message.success('更新菜单成功');
// 处理权限变更
await handlePermissionChange();
} else {
// 创建菜单
const newMenu = await createMenuApi(submitData);
message.success('创建菜单成功');
// 如果有选择的权限,为新菜单添加权限
if (selectedPermissions.value.length > 0 && newMenu?.id) {
for (const permissionId of selectedPermissions.value) {
await addMenuPermissionApi({
menuId: newMenu.id,
permissionId,
});
}
message.success('权限分配成功');
}
// 详情含最新权限,供列表行就地合并,避免整表 reload 导致树折叠
const menu = await getMenuDetailApi(menuId);
emit('success', { mode: 'update', menu });
close();
return;
}
// 创建菜单(结构变化多,仍整表刷新)
const newMenu = await createMenuApi(submitData);
message.success('创建菜单成功');
if (selectedPermissions.value.length > 0 && newMenu?.id) {
for (const permissionId of selectedPermissions.value) {
await addMenuPermissionApi({
menuId: newMenu.id,
permissionId,
});
}
message.success('权限分配成功');
}
emit('success', { mode: 'create' });
close();
emit('success');
} catch (error) {
console.error('提交菜单失败:', error);
message.error('提交菜单失败');
@@ -419,23 +421,38 @@ defineExpose({
/>
</Form.Item>
<Form.Item label="路由图标" name="icon">
<!-- 外层只负责标签;name 仅绑在触发 Input 上,避免 Popover 浮层内控件被同一 Form.Item 收集导致 FormItemContext 刷屏 -->
<Form.Item label="路由图标">
<Popover
placement="bottomRight"
trigger="click"
:content="iconSelectorContent"
overlay-class-name="icon-selector-popover"
>
<Input
v-model:value="formData.icon"
placeholder="请输入路由图标"
readonly
style="cursor: pointer"
>
<template #suffix>
<GridIcon class="h-4 w-4 cursor-pointer" />
</template>
</Input>
<template #content>
<IconSelector v-model="formData.icon" />
</template>
<!-- Popover Trigger 需要落在真实 DOM 上;no-style 的 Form.Item 常无包裹节点,会导致点击无响应 -->
<div class="w-full">
<Form.Item name="icon" no-style>
<Input
v-model:value="formData.icon"
placeholder="点击选择或查看 Iconify 图标名"
readonly
style="cursor: pointer"
>
<template #prefix>
<span class="inline-flex items-center pr-1">
<IconifyIcon
v-if="formData.icon"
:icon="formData.icon"
class="size-4 shrink-0 text-foreground"
/>
<GridIcon v-else class="size-4 shrink-0 opacity-40" />
</span>
</template>
</Input>
</Form.Item>
</div>
</Popover>
</Form.Item>
@@ -5,7 +5,7 @@ import type { MenuVO } from '#/api/system/menu/types';
import { onMounted, ref } from 'vue';
import { Page } from '@vben/common-ui';
import { createIconifyIcon } from '@vben/icons';
import { createIconifyIcon, IconifyIcon } from '@vben/icons';
import { Button, message, Popconfirm, Space, Tag } from 'ant-design-vue';
@@ -23,8 +23,6 @@ defineOptions({ name: 'MenuManagement' });
const PlusIcon = createIconifyIcon('lucide:plus');
const EditIcon = createIconifyIcon('lucide:edit');
const TrashIcon = createIconifyIcon('lucide:trash-2');
const FolderIcon = createIconifyIcon('lucide:folder');
const FileIcon = createIconifyIcon('lucide:file');
const PlusCircleIcon = createIconifyIcon('lucide:plus-circle');
const ExpandIcon = createIconifyIcon('lucide:chevrons-down');
const CollapseIcon = createIconifyIcon('lucide:chevrons-up');
@@ -33,6 +31,21 @@ const CollapseIcon = createIconifyIcon('lucide:chevrons-up');
const loading = ref(false);
const menuFormModalRef = ref();
/** 与 proxy query 返回的 `items` 同一数组引用,用于就地更新行,避免依赖 getTableData 结构差异导致误判走 reload */
const lastMenuFlatItems = ref<MenuVO.MenuInfo[]>([]);
function normalizeMenuParentId(v: unknown): number | null {
if (v === null || v === undefined) {
return null;
}
const n = Number(v);
return Number.isFinite(n) && n > 0 ? n : null;
}
function sameMenuParent(a: unknown, b: unknown) {
return normalizeMenuParentId(a) === normalizeMenuParentId(b);
}
// VbenVxeGrid 表格配置
const gridOptions: VxeGridProps<any> = {
columns: [
@@ -56,6 +69,13 @@ const gridOptions: VxeGridProps<any> = {
minWidth: 150,
showOverflow: 'tooltip' as const,
},
{
field: 'icon',
title: '图标',
width: 72,
align: 'center' as const,
slots: { default: 'icon' },
},
{
field: 'path',
title: '路由路径',
@@ -98,19 +118,15 @@ const gridOptions: VxeGridProps<any> = {
height: 'auto',
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
resizable: true, // 启用列宽调整
rowConfig: {
isHover: true,
drag: true, // 启用行拖拽
},
showOverflow: true,
size: 'small',
rowDragConfig: {
isPeerDrag: true, // 启用同层级拖拽限制
},
columnConfig: {
// 列配置
},
toolbarConfig: {
custom: true,
refresh: true,
@@ -130,6 +146,7 @@ const gridOptions: VxeGridProps<any> = {
try {
loading.value = true;
const data = await getAllMenusApi();
lastMenuFlatItems.value = data;
// 直接返回扁平化数据,让 vxe-table 自动构建树形结构
return {
items: data,
@@ -138,6 +155,7 @@ const gridOptions: VxeGridProps<any> = {
} catch (error) {
console.error('获取菜单列表失败:', error);
message.error('获取菜单列表失败');
lastMenuFlatItems.value = [];
return {
items: [],
total: 0,
@@ -157,6 +175,39 @@ function refreshTable() {
}
}
function getGridMenuFlatFallback(): MenuVO.MenuInfo[] {
const grid = gridApi?.grid as any;
if (!grid?.getTableData) {
return [];
}
const td = grid.getTableData();
return (td?.fullData ?? td?.tableData ?? []) as MenuVO.MenuInfo[];
}
/** 就地合并更新后的菜单行,保持树展开状态;父级变更时需整表重建树结构 */
function patchMenuRowFromServer(menu: MenuVO.MenuInfo) {
if (!menu?.id) {
refreshTable();
return;
}
const idNum = Number(menu.id);
const list =
lastMenuFlatItems.value.length > 0
? lastMenuFlatItems.value
: getGridMenuFlatFallback();
const row = list.find((r) => Number(r.id) === idNum);
if (!row) {
refreshTable();
return;
}
if (!sameMenuParent(row.parentId, menu.parentId)) {
refreshTable();
return;
}
Object.assign(row, menu);
row.permissions = menu.permissions ? [...menu.permissions] : [];
}
// 处理添加菜单
function handleAdd() {
menuFormModalRef.value?.open(false);
@@ -184,8 +235,14 @@ async function handleDelete(record: MenuVO.MenuInfo) {
}
}
// 处理模态框成功
function handleModalSuccess() {
// 处理模态框成功:更新菜单只合并当前行;新建仍整表刷新
function handleModalSuccess(
payload?: { mode: 'update'; menu: MenuVO.MenuInfo } | { mode: 'create' },
) {
if (payload?.mode === 'update' && payload.menu) {
patchMenuRowFromServer(payload.menu);
return;
}
refreshTable();
}
@@ -274,26 +331,6 @@ function getMenuTypeColor(type: string) {
return colorMap[type] || 'default';
}
// 获取菜单图标
function getMenuIcon(menu: MenuVO.MenuInfo) {
if (menu.icon) {
return createIconifyIcon(menu.icon);
}
// 根据菜单类型返回默认图标
switch (menu.type) {
case 'catalog': {
return FolderIcon;
}
case 'embedded':
case 'menu': {
return FileIcon;
}
default: {
return FileIcon;
}
}
}
// 创建 VbenVxeGrid 实例
const [Grid, gridApi] = useVbenVxeGrid({
gridOptions,
@@ -355,9 +392,13 @@ onMounted(() => {
<!-- 菜单名称列 - 移除自定义插槽以支持拖拽控件正常显示 -->
<!-- 图标列 -->
<template #icon="{ row }: { row: any }">
<div v-if="row.icon" class="flex items-center justify-center">
<component :is="getMenuIcon(row)" class="h-4 w-4 text-blue-600" />
<template #icon="{ row }: { row: MenuVO.MenuInfo }">
<div
v-if="row.icon"
class="flex items-center justify-center"
:title="row.icon"
>
<IconifyIcon :icon="row.icon" class="size-4 text-blue-600" />
</div>
<span v-else class="text-gray-400">-</span>
</template>
@@ -69,7 +69,6 @@ function flattenTree(nodes: PermissionVO.PermissionInfoVO[], level = 0, result:
const gridOptions: VxeGridProps<FlatNode> = {
border: true,
stripe: true,
rowConfig: { isHover: true },
showOverflow: true,
size: 'small',
expandAll: true,