xieb
2023-09-13 3667807a7b7418efc090ee3fa6a6b734bc3080bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<template>
<a-modal :visible="sVisible"
         :title="title"
         :closable="false"
         centered
         @update:visible="onVisibleChange"
         @cancel="onCancel"
         @ok="onConfirm">
         <div>
          升级固件版本: {{ deviceUpgradeInfo?.product_version }}
         </div>
</a-modal>
</template>
 
<script lang="ts" setup>
import { defineProps, defineEmits, ref, Ref, watchEffect } from 'vue'
import { Device, DeviceFirmwareStatusEnum, DeviceFirmwareStatus, DeviceFirmwareTypeEnum } from '/@/types/device'
import { getDeviceUpgradeInfo, GetDeviceUpgradeInfoRsp, DeviceUpgradeBody } from '/@/api/device-upgrade'
 
const props = defineProps<{
  visible: boolean,
  title: string,
  device: null | Device,
}>()
 
const emit = defineEmits(['update:visible', 'ok', 'cancel'])
 
const deviceUpgradeInfo:Ref<GetDeviceUpgradeInfoRsp> = ref({} as GetDeviceUpgradeInfoRsp)
const sVisible = ref(false)
 
watchEffect(() => {
  sVisible.value = props.visible
  // 显示弹框时,获取设备升级信息
  if (props.visible) {
    initDeviceUpgradeInfo()
  }
})
 
function onVisibleChange (sVisible: boolean) {
  setVisible(sVisible)
}
 
function setVisible (v: boolean, e?: Event) {
  sVisible.value = v
  emit('update:visible', v, e)
}
 
// 获取设备升级信息
async function initDeviceUpgradeInfo () {
  if (!props.device?.device_name) {
    return
  }
  const { code, data } = await getDeviceUpgradeInfo({ device_name: props.device?.device_name })
  if (code === 0) {
    deviceUpgradeInfo.value = data && data[0]
  }
}
 
// 提交
function checkConfirm () {
  if (!deviceUpgradeInfo.value.product_version) {
    return false
  }
  if (!props.device) {
    return false
  }
  if (props.device.firmware_status !== DeviceFirmwareStatusEnum.ToUpgraded && props.device.firmware_status !== DeviceFirmwareStatusEnum.ConsistencyUpgrade) {
    return false
  }
  return true
}
 
function onConfirm (e: Event) {
  if (!checkConfirm()) {
    return
  }
  setVisible(false, e)
  emit('ok', [{
    device_name: props.device?.device_name,
    sn: props.device?.device_sn,
    product_version: deviceUpgradeInfo.value.product_version,
    firmware_upgrade_type: props.device?.firmware_status === DeviceFirmwareStatusEnum.ToUpgraded ? DeviceFirmwareTypeEnum.ToUpgraded : DeviceFirmwareTypeEnum.ConsistencyUpgrade // 1-普通升级,2-一致性升级
  }] as DeviceUpgradeBody, e)
}
 
function onCancel (e: Event) {
  setVisible(false, e)
  emit('cancel', e)
}
</script>
 
<style lang="scss" scoped>
</style>