feat:【mall 商城】商品发布 - 库存价格 - 添加属性【antd】100%: 组件迁移

pull/235/head
puhui999 2025-10-21 11:00:33 +08:00
parent 3dc2d96514
commit 1ad4f2681e
1 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,144 @@
<!-- 商品发布 - 库存价格 - 添加属性 -->
<script lang="ts" setup>
import type { VbenFormSchema } from '#/adapter/form';
import type { MallPropertyApi } from '#/api/mall/product/property';
import { ref, watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createProperty,
getPropertySimpleList,
} from '#/api/mall/product/property';
import { $t } from '#/locales';
defineOptions({ name: 'ProductPropertyAddForm' });
const props = defineProps({
propertyList: {
type: Array,
default: () => [],
},
});
const emit = defineEmits<{
success: [];
}>();
const attributeList = ref<any[]>([]); //
const attributeOptions = ref<MallPropertyApi.Property[]>([]); //
watch(
() => props.propertyList,
(data) => {
if (!data) return;
attributeList.value = data as any[];
},
{
deep: true,
immediate: true,
},
);
//
const formSchema: VbenFormSchema[] = [
{
fieldName: 'name',
label: '属性名称',
component: 'ApiSelect',
componentProps: {
api: async () => {
const data = await getPropertySimpleList();
attributeOptions.value = data;
return data.map((item: MallPropertyApi.Property) => ({
label: item.name,
value: item.name,
}));
},
showSearch: true,
filterOption: true,
placeholder: '请选择属性名称。如果不存在,可手动输入选择',
//
mode: 'tags',
maxTagCount: 1,
allowClear: true,
},
rules: 'required',
},
];
//
const [Form, formApi] = useVbenForm({
schema: formSchema,
showDefaultActions: false,
});
//
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) return;
const values = await formApi.getValues();
const name = Array.isArray(values.name) ? values.name[0] : values.name;
//
for (const attrItem of attributeList.value) {
if (attrItem.name === name) {
message.error('该属性已存在,请勿重复添加');
return;
}
}
// 使
const existProperty = attributeOptions.value.find(
(item: MallPropertyApi.Property) => item.name === name,
);
if (existProperty) {
attributeList.value.push({
id: existProperty.id,
name,
values: [],
});
await modalApi.close();
emit('success');
return;
}
//
modalApi.lock();
try {
const data = { name } as MallPropertyApi.Property;
const propertyId = await createProperty(data);
//
attributeList.value.push({
id: propertyId,
name,
values: [],
});
message.success($t('common.createSuccess'));
await modalApi.close();
emit('success');
} finally {
modalApi.unlock();
}
},
async onOpenChange(isOpen: boolean) {
if (!isOpen) {
return;
}
//
await formApi.resetForm();
},
});
</script>
<template>
<Modal title="添加商品属性">
<Form />
</Modal>
</template>