Device.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <schema-table
  3. ref="table"
  4. :schema="schema"
  5. >
  6. <table-dialog
  7. ref="tableDialog"
  8. title="分配设备"
  9. :schema="deviceSchema"
  10. @choosen="onChoosen"
  11. />
  12. </schema-table>
  13. </template>
  14. <script>
  15. import { getUserGroups } from '@/api/user'
  16. import {
  17. getProducts,
  18. getDevicesByAdmin,
  19. getDevicesByRelation,
  20. bindDeviceToObject,
  21. unbindDevice
  22. } from '@/api/device'
  23. export default {
  24. name: 'Device',
  25. props: {
  26. isGroup: {
  27. type: [Boolean, String],
  28. default: false
  29. },
  30. tenant: {
  31. type: Object,
  32. required: true
  33. },
  34. value: {
  35. type: String,
  36. required: true
  37. }
  38. },
  39. data () {
  40. return {
  41. schema: {
  42. list: this.getDevices,
  43. buttons: [
  44. { type: 'add', label: '分配', on: this.onAdd }
  45. ],
  46. filters: [
  47. { key: 'name', type: 'search', placeholder: '设备名称' }
  48. ],
  49. cols: [
  50. { prop: 'name', label: '设备名称' },
  51. { prop: 'serialNumber', label: '序列号' },
  52. { prop: 'mac', label: 'MAC' },
  53. { prop: 'remark', label: '地址' },
  54. { type: 'invoke', width: 100, render: [
  55. { label: '移除', on: this.onDel }
  56. ] }
  57. ]
  58. }
  59. }
  60. },
  61. computed: {
  62. deviceSchema () {
  63. return {
  64. condition: { productId: void 0, name: '', tenant: this.tenant.path },
  65. list: getDevicesByAdmin,
  66. filters: [
  67. {
  68. key: 'productId',
  69. type: 'select',
  70. placeholder: '全部产品',
  71. remote: getProducts,
  72. condition: { tenant: this.tenant.path },
  73. pagination: true,
  74. value: 'id',
  75. label: 'name'
  76. },
  77. { key: 'name', type: 'search', placeholder: '设备名称' }
  78. ],
  79. cols: [
  80. { prop: 'name', label: '设备名称' },
  81. { prop: 'serialNumber', label: '序列号' },
  82. { prop: 'mac', label: 'MAC' },
  83. { prop: 'remark', label: '地址' }
  84. ]
  85. }
  86. }
  87. },
  88. watch: {
  89. value () {
  90. this.$userGroup = null
  91. this.$refs.table.pageTo(1)
  92. }
  93. },
  94. methods: {
  95. getDevices (params) {
  96. const queryParams = { ...params }
  97. if (this.isGroup) {
  98. queryParams.org = this.value
  99. } else {
  100. queryParams.user = this.value
  101. }
  102. return getDevicesByRelation(queryParams)
  103. },
  104. onAdd () {
  105. this.$refs.tableDialog.show()
  106. },
  107. async onChoosen ({ value: { id } }) {
  108. const loading = this.$showLoading()
  109. try {
  110. let key = this.value
  111. if (!this.isGroup) {
  112. if (!this.$userGroup) {
  113. const groups = await getUserGroups(this.value)
  114. this.$userGroup = groups[groups.length - 1].path
  115. }
  116. key = `${this.$userGroup}/${key}`
  117. }
  118. await bindDeviceToObject(id, key)
  119. this.$message({
  120. type: 'success',
  121. message: '分配成功'
  122. })
  123. this.$refs.table.pageTo(1)
  124. } catch (e) {
  125. console.log(e)
  126. }
  127. this.$closeLoading(loading)
  128. },
  129. onDel (device) {
  130. unbindDevice(device).then(() => {
  131. this.$refs.table.decrease(1)
  132. })
  133. }
  134. }
  135. }
  136. </script>