diff --git a/src/views/device/apdevice/index.vue b/src/views/device/apdevice/index.vue index b3c86be..d6ce778 100644 --- a/src/views/device/apdevice/index.vue +++ b/src/views/device/apdevice/index.vue @@ -33,12 +33,12 @@ + v-model:columns="columnChecks" + :loading="loading" + :show-delete="false" + @add="handleAdd" + @refresh="getData" + /> @@ -108,7 +108,6 @@ - @@ -164,17 +163,17 @@ - - - - - - - - - - - + + + + + + + + + + + { - try { - siteLoading.value = true; - const { data, error } = await fetchSiteList({ - pageNum: 1, - pageSize: 100 - }); + siteLoading.value = true; + const { data, error } = await fetchSiteList({ + pageNum: 1, + pageSize: 100 + }); - if (!error) { - siteList.value = data.rows || []; - - // 如果有站点数据,默认选择第一个 - if (siteList.value.length > 0) { - selectedSiteId.value = siteList.value[0].siteId; - await getData(); - } + if (!error) { + siteList.value = data.rows || []; + // 如果有站点数据,默认选择第一个 + if (siteList.value.length > 0) { + selectedSiteId.value = siteList.value[0].siteId; + // 获取第一个站点的数据 + await getData(); } - } catch (error) { - console.error('Get site list error:', error); - message.error(t('page.apdevice.getSiteError')); - } finally { - siteLoading.value = false; } + siteLoading.value = false; }; // 处理站点变更 @@ -415,65 +408,40 @@ const handleReset = () => { // 添加相关的处理函数 // 处理打开配置对话框 const handleEditConfig = async (record: Api.Device.ApDevice) => { - try { - if (!selectedSiteId.value) { - throw new Error('No site selected'); - } + currentDevice.value = record; + configVisible.value = true; - const hide = message.loading(t('common.loading'), 0); + const hide = message.loading(t('common.loading'), 0); - // 先获取设备当前的配置 - const response = await getApDeviceConfig(selectedSiteId.value, record.mac); + // 获取设备当前的配置 + const { data, error } = await getApDeviceConfig(selectedSiteId.value, record.mac); - hide(); - - // 保存当前编辑的设备信息 - currentDevice.value = record; - - // 使用获取到的配置更新表单,处理 null 和嵌套对象 + hide(); + if (!error && data) { configForm.value = { - name: response.data?.name || record.name, - ledSetting: response.data?.ledSetting ?? 2, // 如果没有设置,默认使用站点设置 - // tagIds: response.data?.tagIds || [], // 暂时注释掉 - longitude: response.data?.location?.longitude, - latitude: response.data?.location?.latitude, - address: response.data?.location?.address || '' + name: data.name || '', + ledSetting: data.ledSetting || 2, + longitude: data.longitude, + latitude: data.latitude, + address: data.address || '' }; - - // 显示配置对话框 - configVisible.value = true; - } catch (error) { - console.error('Get device config error:', error); - message.error(t('page.apdevice.getConfigError')); } }; const handleForgetDevice = (record: Api.Device.ApDevice) => { Modal.confirm({ - title: t('common.confirm'), - content: t('page.apdevice.forgetConfirm'), + title: t('page.apdevice.confirmForget'), + content: t('page.apdevice.forgetConfirmContent', { name: record.name || record.mac }), + okText: t('common.confirm'), + cancelText: t('common.cancel'), onOk: async () => { - try { - const hide = message.loading(t('common.loading'), 0); - console.log('Forgetting device:', record); + const hide = message.loading(t('common.loading'), 0); + const { error } = await forgetApDevice(selectedSiteId.value, record.mac); + hide(); - if (!selectedSiteId.value || !record.mac) { - throw new Error('Missing required parameters: siteId or mac'); - } - - // 使用当前选中的 siteId - const response = await forgetApDevice(selectedSiteId.value, record.mac); - console.log('Forget device response:', response); - - hide(); + if (!error) { message.success(t('page.apdevice.forgetSuccess')); - - // 重新获取列表数据 - getData(); - } catch (error) { - console.error('Forget device error:', error); - message.error(t('page.apdevice.forgetError')); - throw error; + getData(); // 刷新列表 } } }); @@ -491,61 +459,54 @@ const handleRestart = (record: Api.Device.ApDevice) => { }); }; -// 纳管表单数据 +// 添加纳管表单的引用 +const adoptFormRef = ref(); + +// 添加纳管表单的数据 const adoptForm = ref({ username: '', password: '' }); -// 纳管对话框可见性 +// 添加纳管表单的验证规则 +const adoptRules = { + username: [ + { required: false } + ], + password: [ + { required: false } + ] +}; + +// 添加纳管对话框的可见性控制 const adoptVisible = ref(false); -// 当前要纳管的设备 -const adoptingDevice = ref(null); - // 处理纳管按钮点击 -const handleAdopt = (record: Api.Device.ApDevice) => { - adoptingDevice.value = record; - adoptForm.value = { - username: '', - password: '' - }; +const handleAdopt = async (record: Api.Device.ApDevice) => { + currentDevice.value = record; adoptVisible.value = true; }; // 处理纳管确认 const handleAdoptConfirm = async () => { - try { - if (!adoptingDevice.value || !selectedSiteId.value) { - throw new Error('Missing device or site information'); - } + await adoptFormRef.value?.validate(); - const hide = message.loading(t('common.loading'), 0); + const hide = message.loading(t('common.loading'), 0); - // 构造请求参数,只有在有值时才包含 - const params: { username?: string; password?: string } = {}; - if (adoptForm.value.username) { - params.username = adoptForm.value.username; - } - if (adoptForm.value.password) { - params.password = adoptForm.value.password; - } + const { error } = await adoptApDevice( + selectedSiteId.value, + currentDevice.value?.mac || '', + adoptForm.value + ); - await adoptApDevice(selectedSiteId.value, adoptingDevice.value.mac, params); - - hide(); + hide(); + if (!error) { message.success(t('page.apdevice.adoptSuccess')); adoptVisible.value = false; - - // 重新获取列表数据 - getData(); - } catch (error) { - console.error('Adopt device error:', error); - message.error(t('page.apdevice.adoptError')); + getData(); // 刷新列表 } }; - // 修改表单验证规则的定义 const addDeviceRules = { sn: [ @@ -555,14 +516,14 @@ const addDeviceRules = { message: t('page.apdevice.snFormatError'), trigger: 'blur' } - ] , + ] as Rule[], name: [ { pattern: /^[^ \+\-\@\=]$|^[^ \+\-\@\=].{0,126}[^ ]$/, - message: t('page.apdevice.nameFormatError', { test: '@' }), + message: t('page.apdevice.nameFormatError'), trigger: 'blur' } - ] + ] as Rule[] }; // 添加设备的表单数据 @@ -583,32 +544,30 @@ const handleAdd = () => { // 处理添加设备的确认 const handleAddConfirm = async () => { - try { - // 先验证表单 - await formRef.value?.validate(); + await formRef.value?.validate(); - const hide = message.loading(t('common.loading'), 0); + const hide = message.loading(t('common.loading'), 0); - // 构造请求参数,即使所有字段都为空也要发送一个空对象 - // 构造请求参数,确保 sn 字段存在 - const deviceParams: Api.Device.AddApDeviceItem = { - sn: addDeviceForm.value.sn // sn 是必填的 - }; + // 构造请求参数 + const deviceParams: Api.Device.AddApDeviceItem = { + sn: addDeviceForm.value.sn // sn 是必填的 + }; - // 只添加其他有值的字段 - if (addDeviceForm.value.name) { - deviceParams.name = addDeviceForm.value.name; - } - if (addDeviceForm.value.username) { - deviceParams.username = addDeviceForm.value.username; - } - if (addDeviceForm.value.password) { - deviceParams.password = addDeviceForm.value.password; - } + // 只添加其他有值的字段 + if (addDeviceForm.value.name) { + deviceParams.name = addDeviceForm.value.name; + } + if (addDeviceForm.value.username) { + deviceParams.username = addDeviceForm.value.username; + } + if (addDeviceForm.value.password) { + deviceParams.password = addDeviceForm.value.password; + } - await addApDevice(selectedSiteId.value, deviceParams); + const { error } = await addApDevice(selectedSiteId.value, deviceParams); - hide(); + hide(); + if (!error) { message.success(t('page.apdevice.addSuccess')); addDeviceVisible.value = false; @@ -622,9 +581,6 @@ const handleAddConfirm = async () => { // 重新获取列表数据 getData(); - } catch (error) { - console.error('Add device error:', error); - message.error(t('page.apdevice.addError')); } }; // 添加表单引用 @@ -676,30 +632,25 @@ const currentDevice = ref(null); // 处理配置确认 const handleConfigConfirm = async () => { - try { - await configFormRef.value?.validate(); + await configFormRef.value?.validate(); - if (!currentDevice.value || !selectedSiteId.value) { - throw new Error('Missing device or site information'); - } + if (!currentDevice.value || !selectedSiteId.value) { + return; + } - const hide = message.loading(t('common.loading'), 0); + const hide = message.loading(t('common.loading'), 0); - await updateApDeviceConfig( - selectedSiteId.value, - currentDevice.value.mac, - configForm.value - ); + const { error } = await updateApDeviceConfig( + selectedSiteId.value, + currentDevice.value.mac, + configForm.value + ); - hide(); + hide(); + if (!error) { message.success(t('page.apdevice.configSuccess')); configVisible.value = false; - - // 重新获取列表数据 - getData(); - } catch (error) { - console.error('Update config error:', error); - message.error(t('page.apdevice.configError')); + getData(); // 刷新列表 } }; @@ -709,6 +660,7 @@ const configFormRef = ref(); onMounted(() => { getSiteList(); }); + // 获取状态对应的颜色 const getStatusColor = (status: number) => { switch (status) {