feat(ui): implement dashboard layout and refactor pages
This commit replaces the previous simple layout with a comprehensive dashboard interface using Nuxt UI Pro components. - Introduced a new default layout with `UDashboardSidebar` and `UDashboardPanel`. - Refactored the main page (`index.vue`) by splitting it into `ItemManageStatistics` and `ItemManageTable` components. - Added new computed stats for monthly growth and total images, and displayed them in new statistic cards. - Reorganized components from `item/` to a new `itemManage/` directory. - Added custom primary and secondary theme colors.
This commit is contained in:
53
app/components/itemManage/DeleteModal.vue
Normal file
53
app/components/itemManage/DeleteModal.vue
Normal file
@@ -0,0 +1,53 @@
|
||||
<template>
|
||||
<UModal
|
||||
v-model:open="open"
|
||||
:title="`删除 ${item?.name}?`"
|
||||
:description="`确定吗?此操作不可被还原。`"
|
||||
>
|
||||
<slot />
|
||||
|
||||
<template #body>
|
||||
<div class="flex justify-end gap-2">
|
||||
<UButton
|
||||
label="Cancel"
|
||||
color="neutral"
|
||||
variant="subtle"
|
||||
@click="open = false"
|
||||
/>
|
||||
<UButton
|
||||
label="Delete"
|
||||
color="error"
|
||||
variant="solid"
|
||||
loading-auto
|
||||
@click="onSubmit"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
</UModal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
item?: Item | null;
|
||||
}>(),
|
||||
{
|
||||
item: null,
|
||||
}
|
||||
);
|
||||
|
||||
const { removeItem } = useItemsStore();
|
||||
|
||||
const open = defineModel<boolean>("open", { default: false });
|
||||
|
||||
async function onSubmit() {
|
||||
// await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
if (!props.item || props.item.id == null) {
|
||||
return;
|
||||
}
|
||||
removeItem(props.item.id);
|
||||
open.value = false;
|
||||
}
|
||||
</script>
|
||||
|
||||
<style></style>
|
||||
201
app/components/itemManage/EditModal.vue
Normal file
201
app/components/itemManage/EditModal.vue
Normal file
@@ -0,0 +1,201 @@
|
||||
<template>
|
||||
<UModal
|
||||
v-model:open="open"
|
||||
:title="isEditMode ? '编辑物品' : '新增物品'"
|
||||
:description="isEditMode ? '修改现有物品信息' : '新增一件新的物品'"
|
||||
>
|
||||
<UButton label="新增" icon="lucide:plus" @click="openModal()" />
|
||||
|
||||
<template #body>
|
||||
<UForm
|
||||
ref="itemForm"
|
||||
:schema="formSchema"
|
||||
:state="itemState"
|
||||
@submit="onSubmit"
|
||||
class="space-y-4"
|
||||
>
|
||||
<UFormField label="品牌名称" name="brand">
|
||||
<USelectMenu
|
||||
v-model="itemState.brand"
|
||||
:items="utils.allBrands.value"
|
||||
placeholder="请输入物品品牌名(可选)"
|
||||
class="w-full"
|
||||
create-item
|
||||
@create="(brand: string) => utils.allBrands.value.push(brand)"
|
||||
/>
|
||||
</UFormField>
|
||||
|
||||
<UFormField label="标品名称" required name="name">
|
||||
<UInput
|
||||
v-model="itemState.name"
|
||||
placeholder="请输入物品名称"
|
||||
class="w-full"
|
||||
/>
|
||||
</UFormField>
|
||||
|
||||
<UFormField label="图片 URL">
|
||||
<UInput
|
||||
v-model="itemState.imageUrl"
|
||||
placeholder="https://img.tootaio.com/i/2025/09/26/gxuf17.jpeg"
|
||||
class="w-full"
|
||||
/>
|
||||
<template #hint>
|
||||
<div class="text-xs text-gray-500">请粘贴您的图床图片链接</div>
|
||||
</template>
|
||||
</UFormField>
|
||||
|
||||
<UFormField label="描述">
|
||||
<UTextarea
|
||||
v-model="itemState.description"
|
||||
placeholder="请输入物品描述(可选)"
|
||||
class="w-full"
|
||||
/>
|
||||
</UFormField>
|
||||
|
||||
<UFormField label="标签">
|
||||
<UInput
|
||||
v-model="itemState.tags"
|
||||
placeholder="Johnnie Walker,Whiskey"
|
||||
class="w-full"
|
||||
/>
|
||||
<template #hint>
|
||||
<div class="text-xs text-gray-500">用英文逗号隔开</div>
|
||||
</template>
|
||||
</UFormField>
|
||||
</UForm>
|
||||
</template>
|
||||
|
||||
<template #footer>
|
||||
<div class="flex justify-end w-full gap-2">
|
||||
<UButton
|
||||
label="取消"
|
||||
color="neutral"
|
||||
variant="subtle"
|
||||
@click="open = false"
|
||||
/>
|
||||
<UButton
|
||||
:label="isEditMode ? '保存修改' : '创建'"
|
||||
color="primary"
|
||||
variant="solid"
|
||||
@click="itemForm.submit()"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
</UModal>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import type { FormSubmitEvent } from "@nuxt/ui";
|
||||
import * as z from "zod";
|
||||
|
||||
const itemForm = ref();
|
||||
const open = ref<boolean>(false);
|
||||
const isEditMode = ref<boolean>(false);
|
||||
const currentId = ref<number | null>(null);
|
||||
|
||||
const toast = useToast();
|
||||
const { utils, isNameAvailable, addItem, editItem, items } = useItemsStore();
|
||||
|
||||
// 🧩 基础校验(动态 schema)
|
||||
const baseSchema = z.object({
|
||||
brand: z.string().optional(),
|
||||
name: z.string().min(1, "名称不能为空"),
|
||||
imageUrl: z.string().optional(),
|
||||
description: z.string().nullable().optional(),
|
||||
tags: z.string().nullable().optional(),
|
||||
});
|
||||
|
||||
// ⚙️ 根据模式动态生成 Schema(编辑模式允许当前名称)
|
||||
const formSchema = computed(() =>
|
||||
baseSchema.refine(
|
||||
(data) =>
|
||||
isEditMode.value
|
||||
? isNameAvailable(currentId.value, data.name, data.brand)
|
||||
: isNameAvailable(null, data.name, data.brand),
|
||||
"该名称已被占用,请更换一个"
|
||||
)
|
||||
);
|
||||
|
||||
type ItemSchema = z.output<typeof baseSchema>;
|
||||
|
||||
const itemState = reactive<ItemSchema>({
|
||||
brand: undefined,
|
||||
name: "",
|
||||
imageUrl: "",
|
||||
description: null,
|
||||
tags: null,
|
||||
});
|
||||
|
||||
/** 🔧 打开 Modal(新增 / 编辑) */
|
||||
const openModal = (item?: Item) => {
|
||||
if (item) {
|
||||
// 编辑模式
|
||||
isEditMode.value = true;
|
||||
currentId.value = item.id;
|
||||
itemState.brand = item.brand;
|
||||
itemState.name = item.name;
|
||||
itemState.imageUrl = item.imageUrl;
|
||||
itemState.description = item.description ?? null;
|
||||
itemState.tags = item.tags?.join(", ") ?? null;
|
||||
} else {
|
||||
// 新增模式
|
||||
isEditMode.value = false;
|
||||
itemState.brand = undefined;
|
||||
currentId.value = null;
|
||||
itemState.name = "";
|
||||
itemState.imageUrl = "";
|
||||
itemState.description = null;
|
||||
itemState.tags = null;
|
||||
}
|
||||
open.value = true;
|
||||
};
|
||||
|
||||
const onSubmit = async (event: FormSubmitEvent<ItemSchema>) => {
|
||||
if (isEditMode.value && currentId.value !== null) {
|
||||
// ✅ 编辑模式
|
||||
const updatedItem: Item = {
|
||||
id: currentId.value,
|
||||
brand: event.data.brand,
|
||||
name: event.data.name,
|
||||
imageUrl: event.data.imageUrl,
|
||||
description: event.data.description,
|
||||
tags: event.data.tags?.split(",").map((tag) => tag.trim()),
|
||||
createdAt:
|
||||
items.value.find((i) => i.id === currentId.value)?.createdAt ??
|
||||
new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
|
||||
editItem(updatedItem);
|
||||
toast.add({
|
||||
title: "已保存修改",
|
||||
description: `${event.data.name} 的信息已更新`,
|
||||
color: "success",
|
||||
});
|
||||
} else {
|
||||
// ✅ 新增模式
|
||||
const newItem: Item = {
|
||||
id: 0, // 由添加函数处理
|
||||
brand: event.data.brand,
|
||||
name: event.data.name,
|
||||
imageUrl: event.data.imageUrl,
|
||||
description: event.data.description,
|
||||
tags: event.data.tags?.split(",").map((tag) => tag.trim()),
|
||||
};
|
||||
|
||||
addItem(newItem);
|
||||
toast.add({
|
||||
title: "成功",
|
||||
description: `${event.data.name} 已添加到数据库中`,
|
||||
color: "success",
|
||||
});
|
||||
}
|
||||
|
||||
open.value = false;
|
||||
};
|
||||
|
||||
// expose method so parent can call modal.openModal(item)
|
||||
defineExpose({ openModal });
|
||||
</script>
|
||||
|
||||
<style></style>
|
||||
76
app/components/itemManage/Statistics.vue
Normal file
76
app/components/itemManage/Statistics.vue
Normal file
@@ -0,0 +1,76 @@
|
||||
<template>
|
||||
<!-- 统计 -->
|
||||
<div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
|
||||
<UCard v-for="stat in statistics" :key="stat.label">
|
||||
<div class="space-y-2">
|
||||
<div class="flex items-center justify-between">
|
||||
<p class="text-sm text-gray-500 dark:text-gray-400">
|
||||
{{ stat.label }}
|
||||
</p>
|
||||
<UIcon
|
||||
:name="stat.icon"
|
||||
:style="`color: ${stat.color}`"
|
||||
class="size-5 text-gray-400"
|
||||
/>
|
||||
</div>
|
||||
<p class="text-3xl font-bold">{{ stat.value }}</p>
|
||||
<div v-if="stat.percentage" class="flex items-center gap-1 text-sm">
|
||||
<UIcon
|
||||
:name="stat.percentageIcon"
|
||||
:class="['size-4', stat.percentageColor]"
|
||||
/>
|
||||
<span :class="['font-medium', stat.percentageColor]">{{
|
||||
stat.percentageChange
|
||||
}}</span>
|
||||
<span class="text-gray-500">{{ stat.percentageLabel }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</UCard>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
const { stats } = useItemsStore();
|
||||
|
||||
// 统计
|
||||
const statistics = [
|
||||
{
|
||||
icon: "lucide:box",
|
||||
label: "总物品数",
|
||||
value: stats.totalItems,
|
||||
color: "blue",
|
||||
},
|
||||
{
|
||||
icon: "lucide:package-plus",
|
||||
label: "本月新增",
|
||||
value: stats.addedThisMonth,
|
||||
color: "green",
|
||||
percentage: true,
|
||||
percentageChange:
|
||||
stats.comparedToLastMonth.value >= 0
|
||||
? `+${stats.comparedToLastMonth.value}%`
|
||||
: `${stats.comparedToLastMonth.value}%`,
|
||||
percentageLabel: "from last month",
|
||||
percentageIcon:
|
||||
stats.comparedToLastMonth.value >= 0
|
||||
? "i-lucide-trending-up"
|
||||
: "i-lucide-trending-down",
|
||||
percentageColor:
|
||||
stats.comparedToLastMonth.value >= 0 ? "text-green-500" : "text-red-500",
|
||||
},
|
||||
{
|
||||
icon: "lucide:history",
|
||||
label: "历史记录",
|
||||
value: computed(() => 0),
|
||||
color: "orange",
|
||||
},
|
||||
{
|
||||
icon: "lucide:image",
|
||||
label: "图片总数",
|
||||
value: stats.totalImages,
|
||||
color: "purple",
|
||||
},
|
||||
];
|
||||
</script>
|
||||
|
||||
<style></style>
|
||||
215
app/components/itemManage/Table.vue
Normal file
215
app/components/itemManage/Table.vue
Normal file
@@ -0,0 +1,215 @@
|
||||
<template>
|
||||
<!-- 列表 -->
|
||||
<div class="flex flex-col flex-1 w-full">
|
||||
<div
|
||||
class="flex justify-between items-center gap-2 overflow-x-auto px-4 py-3.5 border-b border-accented"
|
||||
>
|
||||
<div>
|
||||
<UInput
|
||||
v-model="globalFilter"
|
||||
class="max-w-sm"
|
||||
placeholder="Search..."
|
||||
icon="lucide:search"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<ItemManageEditModal ref="itemEditModal" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 给 UTable 一个 key(tableKey)用于在需要时强制重挂载 -->
|
||||
<UTable
|
||||
:key="tableKey"
|
||||
sticky
|
||||
v-model:global-filter="globalFilter"
|
||||
:data="itemsList"
|
||||
:columns="itemColumns"
|
||||
class="max-h-[640px]"
|
||||
>
|
||||
<template #select-cell="{ row }">
|
||||
<UCheckbox
|
||||
:model-value="row.getIsSelected()"
|
||||
@update:model-value="(value: boolean | 'indeterminate') => row.toggleSelected(!!value)"
|
||||
aria-label="Select row"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<template #name-cell="{ row }">
|
||||
<div class="flex items-center gap-3">
|
||||
<UAvatar
|
||||
:src="row.original.imageUrl"
|
||||
size="lg"
|
||||
:alt="row.original.name"
|
||||
/>
|
||||
<div>
|
||||
<p class="font-medium text-highlighted">
|
||||
<strong>{{ row.original.brand }}</strong>
|
||||
{{ row.original.name }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- tags 空值兜底,并加 key 避免重复 key 警告 -->
|
||||
<template #tags-cell="{ row }">
|
||||
<div class="flex gap-2">
|
||||
<UBadge
|
||||
v-for="(tag, idx) in row.original.tags ?? []"
|
||||
:key="`${row.original.id}-tag-${idx}`"
|
||||
variant="subtle"
|
||||
>
|
||||
{{ tag }}
|
||||
</UBadge>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<template #actions-cell="{ row }">
|
||||
<div class="text-right">
|
||||
<UDropdownMenu
|
||||
:items="getRowItems(row)"
|
||||
:content="{ align: 'end' }"
|
||||
aria-label="Actions dropdown"
|
||||
>
|
||||
<UButton
|
||||
icon="i-lucide-ellipsis-vertical"
|
||||
color="neutral"
|
||||
variant="ghost"
|
||||
class="ml-auto"
|
||||
aria-label="Actions dropdown"
|
||||
/>
|
||||
</UDropdownMenu>
|
||||
</div>
|
||||
</template>
|
||||
</UTable>
|
||||
|
||||
<ItemManageDeleteModal
|
||||
v-model:open="deleteModal.open"
|
||||
:item="deleteModal.selectedItem"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { computed, ref, reactive, watch } from "vue";
|
||||
import type { TableColumn, DropdownMenuItem } from "@nuxt/ui";
|
||||
import type { Row } from "@tanstack/vue-table";
|
||||
|
||||
const UCheckbox = resolveComponent("UCheckbox");
|
||||
const UBadge = resolveComponent("UBadge");
|
||||
const UAvatar = resolveComponent("UAvatar");
|
||||
const toast = useToast();
|
||||
|
||||
const { items } = useItemsStore();
|
||||
|
||||
const itemEditModal = ref<any>(null);
|
||||
|
||||
const deleteModal = reactive<{ open: boolean; selectedItem: Item | null }>({
|
||||
open: false,
|
||||
selectedItem: null,
|
||||
});
|
||||
|
||||
const globalFilter = ref<string>("");
|
||||
|
||||
// --- computed 包装,保证传给 UTable 的始终是响应式且值随 items 变化
|
||||
const itemsList = computed(() => items.value ?? []);
|
||||
|
||||
// --- 强制表格重挂载的 key(当 items 改变时递增)
|
||||
const tableKey = ref(0);
|
||||
// 深度监听 items(数组)变化:每次 items 内容或引用变化都让 tableKey++,从而触发 UTable 重新挂载
|
||||
watch(
|
||||
items,
|
||||
() => {
|
||||
tableKey.value += 1;
|
||||
// Console debug(运行时可在浏览器控制台查看)
|
||||
console.debug(
|
||||
"[items watch] tableKey ->",
|
||||
tableKey.value,
|
||||
"items.length ->",
|
||||
(items.value ?? []).length
|
||||
);
|
||||
},
|
||||
{ deep: true }
|
||||
);
|
||||
|
||||
const itemColumns: TableColumn<Item>[] = [
|
||||
{
|
||||
id: "select",
|
||||
header: ({ table }) =>
|
||||
h(UCheckbox, {
|
||||
modelValue: table.getIsSomePageRowsSelected()
|
||||
? "indeterminate"
|
||||
: table.getIsAllPageRowsSelected(),
|
||||
"onUpdate:modelValue": (value: boolean | "indeterminate") =>
|
||||
table.toggleAllPageRowsSelected(!!value),
|
||||
ariaLabel: "Select all",
|
||||
}),
|
||||
},
|
||||
{
|
||||
accessorKey: "id",
|
||||
header: "#",
|
||||
cell: ({ row }) => `#${row.getValue("id")}`,
|
||||
},
|
||||
// 新增一个 brand 列 —— 让它参与全局搜索,但不实际渲染内容(brand 已在 name-cell 中显示)
|
||||
{
|
||||
accessorKey: "brand",
|
||||
header: "", // 留空,避免影响布局
|
||||
// 明确允许全局过滤(通常是默认 true,但写上更明确)
|
||||
enableGlobalFilter: true,
|
||||
// 不在表格中重复显示 brand(因为你在 name-cell 已经显示了)
|
||||
cell: () => null,
|
||||
},
|
||||
{
|
||||
accessorKey: "name",
|
||||
header: "标品名称",
|
||||
},
|
||||
{
|
||||
accessorKey: "tags",
|
||||
header: "标签",
|
||||
accessorFn: (row: Item) => (row.tags ?? []).join(" "), // <-- 关键:把数组转成字符串
|
||||
enableGlobalFilter: true,
|
||||
// 注意:你仍然使用 template #tags-cell 来渲染标签外观(badge),slot 会优先显示
|
||||
},
|
||||
{
|
||||
id: "actions",
|
||||
header: "", // 不需要标题
|
||||
},
|
||||
];
|
||||
|
||||
function getRowItems(row: Row<Item>): DropdownMenuItem[] {
|
||||
return [
|
||||
{ type: "label", label: "操作" },
|
||||
{
|
||||
label: "编辑",
|
||||
color: "primary",
|
||||
icon: "lucide:pencil-line",
|
||||
onSelect() {
|
||||
itemEditModal.value?.openModal(row.original);
|
||||
toast.add({
|
||||
title: `编辑 ${row.original.name}`,
|
||||
description: "已打开编辑模态框。",
|
||||
color: "warning",
|
||||
icon: "lucide:pencil-line",
|
||||
});
|
||||
},
|
||||
},
|
||||
{ type: "separator" },
|
||||
{
|
||||
label: "删除",
|
||||
color: "error",
|
||||
icon: "lucide:trash-2",
|
||||
onSelect() {
|
||||
deleteModal.selectedItem = row.original;
|
||||
deleteModal.open = true;
|
||||
toast.add({
|
||||
title: `Deleting ${row.original.name}...`,
|
||||
color: "error",
|
||||
icon: "lucide:trash-2",
|
||||
});
|
||||
},
|
||||
},
|
||||
] as const;
|
||||
}
|
||||
</script>
|
||||
|
||||
<style></style>
|
||||
Reference in New Issue
Block a user