index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <div>
  3. <div>
  4. <el-upload
  5. :action="action"
  6. v-model:file-list="fileList"
  7. :headers="headers"
  8. :data="data"
  9. :limit="max"
  10. :accept="accept"
  11. :multiple="max > 1"
  12. :on-success="success"
  13. :on-progress="progress"
  14. :show-file-list="false"
  15. >
  16. <el-button type="primary" icon="Plus">{{ btnText }}</el-button>
  17. </el-upload>
  18. </div>
  19. <div class="upload" v-if="drawer">
  20. <div>
  21. <div class="full-width flex flex-justify-end">
  22. <el-icon @click="drawer = false">
  23. <CircleClose />
  24. </el-icon>
  25. </div>
  26. <div class="mt-20 file-content">
  27. <div v-for="file in fileList" :key="file.name" class="mb-20">
  28. <div class="flex flex-align-center">
  29. <div>{{ file.name }} , ({{ bytesToSize(file.size) }})</div>
  30. <div></div>
  31. </div>
  32. <el-progress :percentage="file.percentage" />
  33. </div>
  34. </div>
  35. </div>
  36. </div>
  37. </div>
  38. </template>
  39. <script>
  40. import { bytesToSize } from '@/utils/tools.js'
  41. import api from '@/api/index.js'
  42. import { getToken } from '@/utils/auth.js'
  43. import website from '@/config/website.js'
  44. import { Base64 } from 'js-base64'
  45. export default {
  46. props: {
  47. btnText: {
  48. type: String,
  49. default: '文件上传'
  50. },
  51. accept: {
  52. type: String,
  53. default: ''
  54. },
  55. max: {
  56. type: Number,
  57. default: 9
  58. },
  59. data: Object,
  60. action: {
  61. type: String,
  62. default: api.uploadPath
  63. },
  64. projectId: {
  65. type: String,
  66. required: true,
  67. default: ''
  68. },
  69. stageId: {
  70. type: String,
  71. required: true,
  72. default: ''
  73. },
  74. parentId: {
  75. type: String,
  76. required: true,
  77. default: ''
  78. }
  79. },
  80. data() {
  81. return {
  82. headers: {
  83. Authorization: `Basic ${Base64.encode(
  84. `${website.clientId}:${website.clientSecret}`
  85. )}`,
  86. 'Blade-Auth': 'bearer ' + getToken()
  87. },
  88. drawer: false,
  89. fileList: [],
  90. resultList: []
  91. }
  92. },
  93. methods: {
  94. bytesToSize,
  95. progress(res) {
  96. console.log(res)
  97. this.drawer = true
  98. },
  99. success(res) {
  100. if (res.code === 200) {
  101. this.resultList.push(res.data)
  102. }
  103. if (this.resultList.length === this.fileList.length) {
  104. this.saveFile()
  105. this.saveLibrary()
  106. }
  107. },
  108. /**
  109. * 保存到library
  110. */
  111. async saveLibrary() {
  112. this.resultList
  113. .filter(ele => api.offices.includes(ele.suffix))
  114. .forEach(ele => {
  115. const item = {
  116. category: 4,
  117. fileId: ele.id,
  118. parentId: this.parentId,
  119. projectId: this.projectId,
  120. stageId: this.stageId,
  121. type: 1,
  122. title: ele.originalFileName,
  123. content: ''
  124. }
  125. this.$api.common.submit(item).then(res => {
  126. if (res.code === 200) {
  127. console.log(res)
  128. }
  129. })
  130. })
  131. },
  132. /**
  133. * 保存文件
  134. */
  135. saveFile() {
  136. const data = this.resultList.map(e => {
  137. return {
  138. fileId: e.id,
  139. parentId: this.parentId,
  140. projectId: this.projectId,
  141. stageId: this.stageId
  142. }
  143. })
  144. // fixme 重复文件未处理
  145. this.$api.resource.fileSave(data).then(res => {
  146. if (res.code === 200) {
  147. this.$message.success('文件上传完成')
  148. this.$emit('on-success', this.resultList)
  149. setTimeout(() => {
  150. this.drawer = false
  151. }, 3000)
  152. }
  153. })
  154. }
  155. }
  156. }
  157. </script>
  158. <style lang="scss" scoped>
  159. .upload {
  160. position: fixed;
  161. z-index: 99;
  162. bottom: 0;
  163. right: 0;
  164. background-color: white;
  165. height: 300px;
  166. width: 480px;
  167. border-top-left-radius: 10px;
  168. border: #eeeeee solid 1px;
  169. box-shadow: -2px 2px 20px rgba(0, 0, 0, 0.1);
  170. padding: 20px;
  171. }
  172. .file-content {
  173. height: 280px;
  174. overflow-y: scroll;
  175. }
  176. </style>