index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <div class="deviceList">
  3. <Search :selectedRowKeys="state.selectedRowKeys" :onClickDelete="onClickBatchDelete" :onClickSearch="onClickSearch"
  4. :onClickReset="onClickReset" />
  5. <div class="deviceList-table">
  6. <a-table :scroll="{ x: '100%', y: 500 }" :childrenColumnName="null" rowKey="device_sn"
  7. :loading="state.listLoading" :columns="columns" :dataSource="state.list" @change="refreshData"
  8. :rowClassName="rowClassName" :pagination="paginationConfig" :rowSelection="rowSelection">
  9. <!-- 设备型号 -->
  10. <template #device_name="{ record }">
  11. <CustomCell :record="record" fieldName="device_name" :showIcon="true" />
  12. </template>
  13. <!-- 设备SN -->
  14. <template #device_sn="{ record }">
  15. <CustomCell :record="record" fieldName="device_sn" />
  16. </template>
  17. <!-- 设备名称 -->
  18. <template #nickname="{ record }">
  19. <CustomCell :record="record" fieldName="nickname" :isEdit="!!state.editableData[record.device_sn]" />
  20. </template>
  21. <!-- 固件版本 -->
  22. <template #firmware_version="{ record }">
  23. <CustomCell :record="record" fieldName="firmware_version" />
  24. </template>
  25. <!-- 固件升级 -->
  26. <template #firmware_status="{ record }">
  27. <DeviceFirmwareUpgrade :device="record" />
  28. </template>
  29. <!-- 当前状态 -->
  30. <template #status="{ record }">
  31. <CustomCell :record="record" fieldName="status_text" />
  32. </template>
  33. <!-- 加入项目时间 -->
  34. <template #bound_time="{ record }">
  35. <CustomCell :record="record" fieldName="bound_time" />
  36. </template>
  37. <!-- 最后在线时间 -->
  38. <template #login_time="{ record }">
  39. <CustomCell :record="record" fieldName="login_time" />
  40. </template>
  41. <!-- 操作 -->
  42. <template #action="{ record }">
  43. <!-- 编辑态操作 -->
  44. <div v-if="state.editableData[record.device_sn]">
  45. <a-tooltip title="确定">
  46. <CheckOutlined style="color: #28d445;margin-right: 10px;" @click="onClickSave(record)" />
  47. </a-tooltip>
  48. <a-tooltip title="取消">
  49. <CloseOutlined style="color: #e70102;" @click="() => delete state.editableData[record.device_sn]" />
  50. </a-tooltip>
  51. </div>
  52. <!-- 非编辑态操作 -->
  53. <div v-else class="flex-align-center flex-row" style="color: #2d8cf0">
  54. <a-tooltip title="设备日志">
  55. <CloudServerOutlined style="margin-right: 10px;" @click="onClickDeviceLog(record)" />
  56. </a-tooltip>
  57. <a-tooltip title="告警信息">
  58. <FileSearchOutlined style="margin-right: 10px;" @click="onClickDeviceHms(record)" />
  59. </a-tooltip>
  60. <a-tooltip title="编辑">
  61. <EditOutlined style="margin-right: 10px;" @click="onClickEdit(record)" />
  62. </a-tooltip>
  63. <a-tooltip title="删除">
  64. <DeleteOutlined @click="onClickDelete(record)" />
  65. </a-tooltip>
  66. </div>
  67. </template>
  68. </a-table>
  69. </div>
  70. </div>
  71. <!-- 设备日志 -->
  72. <DeviceLogUploadRecordDrawer v-model:visible="state.deviceLogDrawerVisible" :device="state.currentDevice" />
  73. <!-- 告警信息 -->
  74. <DeviceHmsDrawer v-model:visible="state.deviceHmsDrawerVisible" :device="state.currentDevice" />
  75. </template>
  76. <script lang="ts" setup>
  77. import { reactive, onMounted, onUnmounted } from 'vue';
  78. import { Modal } from 'ant-design-vue';
  79. import { EditOutlined, CheckOutlined, CloseOutlined, DeleteOutlined, FileSearchOutlined, CloudServerOutlined } from '@ant-design/icons-vue';
  80. import Search from './components/Search.vue';
  81. import CustomCell from './components/CustomCell.vue';
  82. import DeviceFirmwareUpgrade from '/@/components/devices/device-upgrade/DeviceFirmwareUpgrade.vue';
  83. import DeviceLogUploadRecordDrawer from '/@/components/devices/device-log/DeviceLogUploadRecordDrawer.vue';
  84. import DeviceHmsDrawer from '/@/components/devices/device-hms/DeviceHmsDrawer.vue';
  85. import { getBindingDevices, updateDevice, unbindDevice } from '/@/api/manage';
  86. import { apis } from '/@/api/custom';
  87. import { getWorkspaceId } from '/@/utils/index';
  88. interface State {
  89. workspaceId: string,
  90. interval: number | null,
  91. query: any,
  92. listLoading: boolean,
  93. list: any[],
  94. selectedRowKeys: string[],
  95. currentDevice: any,
  96. editableData: {
  97. [key: string]: any,
  98. },
  99. deviceLogDrawerVisible: boolean,
  100. deviceHmsDrawerVisible: boolean,
  101. };
  102. const state: State = reactive({
  103. workspaceId: getWorkspaceId(),
  104. interval: null,
  105. query: undefined,
  106. listLoading: false,
  107. list: [],
  108. selectedRowKeys: [],
  109. currentDevice: {},
  110. editableData: {},
  111. deviceLogDrawerVisible: false,
  112. deviceHmsDrawerVisible: false,
  113. })
  114. const paginationConfig = reactive({
  115. pageSizeOptions: ['20', '50', '100'],
  116. showQuickJumper: true,
  117. showSizeChanger: true,
  118. pageSize: 20,
  119. current: 1,
  120. total: 0
  121. })
  122. const fetchList = async () => {
  123. state.listLoading = true;
  124. try {
  125. const res = await getBindingDevices(state.workspaceId, {
  126. ...state.query,
  127. page: paginationConfig.current,
  128. page_size: paginationConfig.pageSize,
  129. });
  130. state.list = res.data.list;
  131. paginationConfig.total = res.data.pagination.total;
  132. paginationConfig.current = res.data.pagination.page;
  133. paginationConfig.pageSize = res.data.pagination.page_size;
  134. await autoUpdateDeviceStatus()
  135. } catch (e) {
  136. console.error(e);
  137. } finally {
  138. state.listLoading = false;
  139. }
  140. }
  141. // 自动更新设备状态
  142. const autoUpdateDeviceStatus = async () => {
  143. if (state.list.length === 0) {
  144. return;
  145. }
  146. const snList: string[] = [];
  147. state.list.forEach(item => {
  148. if (item.children) {
  149. snList.push(item.children.device_sn);
  150. }
  151. snList.push(item.device_sn);
  152. })
  153. const data = {
  154. snList: snList.join(','),
  155. }
  156. const res = await apis.fetchDeviceStatus(data);
  157. const deviceStatusMap = new Map();
  158. res.data.forEach((item: any) => {
  159. deviceStatusMap.set(item.device_sn, item.status_text);
  160. });
  161. state.list.forEach(item => {
  162. if (item.children) {
  163. item.children.status_text = deviceStatusMap.get(item.children.device_sn);
  164. }
  165. item.status_text = deviceStatusMap.get(item.device_sn);
  166. })
  167. }
  168. // 开始定时器
  169. const startAutoUpdate = () => {
  170. state.interval = window.setInterval(autoUpdateDeviceStatus, 1000);
  171. };
  172. // 清除定时器
  173. const clearAutoUpdate = () => {
  174. if (state.interval !== null) {
  175. clearInterval(state.interval);
  176. state.interval = null;
  177. }
  178. };
  179. onMounted(async () => {
  180. await fetchList();
  181. startAutoUpdate(); // 页面加载完成后启动定时器
  182. });
  183. onUnmounted(() => {
  184. clearAutoUpdate(); // 页面卸载前清除定时器
  185. });
  186. const columns = [
  187. {
  188. title: '设备型号',
  189. dataIndex: 'device_name',
  190. width: 150,
  191. ellipsis: true,
  192. sorter: (a: any, b: any) => a.device_name.localeCompare(b.device_name),
  193. slots: { customRender: 'device_name' }
  194. },
  195. {
  196. title: '设备SN',
  197. dataIndex: 'device_sn',
  198. width: 250,
  199. ellipsis: true,
  200. slots: { customRender: 'device_sn' }
  201. },
  202. {
  203. title: '设备名称',
  204. dataIndex: 'nickname',
  205. width: 150,
  206. ellipsis: true,
  207. sorter: (a: any, b: any) => a.nickname.localeCompare(b.nickname),
  208. slots: { customRender: 'nickname' }
  209. },
  210. {
  211. title: '固件版本',
  212. dataIndex: 'firmware_version',
  213. width: 150,
  214. ellipsis: true,
  215. slots: { customRender: 'firmware_version' },
  216. },
  217. {
  218. title: '固件升级',
  219. dataIndex: 'firmware_status',
  220. width: 150,
  221. ellipsis: true,
  222. slots: { customRender: 'firmware_status' },
  223. },
  224. {
  225. title: '当前状态',
  226. dataIndex: 'status',
  227. width: 100,
  228. ellipsis: true,
  229. slots: { customRender: 'status' }
  230. },
  231. {
  232. title: '加入项目时间',
  233. dataIndex: 'bound_time',
  234. width: 200,
  235. sorter: (a: any, b: any) => a.bound_time.localeCompare(b.bound_time),
  236. slots: { customRender: 'bound_time' },
  237. },
  238. {
  239. title: '最后在线时间',
  240. dataIndex: 'login_time',
  241. width: 200,
  242. sorter: (a: any, b: any) => a.login_time.localeCompare(b.login_time),
  243. slots: { customRender: 'login_time' },
  244. },
  245. {
  246. title: '操作',
  247. dataIndex: 'actions',
  248. fixed: 'right',
  249. width: 120,
  250. slots: { customRender: 'action' },
  251. },
  252. ]
  253. const rowClassName = (record: any, index: number) => {
  254. const className = []
  255. if ((index & 1) === 0) {
  256. className.push('table-striped')
  257. }
  258. return className.toString().replaceAll(',', ' ')
  259. }
  260. const refreshData = async (page: any) => {
  261. paginationConfig.current = page?.current!
  262. paginationConfig.pageSize = page?.pageSize!
  263. await fetchList();
  264. }
  265. const rowSelection = {
  266. onChange: (selectedRowKeys: string[]) => {
  267. state.selectedRowKeys = selectedRowKeys;
  268. },
  269. }
  270. // 点击批量删除
  271. const onClickBatchDelete = async () => {
  272. console.log(state.selectedRowKeys, '点击批量删除');
  273. }
  274. // 点击搜索
  275. const onClickSearch = async (query: any) => {
  276. state.query = query;
  277. await fetchList();
  278. }
  279. // 点击重置
  280. const onClickReset = async (query: any) => {
  281. state.query = query;
  282. await fetchList();
  283. }
  284. // 点击设备日志
  285. const onClickDeviceLog = (record: any) => {
  286. state.deviceLogDrawerVisible = true;
  287. state.currentDevice = record;
  288. }
  289. // 点击告警信息
  290. const onClickDeviceHms = (record: any) => {
  291. state.deviceHmsDrawerVisible = true;
  292. state.currentDevice = record;
  293. }
  294. // 点击编辑
  295. const onClickEdit = (record: any) => {
  296. state.editableData[record.device_sn] = record;
  297. }
  298. // 点击保存
  299. const onClickSave = async (record: any) => {
  300. delete state.editableData[record.device_sn];
  301. await updateDevice({ nickname: record.nickname }, state.workspaceId, record.device_sn)
  302. if (record.children) {
  303. await updateDevice({ nickname: record.children.nickname }, state.workspaceId, record.children.device_sn)
  304. }
  305. }
  306. // 点击删除
  307. const onClickDelete = (record: any) => {
  308. Modal.confirm({
  309. title: '提示',
  310. content: `确定删除${record.device_name}吗?`,
  311. onOk: async () => {
  312. const res = await unbindDevice(record.device_sn);
  313. if (res.code !== 0) {
  314. return
  315. }
  316. await fetchList();
  317. },
  318. });
  319. }
  320. </script>
  321. <style lang="scss">
  322. .deviceList {
  323. padding: 20px;
  324. }
  325. .ant-table {
  326. border-top: 1px solid rgb(0, 0, 0, 0.06);
  327. border-bottom: 1px solid rgb(0, 0, 0, 0.06);
  328. }
  329. .ant-table-tbody tr td {
  330. border: 0;
  331. }
  332. .table-striped {
  333. background-color: #f7f9fa;
  334. }
  335. </style>