feat: 新增产品分类管理、产品管理功能

- 添加产品列表等接口
- 实现产品信息展示、编辑等功能
- 添加产品分类管理相关功能
This commit is contained in:
fhysy 2025-08-08 17:54:08 +08:00
parent da0cb82255
commit de7568b525
14 changed files with 806 additions and 5 deletions

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

View File

@ -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}`);
}

View File

@ -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;
}

View File

@ -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

View File

@ -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: '能挂载子设备与平台进行通信的设备',
},
];

View File

@ -1,4 +1,6 @@
/* 本地字典 */
export * from './device';
export * from './operations';
// 启用状态

View File

@ -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' },
];

View File

@ -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,

View File

@ -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: [''],
},
},
];

View File

@ -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',
},
// 使i18ngetter
// 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>

View File

@ -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;
}
// getValuesreadonly
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>