import type { VxeGridProps } from '#/adapter/vxe-table'; import { computed, h } from 'vue'; import { z } from '@vben/common-ui'; import { postOneNetAccountListAsync, postFilesUpload } from '#/api-client'; import { $t } from '#/locales'; export const querySchema = computed(() => [ { component: 'Input', fieldName: 'productName', label: $t('abp.OneNETManagement.ProductName'), }, ]); export const tableSchema: any = computed((): VxeGridProps['columns'] => [ { title: $t('common.seq'), type: 'seq', width: 50 }, { field: 'oneNETAccountName', title: $t('abp.OneNETManagement.BelongingAccountName'), minWidth: '150', }, { field: 'ioTPlatformProductId', title: $t('abp.OneNETManagement.OneNETProductId'), minWidth: '150', }, { field: 'productName', title: $t('abp.OneNETManagement.ProductName'), minWidth: '150', }, { field: 'productAccesskey', title: $t('abp.OneNETManagement.ProductAccesskey'), minWidth: '150', }, { field: 'communicationAddress', title: $t('abp.OneNETManagement.CommunicationAddress'), minWidth: '150', }, { field: 'communicationAddressTLS', title: $t('abp.OneNETManagement.CommunicationAddressTLS'), minWidth: '150', }, { field: 'isEnabled', title: $t('common.isEnable'), minWidth: '150', slots: { default: 'isEnable' }, }, { title: $t('common.action'), field: 'action', fixed: 'right', width: '150', slots: { default: 'action' }, }, ]); export const addProductFormSchema: any = computed(() => [ { component: 'ApiSelect', fieldName: 'oneNETAccountId', label: $t('abp.OneNETManagement.BelongingAccountName'), componentProps: { api: postOneNetAccountListAsync, params: { body: { pageIndex: 1, pageSize: 1000, }, }, labelField: 'accountName', valueField: 'oneNETAccountId', immediate: true, afterFetch: (res: any) => { // 如果是 Axios 响应对象,提取 data if (res && res.data) { const data = res.data; // 确保返回的是数组格式 if (Array.isArray(data)) { return data; } // 如果是包装在 items 中的,提取出来 if (data && Array.isArray(data.items)) { return data.items; } // 如果是包装在 data 中的,提取出来 if (data && Array.isArray(data.data)) { return data.data; } } // 如果都不是,返回空数组 return []; }, placeholder: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingAccountName')}`, }, rules: z.string().min(1, { message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingAccountName')}`, }), }, { component: 'Input', fieldName: 'IoTPlatformProductId', label: $t('abp.OneNETManagement.OneNETProductId'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.OneNETProductId')}`, }), }, { component: 'Input', fieldName: 'productAccesskey', label: $t('abp.OneNETManagement.ProductAccesskey'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.ProductAccesskey')}`, }), }, { component: 'Input', fieldName: 'communicationAddress', label: $t('abp.OneNETManagement.CommunicationAddress'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.CommunicationAddress')}`, }), }, { component: 'Input', fieldName: 'CommunicationAddressTLS', label: $t('abp.OneNETManagement.CommunicationAddressTLS'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.CommunicationAddressTLS')}`, }), }, { component: 'Input', fieldName: 'DeviceThingModelFileId', label: $t('abp.OneNETManagement.DeviceThingModelFileName'), componentProps: { placeholder: '请选择文件', readonly: true, addonAfter: h('button', { type: 'button', style: 'border: none; background: #1890ff; color: white; padding: 4px 8px; border-radius: 4px; cursor: pointer;', onClick: () => { const input = document.createElement('input'); input.type = 'file'; input.accept = '*/*'; input.onchange = async (e: any) => { const file = e.target.files[0]; if (file) { try { const result = await postFilesUpload({ body: { files: [file] } }); if (result.status === 204 || result.status === 200) { // 假设返回的是文件信息数组,取第一个文件的ID const fileInfo = result.data?.[0]; if (fileInfo && fileInfo.id) { // 查找当前输入框并更新值 const currentInput = document.querySelector('input[placeholder="请选择文件"]'); if (currentInput) { (currentInput as HTMLInputElement).value = fileInfo.id; // 触发change事件 currentInput.dispatchEvent(new Event('input', { bubbles: true })); currentInput.dispatchEvent(new Event('change', { bubbles: true })); // 同时设置文件名到隐藏字段 const fileNameInput = document.querySelector('input[name="DeviceThingModelFileName"]') || document.querySelector('input[data-field="DeviceThingModelFileName"]'); if (fileNameInput) { (fileNameInput as HTMLInputElement).value = file.name; fileNameInput.dispatchEvent(new Event('input', { bubbles: true })); fileNameInput.dispatchEvent(new Event('change', { bubbles: true })); } } console.log('文件上传成功,文件ID:', fileInfo.id, '文件名:', file.name); } else { console.error('文件上传成功但未获取到文件ID'); } } else { console.error('文件上传失败'); } } catch (error) { console.error('文件上传失败:', error); } } }; input.click(); } }, '选择文件'), }, rules: z.string().min(1, { message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.DeviceThingModelFileName')}`, }), }, { component: 'Input', fieldName: 'DeviceThingModelFileName', label: '', componentProps: { type: 'hidden', }, }, ]); export const editProductFormSchemaEdit: any = computed(() => [ { component: 'ApiSelect', fieldName: 'oneNETAccountId', label: $t('abp.OneNETManagement.BelongingAccountName'), componentProps: { api: postOneNetAccountListAsync, params: { body: { pageIndex: 1, pageSize: 1000, }, }, labelField: 'accountName', valueField: 'oneNETAccountId', immediate: true, afterFetch: (res: any) => { // 如果是 Axios 响应对象,提取 data if (res && res.data) { const data = res.data; // 确保返回的是数组格式 if (Array.isArray(data)) { return data; } // 如果是包装在 items 中的,提取出来 if (data && Array.isArray(data.items)) { return data.items; } // 如果是包装在 data 中的,提取出来 if (data && Array.isArray(data.data)) { return data.data; } } // 如果都不是,返回空数组 return []; }, placeholder: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingAccountName')}`, }, rules: z.string().min(1, { message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.BelongingAccountName')}`, }), }, { component: 'Input', fieldName: 'IoTPlatformProductId', label: $t('abp.OneNETManagement.OneNETProductId'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.OneNETProductId')}`, }), }, { component: 'Input', fieldName: 'productAccesskey', label: $t('abp.OneNETManagement.ProductAccesskey'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.ProductAccesskey')}`, }), }, { component: 'Input', fieldName: 'communicationAddress', label: $t('abp.OneNETManagement.CommunicationAddress'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.CommunicationAddress')}`, }), }, { component: 'Input', fieldName: 'CommunicationAddressTLS', label: $t('abp.OneNETManagement.CommunicationAddressTLS'), rules: z.string().min(1, { message: `${$t('common.pleaseInput')}${$t('common.info')}${$t('abp.OneNETManagement.CommunicationAddressTLS')}`, }), }, { component: 'Input', fieldName: 'DeviceThingModelFileId', label: $t('abp.OneNETManagement.DeviceThingModelFileName'), componentProps: { placeholder: '请选择文件', readonly: true, addonAfter: h('button', { type: 'button', style: 'border: none; background: #1890ff; color: white; padding: 4px 8px; border-radius: 4px; cursor: pointer;', onClick: () => { const input = document.createElement('input'); input.type = 'file'; input.accept = '*/*'; input.onchange = async (e: any) => { const file = e.target.files[0]; if (file) { try { const result = await postFilesUpload({ body: { files: [file] } }); if (result.status === 204 || result.status === 200) { // 假设返回的是文件信息数组,取第一个文件的ID const fileInfo = result.data?.[0]; if (fileInfo && fileInfo.id) { // 查找当前输入框并更新值 const currentInput = document.querySelector('input[placeholder="请选择文件"]'); if (currentInput) { (currentInput as HTMLInputElement).value = fileInfo.id; // 触发change事件 currentInput.dispatchEvent(new Event('input', { bubbles: true })); currentInput.dispatchEvent(new Event('change', { bubbles: true })); // 同时设置文件名到隐藏字段 const fileNameInput = document.querySelector('input[name="DeviceThingModelFileName"]') || document.querySelector('input[data-field="DeviceThingModelFileName"]'); if (fileNameInput) { (fileNameInput as HTMLInputElement).value = file.name; fileNameInput.dispatchEvent(new Event('input', { bubbles: true })); fileNameInput.dispatchEvent(new Event('change', { bubbles: true })); } } console.log('文件上传成功,文件ID:', fileInfo.id, '文件名:', file.name); } else { console.error('文件上传成功但未获取到文件ID'); } } else { console.error('文件上传失败'); } } catch (error) { console.error('文件上传失败:', error); } } }; input.click(); } }, '选择文件'), }, rules: z.string().min(1, { message: `${$t('common.pleaseSelect')}${$t('abp.OneNETManagement.DeviceThingModelFileName')}`, }), }, { component: 'Input', fieldName: 'DeviceThingModelFileName', label: '', componentProps: { type: 'hidden', }, }, ]);