index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <schema-table
  9. ref="table"
  10. :schema="schema"
  11. />
  12. <confirm-dialog
  13. ref="editDialog"
  14. :title="dialogTitle"
  15. @confirm="onSave"
  16. >
  17. <div class="c-grid-form u-align-self--center">
  18. <span class="c-grid-form__label required">名称:</span>
  19. <el-input
  20. v-model.trim="currObj.name"
  21. placeholder="最多50个字符"
  22. maxlength="50"
  23. clearable
  24. />
  25. <span class="c-grid-form__label">备注:</span>
  26. <el-input
  27. v-model="currObj.remark"
  28. type="textarea"
  29. maxlength="100"
  30. :rows="3"
  31. show-word-limit
  32. />
  33. </div>
  34. </confirm-dialog>
  35. <table-dialog
  36. ref="subDeviceDialog"
  37. title="设备"
  38. :schema="subDeviceSchema"
  39. size="large"
  40. />
  41. <table-dialog
  42. ref="deviceDialog"
  43. title="添加设备"
  44. :schema="deviceSchema"
  45. append-to-body
  46. @choosen="onChoosenDevice"
  47. />
  48. </wrapper>
  49. </template>
  50. <script>
  51. import {
  52. getDeviceGroups,
  53. addDeviceGroup,
  54. updateDeviceGroup,
  55. deleteDeviceGroup,
  56. getDevicesByGroup,
  57. getDevices,
  58. addDeviceToGroup,
  59. deleteDeviceFromGroup
  60. } from '@/api/device'
  61. export default {
  62. name: 'DeviceGroup',
  63. data () {
  64. return {
  65. isAdd: false,
  66. currObj: {},
  67. schema: {
  68. condition: { name: '' },
  69. list: getDeviceGroups,
  70. buttons: [
  71. { type: 'add', on: this.onAdd }
  72. ],
  73. filters: [
  74. { key: 'name', type: 'search', placeholder: '分组名称' }
  75. ],
  76. cols: [
  77. { prop: 'name', label: '分组名称' },
  78. { prop: 'remark', label: '备注' },
  79. { type: 'invoke', width: 160, render: [
  80. { label: '编辑', on: this.onEdit },
  81. { label: '设备', on: this.onViewDevices },
  82. { label: '删除', on: this.onDel }
  83. ] }
  84. ]
  85. },
  86. subDeviceSchema: {
  87. singlePage: true,
  88. list: this.getDevicesByGroup,
  89. buttons: [
  90. { type: 'add', label: '添加设备', on: this.onAddDevice }
  91. ],
  92. cols: [
  93. { prop: 'name', label: '设备名称' },
  94. { prop: 'productName', label: '产品' },
  95. { prop: 'serialNumber', label: '序列号' },
  96. { prop: 'mac', label: 'MAC' },
  97. { prop: 'resolutionRatio', label: '分辨率' },
  98. { prop: 'remark', label: '地址' },
  99. { type: 'invoke', width: 100, render: [
  100. { label: '移除', on: this.onDelDevice }
  101. ] }
  102. ]
  103. },
  104. deviceSchema: {
  105. condition: { name: '' },
  106. filters: [
  107. { key: 'name', type: 'search', placeholder: '设备名称' }
  108. ],
  109. list: getDevices,
  110. cols: [
  111. { prop: 'name', label: '设备名称' },
  112. { prop: 'productName', label: '产品' },
  113. { prop: 'serialNumber', label: '序列号' },
  114. { prop: 'mac', label: 'MAC' },
  115. { prop: 'resolutionRatio', label: '分辨率' },
  116. { prop: 'remark', label: '地址' }
  117. ]
  118. }
  119. }
  120. },
  121. computed: {
  122. dialogTitle () {
  123. return this.isAdd ? '新增设备分组' : '编辑设备分组'
  124. }
  125. },
  126. methods: {
  127. onAdd () {
  128. this.isAdd = true
  129. this.currObj = {
  130. name: '',
  131. remark: ''
  132. }
  133. this.$refs.editDialog.show()
  134. },
  135. onEdit (deviceGroup) {
  136. this.isAdd = false
  137. const { id, name, remark } = deviceGroup
  138. this.currObj = { id, name, remark }
  139. this.$currObj = deviceGroup
  140. this.$refs.editDialog.show()
  141. },
  142. onSave (done) {
  143. if (!this.currObj.name) {
  144. this.$message({
  145. type: 'warning',
  146. message: '分组名称不能为空'
  147. })
  148. return
  149. }
  150. this.save(done).then(() => {
  151. if (this.isAdd) {
  152. this.$refs.table.resetCondition({ name: this.currObj.name })
  153. } else {
  154. this.$refs.table.pageTo()
  155. }
  156. })
  157. },
  158. save (done) {
  159. if (!this.isAdd && Object.keys(this.currObj).every(key => this.currObj[key] === this.$currObj[key])) {
  160. done()
  161. return Promise.reject()
  162. }
  163. return (this.isAdd ? addDeviceGroup : updateDeviceGroup)(this.currObj).then(done)
  164. },
  165. onDel (deviceGroup) {
  166. return deleteDeviceGroup(deviceGroup).then(() => {
  167. this.$refs.table.decrease(1)
  168. })
  169. },
  170. onViewDevices ({ id }) {
  171. this.$devices = null
  172. this.$refs.subDeviceDialog.show({ id })
  173. },
  174. getDevicesByGroup ({ id }) {
  175. if (this.$devices) {
  176. return Promise.resolve({ data: this.$devices })
  177. }
  178. return getDevicesByGroup(id).then(({ data }) => {
  179. this.$devices = data
  180. return { data }
  181. })
  182. },
  183. onDelDevice (device) {
  184. const table = this.$refs.subDeviceDialog.getTable()
  185. deleteDeviceFromGroup(table.getCondition().id, device).then(() => {
  186. this.$devices = null
  187. table.decrease(1)
  188. })
  189. },
  190. onAddDevice () {
  191. this.$refs.deviceDialog.show()
  192. },
  193. canJoin (device) {
  194. const data = this.$refs.subDeviceDialog.getTable().getData()
  195. if (data.length === 0 || data.some(({ resolutionRatio }) => device.resolutionRatio === resolutionRatio)) {
  196. return Promise.resolve()
  197. }
  198. return this.$confirm(
  199. `设备 ${device.name} 与分组中的设备分辨率不一致,确定加入分组?`,
  200. { type: 'warning' }
  201. )
  202. },
  203. onChoosenDevice ({ value, done }) {
  204. const table = this.$refs.subDeviceDialog.getTable()
  205. this.canJoin(value).then(() => {
  206. addDeviceToGroup(table.getCondition().id, value.id).then(() => {
  207. done()
  208. this.$devices = null
  209. table.pageTo(1)
  210. })
  211. })
  212. }
  213. }
  214. }
  215. </script>