deviceBind.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="mutiple-bind-dialog-wrap">
  3. <el-dialog title="选择设备" v-model="isShowDialog" width="90%">
  4. <el-form :model="ruleForm" ref="formRef" :rules="rules" size="small" label-width="110px">
  5. <el-form-item label="设备名称" prop="name">
  6. <el-input v-model="tableData.param.name" placeholder="请输入设备名称" clearable style="width: 240px" @keyup.enter.native="getDeviceList" />
  7. <el-button style="margin-left: 20px;" type="primary" @click="getDeviceList()">查询</el-button>
  8. <el-button style="margin-left: 20px;" :disabled="!deviceKeyList.length" type="danger" @click="confirmBind()">确定选择</el-button>
  9. </el-form-item>
  10. </el-form>
  11. <el-table ref="multipleTable" :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" v-loading="tableData.loading">
  12. <el-table-column type="selection" width="55" align="center" />
  13. <el-table-column label="标识" prop="key" width="130" show-overflow-tooltip v-col="'key'" />
  14. <el-table-column label="设备名称" prop="name" show-overflow-tooltip v-col="'name'" />
  15. <el-table-column label="产品名称" prop="productName" show-overflow-tooltip v-col="'productName'" />
  16. <el-table-column prop="status" label="状态" width="100" align="center" v-col="'status'">
  17. <template #default="scope">
  18. <el-tag type="info" size="small" v-if="scope.row.status == 1">离线</el-tag>
  19. <el-tag type="success" size="small" v-if="scope.row.status == 2">在线</el-tag>
  20. <el-tag type="info" size="small" v-if="scope.row.status == 0">未启用</el-tag>
  21. </template>
  22. </el-table-column>
  23. <el-table-column prop="registryTime" label="激活时间" align="center" width="150" v-col="'registryTime'"></el-table-column>
  24. <el-table-column prop="desc" label="说明" v-col="'desc'"></el-table-column>
  25. </el-table>
  26. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="getDeviceList" />
  27. </el-dialog>
  28. </div>
  29. </template>
  30. <script lang="ts">
  31. import { toRefs, reactive, defineComponent, nextTick, getCurrentInstance } from 'vue'
  32. import { ElMessageBox, ElMessage } from 'element-plus'
  33. import 'vue3-json-viewer/dist/index.css'
  34. import { useRoute } from 'vue-router'
  35. import api from '/@/api/device'
  36. interface TableDataState {
  37. isShowDialog: boolean,
  38. productData: object[],
  39. deviceKeyList: string[];
  40. deviceIdList: string[];
  41. deviceNameList: string[];
  42. checkIdList: string[];
  43. tableData: {
  44. data: []
  45. total: number
  46. loading: boolean
  47. param: {
  48. pageNum: number
  49. pageSize: number
  50. name: string
  51. productId: number
  52. }
  53. },
  54. ruleForm: {
  55. productId: string | number
  56. },
  57. rules: {}
  58. }
  59. export default defineComponent({
  60. name: 'DeviceBindDialog',
  61. setup(prop, { emit }) {
  62. const { proxy } = getCurrentInstance() as any;
  63. const route = useRoute()
  64. const state = reactive<TableDataState>({
  65. deviceKeyList: [],
  66. deviceIdList: [],
  67. deviceNameList: [],
  68. isShowDialog: false,
  69. productData: [],
  70. checkIdList: [],
  71. tableData: {
  72. data: [],
  73. total: 0,
  74. loading: false,
  75. param: {
  76. pageNum: 1,
  77. pageSize: 10,
  78. name: '',
  79. productId: 0,
  80. },
  81. },
  82. ruleForm: {
  83. productId: ''
  84. },
  85. rules: {
  86. productId: [{ required: true, message: '所属产品不能为空', trigger: 'blur' }],
  87. }
  88. })
  89. const getDeviceList = () => {
  90. // if (!state.ruleForm.productId) {
  91. // state.tableData.data = [];
  92. // state.tableData.total = 0;
  93. // return;
  94. // }
  95. state.isShowDialog = true;
  96. state.tableData.loading = true;
  97. api.instance.getList(state.tableData.param).then((res: any) => {
  98. state.tableData.data = res.device;
  99. state.tableData.total = res.total;
  100. changeSelect();
  101. }).finally(() => (state.tableData.loading = false));
  102. };
  103. const getProductList = () => {
  104. api.product.getSubList().then((res: any) => {
  105. let productDataList = res.product
  106. state.productData = productDataList;
  107. state.ruleForm.productId = state.productData[0].id
  108. getDeviceList()
  109. });
  110. };
  111. const openDialog = (checkIdData: any, productId: any) => {
  112. state.checkIdList = checkIdData;
  113. state.tableData.param.productId = productId;
  114. getDeviceList()
  115. };
  116. // 多选框选中数据
  117. const handleSelectionChange = (selection: any[]) => {
  118. state.deviceKeyList = selection.map((item) => item.key);
  119. state.deviceIdList = selection.map((item) => item.id);
  120. state.deviceNameList = selection.map((item) => item.name);
  121. };
  122. const confirmBind = () => {
  123. let msg = '是否确定选择设备?';
  124. if (state.deviceKeyList.length === 0) {
  125. ElMessage.error('请选择要确定绑定的数据。');
  126. return;
  127. }
  128. ElMessageBox.confirm(msg, '提示', {
  129. confirmButtonText: '确认',
  130. cancelButtonText: '取消',
  131. type: 'warning',
  132. }).then(() => {
  133. emit('bindSuccess', state.deviceIdList, state.deviceNameList)
  134. state.isShowDialog = false;
  135. })
  136. };
  137. const handleChange = (productId: number) => {
  138. state.ruleForm.productId = productId;
  139. getDeviceList()
  140. }
  141. const changeSelect = () => {
  142. nextTick(() => {
  143. state.tableData.data.forEach((item) => {
  144. if (state.checkIdList.includes(item.id)) {
  145. proxy.$refs.multipleTable.toggleRowSelection(item, true);
  146. }
  147. })
  148. });
  149. };
  150. return {
  151. openDialog,
  152. getProductList,
  153. confirmBind,
  154. getDeviceList,
  155. handleSelectionChange,
  156. handleChange,
  157. ...toRefs(state),
  158. }
  159. },
  160. })
  161. </script>