upload.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <template>
  2. <div v-loading.fullscreen.lock="fullscreenLoading">
  3. <div class="upload">
  4. <el-row>
  5. <el-col :span="12">
  6. <el-upload
  7. :action="`${path}/fileUploadAndDownload/upload`"
  8. :before-upload="checkFile"
  9. :headers="{ 'x-token': token }"
  10. :on-error="uploadError"
  11. :on-success="uploadSuccess"
  12. :show-file-list="false"
  13. >
  14. <el-button size="small" type="primary">点击上传</el-button>
  15. <template #tip>
  16. <div class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
  17. </template>
  18. </el-upload>
  19. </el-col>
  20. <el-col :span="12">
  21. 带压缩的上传, (512(k)为压缩限制)
  22. <upload-image v-model="imageUrl" :file-size="512" :max-w-h="1080" @on-success="getTableData" />
  23. 已上传文件 {{ imageUrl }}
  24. </el-col>
  25. </el-row>
  26. <el-table :data="tableData" border stripe>
  27. <el-table-column label="预览" width="100">
  28. <template #default="scope">
  29. <CustomPic pic-type="file" :pic-src="scope.row.url" />
  30. </template>
  31. </el-table-column>
  32. <el-table-column label="日期" prop="UpdatedAt" width="180">
  33. <template #default="scope">
  34. <div>{{ formatDate(scope.row.UpdatedAt) }}</div>
  35. </template>
  36. </el-table-column>
  37. <el-table-column label="文件名" prop="name" width="180" />
  38. <el-table-column label="链接" prop="url" min-width="300" />
  39. <el-table-column label="标签" prop="tag" width="100">
  40. <template #default="scope">
  41. <el-tag
  42. :type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
  43. disable-transitions
  44. >{{ scope.row.tag }}</el-tag>
  45. </template>
  46. </el-table-column>
  47. <el-table-column label="操作" width="160">
  48. <template #default="scope">
  49. <el-button size="small" type="text" @click="downloadFile(scope.row)">下载</el-button>
  50. <el-button size="small" type="text" @click="deleteFile(scope.row)">删除</el-button>
  51. </template>
  52. </el-table-column>
  53. </el-table>
  54. <el-pagination
  55. :current-page="page"
  56. :page-size="pageSize"
  57. :page-sizes="[10, 30, 50, 100]"
  58. :style="{ float: 'right', padding: '20px' }"
  59. :total="total"
  60. layout="total, sizes, prev, pager, next, jumper"
  61. @current-change="handleCurrentChange"
  62. @size-change="handleSizeChange"
  63. />
  64. </div>
  65. </div>
  66. </template>
  67. <script>
  68. const path = import.meta.env.VITE_BASE_API
  69. import { mapGetters } from 'vuex'
  70. import infoList from '@/mixins/infoList'
  71. import { getFileList, deleteFile } from '@/api/fileUploadAndDownload'
  72. import { downloadImage } from '@/utils/downloadImg'
  73. import CustomPic from '@/components/customPic/index.vue'
  74. import UploadImage from '@/components/upload/image.vue'
  75. export default {
  76. name: 'Upload',
  77. components: {
  78. CustomPic,
  79. UploadImage
  80. },
  81. mixins: [infoList],
  82. data() {
  83. return {
  84. fullscreenLoading: false,
  85. listApi: getFileList,
  86. path: path,
  87. tableData: [],
  88. imageUrl: ''
  89. }
  90. },
  91. computed: {
  92. ...mapGetters('user', ['userInfo', 'token'])
  93. },
  94. created() {
  95. this.getTableData()
  96. },
  97. methods: {
  98. async deleteFile(row) {
  99. this.$confirm('此操作将永久文件, 是否继续?', '提示', {
  100. confirmButtonText: '确定',
  101. cancelButtonText: '取消',
  102. type: 'warning'
  103. })
  104. .then(async() => {
  105. const res = await deleteFile(row)
  106. if (res.code === 0) {
  107. this.$message({
  108. type: 'success',
  109. message: '删除成功!'
  110. })
  111. if (this.tableData.length === 1 && this.page > 1) {
  112. this.page--
  113. }
  114. this.getTableData()
  115. }
  116. })
  117. .catch(() => {
  118. this.$message({
  119. type: 'info',
  120. message: '已取消删除'
  121. })
  122. })
  123. },
  124. checkFile(file) {
  125. this.fullscreenLoading = true
  126. const isJPG = file.type === 'image/jpeg'
  127. const isPng = file.type === 'image/png'
  128. const isLt2M = file.size / 1024 / 1024 < 2
  129. if (!isJPG && !isPng) {
  130. this.$message.error('上传头像图片只能是 JPG或png 格式!')
  131. this.fullscreenLoading = false
  132. }
  133. if (!isLt2M) {
  134. this.$message.error('上传头像图片大小不能超过 2MB!')
  135. this.fullscreenLoading = false
  136. }
  137. return (isPng || isJPG) && isLt2M
  138. },
  139. uploadSuccess(res) {
  140. this.fullscreenLoading = false
  141. if (res.code === 0) {
  142. this.$message({
  143. type: 'success',
  144. message: '上传成功'
  145. })
  146. if (res.code === 0) {
  147. this.getTableData()
  148. }
  149. } else {
  150. this.$message({
  151. type: 'warning',
  152. message: res.msg
  153. })
  154. }
  155. },
  156. uploadError() {
  157. this.$message({
  158. type: 'error',
  159. message: '上传失败'
  160. })
  161. this.fullscreenLoading = false
  162. },
  163. downloadFile(row) {
  164. downloadImage(row.url, row.name)
  165. }
  166. }
  167. }
  168. </script>