upload.vue 5.1 KB
Newer Older
Mr.奇淼('s avatar
Mr.奇淼( 已提交
1
<template>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
2
  <div v-loading.fullscreen.lock="fullscreenLoading">
J
jinlan.du 已提交
3
    <div class="upload">
P
piexlmax 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
      <div class="upload-box">
        <el-upload
          :action="`${path}/fileUploadAndDownload/upload`"
          :before-upload="checkFile"
          :headers="{ 'x-token': token }"
          :on-error="uploadError"
          :on-success="uploadSuccess"
          :show-file-list="false"
          class="upload-btn"
        >
          <el-button size="mini" type="primary">普通上传</el-button>
        </el-upload>
        <upload-image
          v-model="imageUrl"
          :file-size="512"
          :max-w-h="1080"
          class="upload-btn"
          @on-success="getTableData"
        />
      </div>
fv2010's avatar
fv2010 已提交
24

25 26
      <el-table :data="tableData" border stripe>
        <el-table-column label="预览" width="100">
27
          <template #default="scope">
何秀钢 已提交
28
            <CustomPic pic-type="file" :pic-src="scope.row.url" />
29 30 31
          </template>
        </el-table-column>
        <el-table-column label="日期" prop="UpdatedAt" width="180">
32 33
          <template #default="scope">
            <div>{{ formatDate(scope.row.UpdatedAt) }}</div>
34 35
          </template>
        </el-table-column>
何秀钢 已提交
36 37
        <el-table-column label="文件名" prop="name" width="180" />
        <el-table-column label="链接" prop="url" min-width="300" />
38
        <el-table-column label="标签" prop="tag" width="100">
39
          <template #default="scope">
40 41 42
            <el-tag
              :type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
              disable-transitions
43
            >{{ scope.row.tag }}</el-tag>
44 45
          </template>
        </el-table-column>
J
jinlan.du 已提交
46
        <el-table-column label="操作" width="160">
47
          <template #default="scope">
何秀钢 已提交
48 49
            <el-button size="small" type="text" @click="downloadFile(scope.row)">下载</el-button>
            <el-button size="small" type="text" @click="deleteFile(scope.row)">删除</el-button>
50 51 52 53 54 55 56 57 58
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-size="pageSize"
        :page-sizes="[10, 30, 50, 100]"
        :style="{ float: 'right', padding: '20px' }"
        :total="total"
何秀钢 已提交
59
        layout="total, sizes, prev, pager, next, jumper"
60 61
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
何秀钢 已提交
62
      />
J
jinlan.du 已提交
63
    </div>
64
  </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
65
</template>
66

Mr.奇淼('s avatar
Mr.奇淼( 已提交
67
<script>
68
const path = import.meta.env.VITE_BASE_API
何秀钢 已提交
69 70 71 72
import { mapGetters } from 'vuex'
import infoList from '@/mixins/infoList'
import { getFileList, deleteFile } from '@/api/fileUploadAndDownload'
import { downloadImage } from '@/utils/downloadImg'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
73
import CustomPic from '@/components/customPic/index.vue'
何秀钢 已提交
74
import UploadImage from '@/components/upload/image.vue'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
75
export default {
何秀钢 已提交
76
  name: 'Upload',
77
  components: {
fv2010's avatar
fv2010 已提交
78
    CustomPic,
79
    UploadImage
fv2010's avatar
fv2010 已提交
80
  },
何秀钢 已提交
81
  mixins: [infoList],
82 83
  data() {
    return {
84
      fullscreenLoading: false,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
85
      listApi: getFileList,
86
      path: path,
87
      tableData: [],
何秀钢 已提交
88 89
      imageUrl: ''
    }
90 91
  },
  computed: {
何秀钢 已提交
92
    ...mapGetters('user', ['userInfo', 'token'])
93
  },
何秀钢 已提交
94 95
  created() {
    this.getTableData()
96
  },
97
  methods: {
98
    async deleteFile(row) {
何秀钢 已提交
99 100 101 102
      this.$confirm('此操作将永久文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
103
      })
何秀钢 已提交
104 105 106
        .then(async() => {
          const res = await deleteFile(row)
          if (res.code === 0) {
107
            this.$message({
何秀钢 已提交
108 109 110 111 112
              type: 'success',
              message: '删除成功!'
            })
            if (this.tableData.length === 1 && this.page > 1) {
              this.page--
113
            }
何秀钢 已提交
114
            this.getTableData()
115 116 117 118
          }
        })
        .catch(() => {
          this.$message({
何秀钢 已提交
119 120 121 122
            type: 'info',
            message: '已取消删除'
          })
        })
123
    },
124
    checkFile(file) {
何秀钢 已提交
125 126 127
      this.fullscreenLoading = true
      const isJPG = file.type === 'image/jpeg'
      const isPng = file.type === 'image/png'
P
piexlmax 已提交
128
      const isLt2M = file.size / 1024 / 1024 < 0.5
129
      if (!isJPG && !isPng) {
P
piexlmax 已提交
130
        this.$message.error('上传图片只能是 jpg或png 格式!')
何秀钢 已提交
131
        this.fullscreenLoading = false
132 133
      }
      if (!isLt2M) {
P
piexlmax 已提交
134
        this.$message.error('未压缩未见上传图片大小不能超过 500KB,请使用压缩上传')
何秀钢 已提交
135
        this.fullscreenLoading = false
136
      }
何秀钢 已提交
137
      return (isPng || isJPG) && isLt2M
138
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
139
    uploadSuccess(res) {
何秀钢 已提交
140 141
      this.fullscreenLoading = false
      if (res.code === 0) {
142
        this.$message({
何秀钢 已提交
143 144 145 146 147
          type: 'success',
          message: '上传成功'
        })
        if (res.code === 0) {
          this.getTableData()
148 149 150
        }
      } else {
        this.$message({
何秀钢 已提交
151
          type: 'warning',
152
          message: res.msg
何秀钢 已提交
153
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
154
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
155
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
156
    uploadError() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
157
      this.$message({
何秀钢 已提交
158 159 160 161
        type: 'error',
        message: '上传失败'
      })
      this.fullscreenLoading = false
Mr.奇淼('s avatar
Mr.奇淼( 已提交
162 163
    },
    downloadFile(row) {
164 165 166 167 168
      if (row.url.indexOf('http://') > -1 || row.url.indexOf('https://') > -1) {
        downloadImage(row.url, row.name)
      } else {
        downloadImage(this.path + row.url, row.name)
      }
169 170
    }
  }
何秀钢 已提交
171
}
172
</script>