subDeviceMutipleBind.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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="productId">
  6. <el-select @change="handleChange" v-model="ruleForm.productId" placeholder="请选择所属产品" style="width: 300px;">
  7. <el-option v-for="item in productData" :key="item.id" :label="item.name" :value="item.id" />
  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="true" v-col="'key'" />
  15. <el-table-column label="设备名称" prop="name" :show-overflow-tooltip="true" v-col="'name'" />
  16. <el-table-column label="产品名称" prop="productName" :show-overflow-tooltip="true" 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
  28. v-show="tableData.total > 0"
  29. :total="tableData.total"
  30. v-model:page="tableData.param.pageNum"
  31. v-model:limit="tableData.param.pageSize"
  32. @pagination="getDeviceList"
  33. />
  34. </el-dialog>
  35. </div>
  36. </template>
  37. <script lang="ts">
  38. import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue'
  39. import { ElMessageBox, ElMessage, FormInstance } from 'element-plus'
  40. import 'vue3-json-viewer/dist/index.css'
  41. import { useRoute } from 'vue-router'
  42. import api from '/@/api/device'
  43. interface TableDataState {
  44. isShowDialog: boolean,
  45. productData: object[],
  46. deviceKeyList: string[];
  47. gatewayKey: string;
  48. tableData: {
  49. data: []
  50. total: number
  51. loading: boolean
  52. param: {
  53. pageNum: number
  54. pageSize: number
  55. productId: number
  56. status: string
  57. dateRange: string[]
  58. }
  59. },
  60. ruleForm: {
  61. productId: string|number
  62. },
  63. rules: {}
  64. }
  65. export default defineComponent({
  66. name: 'MutipleBindDialog',
  67. // components: { EditDic, EditAttr, EditFun, EditEvent, EditTab, devantd, ListDic, functionCom },
  68. setup(prop, { emit }) {
  69. console.log(prop)
  70. const route = useRoute()
  71. // const editDicRef = ref()
  72. const state = reactive<TableDataState>({
  73. gatewayKey: '',
  74. deviceKeyList: [],
  75. isShowDialog: false,
  76. productData: [],
  77. tableData: {
  78. data: [],
  79. total: 0,
  80. loading: false,
  81. param: {
  82. pageNum: 1,
  83. pageSize: 10,
  84. productId: 0,
  85. status: '',
  86. dateRange: [],
  87. },
  88. },
  89. ruleForm: {
  90. productId: ''
  91. },
  92. rules: {
  93. productId: [{ required: true, message: '所属产品不能为空', trigger: 'blur' }],
  94. }
  95. })
  96. onMounted(() => {
  97. console.log('第一次加载')
  98. // getProductList()
  99. })
  100. const getDeviceList = () => {
  101. if(!state.ruleForm.productId) {
  102. state.tableData.data = [];
  103. state.tableData.total = 0;
  104. return;
  105. }
  106. state.tableData.loading = true;
  107. api.device.getSubList({
  108. "productId": state.ruleForm.productId,
  109. "pageSize": state.tableData.param.pageSize,
  110. "pageNum": state.tableData.param.pageNum
  111. }).then((res: any) => {
  112. console.log(res)
  113. state.tableData.data = res.device;
  114. state.tableData.total = res.Total;
  115. }).finally(() => (state.tableData.loading = false));
  116. };
  117. const getProductList = () => {
  118. api.product.getSubList().then((res: any) => {
  119. let productDataList = [{id: "", name: "全部"}].concat(res.product)
  120. state.productData = productDataList;
  121. state.ruleForm.productId = state.productData[0].id
  122. getDeviceList()
  123. state.isShowDialog = true;
  124. });
  125. };
  126. const openDialog = (gatewayKey: any) => {
  127. console.log('第二次加载')
  128. state.gatewayKey = gatewayKey;
  129. getProductList()
  130. // state.isShowDialog = true;
  131. };
  132. // 多选框选中数据
  133. const handleSelectionChange = (selection: any[]) => {
  134. console.log(selection)
  135. console.log(typeof selection)
  136. // selection.forEach((item:any) => {
  137. // console.log(item)
  138. // state.deviceKeyList.push(item.key)
  139. // })
  140. state.deviceKeyList = selection.map((item) => item.key);
  141. console.log(state.deviceKeyList)
  142. console.log(typeof state.deviceKeyList)
  143. };
  144. const confirmBind = () => {
  145. let msg = '是否进行批量绑定?';
  146. if (state.deviceKeyList.length === 0) {
  147. ElMessage.error('请选择要批量绑定的数据。');
  148. return;
  149. }
  150. // return
  151. ElMessageBox.confirm(msg, '提示', {
  152. confirmButtonText: '确认',
  153. cancelButtonText: '取消',
  154. type: 'warning',
  155. })
  156. .then(() => {
  157. api.device.mutipleBind({
  158. "gatewayKey":state.gatewayKey,
  159. "subKeys": state.deviceKeyList
  160. }).then(() => {
  161. ElMessage.success('绑定成功');
  162. emit('bindSuccess')
  163. state.isShowDialog = false;
  164. });
  165. })
  166. .catch(() => { });
  167. };
  168. const handleChange = (productId: number) => {
  169. state.ruleForm.productId = productId;
  170. getDeviceList()
  171. }
  172. return {
  173. openDialog,
  174. getProductList,
  175. confirmBind,
  176. getDeviceList,
  177. handleSelectionChange,
  178. handleChange,
  179. ...toRefs(state),
  180. }
  181. },
  182. })
  183. </script>
  184. <style lang="scss" scoped>
  185. .mutiple-bind-dialog-wrap {
  186. background: pink;
  187. }
  188. </style>