wk-file-content.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <template>
  2. <view class="wk-file-content">
  3. <view v-if="list.length > 0" class="file-group">
  4. <view
  5. v-for="(item, index) in list"
  6. :key="index"
  7. class="file-item"
  8. @click.stop="handleDownloadFile(item)">
  9. <image
  10. :src="calcIcon(item.name)"
  11. alt=""
  12. class="file-icon" />
  13. <text class="file-name">
  14. {{ item.name }}
  15. </text>
  16. <text class="file-size">
  17. {{ getSize(item.size) }}
  18. </text>
  19. <!-- <text
  20. v-if="showDelete"
  21. class="wk wk-close file-delete"
  22. @click.stop="deleteFile(index)" /> -->
  23. <text
  24. v-if="showDelete && item.readOnly !== 1"
  25. class="wk wk-close file-delete"
  26. @click.stop="deleteFile(index)" />
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. /**
  33. * 附件模块
  34. * @author yxk
  35. * @property {Array} list 附件列表
  36. * @property {Boolean} showDelete 是否可删除,默认false不可删除
  37. * @event {Function} delete 点击确认删除时触发
  38. */
  39. import {calcIcon, downloadFile} from 'UTIL/file'
  40. export default {
  41. name: 'WkFileContent',
  42. props: {
  43. list: {
  44. type: Array,
  45. default: () => []
  46. },
  47. showDelete: {
  48. type: Boolean,
  49. default: false
  50. }
  51. },
  52. data() {
  53. return {}
  54. },
  55. computed: {
  56. calcIcon() {
  57. return calcIcon
  58. }
  59. },
  60. methods: {
  61. /**
  62. * 计算文件大小
  63. */
  64. getSize(size) {
  65. if (!size || size == '') {
  66. return '0KB';
  67. }
  68. if (isNaN(Number(size))) return size
  69. const unitArr = ['B', 'KB', 'MB', 'GB', 'TB']
  70. let index = 0;
  71. let calcRes = '0KB'
  72. function calcSize(num) {
  73. let res = num / 1024
  74. if (res < 1) {
  75. calcRes = Number(num.toFixed(2)) + unitArr[index]
  76. } else {
  77. index++;
  78. if (index > 4) {
  79. calcRes = Number(num.toFixed(2)) + unitArr[4]
  80. } else {
  81. calcSize(res)
  82. }
  83. }
  84. }
  85. calcSize(Number(size))
  86. return calcRes
  87. },
  88. /**
  89. * 删除回调
  90. */
  91. deleteFile(index) {
  92. this.$emit('delete', index, this.list[index])
  93. },
  94. /**
  95. * 下载附件
  96. */
  97. handleDownloadFile(fileItem) {
  98. downloadFile(fileItem)
  99. }
  100. }
  101. }
  102. </script>
  103. <style scoped lang="scss">
  104. .wk-file-content {
  105. margin: 10rpx 0;
  106. .file-group {
  107. .file-item {
  108. height: 80rpx;
  109. padding: 0 20rpx;
  110. background-color: $light-bg;
  111. margin-bottom: 20rpx;
  112. @include left;
  113. &:last-child {
  114. margin-bottom: 0;
  115. }
  116. .file-icon {
  117. width: 28rpx;
  118. height: 34rpx;
  119. margin-right: 20rpx;
  120. }
  121. .file-name {
  122. flex: 1;
  123. font-size: 26rpx;
  124. line-height: 1.5;
  125. color: $dark;
  126. @include ellipsis;
  127. }
  128. .file-size {
  129. font-size: 26rpx;
  130. line-height: 1.5;
  131. color: $light;
  132. margin-left: 20rpx;
  133. }
  134. .file-delete {
  135. font-size: $wk-font-base;
  136. font-weight: bold;
  137. color: $light;
  138. margin-left: 20rpx;
  139. }
  140. }
  141. }
  142. }
  143. </style>