haoxuan
2023-11-06 2bb9a863e75312fe90869ea3deea137b46b1bb1e
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<template>
  <div class="device-check-list">
    <CommonModal v-model="modelData" @close="closeModal">
      <template #title>设备选择</template>
      <div class="device-box">
        <el-scrollbar always class="scroller">
          <template v-if="deviceInfo?.deviceIDList?.length">
            <div
              v-for="(item, index) in deviceInfo?.deviceIDList"
              :key="index"
              :class="selectedDevice === item ? 'device-item check-item' : 'device-item'"
              @click="deviceClick(item)"
            >
              <div class="item-l">
                <span>{{ item }}</span>
                <!--  {{ item }}-->
              </div>
              <div v-if="selectedDevice === item" class="item-r">
                <el-icon class="item-icon" size="22" color="#00ff00"><CircleCheckFilled /></el-icon>
              </div>
            </div>
          </template>
        </el-scrollbar>
      </div>
      <template #footer>
        <div class="device-b dialog-footer">
          <BigButton class="btn1" bg-color="#213e9e" @click="closeModal">取消</BigButton>
          <BigButton class="btn" bg-color="#0dfde6" @click="saveModal">保存</BigButton>
        </div>
      </template>
    </CommonModal>
  </div>
</template>
<script setup lang="ts">
import { useVModel } from '@vueuse/core'
import { CircleCheckFilled } from '@element-plus/icons-vue'
import BigButton from '@/views/dashboard/components/BigButton.vue'
import { useDevicesStore } from '@/stores/devices'
import { storeToRefs } from 'pinia'
import { ref, watch } from 'vue'
import { apiSetCurrentDevice } from '@/api'
import { ElMessage } from 'element-plus'
export interface DeviceCheckListProps {
  modelValue: boolean
}
const props = withDefaults(defineProps<DeviceCheckListProps>(), {
  modelValue: false
})
 
const emit = defineEmits<{
  'update:modelValue': [show: boolean]
  shouldReload: []
}>()
const modelData = useVModel(props, 'modelValue', emit)
const deviceStore = useDevicesStore()
const { deviceInfo } = storeToRefs(deviceStore)
 
// 弹窗打开时设定当前选中的设备
const selectedDevice = ref<string>('')
watch(modelData, () => {
  if (modelData.value) {
    selectedDevice.value = deviceInfo.value?.currentDeviceID ?? ''
  }
})
 
function deviceClick(deviceId: string) {
  selectedDevice.value = deviceId
}
 
function saveModal() {
  if (!selectedDevice.value) {
    ElMessage({
      message: '请先选中一个设备',
      type: 'error',
      duration: 3 * 1000
    })
    return
  }
  apiSetCurrentDevice({ currentDeviceID: selectedDevice.value })
    .then(() => {
      ElMessage({
        message: '设定成功',
        type: 'success',
        duration: 2 * 1000
      })
      modelData.value = false
      emit('shouldReload')
    })
    .catch((err) => {
      console.error(err)
      ElMessage({
        message: err.msg,
        type: 'error',
        duration: 3 * 1000
      })
    })
    .finally(() => {
      deviceStore.startPollingDevice()
    })
}
function closeModal() {
  modelData.value = false
}
</script>
<style scoped lang="scss">
$status-done: #0dfde6;
$status-d: #213e9e;
:deep(.el-dialog__body) {
  padding-top: 10px !important;
  padding-bottom: 10px !important;
}
.device-box {
  height: 200px;
  overflow-y: auto;
  .check-item {
    border: 1px solid $status-done;
  }
  .device-item {
    box-sizing: border-box;
    width: 100%;
    height: 45px;
    line-height: 45px;
    padding: 0 10px;
    border-radius: 6px;
    font-size: 14px;
    color: #fff;
    cursor: pointer;
    .item-l {
      width: calc(100% - 80px);
      float: left;
      span {
        margin-right: 10px;
      }
    }
    .item-icon {
      height: 100%;
      display: flex;
      align-items: center;
    }
    .item-r {
      height: 100%;
      float: right;
    }
  }
}
.device-b {
  width: 210px;
  margin: 10px auto 10px;
  display: flex;
  align-items: center;
  .btn,
  .btn1 {
    width: 100px;
    height: 40px;
    line-height: 40px;
    border-radius: 4px;
    float: left;
    color: #333;
    font-size: 14px;
    cursor: pointer;
    text-align: center;
  }
  .btn1 {
    color: #fff;
    margin-right: 10px;
  }
}
.scroller {
  padding: 4px 16px;
}
</style>