subDeviceMutipleBind.vue 5.1 KB

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