index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <wrapper
  3. fill
  4. margin
  5. padding
  6. background
  7. >
  8. <schema-table
  9. ref="table"
  10. row-key="id"
  11. :schema="schema"
  12. @row-click="onRowClick"
  13. />
  14. <event-target-dialog
  15. ref="eventTargetDialog"
  16. :ratio="ratio"
  17. @choosen="onChoosen"
  18. />
  19. </wrapper>
  20. </template>
  21. <script>
  22. import {
  23. getDevices,
  24. getSubDevices,
  25. activateDevice,
  26. deactivateDevice
  27. } from '@/api/device'
  28. import { publish } from '@/api/platform'
  29. import {
  30. Access,
  31. EventPriority,
  32. EventFreq,
  33. PublishType
  34. } from '@/constant'
  35. import { toDateStr } from '@/utils/event'
  36. export default {
  37. name: 'DeviceList',
  38. data () {
  39. const canEdit = this.accessSet.has(Access.MANAGE_DEVICE)
  40. return {
  41. ratio: null,
  42. schema: {
  43. keepalive: true,
  44. condition: { name: '' },
  45. list: getDevices,
  46. transform: this.transform,
  47. transformData: this.transformTableData,
  48. filters: [
  49. { key: 'name', type: 'search', placeholder: '设备名称' }
  50. ],
  51. cols: [
  52. { type: 'refresh', render (data, h) {
  53. return data.isMaster
  54. ? h('i', {
  55. staticClass: `o-expand-icon u-pointer ${data.loading ? 'el-icon-loading' : 'el-icon-arrow-right'}`,
  56. class: { expand: data.expand }
  57. })
  58. : null
  59. } },
  60. { label: '设备名称', 'min-width': 120, render (data, h) {
  61. return data.empty ? h('span', { staticClass: 'u-color--info' }, '暂无备份设备') : data.name
  62. } },
  63. { prop: 'productName', label: '产品' },
  64. { prop: 'serialNumber', label: '序列号', 'min-width': 100 },
  65. { prop: 'mac', label: 'MAC', 'min-width': 100 },
  66. { prop: 'address', label: '地址' },
  67. { type: 'tag', 'width': 100, render ({ empty, activate, onlineStatus }) {
  68. return empty
  69. ? null
  70. : {
  71. type: activate
  72. ? activate === 1
  73. ? void 0
  74. : onlineStatus === 1
  75. ? 'success'
  76. : 'danger'
  77. : 'warning',
  78. label: activate
  79. ? activate === 1
  80. ? '已激活'
  81. : onlineStatus === 1
  82. ? '在线'
  83. : '离线'
  84. : '未激活'
  85. }
  86. }, on: this.onTagClick },
  87. { type: 'invoke', width: canEdit ? 160 : 120, render: [
  88. { label: '查看', render ({ empty }) { return !empty }, on: this.onViewDevice },
  89. canEdit ? { label: '默认播放', render ({ isMaster, activate }) { return isMaster && activate }, on: this.onSetDefaultProgram } : null
  90. ].filter(Boolean) }
  91. ]
  92. }
  93. }
  94. },
  95. methods: {
  96. transform (data) {
  97. return {
  98. ...data,
  99. loaded: false,
  100. loading: false,
  101. expand: false,
  102. subs: [],
  103. isMaster: true
  104. }
  105. },
  106. transformTableData (data) {
  107. const arr = []
  108. data.forEach(item => {
  109. arr.push(item)
  110. if (item.loaded && item.expand) {
  111. arr.push(...item.subs)
  112. }
  113. })
  114. return arr
  115. },
  116. onViewDevice (item) {
  117. this.$router.push({
  118. name: 'device-detail',
  119. params: {
  120. id: item.id
  121. }
  122. })
  123. },
  124. reloadSubDevices (item) {
  125. item.loaded = false
  126. item.children = []
  127. this.getSubDevices(item)
  128. },
  129. getSubDevices (item, pageSize = 999) {
  130. item.loading = true
  131. getSubDevices({
  132. id: item.id,
  133. pageNum: 1,
  134. pageSize
  135. }).then(({ data, totalCount }) => {
  136. if (totalCount > pageSize) {
  137. this.getSubDevices(item, totalCount)
  138. } else {
  139. item.loading = false
  140. item.loaded = true
  141. item.expand = true
  142. if (data.length === 0) {
  143. item.subs = [{ id: `${Math.random()}`, empty: true }]
  144. } else {
  145. item.subs = data.map(device => {
  146. return {
  147. ...device,
  148. isMaster: false,
  149. empty: false,
  150. parent: item
  151. }
  152. })
  153. }
  154. }
  155. }, () => {
  156. item.loading = false
  157. })
  158. },
  159. onRowClick (data) {
  160. if (data.isMaster) {
  161. if (data.loaded) {
  162. data.expand = !data.expand
  163. } else if (!data.loading) {
  164. this.getSubDevices(data)
  165. }
  166. }
  167. },
  168. onTagClick (data) {
  169. (data.activate ? deactivateDevice : activateDevice)(data).then(() => {
  170. if (data.isMaster) {
  171. this.$refs.table.pageTo()
  172. } else {
  173. this.reloadSubDevices(data.parent)
  174. }
  175. })
  176. },
  177. onSetDefaultProgram (device) {
  178. this.ratio = device.resolutionRatio
  179. this.$device = {
  180. id: device.id,
  181. name: device.name
  182. }
  183. this.$refs.eventTargetDialog.show()
  184. },
  185. onChoosen ({ value, done }) {
  186. this.$confirm(
  187. `将设备 ${this.$device.name} 的默认播放设置为 ${value.name} ?`,
  188. { type: 'warning' }
  189. ).then(() => publish(
  190. [this.$device.id],
  191. {
  192. type: PublishType.EVENT,
  193. detail: {
  194. priority: EventPriority.DEFAULT,
  195. freq: EventFreq.ONCE,
  196. start: `${toDateStr(new Date())} 00:00:00`,
  197. until: null,
  198. target: value
  199. }
  200. },
  201. {
  202. programCalendarName: value.name,
  203. resolutionRatio: this.ratio
  204. }
  205. )).then(done)
  206. }
  207. }
  208. }
  209. </script>