subDeviceMutipleBind.vue 5.1 KB

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