upload.vue 5.4 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">
fv2010's avatar
fv2010 已提交
4 5 6 7 8 9 10 11 12 13 14
      <el-row>
        <el-col :span="12">
          <el-upload
            :action="`${path}/fileUploadAndDownload/upload`"
            :before-upload="checkFile"
            :headers="{ 'x-token': token }"
            :on-error="uploadError"
            :on-success="uploadSuccess"
            :show-file-list="false"
          >
            <el-button size="small" type="primary">点击上传</el-button>
15
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
fv2010's avatar
fv2010 已提交
16 17 18 19 20 21 22 23 24
          </el-upload>
        </el-col>
        <el-col :span="12">
          带压缩的上传, (512(k)为压缩限制)
          <upload-image v-model="imageUrl" :fileSize="512" :maxWH="1080" />
          已上传文件 {{ imageUrl }}
        </el-col>
      </el-row>

25 26 27
      <el-table :data="tableData" border stripe>
        <el-table-column label="预览" width="100">
          <template slot-scope="scope">
fv2010's avatar
fv2010 已提交
28
            <CustomPic picType="file" :picSrc="scope.row.url" />
29 30 31 32 33 34 35
          </template>
        </el-table-column>
        <el-table-column label="日期" prop="UpdatedAt" width="180">
          <template slot-scope="scope">
            <div>{{ scope.row.UpdatedAt | formatDate }}</div>
          </template>
        </el-table-column>
36 37
        <el-table-column label="文件名" prop="name" width="180"></el-table-column>
        <el-table-column label="链接" prop="url" min-width="300"></el-table-column>
38 39 40 41 42
        <el-table-column label="标签" prop="tag" width="100">
          <template slot-scope="scope">
            <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 slot-scope="scope">
48 49
            <el-button @click="downloadFile(scope.row)" size="small" type="text">下载</el-button>
            <el-button @click="deleteFile(scope.row)" size="small" type="text">删除</el-button>
50 51 52 53 54 55 56 57 58 59 60 61 62
          </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"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
J
jinlan.du 已提交
63
    </div>
64
  </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
65
</template>
66

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