index.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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="新增素材包"
  15. @confirm="onSave"
  16. >
  17. <div class="c-grid-form u-align-self--center">
  18. <span class="c-grid-form__label u-required">名称</span>
  19. <el-input
  20. v-model.trim="dataset.name"
  21. placeholder="最多30个字符"
  22. maxlength="30"
  23. clearable
  24. />
  25. </div>
  26. </confirm-dialog>
  27. <table-dialog
  28. ref="assetDialog"
  29. title="上播内容"
  30. :schema="assetSchema"
  31. />
  32. <selection-table-dialog
  33. ref="assetTableDialog"
  34. title="上播内容选择"
  35. message="请选择上播内容"
  36. :schema="assetTableSchema"
  37. @confirm="onChoosenAsset"
  38. />
  39. <table-dialog
  40. ref="deviceDialog"
  41. title="关联的设备"
  42. :schema="deviceSchema"
  43. @row-click="onToggleSelection"
  44. @selection-change="onSelectionChange"
  45. />
  46. <radio-table-dialog
  47. ref="devicesDialog"
  48. title="绑定设备"
  49. message="请选择设备"
  50. :schema="devicesSchema"
  51. append-to-body
  52. @confirm="onChoosenDevice"
  53. />
  54. <preview-dialog ref="previewDialog" />
  55. </wrapper>
  56. </template>
  57. <script>
  58. import {
  59. AssetTagInfo,
  60. AssetType,
  61. AssetTypeInfo
  62. } from '@/constant'
  63. import {
  64. parseDuration,
  65. getAssetThumb,
  66. getAssetDuration
  67. } from '@/utils'
  68. import { getDevices } from '@/api/device'
  69. import {
  70. getDatasets,
  71. addDataset,
  72. updateDataset,
  73. getDataset,
  74. deleteDataset,
  75. bindAssetsToDataset,
  76. unbindAssetsFromDataset,
  77. updateDatasetAssetDuration,
  78. getDevicesByDataset,
  79. bindDatasetToDevice,
  80. unbindDatasetByDevices,
  81. unbindDatasetByDevice
  82. } from './api'
  83. import { assetPublicTableMixin } from '@/mixins/asset-table'
  84. export default {
  85. name: 'DatasetTenant',
  86. mixins: [assetPublicTableMixin],
  87. data () {
  88. return {
  89. schema: {
  90. buttons: [
  91. { type: 'add', on: this.onAdd }
  92. ],
  93. list: getDatasets,
  94. cols: [
  95. { prop: 'name', label: '名称', render: (data, h) => h('edit-input', {
  96. props: {
  97. value: `${data.name}`
  98. },
  99. on: { edit: val => this.onEditName(data, val) }
  100. }), 'class-name': 'c-edit-column' },
  101. // { label: '使用情况', type: 'tag', render: ({ delFlag }) => {
  102. // return {
  103. // type: ['primary', 'success'][delFlag],
  104. // label: ['暂未使用', '已使用'][delFlag]
  105. // }
  106. // }, align: 'center' },
  107. { type: 'invoke', render: [
  108. { label: '上播内容', on: this.onEdit },
  109. { label: '关联设备', on: this.onViewDevices },
  110. { label: '删除', on: this.onDel }
  111. ], width: 240 }
  112. ]
  113. },
  114. assetSchema: {
  115. nonPagination: true,
  116. list: this.getAssetsByDataset,
  117. transform: this.transformDatasetAsset,
  118. buttons: [
  119. { type: 'add', on: this.onAddAsset }
  120. ],
  121. cols: [
  122. { prop: 'tagInfo', label: '类型', align: 'center', width: 80 },
  123. { prop: 'typeName', label: '文件', align: 'center', width: 80 },
  124. { prop: 'file', label: '', type: 'asset', on: this.onViewAsset },
  125. { prop: 'name', label: '' },
  126. { label: '上播时长(s)', render: (data, h) => data.type === AssetType.IMAGE
  127. ? h('edit-input', {
  128. staticClass: 'border',
  129. props: {
  130. value: `${data.adDuration}`,
  131. align: 'center'
  132. },
  133. on: {
  134. edit: val => this.onEditAssetDuration(data, val)
  135. }
  136. })
  137. : parseDuration(data.adDuration), width: 100, align: 'center' },
  138. { type: 'invoke', render: [
  139. { label: '查看', on: this.onViewAsset },
  140. { label: '移除', on: this.onDelAsset }
  141. ] }
  142. ]
  143. },
  144. deviceSchema: {
  145. list: getDevicesByDataset,
  146. buttons: [
  147. { type: 'add', label: '绑定设备', on: this.onBindDevice },
  148. { icon: 'el-icon-scissors', label: '解绑', on: this.onUnbindDevices }
  149. ],
  150. cols: [
  151. { type: 'selection' },
  152. { prop: 'name', label: '名称' },
  153. { prop: 'address', label: '地址', 'min-width': 120 },
  154. { type: 'invoke', render: [
  155. { label: '解绑', on: this.onUnbind }
  156. ] }
  157. ]
  158. },
  159. devicesSchema: {
  160. list: getDevices,
  161. filters: [
  162. { key: 'name', type: 'search', placeholder: '设备名称' }
  163. ],
  164. cols: [
  165. { prop: 'name', label: '设备名称' },
  166. { prop: 'address', label: '地址' }
  167. ]
  168. },
  169. dataset: {}
  170. }
  171. },
  172. methods: {
  173. onAdd () {
  174. this.dataset = { name: '' }
  175. this.$refs.editDialog.show()
  176. },
  177. onSave (done) {
  178. const { name } = this.dataset
  179. if (!name) {
  180. this.$message({
  181. type: 'warning',
  182. message: '请填写素材包名称'
  183. })
  184. return
  185. }
  186. addDataset({ name }).then(({ data }) => {
  187. done()
  188. this.onEdit({ id: data })
  189. this.$refs.table.pageTo(1)
  190. })
  191. },
  192. onEditName (dataset, { newVal, oldVal }) {
  193. if (newVal === oldVal) {
  194. return
  195. }
  196. if (!newVal) {
  197. this.$message({
  198. type: 'warning',
  199. message: '请填写素材包名称'
  200. })
  201. return
  202. }
  203. dataset.name = newVal
  204. updateDataset({
  205. id: dataset.id,
  206. name: newVal
  207. }).catch(() => {
  208. dataset.name = oldVal
  209. })
  210. },
  211. onEdit ({ id }) {
  212. this.$datasetId = id
  213. this.$refs.assetDialog.show()
  214. },
  215. getAssetsByDataset () {
  216. return getDataset(this.$datasetId).then(({ data: { mediaList } }) => {
  217. return {
  218. data: mediaList
  219. }
  220. })
  221. },
  222. transformDatasetAsset (asset) {
  223. asset.tagInfo = AssetTagInfo[asset.tag]
  224. asset.typeName = AssetTypeInfo[asset.type]
  225. asset.file = {
  226. type: asset.type,
  227. url: asset.keyName,
  228. thumb: getAssetThumb(asset.minioData)
  229. }
  230. asset.name = asset.minioData.originalName
  231. return asset
  232. },
  233. onAddAsset () {
  234. this.$refs.assetTableDialog.show()
  235. },
  236. onChoosenAsset ({ value, done }) {
  237. bindAssetsToDataset(this.$datasetId, value.map(asset => {
  238. const { tag, type, keyName } = asset
  239. return {
  240. tag,
  241. type,
  242. keyName,
  243. adDuration: getAssetDuration(asset)
  244. }
  245. })).then(() => {
  246. done()
  247. this.$refs.assetDialog.getTable().pageTo()
  248. })
  249. },
  250. onEditAssetDuration (asset, { newVal, oldVal }) {
  251. if (!newVal || !/^\d+$/.test(newVal) || Number(newVal) < 1) {
  252. return
  253. }
  254. if (newVal !== oldVal) {
  255. asset.adDuration = Number(newVal)
  256. updateDatasetAssetDuration(asset.relationId, asset.adDuration).catch(() => {
  257. asset.adDuration = oldVal
  258. })
  259. }
  260. },
  261. onDelAsset ({ keyName }) {
  262. unbindAssetsFromDataset(this.$datasetId, [keyName]).then(() => {
  263. this.$refs.assetDialog.getTable().pageTo()
  264. })
  265. },
  266. onViewDevices ({ id }) {
  267. this.$datasetId = id
  268. this.$selectionItems = null
  269. this.$refs.deviceDialog.show({ datasetId: id })
  270. },
  271. onDel (dataset) {
  272. deleteDataset(dataset).then(() => {
  273. this.$refs.table.decrease(1)
  274. })
  275. },
  276. onUnbind (device) {
  277. unbindDatasetByDevice(device).then(() => {
  278. this.$refs.deviceDialog.getTable().decrease(1)
  279. })
  280. },
  281. onUnbindDevices () {
  282. if (this.$selectionItems?.length) {
  283. unbindDatasetByDevices(this.$selectionItems.map(({ id }) => id)).then(() => {
  284. this.$refs.deviceDialog.getTable().decrease(this.$selectionItems.length)
  285. this.$selectionItems = null
  286. })
  287. } else {
  288. this.$message({
  289. type: 'warning',
  290. message: '请先选择需要解绑的设备'
  291. })
  292. }
  293. },
  294. onToggleSelection (row) {
  295. this.$refs.table.getInst().toggleRowSelection(row)
  296. },
  297. onSelectionChange (val) {
  298. this.$selectionItems = val
  299. },
  300. onBindDevice () {
  301. this.$refs.devicesDialog.show()
  302. },
  303. onChoosenDevice ({ value, done }) {
  304. bindDatasetToDevice({
  305. deviceId: value.id,
  306. datasetId: this.$datasetId
  307. }).then(() => {
  308. done()
  309. this.$refs.deviceDialog.getTable().pageTo(1)
  310. })
  311. }
  312. }
  313. }
  314. </script>