UploadFile.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <div>
  3. <el-upload
  4. v-if="!isCheck"
  5. ref="fileUpload"
  6. :data="data"
  7. :auto-upload="autoUpload"
  8. :class="{ 'el-upload-hidden': !chooseState }"
  9. :action="action"
  10. :headers="headers"
  11. :on-success="uploadSuccess"
  12. :before-upload="beforeUpload"
  13. :on-error="uploadError"
  14. :limit="limit"
  15. :multiple="multiple"
  16. :accept="accept"
  17. :file-list="fileList"
  18. >
  19. <div v-if="tip" slot="tip" class="el-upload__tip">{{ tip }}</div>
  20. <el-button v-if="!isCheck" size="mini" type="primary">选择文件</el-button>
  21. </el-upload>
  22. <div v-for="(file, index) in dataList" :key="index" class="file-list">
  23. <span>{{ file.name }} </span>
  24. <div>
  25. <el-button type="text" size="small" @click="handlePreview(file.response)">预览</el-button>
  26. <el-button type="text" size="small" style="color: red" @click="handleRemove(index)">删除</el-button>
  27. </div>
  28. </div>
  29. </div>
  30. </template>
  31. <script>
  32. import { mapGetters } from 'vuex'
  33. export default {
  34. name: 'UploadFile',
  35. props: {
  36. tip: {
  37. type: String,
  38. default: ''
  39. },
  40. multiple: {
  41. type: Boolean,
  42. default: false
  43. },
  44. limit: {
  45. type: Number,
  46. default: 1
  47. },
  48. accept: {
  49. type: String,
  50. default: '.docx,.pptx,.pdf'
  51. },
  52. listType: {
  53. type: String,
  54. default: 'picture-card'
  55. },
  56. fileList: {
  57. type: Array,
  58. default: () => []
  59. },
  60. uuid: {
  61. type: Number,
  62. default: 0
  63. },
  64. beforeUpload: {
  65. type: Function,
  66. default: () => true
  67. },
  68. autoUpload: {
  69. type: Boolean,
  70. default: true
  71. },
  72. data: {
  73. type: Object,
  74. default: () => ({})
  75. },
  76. mode: {
  77. type: String,
  78. default: 'brand'
  79. },
  80. list: {
  81. type: Array,
  82. default: () => []
  83. },
  84. isCheck: {
  85. type: Boolean,
  86. default: () => true
  87. }
  88. },
  89. data() {
  90. return {
  91. dialogVisible: false,
  92. dialogImageUrl: '',
  93. dataList: [],
  94. iframUrl: ''
  95. }
  96. },
  97. computed: {
  98. ...mapGetters(['token']),
  99. chooseState() {
  100. return this.dataList.length < this.limit
  101. },
  102. action() {
  103. return process.env.VUE_APP_BASE_API + '/formData/MultiPictareaddData'
  104. },
  105. headers() {
  106. return {
  107. 'X-Token': this.token
  108. }
  109. }
  110. },
  111. watch: {
  112. list: {
  113. handler(val) {
  114. console.log(val)
  115. this.dataList = val
  116. },
  117. deep: true
  118. }
  119. },
  120. methods: {
  121. // 上传成功
  122. uploadSuccess(response, file, fileList) {
  123. if (file.response.data) {
  124. this.dataList.push(file)
  125. console.log(response, file)
  126. this.$emit('success', { fileList: this.dataList })
  127. } else {
  128. this.$message({
  129. type: 'error',
  130. message: '上传失败'
  131. })
  132. }
  133. },
  134. // 删除
  135. handleRemove(index) {
  136. this.dataList = this.dataList.filter((_, i) => index !== i)
  137. this.$emit('remove', { fileList: this.dataList })
  138. },
  139. // 变化
  140. handleChange(file, fileList) {
  141. this.dataList = fileList
  142. this.$emit('change', { fileList: this.dataList })
  143. },
  144. // 上传失败
  145. uploadError(err, file, fileList) {
  146. this.dataList = fileList
  147. this.$emit('error', { err, file, fileList })
  148. },
  149. handlePreview(f) {
  150. if (f.data.indexOf('.docx') !== -1 || f.data.indexOf('.pptx') !== -1) {
  151. window.open(`https://view.officeapps.live.com/op/view.aspx?src=${encodeURIComponent(f.data)}`)
  152. } else {
  153. window.open(f.data)
  154. }
  155. }
  156. }
  157. }
  158. </script>
  159. <style lang="scss" scoped>
  160. ::v-deep .el-upload-list {
  161. display: none;
  162. }
  163. ::v-deep .el-upload__tip {
  164. position: static;
  165. }
  166. .file-list {
  167. display: flex;
  168. justify-content: space-between;
  169. padding-right: 30px;
  170. margin-top: 10px;
  171. }
  172. </style>