DatasetTable.vue 8.3 KB

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