index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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" />
  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 } 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 { getWorkspaceId } from '/@/utils/index';
  87. interface State {
  88. workspaceId: string,
  89. query: any,
  90. listLoading: boolean,
  91. list: any[],
  92. selectedRowKeys: string[],
  93. currentDevice: any,
  94. editableData: {
  95. [key: string]: any,
  96. },
  97. deviceLogDrawerVisible: boolean,
  98. deviceHmsDrawerVisible: boolean,
  99. };
  100. const state: State = reactive({
  101. workspaceId: getWorkspaceId(),
  102. query: undefined,
  103. listLoading: false,
  104. list: [],
  105. selectedRowKeys: [],
  106. currentDevice: {},
  107. editableData: {},
  108. deviceLogDrawerVisible: false,
  109. deviceHmsDrawerVisible: false,
  110. })
  111. const paginationConfig = reactive({
  112. pageSizeOptions: ['20', '50', '100'],
  113. showQuickJumper: true,
  114. showSizeChanger: true,
  115. pageSize: 20,
  116. current: 1,
  117. total: 0
  118. })
  119. const fetchList = async () => {
  120. state.listLoading = true;
  121. try {
  122. const res = await getBindingDevices(state.workspaceId, {
  123. ...state.query,
  124. page: paginationConfig.current,
  125. page_size: paginationConfig.pageSize,
  126. });
  127. state.list = res.data.list;
  128. paginationConfig.total = res.data.pagination.total
  129. paginationConfig.current = res.data.pagination.page
  130. paginationConfig.pageSize = res.data.pagination.page_size
  131. } catch (e) {
  132. console.error(e);
  133. } finally {
  134. state.listLoading = false;
  135. }
  136. }
  137. onMounted(async () => {
  138. await fetchList();
  139. })
  140. const columns = [
  141. {
  142. title: '设备型号',
  143. dataIndex: 'device_name',
  144. width: 150,
  145. ellipsis: true,
  146. sorter: (a: any, b: any) => a.device_name.localeCompare(b.device_name),
  147. slots: { customRender: 'device_name' }
  148. },
  149. {
  150. title: '设备SN',
  151. dataIndex: 'device_sn',
  152. width: 250,
  153. ellipsis: true,
  154. slots: { customRender: 'device_sn' }
  155. },
  156. {
  157. title: '设备名称',
  158. dataIndex: 'nickname',
  159. width: 150,
  160. ellipsis: true,
  161. sorter: (a: any, b: any) => a.nickname.localeCompare(b.nickname),
  162. slots: { customRender: 'nickname' }
  163. },
  164. {
  165. title: '固件版本',
  166. dataIndex: 'firmware_version',
  167. width: 150,
  168. ellipsis: true,
  169. slots: { customRender: 'firmware_version' },
  170. },
  171. {
  172. title: '固件升级',
  173. dataIndex: 'firmware_status',
  174. width: 150,
  175. ellipsis: true,
  176. slots: { customRender: 'firmware_status' },
  177. },
  178. {
  179. title: '当前状态',
  180. dataIndex: 'status',
  181. width: 100,
  182. ellipsis: true,
  183. slots: { customRender: 'status' }
  184. },
  185. {
  186. title: '加入项目时间',
  187. dataIndex: 'bound_time',
  188. width: 200,
  189. sorter: (a: any, b: any) => a.bound_time.localeCompare(b.bound_time),
  190. slots: { customRender: 'bound_time' },
  191. },
  192. {
  193. title: '最后在线时间',
  194. dataIndex: 'login_time',
  195. width: 200,
  196. sorter: (a: any, b: any) => a.login_time.localeCompare(b.login_time),
  197. slots: { customRender: 'login_time' },
  198. },
  199. {
  200. title: '操作',
  201. dataIndex: 'actions',
  202. fixed: 'right',
  203. width: 120,
  204. slots: { customRender: 'action' },
  205. },
  206. ]
  207. const rowClassName = (record: any, index: number) => {
  208. const className = []
  209. if ((index & 1) === 0) {
  210. className.push('table-striped')
  211. }
  212. return className.toString().replaceAll(',', ' ')
  213. }
  214. const refreshData = async (page: any) => {
  215. paginationConfig.current = page?.current!
  216. paginationConfig.pageSize = page?.pageSize!
  217. await fetchList();
  218. }
  219. const rowSelection = {
  220. onChange: (selectedRowKeys: string[]) => {
  221. state.selectedRowKeys = selectedRowKeys;
  222. },
  223. }
  224. // 点击批量删除
  225. const onClickBatchDelete = async () => {
  226. console.log(state.selectedRowKeys, '点击批量删除');
  227. }
  228. // 点击搜索
  229. const onClickSearch = async (query: any) => {
  230. state.query = query;
  231. await fetchList();
  232. }
  233. // 点击重置
  234. const onClickReset = async (query: any) => {
  235. state.query = query;
  236. await fetchList();
  237. }
  238. // 点击设备日志
  239. const onClickDeviceLog = (record: any) => {
  240. state.deviceLogDrawerVisible = true;
  241. state.currentDevice = record;
  242. }
  243. // 点击告警信息
  244. const onClickDeviceHms = (record: any) => {
  245. state.deviceHmsDrawerVisible = true;
  246. state.currentDevice = record;
  247. }
  248. // 点击编辑
  249. const onClickEdit = (record: any) => {
  250. state.editableData[record.device_sn] = record;
  251. }
  252. // 点击保存
  253. const onClickSave = async (record: any) => {
  254. delete state.editableData[record.device_sn];
  255. await updateDevice({ nickname: record.nickname }, state.workspaceId, record.device_sn)
  256. if (record.children) {
  257. await updateDevice({ nickname: record.children.nickname }, state.workspaceId, record.children.device_sn)
  258. }
  259. }
  260. // 点击删除
  261. const onClickDelete = (record: any) => {
  262. Modal.confirm({
  263. title: '提示',
  264. content: `确定删除${record.device_name}吗?`,
  265. onOk: async () => {
  266. const res = await unbindDevice(record.device_sn);
  267. if (res.code !== 0) {
  268. return
  269. }
  270. await fetchList();
  271. },
  272. });
  273. }
  274. </script>
  275. <style lang="scss">
  276. .deviceList {
  277. padding: 20px;
  278. }
  279. .ant-table {
  280. border-top: 1px solid rgb(0, 0, 0, 0.06);
  281. border-bottom: 1px solid rgb(0, 0, 0, 0.06);
  282. }
  283. .ant-table-tbody tr td {
  284. border: 0;
  285. }
  286. .table-striped {
  287. background-color: #f7f9fa;
  288. }
  289. </style>