subDeviceMutipleBind.vue 6.0 KB

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