feat: 新增产品分类管理、产品管理功能
- 添加产品列表等接口 - 实现产品信息展示、编辑等功能 - 添加产品分类管理相关功能
This commit is contained in:
parent
da0cb82255
commit
de7568b525
Binary file not shown.
After Width: | Height: | Size: 7.2 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.6 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.7 KiB |
Binary file not shown.
After Width: | Height: | Size: 1.8 KiB |
|
@ -0,0 +1,61 @@
|
|||
import type { ProductVO, ProductForm, ProductQuery } from './model';
|
||||
|
||||
import type { ID, IDS } from '#/api/common';
|
||||
import type { PageResult } from '#/api/common';
|
||||
|
||||
import { commonExport } from '#/api/helper';
|
||||
import { requestClient } from '#/api/request';
|
||||
|
||||
/**
|
||||
* 查询设备产品列表
|
||||
* @param params
|
||||
* @returns 设备产品列表
|
||||
*/
|
||||
export function productList(params?: ProductQuery) {
|
||||
return requestClient.get<PageResult<ProductVO>>('/device/product/list', { params });
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出设备产品列表
|
||||
* @param params
|
||||
* @returns 设备产品列表
|
||||
*/
|
||||
export function productExport(params?: ProductQuery) {
|
||||
return commonExport('/device/product/export', params ?? {});
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询设备产品详情
|
||||
* @param id id
|
||||
* @returns 设备产品详情
|
||||
*/
|
||||
export function productInfo(id: ID) {
|
||||
return requestClient.get<ProductVO>(`/device/product/${id}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增设备产品
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function productAdd(data: ProductForm) {
|
||||
return requestClient.postWithMsg<void>('/device/product', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新设备产品
|
||||
* @param data
|
||||
* @returns void
|
||||
*/
|
||||
export function productUpdate(data: ProductForm) {
|
||||
return requestClient.putWithMsg<void>('/device/product', data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除设备产品
|
||||
* @param id id
|
||||
* @returns void
|
||||
*/
|
||||
export function productRemove(id: ID | IDS) {
|
||||
return requestClient.deleteWithMsg<void>(`/device/product/${id}`);
|
||||
}
|
|
@ -0,0 +1,204 @@
|
|||
import type { PageQuery, BaseEntity } from '#/api/common';
|
||||
|
||||
export interface ProductVO {
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
id: string | number;
|
||||
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
productName: string;
|
||||
|
||||
/**
|
||||
* 产品图片
|
||||
*/
|
||||
imgId: string | number;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
description: string;
|
||||
|
||||
/**
|
||||
* 分类id
|
||||
*/
|
||||
categoryId: string | number;
|
||||
|
||||
/**
|
||||
* 协议id
|
||||
*/
|
||||
protocolId: string | number;
|
||||
|
||||
/**
|
||||
* 物模型
|
||||
*/
|
||||
metadata: string;
|
||||
|
||||
/**
|
||||
* 设备类型
|
||||
*/
|
||||
deviceType: string;
|
||||
|
||||
/**
|
||||
* 协议配置
|
||||
*/
|
||||
protocolConf: string;
|
||||
|
||||
/**
|
||||
* 启用状态 0 禁用
|
||||
*/
|
||||
enabled: string;
|
||||
|
||||
/**
|
||||
* 接入方式
|
||||
*/
|
||||
provider: string | number;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
storePolicy: string;
|
||||
|
||||
/**
|
||||
* 存储策略配置
|
||||
*/
|
||||
storePolicyConf: string;
|
||||
|
||||
}
|
||||
|
||||
export interface ProductForm extends BaseEntity {
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
id?: string | number;
|
||||
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
productName?: string;
|
||||
|
||||
/**
|
||||
* 产品图片
|
||||
*/
|
||||
imgId?: string | number;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* 分类id
|
||||
*/
|
||||
categoryId?: string | number;
|
||||
|
||||
/**
|
||||
* 协议id
|
||||
*/
|
||||
protocolId?: string | number;
|
||||
|
||||
/**
|
||||
* 物模型
|
||||
*/
|
||||
metadata?: string;
|
||||
|
||||
/**
|
||||
* 设备类型
|
||||
*/
|
||||
deviceType?: string;
|
||||
|
||||
/**
|
||||
* 协议配置
|
||||
*/
|
||||
protocolConf?: string;
|
||||
|
||||
/**
|
||||
* 启用状态 0 禁用
|
||||
*/
|
||||
enabled?: string;
|
||||
|
||||
/**
|
||||
* 接入方式
|
||||
*/
|
||||
provider?: string | number;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
storePolicy?: string;
|
||||
|
||||
/**
|
||||
* 存储策略配置
|
||||
*/
|
||||
storePolicyConf?: string;
|
||||
|
||||
}
|
||||
|
||||
export interface ProductQuery extends PageQuery {
|
||||
/**
|
||||
* 产品名称
|
||||
*/
|
||||
productName?: string;
|
||||
|
||||
/**
|
||||
* 产品图片
|
||||
*/
|
||||
imgId?: string | number;
|
||||
|
||||
/**
|
||||
* 描述
|
||||
*/
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* 分类id
|
||||
*/
|
||||
categoryId?: string | number;
|
||||
|
||||
/**
|
||||
* 协议id
|
||||
*/
|
||||
protocolId?: string | number;
|
||||
|
||||
/**
|
||||
* 物模型
|
||||
*/
|
||||
metadata?: string;
|
||||
|
||||
/**
|
||||
* 设备类型
|
||||
*/
|
||||
deviceType?: string;
|
||||
|
||||
/**
|
||||
* 协议配置
|
||||
*/
|
||||
protocolConf?: string;
|
||||
|
||||
/**
|
||||
* 启用状态 0 禁用
|
||||
*/
|
||||
enabled?: string;
|
||||
|
||||
/**
|
||||
* 接入方式
|
||||
*/
|
||||
provider?: string | number;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
storePolicy?: string;
|
||||
|
||||
/**
|
||||
* 存储策略配置
|
||||
*/
|
||||
storePolicyConf?: string;
|
||||
|
||||
/**
|
||||
* 日期范围参数
|
||||
*/
|
||||
params?: any;
|
||||
}
|
|
@ -20,6 +20,18 @@ export function productCategoryList(params?: ProductCategoryQuery) {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询产品分类列表
|
||||
* @param params
|
||||
* @returns 产品分类列表
|
||||
*/
|
||||
export function productCategoryTreeList(params?: ProductCategoryQuery) {
|
||||
return requestClient.get<ProductCategoryVO[]>(
|
||||
`/device/productCategory/treeList`,
|
||||
{ params },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询产品分类详情
|
||||
* @param id id
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
/* 运维字典 */
|
||||
|
||||
// 网络类型
|
||||
export const deviceTypeOptions = [
|
||||
{
|
||||
label: '直连设备',
|
||||
value: 'device',
|
||||
iconUrl: './device/device-type-1.png',
|
||||
tooltip: '直连物联网平台的设备',
|
||||
},
|
||||
{
|
||||
label: '网关子设备',
|
||||
value: 'childrenDevice',
|
||||
iconUrl: './device/device-type-2.png',
|
||||
tooltip: '作为网关的子设备,由网关代理连接到物联网平台',
|
||||
},
|
||||
{
|
||||
label: '网关设备',
|
||||
value: 'gateway',
|
||||
iconUrl: './device/device-type-3.png',
|
||||
tooltip: '能挂载子设备与平台进行通信的设备',
|
||||
},
|
||||
];
|
|
@ -1,4 +1,6 @@
|
|||
/* 本地字典 */
|
||||
|
||||
export * from './device';
|
||||
export * from './operations';
|
||||
|
||||
// 启用状态
|
||||
|
|
|
@ -1,3 +1,6 @@
|
|||
/* 运维字典 */
|
||||
|
||||
// 网络类型
|
||||
export const networkTypeOptions = [
|
||||
{
|
||||
label: 'MQTT客户端',
|
||||
|
@ -16,7 +19,3 @@ export const networkTypeOptions = [
|
|||
'HTTP服务是一个简单的请求-响应的基于TCP的无状态协议。设备通过HTTP服务与平台进行灵活的短链接通信,仅支持设备和平台之间单对单的请求-响应模式',
|
||||
},
|
||||
];
|
||||
export const enabledOptions = [
|
||||
{ label: '启用', value: '1' },
|
||||
{ label: '禁用', value: '0' },
|
||||
];
|
|
@ -31,7 +31,6 @@ const routeMetaMapping: Record<string, Omit<RouteMeta, 'title'>> = {
|
|||
activePath: '/system/role',
|
||||
requireHomeRedirect: true,
|
||||
},
|
||||
|
||||
'/system/oss-config/index': {
|
||||
activePath: '/system/oss',
|
||||
requireHomeRedirect: true,
|
||||
|
|
|
@ -0,0 +1,158 @@
|
|||
import type { FormSchemaGetter } from '#/adapter/form';
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
|
||||
import { h } from 'vue';
|
||||
|
||||
import { deviceTypeOptions, enabledOptions } from '#/constants/dicts';
|
||||
|
||||
export const querySchema: FormSchemaGetter = () => [
|
||||
{
|
||||
component: 'Input',
|
||||
fieldName: 'productName',
|
||||
label: '产品名称',
|
||||
},
|
||||
{
|
||||
component: 'TreeSelect',
|
||||
fieldName: 'categoryId',
|
||||
componentProps: {
|
||||
fieldNames: { children: 'children', label: 'label', value: 'id' },
|
||||
},
|
||||
label: '产品分类',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
options: deviceTypeOptions,
|
||||
},
|
||||
fieldName: 'deviceType',
|
||||
label: '设备类型',
|
||||
},
|
||||
{
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
options: enabledOptions,
|
||||
},
|
||||
fieldName: 'enabled',
|
||||
label: '启用状态',
|
||||
},
|
||||
];
|
||||
|
||||
// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
|
||||
// export const columns: () => VxeGridProps['columns'] = () => [
|
||||
export const columns: VxeGridProps['columns'] = [
|
||||
{ type: 'checkbox', width: 60 },
|
||||
// {
|
||||
// title: '编号',
|
||||
// field: 'id',
|
||||
// },
|
||||
{
|
||||
title: '产品编码',
|
||||
field: 'productKey',
|
||||
},
|
||||
{
|
||||
title: '产品名称',
|
||||
field: 'productName',
|
||||
},
|
||||
{
|
||||
title: '设备类型',
|
||||
field: 'deviceType',
|
||||
},
|
||||
{
|
||||
title: '启用状态',
|
||||
field: 'enabled',
|
||||
slots: { default: 'enabled' },
|
||||
},
|
||||
{
|
||||
title: '描述',
|
||||
field: 'description',
|
||||
},
|
||||
{
|
||||
field: 'action',
|
||||
fixed: 'right',
|
||||
slots: { default: 'action' },
|
||||
title: '操作',
|
||||
width: 180,
|
||||
},
|
||||
];
|
||||
|
||||
export const drawerSchema: FormSchemaGetter = () => [
|
||||
{
|
||||
label: '编号',
|
||||
fieldName: 'id',
|
||||
component: 'Input',
|
||||
dependencies: {
|
||||
show: () => false,
|
||||
triggerFields: [''],
|
||||
},
|
||||
},
|
||||
{
|
||||
label: '产品编码',
|
||||
fieldName: 'productKey',
|
||||
component: 'Input',
|
||||
},
|
||||
{
|
||||
label: '产品名称',
|
||||
fieldName: 'productName',
|
||||
component: 'Input',
|
||||
rules: 'required',
|
||||
},
|
||||
{
|
||||
label: '产品图片',
|
||||
fieldName: 'imgId',
|
||||
component: 'ImageUpload',
|
||||
componentProps: {
|
||||
// accept: 'image/*', // 可选拓展名或者mime类型 ,拼接
|
||||
// maxCount: 1, // 最大上传文件数 默认为1 为1会绑定为string而非string[]类型
|
||||
},
|
||||
},
|
||||
{
|
||||
component: 'TreeSelect',
|
||||
fieldName: 'categoryId',
|
||||
componentProps: {
|
||||
fieldNames: { children: 'children', label: 'label', value: 'id' },
|
||||
},
|
||||
label: '产品分类',
|
||||
},
|
||||
{
|
||||
label: '设备类型',
|
||||
fieldName: 'deviceType',
|
||||
component: 'Select',
|
||||
componentProps: {
|
||||
options: deviceTypeOptions,
|
||||
},
|
||||
rules: 'selectRequired',
|
||||
renderComponentContent: () => {
|
||||
return {
|
||||
option: (option: any) => {
|
||||
return h('div', { class: 'flex flex-col', title: option.tooltip }, [
|
||||
h('span', { class: 'font-medium text-blue-500' }, option.label),
|
||||
h(
|
||||
'span',
|
||||
{
|
||||
class: 'text-[14px] text-black/25 truncate',
|
||||
style:
|
||||
'max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;',
|
||||
},
|
||||
option.tooltip,
|
||||
),
|
||||
]);
|
||||
},
|
||||
};
|
||||
},
|
||||
},
|
||||
{
|
||||
label: '描述',
|
||||
fieldName: 'description',
|
||||
component: 'Textarea',
|
||||
},
|
||||
{
|
||||
label: '启用状态',
|
||||
fieldName: 'enabled',
|
||||
component: 'Input',
|
||||
defaultValue: '0',
|
||||
dependencies: {
|
||||
show: () => false,
|
||||
triggerFields: [''],
|
||||
},
|
||||
},
|
||||
];
|
|
@ -0,0 +1,220 @@
|
|||
<script setup lang="ts">
|
||||
import type { VbenFormProps } from '@vben/common-ui';
|
||||
|
||||
import type { VxeGridProps } from '#/adapter/vxe-table';
|
||||
import type { ProductForm } from '#/api/device/product/model';
|
||||
|
||||
import { Page, useVbenDrawer } from '@vben/common-ui';
|
||||
import { getVxePopupContainer } from '@vben/utils';
|
||||
|
||||
import { Modal, Popconfirm, Space, Tag } from 'ant-design-vue';
|
||||
|
||||
import { useVbenVxeGrid, vxeCheckboxChecked } from '#/adapter/vxe-table';
|
||||
import {
|
||||
productExport,
|
||||
productList,
|
||||
productRemove,
|
||||
} from '#/api/device/product';
|
||||
import { productCategoryTreeList } from '#/api/device/productCategory';
|
||||
import { commonDownloadExcel } from '#/utils/file/download';
|
||||
|
||||
import { columns, querySchema } from './data';
|
||||
import productDrawer from './product-drawer.vue';
|
||||
|
||||
const formOptions: VbenFormProps = {
|
||||
commonConfig: {
|
||||
labelWidth: 80,
|
||||
componentProps: {
|
||||
allowClear: true,
|
||||
},
|
||||
},
|
||||
schema: querySchema(),
|
||||
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
|
||||
// 处理区间选择器RangePicker时间格式 将一个字段映射为两个字段 搜索/导出会用到
|
||||
// 不需要直接删除
|
||||
// fieldMappingTime: [
|
||||
// [
|
||||
// 'createTime',
|
||||
// ['params[beginTime]', 'params[endTime]'],
|
||||
// ['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
|
||||
// ],
|
||||
// ],
|
||||
};
|
||||
|
||||
const gridOptions: VxeGridProps = {
|
||||
checkboxConfig: {
|
||||
// 高亮
|
||||
highlight: true,
|
||||
// 翻页时保留选中状态
|
||||
reserve: true,
|
||||
// 点击行选中
|
||||
// trigger: 'row',
|
||||
},
|
||||
// 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
|
||||
// columns: columns(),
|
||||
columns,
|
||||
height: 'auto',
|
||||
keepSource: true,
|
||||
pagerConfig: {},
|
||||
proxyConfig: {
|
||||
ajax: {
|
||||
query: async ({ page }, formValues = {}) => {
|
||||
return await productList({
|
||||
pageNum: page.currentPage,
|
||||
pageSize: page.pageSize,
|
||||
...formValues,
|
||||
});
|
||||
},
|
||||
},
|
||||
},
|
||||
rowConfig: {
|
||||
keyField: 'id',
|
||||
},
|
||||
// 表格全局唯一表示 保存列配置需要用到
|
||||
id: 'device-product-index',
|
||||
};
|
||||
|
||||
const [BasicTable, tableApi] = useVbenVxeGrid({
|
||||
formOptions,
|
||||
gridOptions,
|
||||
});
|
||||
|
||||
const [ProductDrawer, drawerApi] = useVbenDrawer({
|
||||
connectedComponent: productDrawer,
|
||||
});
|
||||
|
||||
// 动态加载表单选项
|
||||
async function loadFormOptions() {
|
||||
try {
|
||||
const categoryOptions = await productCategoryTreeList();
|
||||
const placeholder = categoryOptions.length > 0 ? '请选择' : '暂无产品分类';
|
||||
tableApi.formApi.updateSchema([
|
||||
{
|
||||
componentProps: {
|
||||
treeData: categoryOptions || [],
|
||||
placeholder,
|
||||
},
|
||||
fieldName: 'categoryId',
|
||||
},
|
||||
]);
|
||||
} catch (error) {
|
||||
console.error('加载表单选项失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 组件挂载后加载选项
|
||||
loadFormOptions();
|
||||
|
||||
function handleAdd() {
|
||||
drawerApi.setData({});
|
||||
drawerApi.open();
|
||||
}
|
||||
|
||||
async function handleView(row: Required<ProductForm>) {
|
||||
router.push(`/device/product/detail/${row.id}`);
|
||||
}
|
||||
|
||||
async function handleEdit(row: Required<ProductForm>) {
|
||||
drawerApi.setData({ id: row.id });
|
||||
drawerApi.open();
|
||||
}
|
||||
|
||||
async function handleDelete(row: Required<ProductForm>) {
|
||||
await productRemove(row.id);
|
||||
await tableApi.query();
|
||||
}
|
||||
|
||||
function handleMultiDelete() {
|
||||
const rows = tableApi.grid.getCheckboxRecords();
|
||||
const ids = rows.map((row: Required<ProductForm>) => row.id);
|
||||
Modal.confirm({
|
||||
title: '提示',
|
||||
okType: 'danger',
|
||||
content: `确认删除选中的${ids.length}条记录吗?`,
|
||||
onOk: async () => {
|
||||
await productRemove(ids);
|
||||
await tableApi.query();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
function handleDownloadExcel() {
|
||||
commonDownloadExcel(
|
||||
productExport,
|
||||
'设备产品数据',
|
||||
tableApi.formApi.form.values,
|
||||
{
|
||||
fieldMappingTime: formOptions.fieldMappingTime,
|
||||
},
|
||||
);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Page :auto-content-height="true">
|
||||
<BasicTable table-title="设备产品列表">
|
||||
<template #toolbar-tools>
|
||||
<Space>
|
||||
<a-button
|
||||
v-access:code="['device:product:export']"
|
||||
@click="handleDownloadExcel"
|
||||
>
|
||||
{{ $t('pages.common.export') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
:disabled="!vxeCheckboxChecked(tableApi)"
|
||||
danger
|
||||
type="primary"
|
||||
v-access:code="['device:product:remove']"
|
||||
@click="handleMultiDelete"
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</a-button>
|
||||
<a-button
|
||||
type="primary"
|
||||
v-access:code="['device:product:add']"
|
||||
@click="handleAdd"
|
||||
>
|
||||
{{ $t('pages.common.add') }}
|
||||
</a-button>
|
||||
</Space>
|
||||
</template>
|
||||
<template #enabled="{ row }">
|
||||
<Tag :color="row.enabled === '1' ? 'success' : 'error'">
|
||||
{{ row.enabled === '1' ? '启用' : '禁用' }}
|
||||
</Tag>
|
||||
</template>
|
||||
<template #action="{ row }">
|
||||
<Space>
|
||||
<ghost-button
|
||||
v-access:code="['device:product:view']"
|
||||
@click.stop="handleView(row)"
|
||||
>
|
||||
详情
|
||||
</ghost-button>
|
||||
<ghost-button
|
||||
v-access:code="['device:product:edit']"
|
||||
@click.stop="handleEdit(row)"
|
||||
>
|
||||
{{ $t('pages.common.edit') }}
|
||||
</ghost-button>
|
||||
<Popconfirm
|
||||
:get-popup-container="getVxePopupContainer"
|
||||
placement="left"
|
||||
title="确认删除?"
|
||||
@confirm="handleDelete(row)"
|
||||
>
|
||||
<ghost-button
|
||||
danger
|
||||
v-access:code="['device:product:remove']"
|
||||
@click.stop=""
|
||||
>
|
||||
{{ $t('pages.common.delete') }}
|
||||
</ghost-button>
|
||||
</Popconfirm>
|
||||
</Space>
|
||||
</template>
|
||||
</BasicTable>
|
||||
<ProductDrawer @reload="tableApi.query()" />
|
||||
</Page>
|
||||
</template>
|
|
@ -0,0 +1,123 @@
|
|||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import { useVbenDrawer } from '@vben/common-ui';
|
||||
import { $t } from '@vben/locales';
|
||||
import { cloneDeep } from '@vben/utils';
|
||||
|
||||
import { useVbenForm } from '#/adapter/form';
|
||||
import { productAdd, productInfo, productUpdate } from '#/api/device/product';
|
||||
import { productCategoryTreeList } from '#/api/device/productCategory';
|
||||
import { defaultFormValueGetter, useBeforeCloseDiff } from '#/utils/popup';
|
||||
|
||||
import { drawerSchema } from './data';
|
||||
|
||||
const emit = defineEmits<{ reload: [] }>();
|
||||
|
||||
const isUpdate = ref(false);
|
||||
const title = computed(() => {
|
||||
return isUpdate.value ? $t('pages.common.edit') : $t('pages.common.add');
|
||||
});
|
||||
|
||||
const [BasicForm, formApi] = useVbenForm({
|
||||
commonConfig: {
|
||||
// 默认占满两列
|
||||
formItemClass: 'col-span-2',
|
||||
// 默认label宽度 px
|
||||
labelWidth: 80,
|
||||
// 通用配置项 会影响到所有表单项
|
||||
componentProps: {
|
||||
class: 'w-full',
|
||||
},
|
||||
},
|
||||
schema: drawerSchema(),
|
||||
showDefaultActions: false,
|
||||
wrapperClass: 'grid-cols-2',
|
||||
});
|
||||
|
||||
const { onBeforeClose, markInitialized, resetInitialized } = useBeforeCloseDiff(
|
||||
{
|
||||
initializedGetter: defaultFormValueGetter(formApi),
|
||||
currentGetter: defaultFormValueGetter(formApi),
|
||||
},
|
||||
);
|
||||
|
||||
const [BasicDrawer, drawerApi] = useVbenDrawer({
|
||||
// 在这里更改宽度
|
||||
class: 'w-[550px]',
|
||||
fullscreenButton: false,
|
||||
onBeforeClose,
|
||||
onClosed: handleClosed,
|
||||
onConfirm: handleConfirm,
|
||||
onOpenChange: async (isOpen) => {
|
||||
if (!isOpen) {
|
||||
return null;
|
||||
}
|
||||
drawerApi.drawerLoading(true);
|
||||
|
||||
const { id } = drawerApi.getData() as { id?: number | string };
|
||||
isUpdate.value = !!id;
|
||||
|
||||
if (isUpdate.value && id) {
|
||||
const record = await productInfo(id);
|
||||
await formApi.setValues(record);
|
||||
}
|
||||
await markInitialized();
|
||||
|
||||
drawerApi.drawerLoading(false);
|
||||
},
|
||||
});
|
||||
|
||||
async function handleConfirm() {
|
||||
try {
|
||||
drawerApi.lock(true);
|
||||
const { valid } = await formApi.validate();
|
||||
if (!valid) {
|
||||
return;
|
||||
}
|
||||
// getValues获取为一个readonly的对象 需要修改必须先深拷贝一次
|
||||
const data = cloneDeep(await formApi.getValues());
|
||||
await (isUpdate.value ? productUpdate(data) : productAdd(data));
|
||||
resetInitialized();
|
||||
emit('reload');
|
||||
drawerApi.close();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
drawerApi.lock(false);
|
||||
}
|
||||
}
|
||||
|
||||
// 动态加载表单选项
|
||||
async function loadFormOptions() {
|
||||
try {
|
||||
const categoryOptions = await productCategoryTreeList();
|
||||
const placeholder = categoryOptions.length > 0 ? '请选择' : '暂无产品分类';
|
||||
formApi.updateSchema([
|
||||
{
|
||||
componentProps: {
|
||||
treeData: categoryOptions || [],
|
||||
placeholder,
|
||||
},
|
||||
fieldName: 'categoryId',
|
||||
},
|
||||
]);
|
||||
} catch (error) {
|
||||
console.error('加载表单选项失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 组件挂载后加载选项
|
||||
loadFormOptions();
|
||||
|
||||
async function handleClosed() {
|
||||
await formApi.resetForm();
|
||||
resetInitialized();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BasicDrawer :title="title">
|
||||
<BasicForm />
|
||||
</BasicDrawer>
|
||||
</template>
|
Loading…
Reference in New Issue