Device.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <template>
  2. <schema-table
  3. ref="table"
  4. :schema="schema"
  5. @row-click="onToggleSelection"
  6. @selection-change="onSelectionChange"
  7. >
  8. <selection-table-dialog
  9. ref="tableDialog"
  10. size="lg"
  11. title="分配设备(分配已有归属的设备将移除原有关联)"
  12. message="请选择需要分配的设备"
  13. :schema="deviceSchema"
  14. @confirm="onBindDevices"
  15. />
  16. </schema-table>
  17. </template>
  18. <script>
  19. import {
  20. getBoundDevices,
  21. getDevices,
  22. bindDevices,
  23. unbindDevice,
  24. unbindDevices
  25. } from './api'
  26. export default {
  27. name: 'Device',
  28. props: {
  29. groups: {
  30. type: Array,
  31. required: true
  32. },
  33. group: {
  34. type: Object,
  35. required: true
  36. }
  37. },
  38. data () {
  39. return {
  40. schema: {
  41. list: this.getBoundDevices,
  42. condition: { termQuery: 0 },
  43. buttons: [
  44. { type: 'add', label: '分配', on: this.onAdd },
  45. { type: 'del', label: '移除', on: this.onBatchDel }
  46. ],
  47. filters: [
  48. { key: 'termQuery', type: 'select', options: [
  49. { value: 0, label: '级联' },
  50. { value: 1, label: '精准' }
  51. ] },
  52. { key: 'name', type: 'search', placeholder: '设备名称' }
  53. ],
  54. cols: [
  55. { type: 'selection' },
  56. { prop: 'name', label: '设备名称' },
  57. { label: '归属部门', render: ({ org }) => org ? this.groupMap[org] || '未知' : '-' },
  58. { prop: 'address', label: '地址' },
  59. { type: 'invoke', width: 100, render: [
  60. { label: '移除', on: this.onDel }
  61. ] }
  62. ]
  63. },
  64. deviceSchema: {
  65. list: this.getDevices,
  66. condition: { isFullDisplay: 0 },
  67. filters: [
  68. { key: 'isFullDisplay', type: 'select', options: [
  69. { value: 0, label: '未绑定' },
  70. { value: 1, label: '未绑定自身' }
  71. ] },
  72. { key: 'name', type: 'search', placeholder: '设备名称' }
  73. ],
  74. cols: [
  75. { prop: 'name', label: '设备名称' },
  76. { label: '归属部门', render: ({ org }) => org ? this.groupMap[org] || '未知' : '-' },
  77. { prop: 'address', label: '地址' }
  78. ]
  79. }
  80. }
  81. },
  82. computed: {
  83. groupMap () {
  84. const map = {}
  85. this.createMap(this.groups, map)
  86. return map
  87. }
  88. },
  89. watch: {
  90. group () {
  91. this.$refs.table.pageTo(1)
  92. }
  93. },
  94. methods: {
  95. createMap (arr, map) {
  96. arr?.forEach(({ path, name, children }) => {
  97. map[path] = name
  98. this.createMap(children, map)
  99. })
  100. },
  101. getBoundDevices (params) {
  102. return getBoundDevices({
  103. org: this.group.path,
  104. ...params
  105. })
  106. },
  107. getDevices (params) {
  108. return getDevices({
  109. org: this.group.path,
  110. ...params
  111. })
  112. },
  113. onAdd () {
  114. this.$refs.tableDialog.show()
  115. },
  116. onBindDevices ({ value, done }) {
  117. bindDevices(this.group.path, value.map(({ id }) => id)).then(() => {
  118. done()
  119. this.$refs.table.pageTo(1)
  120. })
  121. },
  122. onDel (device) {
  123. unbindDevice(device).then(() => {
  124. this.$refs.table.decrease(1)
  125. })
  126. },
  127. onBatchDel () {
  128. if (this.$selectionItems?.length) {
  129. unbindDevices(this.$selectionItems.map(({ deviceRelationId }) => deviceRelationId)).then(() => {
  130. this.$refs.table.decrease(this.$selectionItems.length)
  131. this.$selectionItems = null
  132. })
  133. } else {
  134. this.$message({
  135. type: 'warning',
  136. message: '请先选择需要移除的设备'
  137. })
  138. }
  139. },
  140. onToggleSelection (row) {
  141. this.$refs.table.getInst().toggleRowSelection(row)
  142. },
  143. onSelectionChange (val) {
  144. this.$selectionItems = val
  145. }
  146. }
  147. }
  148. </script>