mixin.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import {
  2. getAssets,
  3. updateAsset,
  4. deleteAsset,
  5. submitAsset
  6. } from '@/api/asset'
  7. import {
  8. State,
  9. AssetType
  10. } from '@/constant'
  11. import {
  12. createListOptions,
  13. parseByte,
  14. parseDuration
  15. } from '@/utils'
  16. export default {
  17. props: {
  18. status: {
  19. type: Number,
  20. default: State.READY
  21. }
  22. },
  23. data () {
  24. return {
  25. type: AssetType.IMAGE,
  26. active: `${this.status}`,
  27. [AssetType.IMAGE]: {
  28. [State.READY]: createListOptions({ type: AssetType.IMAGE, status: State.READY }),
  29. [State.SUBMITTED]: createListOptions({ type: AssetType.IMAGE, status: State.SUBMITTED }),
  30. [State.RESOLVED]: createListOptions({ type: AssetType.IMAGE, status: State.RESOLVED, originalName: '' }),
  31. [State.REJECTED]: createListOptions({ type: AssetType.IMAGE, status: State.REJECTED })
  32. },
  33. [AssetType.VIDEO]: {
  34. [State.READY]: createListOptions({ type: AssetType.VIDEO, status: State.READY }),
  35. [State.SUBMITTED]: createListOptions({ type: AssetType.VIDEO, status: State.SUBMITTED }),
  36. [State.RESOLVED]: createListOptions({ type: AssetType.VIDEO, status: State.RESOLVED, originalName: '' }),
  37. [State.REJECTED]: createListOptions({ type: AssetType.VIDEO, status: State.REJECTED })
  38. },
  39. [AssetType.AUDIO]: {
  40. [State.READY]: createListOptions({ type: AssetType.AUDIO, status: State.READY }),
  41. [State.SUBMITTED]: createListOptions({ type: AssetType.AUDIO, status: State.SUBMITTED }),
  42. [State.RESOLVED]: createListOptions({ type: AssetType.AUDIO, status: State.RESOLVED, originalName: '' }),
  43. [State.REJECTED]: createListOptions({ type: AssetType.AUDIO, status: State.REJECTED })
  44. }
  45. }
  46. },
  47. computed: {
  48. schema () {
  49. const canEdit = this.active === `${State.READY}`
  50. return {
  51. list: getAssets,
  52. transform: this.transform,
  53. filters: this.active === `${State.RESOLVED}` ? [
  54. { key: 'originalName', type: 'search', placeholder: '媒资名称' }
  55. ] : [],
  56. cols: [
  57. { prop: 'file', type: 'asset', on: this.onViewAsset },
  58. { prop: 'name', 'min-width': 100, render: canEdit ? (data, h) => {
  59. return h('edit-input', {
  60. model: {
  61. value: data.name,
  62. callback (val) {
  63. data.name = typeof val === 'string' ? val.trim() : val
  64. }
  65. },
  66. on: {
  67. edit: () => this.onEdit(data)
  68. }
  69. })
  70. } : null },
  71. this.isImage ? null : { prop: 'duration', label: '时长' },
  72. { prop: 'size', label: '文件大小' },
  73. { prop: 'createTime', label: '上传时间' },
  74. { type: 'invoke', align: 'center', width: 140, render: [
  75. { label: '查看', on: this.onView },
  76. canEdit ? { label: '提交', on: this.onSubmit } : null,
  77. { label: '删除', render ({ del }) { return del }, on: this.onDel }
  78. ].filter(val => val) }
  79. ]
  80. }
  81. },
  82. currOptions: {
  83. get () {
  84. return this[this.type][this.active]
  85. },
  86. set (val) {
  87. this[this.type][this.active] = val
  88. }
  89. },
  90. isImage () {
  91. return this.type === AssetType.IMAGE
  92. },
  93. isVideo () {
  94. return this.type === AssetType.VIDEO
  95. },
  96. isAudio () {
  97. return this.type === AssetType.AUDIO
  98. }
  99. },
  100. methods: {
  101. adjustHeader ({ column }) {
  102. return column.property === 'name' && this.isAudio ? 'o-media-name' : ''
  103. },
  104. transform (asset) {
  105. asset.name = asset.originalName
  106. asset.file = {
  107. type: asset.type,
  108. url: asset.keyName,
  109. thumbnail: asset.thumbnail
  110. }
  111. asset.duration = parseDuration(asset.duration)
  112. asset.size = parseByte(asset.size)
  113. return asset
  114. },
  115. to (type) {
  116. if (this.type !== AssetType[type]) {
  117. this.type = null
  118. this.$nextTick(() => {
  119. this.type = AssetType[type]
  120. })
  121. }
  122. },
  123. onView ({ file }) {
  124. this.onViewAsset(file)
  125. },
  126. onViewAsset (asset) {
  127. this.$refs.previewDialog.show(asset)
  128. },
  129. onEdit (asset) {
  130. if (!asset.name) {
  131. asset.name = asset.originalName
  132. return
  133. }
  134. if (asset.name === asset.originalName) {
  135. return
  136. }
  137. updateAsset({
  138. keyName: asset.keyName,
  139. originalName: asset.name
  140. }).then(
  141. () => {
  142. asset.originalName = asset.name
  143. },
  144. () => {
  145. asset.name = asset.originalName
  146. }
  147. )
  148. },
  149. onSubmit (asset) {
  150. submitAsset(asset).then(() => {
  151. this.$refs.table.decrease(1)
  152. })
  153. },
  154. onDel (asset) {
  155. deleteAsset(asset).then(() => {
  156. this.$refs.table.decrease(1)
  157. })
  158. }
  159. }
  160. }