feat(wms):增加 category 模块的迁移

pull/345/head
YunaiV 2026-05-17 16:47:27 +08:00
parent bb63ca9541
commit 0163794e3f
11 changed files with 1128 additions and 0 deletions

View File

@ -0,0 +1,2 @@
export { default as WmsItemCategorySelect } from './item-category-select.vue';
export { default as WmsItemCategoryTree } from './item-category-tree.vue';

View File

@ -0,0 +1,68 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { computed, onMounted, ref } from 'vue';
import { handleTree } from '@vben/utils';
import { TreeSelect } from 'ant-design-vue';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
/** WMS 商品分类选择器 */
defineOptions({ name: 'WmsItemCategorySelect', inheritAttrs: false });
const props = withDefaults(
defineProps<{
allowClear?: boolean;
disabled?: boolean;
modelValue?: number;
placeholder?: string;
}>(),
{
allowClear: true,
disabled: false,
modelValue: undefined,
placeholder: '请选择商品分类',
},
);
const emit = defineEmits<{
'update:modelValue': [value: number | undefined];
}>();
const categoryTree = ref<WmsItemCategoryApi.ItemCategory[]>([]);
const selectValue = computed({
get: () => props.modelValue,
set: (value: number | undefined) => {
emit('update:modelValue', value);
},
});
/** 查询商品分类树 */
async function getCategoryTree() {
const data = await getItemCategorySimpleList();
categoryTree.value = handleTree(data, 'id', 'parentId');
}
onMounted(() => {
getCategoryTree();
});
</script>
<template>
<TreeSelect
v-bind="$attrs"
v-model:value="selectValue"
:allow-clear="allowClear"
:disabled="disabled"
:field-names="{ children: 'children', label: 'name', value: 'id' }"
:placeholder="placeholder"
:tree-data="categoryTree"
class="w-full"
tree-default-expand-all
tree-node-filter-prop="name"
show-search
/>
</template>

View File

@ -0,0 +1,123 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { onMounted, ref, watch } from 'vue';
import { IconifyIcon } from '@vben/icons';
import { handleTree } from '@vben/utils';
import { Input, Spin, Tree } from 'ant-design-vue';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
/** WMS 商品分类树组件 */
defineOptions({ name: 'WmsItemCategoryTree' });
withDefaults(
defineProps<{
filterPlaceholder?: string;
}>(),
{
filterPlaceholder: '请输入分类名称',
},
);
const emit = defineEmits<{
nodeClick: [categoryId: number | undefined];
}>();
const loading = ref(false);
const filterText = ref('');
const currentNodeId = ref<number>();
const selectedKeys = ref<number[]>([]);
const categoryList = ref<WmsItemCategoryApi.ItemCategory[]>([]);
const categoryTree = ref<any[]>([]);
/** 加载分类树 */
async function loadTree() {
loading.value = true;
try {
const data = await getItemCategorySimpleList();
categoryList.value = data;
categoryTree.value = handleTree(data, 'id', 'parentId');
} finally {
loading.value = false;
}
}
/** 处理搜索逻辑 */
function handleSearch(value: string) {
filterText.value = value;
const filteredList = value
? categoryList.value.filter((item) => item.name?.includes(value))
: categoryList.value;
categoryTree.value = handleTree(filteredList, 'id', 'parentId');
}
/** 处理节点点击:支持点击同一节点取消选中 */
function handleSelect(_selectedKeys: any[], info: any) {
const row = info.node.dataRef as WmsItemCategoryApi.ItemCategory;
if (currentNodeId.value === row.id) {
currentNodeId.value = undefined;
selectedKeys.value = [];
emit('nodeClick', undefined);
return;
}
currentNodeId.value = row.id;
selectedKeys.value = row.id === undefined ? [] : [row.id];
emit('nodeClick', row.id);
}
/** 清空选中状态 */
function reset() {
currentNodeId.value = undefined;
filterText.value = '';
selectedKeys.value = [];
categoryTree.value = handleTree(categoryList.value, 'id', 'parentId');
}
/** 设置当前选中分类 */
function setCurrent(categoryId: number) {
currentNodeId.value = categoryId;
selectedKeys.value = [categoryId];
}
watch(filterText, (value) => {
handleSearch(value);
});
defineExpose({ reset, setCurrent });
onMounted(() => {
loadTree();
});
</script>
<template>
<div class="h-full">
<Input
v-model:value="filterText"
:placeholder="filterPlaceholder"
allow-clear
class="w-full"
>
<template #prefix>
<IconifyIcon class="size-4" icon="lucide:search" />
</template>
</Input>
<Spin :spinning="loading" wrapper-class-name="w-full">
<Tree
v-if="categoryTree.length > 0"
:default-expand-all="true"
:field-names="{ title: 'name', key: 'id', children: 'children' }"
:selected-keys="selectedKeys"
:tree-data="categoryTree"
class="pt-2"
@select="handleSelect"
/>
<div v-else-if="!loading" class="py-4 text-center text-gray-500">
暂无数据
</div>
</Spin>
</div>
</template>

View File

@ -0,0 +1,187 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { h } from 'vue';
import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';
import { Button } from 'ant-design-vue';
import { z } from '#/adapter/form';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
import { generateWmsCode } from '#/views/wms/utils/constants';
/** 新增/修改商品分类的表单 */
export function useFormSchema(formApi?: any): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'parentId',
label: '上级分类',
component: 'ApiTreeSelect',
componentProps: {
allowClear: true,
api: async () => {
const data = await getItemCategorySimpleList();
return [
{
id: 0,
name: '顶级分类',
children: handleTree(data),
},
];
},
childrenField: 'children',
labelField: 'name',
placeholder: '请选择上级分类',
treeDefaultExpandAll: true,
treeNodeFilterProp: 'name',
valueField: 'id',
},
rules: 'selectRequired',
},
{
fieldName: 'code',
label: '分类编号',
component: 'Input',
componentProps: {
maxLength: 20,
placeholder: '请输入分类编号',
},
rules: z.string().min(1, '分类编号不能为空').max(20),
suffix: () => {
return h(
Button,
{
type: 'default',
onClick: () => {
formApi?.setFieldValue('code', generateWmsCode('C'));
},
},
{ default: () => '生成' },
);
},
},
{
fieldName: 'name',
label: '分类名称',
component: 'Input',
componentProps: {
placeholder: '请输入分类名称',
},
rules: 'required',
},
{
fieldName: 'sort',
label: '显示排序',
component: 'InputNumber',
componentProps: {
class: '!w-full',
min: 0,
},
rules: z.number().default(0),
},
{
fieldName: 'status',
label: '状态',
component: 'RadioGroup',
componentProps: {
buttonStyle: 'solid',
optionType: 'button',
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
},
rules: z.number().default(CommonStatusEnum.ENABLE),
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'code',
label: '分类编号',
component: 'Input',
componentProps: {
allowClear: true,
placeholder: '请输入分类编号',
},
},
{
fieldName: 'name',
label: '分类名称',
component: 'Input',
componentProps: {
allowClear: true,
placeholder: '请输入分类名称',
},
},
{
fieldName: 'status',
label: '分类状态',
component: 'Select',
componentProps: {
allowClear: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
placeholder: '请选择分类状态',
},
},
];
}
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<WmsItemCategoryApi.ItemCategory>['columns'] {
return [
{
field: 'name',
title: '分类名称',
minWidth: 200,
align: 'left',
treeNode: true,
},
{
field: 'code',
title: '分类编号',
width: 160,
align: 'center',
},
{
field: 'sort',
title: '排序',
width: 120,
align: 'center',
},
{
field: 'status',
title: '状态',
width: 120,
align: 'center',
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS },
},
},
{
field: 'createTime',
title: '创建时间',
width: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 240,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@ -0,0 +1,102 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { CommonStatusEnum } from '@vben/constants';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createItemCategory,
getItemCategory,
updateItemCategory,
} from '#/api/wms/md/item/category';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
defineOptions({ name: 'WmsItemCategoryForm' });
const emit = defineEmits(['success']);
const formData = ref<WmsItemCategoryApi.ItemCategory>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['商品分类'])
: $t('ui.actionTitle.create', ['商品分类']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 80,
},
layout: 'horizontal',
schema: [],
showDefaultActions: false,
});
/** 表单 schema 需要 formApi 引用,所以通过 setState 设置 schema */
formApi.setState({ schema: useFormSchema(formApi) });
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
//
const data =
(await formApi.getValues()) as WmsItemCategoryApi.ItemCategory;
try {
await (formData.value?.id
? updateItemCategory(data)
: createItemCategory(data));
//
await modalApi.close();
emit('success');
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
await formApi.resetForm();
const data = modalApi.getData<WmsItemCategoryApi.ItemCategory>();
if (!data || !data.id) {
formData.value = data;
await formApi.setValues({
sort: 0,
status: CommonStatusEnum.ENABLE,
...data,
});
return;
}
//
modalApi.lock();
try {
formData.value = await getItemCategory(data.id);
// values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle" class="w-1/4">
<Form class="mx-4" />
</Modal>
</template>

View File

@ -0,0 +1,2 @@
export { default as WmsItemCategorySelect } from './item-category-select.vue';
export { default as WmsItemCategoryTree } from './item-category-tree.vue';

View File

@ -0,0 +1,69 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { computed, onMounted, ref } from 'vue';
import { handleTree } from '@vben/utils';
import { ElTreeSelect } from 'element-plus';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
/** WMS 商品分类选择器 */
defineOptions({ name: 'WmsItemCategorySelect', inheritAttrs: false });
const props = withDefaults(
defineProps<{
clearable?: boolean;
disabled?: boolean;
modelValue?: number;
placeholder?: string;
}>(),
{
clearable: true,
disabled: false,
modelValue: undefined,
placeholder: '请选择商品分类',
},
);
const emit = defineEmits<{
'update:modelValue': [value: number | undefined];
}>();
const categoryTree = ref<WmsItemCategoryApi.ItemCategory[]>([]);
const selectValue = computed({
get: () => props.modelValue,
set: (value: number | undefined) => {
emit('update:modelValue', value);
},
});
/** 查询商品分类树 */
async function getCategoryTree() {
const data = await getItemCategorySimpleList();
categoryTree.value = handleTree(data, 'id', 'parentId');
}
onMounted(() => {
getCategoryTree();
});
</script>
<template>
<ElTreeSelect
v-bind="$attrs"
v-model="selectValue"
:clearable="clearable"
:data="categoryTree"
:disabled="disabled"
:placeholder="placeholder"
:props="{ children: 'children', label: 'name' }"
check-strictly
class="w-full"
default-expand-all
filterable
node-key="id"
/>
</template>

View File

@ -0,0 +1,124 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { onMounted, ref, watch } from 'vue';
import { Search } from '@vben/icons';
import { handleTree } from '@vben/utils';
import { ElInput, ElTree } from 'element-plus';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
/** WMS 商品分类树组件 */
defineOptions({ name: 'WmsItemCategoryTree' });
withDefaults(
defineProps<{
filterPlaceholder?: string;
}>(),
{
filterPlaceholder: '请输入分类名称',
},
);
const emit = defineEmits<{
nodeClick: [categoryId: number | undefined];
}>();
const loading = ref(false);
const filterText = ref('');
const currentNodeId = ref<number>();
const categoryList = ref<WmsItemCategoryApi.ItemCategory[]>([]);
const categoryTree = ref<WmsItemCategoryApi.ItemCategory[]>([]);
const treeRef = ref<InstanceType<typeof ElTree>>();
/** 加载分类树 */
async function loadTree() {
loading.value = true;
try {
const data = await getItemCategorySimpleList();
categoryList.value = data;
categoryTree.value = handleTree(data, 'id', 'parentId');
} finally {
loading.value = false;
}
}
/** 处理搜索逻辑 */
function handleSearch(value: string) {
filterText.value = value;
const filteredList = value
? categoryList.value.filter((item) => item.name?.includes(value))
: categoryList.value;
categoryTree.value = handleTree(filteredList, 'id', 'parentId');
}
/** 处理节点点击:支持点击同一节点取消选中 */
function handleNodeClick(row: WmsItemCategoryApi.ItemCategory) {
if (currentNodeId.value === row.id) {
currentNodeId.value = undefined;
treeRef.value?.setCurrentKey(undefined);
emit('nodeClick', undefined);
return;
}
currentNodeId.value = row.id;
emit('nodeClick', row.id);
}
/** 清空选中状态 */
function reset() {
currentNodeId.value = undefined;
filterText.value = '';
treeRef.value?.setCurrentKey(undefined);
categoryTree.value = handleTree(categoryList.value, 'id', 'parentId');
}
/** 设置当前选中分类 */
function setCurrent(categoryId: number) {
currentNodeId.value = categoryId;
treeRef.value?.setCurrentKey(categoryId);
}
watch(filterText, (value) => {
handleSearch(value);
});
defineExpose({ reset, setCurrent });
onMounted(() => {
loadTree();
});
</script>
<template>
<div class="h-full">
<ElInput
v-model="filterText"
:placeholder="filterPlaceholder"
class="w-full"
clearable
>
<template #prefix>
<Search class="size-4" />
</template>
</ElInput>
<div v-loading="loading">
<ElTree
v-if="categoryTree.length > 0"
ref="treeRef"
:data="categoryTree"
:expand-on-click-node="false"
:props="{ label: 'name', children: 'children' }"
class="pt-2"
default-expand-all
highlight-current
node-key="id"
@node-click="handleNodeClick"
/>
<div v-else-if="!loading" class="py-4 text-center text-gray-500">
暂无数据
</div>
</div>
</div>
</template>

View File

@ -0,0 +1,186 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { h } from 'vue';
import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { handleTree } from '@vben/utils';
import { ElButton } from 'element-plus';
import { z } from '#/adapter/form';
import { getItemCategorySimpleList } from '#/api/wms/md/item/category';
import { generateWmsCode } from '#/views/wms/utils/constants';
/** 新增/修改商品分类的表单 */
export function useFormSchema(formApi?: any): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'parentId',
label: '上级分类',
component: 'ApiTreeSelect',
componentProps: {
clearable: true,
api: async () => {
const data = await getItemCategorySimpleList();
return [
{
id: 0,
name: '顶级分类',
children: handleTree(data),
},
];
},
checkStrictly: true,
childrenField: 'children',
defaultExpandAll: true,
labelField: 'name',
placeholder: '请选择上级分类',
valueField: 'id',
},
rules: 'selectRequired',
},
{
fieldName: 'code',
label: '分类编号',
component: 'Input',
componentProps: {
maxLength: 20,
placeholder: '请输入分类编号',
},
rules: z.string().min(1, '分类编号不能为空').max(20),
suffix: () => {
return h(
ElButton,
{
type: 'default',
onClick: () => {
formApi?.setFieldValue('code', generateWmsCode('C'));
},
},
{ default: () => '生成' },
);
},
},
{
fieldName: 'name',
label: '分类名称',
component: 'Input',
componentProps: {
placeholder: '请输入分类名称',
},
rules: 'required',
},
{
fieldName: 'sort',
label: '显示排序',
component: 'InputNumber',
componentProps: {
class: '!w-full',
controlsPosition: 'right',
min: 0,
},
rules: z.number().default(0),
},
{
fieldName: 'status',
label: '状态',
component: 'RadioGroup',
componentProps: {
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
},
rules: z.number().default(CommonStatusEnum.ENABLE),
},
];
}
/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'code',
label: '分类编号',
component: 'Input',
componentProps: {
clearable: true,
placeholder: '请输入分类编号',
},
},
{
fieldName: 'name',
label: '分类名称',
component: 'Input',
componentProps: {
clearable: true,
placeholder: '请输入分类名称',
},
},
{
fieldName: 'status',
label: '分类状态',
component: 'Select',
componentProps: {
clearable: true,
options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
placeholder: '请选择分类状态',
},
},
];
}
/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<WmsItemCategoryApi.ItemCategory>['columns'] {
return [
{
field: 'name',
title: '分类名称',
minWidth: 200,
align: 'left',
treeNode: true,
},
{
field: 'code',
title: '分类编号',
width: 160,
align: 'center',
},
{
field: 'sort',
title: '排序',
width: 120,
align: 'center',
},
{
field: 'status',
title: '状态',
width: 120,
align: 'center',
cellRender: {
name: 'CellDict',
props: { type: DICT_TYPE.COMMON_STATUS },
},
},
{
field: 'createTime',
title: '创建时间',
width: 180,
formatter: 'formatDateTime',
},
{
title: '操作',
width: 240,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@ -0,0 +1,163 @@
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { ref } from 'vue';
import { Page, useVbenModal } from '@vben/common-ui';
import { ElLoading, ElMessage } from 'element-plus';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deleteItemCategory,
getItemCategoryList,
} from '#/api/wms/md/item/category';
import { $t } from '#/locales';
import { useGridColumns, useGridFormSchema } from './data';
import Form from './modules/form.vue';
defineOptions({ name: 'WmsItemCategory' });
const [FormModal, formModalApi] = useVbenModal({
connectedComponent: Form,
destroyOnClose: true,
});
/** 切换树形展开/收缩状态 */
const isExpanded = ref(true);
function handleExpand() {
isExpanded.value = !isExpanded.value;
gridApi.grid.setAllTreeExpand(isExpanded.value);
}
/** 刷新表格 */
function handleRefresh() {
gridApi.query();
}
/** 创建分类 */
function handleCreate() {
formModalApi.setData(null).open();
}
/** 添加下级分类 */
function handleAppend(row: WmsItemCategoryApi.ItemCategory) {
formModalApi.setData({ parentId: row.id }).open();
}
/** 编辑分类 */
function handleEdit(row: WmsItemCategoryApi.ItemCategory) {
formModalApi.setData(row).open();
}
/** 删除分类 */
async function handleDelete(row: WmsItemCategoryApi.ItemCategory) {
const loadingInstance = ElLoading.service({
text: $t('ui.actionMessage.deleting', [row.name]),
});
try {
await deleteItemCategory(row.id!);
ElMessage.success($t('ui.actionMessage.deleteSuccess', [row.name]));
handleRefresh();
} finally {
loadingInstance.close();
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useGridFormSchema(),
},
gridOptions: {
columns: useGridColumns(),
height: 'auto',
keepSource: true,
pagerConfig: {
enabled: false,
},
proxyConfig: {
ajax: {
query: async (_, formValues) => {
return await getItemCategoryList(formValues);
},
},
},
rowConfig: {
keyField: 'id',
isHover: true,
},
toolbarConfig: {
refresh: true,
search: true,
},
treeConfig: {
parentField: 'parentId',
rowField: 'id',
transform: true,
expandAll: true,
reserve: true,
},
} as VxeTableGridOptions<WmsItemCategoryApi.ItemCategory>,
});
</script>
<template>
<Page auto-content-height>
<FormModal @success="handleRefresh" />
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['分类']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['wms:item-category:create'],
onClick: handleCreate,
},
{
label: isExpanded ? '收缩' : '展开',
type: 'primary',
onClick: handleExpand,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: '新增下级',
type: 'primary',
link: true,
icon: ACTION_ICON.ADD,
auth: ['wms:item-category:create'],
onClick: handleAppend.bind(null, row),
},
{
label: $t('common.edit'),
type: 'primary',
link: true,
icon: ACTION_ICON.EDIT,
auth: ['wms:item-category:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'danger',
link: true,
icon: ACTION_ICON.DELETE,
auth: ['wms:item-category:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</Page>
</template>

View File

@ -0,0 +1,102 @@
<script lang="ts" setup>
import type { WmsItemCategoryApi } from '#/api/wms/md/item/category';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { CommonStatusEnum } from '@vben/constants';
import { ElMessage } from 'element-plus';
import { useVbenForm } from '#/adapter/form';
import {
createItemCategory,
getItemCategory,
updateItemCategory,
} from '#/api/wms/md/item/category';
import { $t } from '#/locales';
import { useFormSchema } from '../data';
defineOptions({ name: 'WmsItemCategoryForm' });
const emit = defineEmits(['success']);
const formData = ref<WmsItemCategoryApi.ItemCategory>();
const getTitle = computed(() => {
return formData.value?.id
? $t('ui.actionTitle.edit', ['商品分类'])
: $t('ui.actionTitle.create', ['商品分类']);
});
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
formItemClass: 'col-span-2',
labelWidth: 80,
},
layout: 'horizontal',
schema: [],
showDefaultActions: false,
});
/** 表单 schema 需要 formApi 引用,所以通过 setState 设置 schema */
formApi.setState({ schema: useFormSchema(formApi) });
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
//
const data =
(await formApi.getValues()) as WmsItemCategoryApi.ItemCategory;
try {
await (formData.value?.id
? updateItemCategory(data)
: createItemCategory(data));
//
await modalApi.close();
emit('success');
ElMessage.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
formData.value = undefined;
return;
}
await formApi.resetForm();
const data = modalApi.getData<WmsItemCategoryApi.ItemCategory>();
if (!data || !data.id) {
formData.value = data;
await formApi.setValues({
sort: 0,
status: CommonStatusEnum.ENABLE,
...data,
});
return;
}
//
modalApi.lock();
try {
formData.value = await getItemCategory(data.id);
// values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle" class="w-1/4">
<Form class="mx-4" />
</Modal>
</template>