feat: mall product property

pull/133/MERGE
xingyu4j 2025-06-06 23:26:09 +08:00
parent fff84e746f
commit c35ef82788
6 changed files with 685 additions and 25 deletions

View File

@ -0,0 +1,176 @@
import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { getPropertySimpleList } from '#/api/mall/product/property';
// ============================== 属性 ==============================
/** 类型新增/修改的表单 */
export function usePropertyFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'name',
label: '名称',
component: 'Input',
componentProps: {
placeholder: '请输入名称',
},
rules: 'required',
},
{
fieldName: 'remark',
label: '备注',
component: 'Textarea',
componentProps: {
placeholder: '请输入备注',
},
},
];
}
/** 类型列表的搜索表单 */
export function usePropertyGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: '名称',
component: 'Input',
componentProps: {
placeholder: '请输入名称',
clearable: true,
},
},
];
}
/** 类型列表的字段 */
export function usePropertyGridColumns(): VxeTableGridOptions['columns'] {
return [
{
field: 'id',
title: '编号',
},
{
field: 'name',
title: '名称',
},
{
field: 'remark',
title: '备注',
},
{
field: 'createTime',
title: '创建时间',
formatter: 'formatDateTime',
},
{
title: '操作',
width: 160,
fixed: 'right',
slots: { default: 'actions' },
},
];
}
// ============================== 值数据 ==============================
/** 数据新增/修改的表单 */
export function useValueFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'id',
component: 'Input',
dependencies: {
triggerFields: [''],
show: () => false,
},
},
{
fieldName: 'propertyId',
label: '属性编号',
component: 'ApiSelect',
componentProps: (values) => {
return {
api: getPropertySimpleList,
labelField: 'name',
valueField: 'id',
disabled: !!values.id,
};
},
rules: 'required',
dependencies: {
triggerFields: [''],
},
},
{
fieldName: 'name',
label: '名称',
component: 'Input',
componentProps: {
placeholder: '请输入名称',
},
rules: 'required',
},
{
fieldName: 'remark',
label: '备注',
component: 'Textarea',
componentProps: {
placeholder: '请输入备注',
},
},
];
}
/** 字典数据列表搜索表单 */
export function useValueGridFormSchema(): VbenFormSchema[] {
return [
{
fieldName: 'name',
label: '名称',
component: 'Input',
componentProps: {
clearable: true,
},
},
];
}
/**
*
*/
export function useValueGridColumns(): VxeTableGridOptions['columns'] {
return [
{
field: 'id',
title: '编号',
},
{
field: 'name',
title: '属性值名称',
},
{
field: 'remark',
title: '备注',
},
{
title: '创建时间',
field: 'createTime',
formatter: 'formatDateTime',
},
{
title: '操作',
width: 160,
fixed: 'right',
slots: { default: 'actions' },
},
];
}

View File

@ -1,34 +1,37 @@
<script lang="ts" setup>
import { ref } from 'vue';
import { Page } from '@vben/common-ui';
import { Button } from 'ant-design-vue';
import { DocAlert } from '#/components/doc-alert';
import PropertyGrid from './modules/property-grid.vue';
import ValueGrid from './modules/value-grid.vue';
const searchPropertyId = ref<number>(); // ID
function handlePropertyIdSelect(propertyId: number) {
searchPropertyId.value = propertyId;
}
</script>
<template>
<Page>
<DocAlert
title="【商品】商品属性"
url="https://doc.iocoder.cn/mall/product-property/"
/>
<Button
danger
type="link"
target="_blank"
href="https://github.com/yudaocode/yudao-ui-admin-vue3"
>
该功能支持 Vue3 + element-plus 版本
</Button>
<br />
<Button
type="link"
target="_blank"
href="https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/product/property/index"
>
可参考
https://github.com/yudaocode/yudao-ui-admin-vue3/blob/master/src/views/mall/product/property/index
代码pull request 贡献给我们
</Button>
<Page auto-content-height>
<template #doc>
<DocAlert
title="【商品】商品属性"
url="https://doc.iocoder.cn/mall/product-property/"
/>
</template>
<div class="flex h-full">
<!-- 左侧属性列表 -->
<div class="w-1/2 pr-3">
<PropertyGrid @select="handlePropertyIdSelect" />
</div>
<!-- 右侧属性数据列表 -->
<div class="w-1/2">
<ValueGrid :property-id="searchPropertyId" />
</div>
</div>
</Page>
</template>

View File

@ -0,0 +1,88 @@
<script lang="ts" setup>
import type { MallPropertyApi } from '#/api/mall/product/property';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createProperty,
getProperty,
updateProperty,
} from '#/api/mall/product/property';
import { $t } from '#/locales';
import { usePropertyFormSchema } from '../data';
const emit = defineEmits(['success']);
const formData = ref<MallPropertyApi.Property>();
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: usePropertyFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
//
const data = (await formApi.getValues()) as MallPropertyApi.Property;
try {
await (formData.value?.id ? updateProperty(data) : createProperty(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;
}
//
const data = modalApi.getData<MallPropertyApi.Property>();
if (!data || !data.id) {
return;
}
modalApi.lock();
try {
formData.value = await getProperty(data.id as number);
// values
if (formData.value) {
await formApi.setValues(formData.value);
}
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>

View File

@ -0,0 +1,142 @@
<script lang="ts" setup>
import type {
VxeGridListeners,
VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { MallPropertyApi } from '#/api/mall/product/property';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { deleteProperty, getPropertyPage } from '#/api/mall/product/property';
import { $t } from '#/locales';
import { usePropertyGridColumns, usePropertyGridFormSchema } from '../data';
import PropertyForm from './property-form.vue';
const emit = defineEmits(['select']);
const [PropertyFormModal, propertyFormModalApi] = useVbenModal({
connectedComponent: PropertyForm,
destroyOnClose: true,
});
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
/** 创建属性 */
function handleCreate() {
propertyFormModalApi.setData(null).open();
}
/** 编辑属性 */
function handleEdit(row: any) {
propertyFormModalApi.setData(row).open();
}
/** 删除属性 */
async function handleDelete(row: MallPropertyApi.Property) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
key: 'action_key_msg',
});
try {
await deleteProperty(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
key: 'action_key_msg',
});
onRefresh();
} finally {
hideLoading();
}
}
/** 表格事件 */
const gridEvents: VxeGridListeners<MallPropertyApi.Property> = {
cellClick: ({ row }) => {
emit('select', row.id);
},
};
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: usePropertyGridFormSchema(),
},
gridOptions: {
columns: usePropertyGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getPropertyPage({
pageNo: page.currentPage,
pageSize: page.pageSize,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
isCurrent: true,
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<MallPropertyApi.Property>,
gridEvents,
});
</script>
<template>
<div class="h-full">
<PropertyFormModal @success="onRefresh" />
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['属性']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['product:property:create'],
onClick: handleCreate,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['product:property:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['product:property:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</div>
</template>

View File

@ -0,0 +1,100 @@
<script lang="ts" setup>
import type { MallPropertyApi } from '#/api/mall/product/property';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import {
createPropertyValue,
getPropertyValue,
updatePropertyValue,
} from '#/api/mall/product/property';
import { $t } from '#/locales';
import { useValueFormSchema } from '../data';
defineOptions({ name: 'MallPropertyValueForm' });
const emit = defineEmits(['success']);
const formData = ref<MallPropertyApi.PropertyValue>();
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: useValueFormSchema(),
showDefaultActions: false,
});
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
//
const data = (await formApi.getValues()) as MallPropertyApi.PropertyValue;
try {
await (formData.value?.id
? updatePropertyValue(data)
: createPropertyValue(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;
}
//
const data = modalApi.getData<
MallPropertyApi.PropertyValue | { propertyId?: string }
>();
// ID
if (data && 'id' in data && data.id) {
modalApi.lock();
try {
formData.value = await getPropertyValue(data.id as number);
// values
if (formData.value) {
await formApi.setValues(formData.value);
}
} finally {
modalApi.unlock();
}
} else if (data && 'propertyId' in data && data.propertyId) {
// propertyId
await formApi.setValues({
propertyId: data.propertyId,
});
}
},
});
</script>
<template>
<Modal :title="getTitle">
<Form class="mx-4" />
</Modal>
</template>

View File

@ -0,0 +1,151 @@
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { MallPropertyApi } from '#/api/mall/product/property';
import { watch } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
deletePropertyValue,
getPropertyValuePage,
} from '#/api/mall/product/property';
import { $t } from '#/locales';
import { useValueGridColumns, useValueGridFormSchema } from '../data';
import ValueForm from './value-form.vue';
const props = defineProps({
propertyId: {
type: Number,
default: undefined,
},
});
const [ValueFormModal, valueFormModalApi] = useVbenModal({
connectedComponent: ValueForm,
destroyOnClose: true,
});
/** 刷新表格 */
function onRefresh() {
gridApi.query();
}
/** 创建字典数据 */
function handleCreate() {
valueFormModalApi.setData({ propertyId: props.propertyId }).open();
}
/** 编辑字典数据 */
function handleEdit(row: MallPropertyApi.PropertyValue) {
valueFormModalApi.setData(row).open();
}
/** 删除字典数据 */
async function handleDelete(row: MallPropertyApi.PropertyValue) {
const hideLoading = message.loading({
content: $t('ui.actionMessage.deleting', [row.name]),
key: 'action_key_msg',
});
try {
await deletePropertyValue(row.id as number);
message.success({
content: $t('ui.actionMessage.deleteSuccess', [row.name]),
key: 'action_key_msg',
});
onRefresh();
} finally {
hideLoading();
}
}
const [Grid, gridApi] = useVbenVxeGrid({
formOptions: {
schema: useValueGridFormSchema(),
},
gridOptions: {
columns: useValueGridColumns(),
height: 'auto',
keepSource: true,
proxyConfig: {
ajax: {
query: async ({ page }, formValues) => {
return await getPropertyValuePage({
pageNo: page.currentPage,
pageSize: page.pageSize,
propertyId: props.propertyId,
...formValues,
});
},
},
},
rowConfig: {
keyField: 'id',
},
toolbarConfig: {
refresh: { code: 'query' },
search: true,
},
} as VxeTableGridOptions<MallPropertyApi.PropertyValue>,
});
/** 监听 dictType 变化,重新查询 */
watch(
() => props.propertyId,
() => {
if (props.propertyId) {
onRefresh();
}
},
);
</script>
<template>
<div class="flex h-full flex-col">
<ValueFormModal @success="onRefresh" />
<Grid table-title="">
<template #toolbar-tools>
<TableAction
:actions="[
{
label: $t('ui.actionTitle.create', ['属性值']),
type: 'primary',
icon: ACTION_ICON.ADD,
auth: ['product:property:create'],
onClick: handleCreate,
},
]"
/>
</template>
<template #actions="{ row }">
<TableAction
:actions="[
{
label: $t('common.edit'),
type: 'link',
icon: ACTION_ICON.EDIT,
auth: ['product:property:update'],
onClick: handleEdit.bind(null, row),
},
{
label: $t('common.delete'),
type: 'link',
danger: true,
icon: ACTION_ICON.DELETE,
auth: ['product:property:delete'],
popConfirm: {
title: $t('ui.actionMessage.deleteConfirm', [row.name]),
confirm: handleDelete.bind(null, row),
},
},
]"
/>
</template>
</Grid>
</div>
</template>