2019-10-20 11:42:56 +08:00
|
|
|
|
<template>
|
2019-12-27 16:28:41 +08:00
|
|
|
|
<div v-loading.fullscreen.lock="fullscreenLoading">
|
2020-02-03 10:58:29 +08:00
|
|
|
|
<div class="upload">
|
2020-10-09 13:22:28 +08:00
|
|
|
|
<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>
|
2020-12-27 17:20:54 +08:00
|
|
|
|
<div class="el-upload__tip" slot="tip">只能上传jpg/png文件,且不超过500kb</div>
|
2020-10-09 13:22:28 +08:00
|
|
|
|
</el-upload>
|
|
|
|
|
</el-col>
|
|
|
|
|
<el-col :span="12">
|
|
|
|
|
带压缩的上传, (512(k)为压缩限制)
|
|
|
|
|
<upload-image v-model="imageUrl" :fileSize="512" :maxWH="1080" />
|
|
|
|
|
已上传文件 {{ imageUrl }}
|
|
|
|
|
</el-col>
|
|
|
|
|
</el-row>
|
|
|
|
|
|
2020-04-25 10:14:17 +08:00
|
|
|
|
<el-table :data="tableData" border stripe>
|
|
|
|
|
<el-table-column label="预览" width="100">
|
|
|
|
|
<template slot-scope="scope">
|
2020-10-09 13:22:28 +08:00
|
|
|
|
<CustomPic picType="file" :picSrc="scope.row.url" />
|
2020-04-25 10:14:17 +08:00
|
|
|
|
</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>
|
2020-12-27 17:20:54 +08:00
|
|
|
|
<el-table-column label="文件名" prop="name" width="180"></el-table-column>
|
|
|
|
|
<el-table-column label="链接" prop="url" min-width="300"></el-table-column>
|
2020-04-25 10:14:17 +08:00
|
|
|
|
<el-table-column label="标签" prop="tag" width="100">
|
|
|
|
|
<template slot-scope="scope">
|
|
|
|
|
<el-tag
|
|
|
|
|
:type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
|
|
|
|
|
disable-transitions
|
2020-12-27 17:20:54 +08:00
|
|
|
|
>{{ scope.row.tag }}</el-tag>
|
2020-04-25 10:14:17 +08:00
|
|
|
|
</template>
|
|
|
|
|
</el-table-column>
|
2020-05-18 13:29:48 +08:00
|
|
|
|
<el-table-column label="操作" width="160">
|
2020-04-25 10:14:17 +08:00
|
|
|
|
<template slot-scope="scope">
|
2020-12-27 17:20:54 +08:00
|
|
|
|
<el-button @click="downloadFile(scope.row)" size="small" type="text">下载</el-button>
|
|
|
|
|
<el-button @click="deleteFile(scope.row)" size="small" type="text">删除</el-button>
|
2020-04-25 10:14:17 +08:00
|
|
|
|
</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>
|
2020-02-03 10:58:29 +08:00
|
|
|
|
</div>
|
2019-10-25 17:34:36 +08:00
|
|
|
|
</div>
|
2019-10-20 11:42:56 +08:00
|
|
|
|
</template>
|
2020-04-25 10:14:17 +08:00
|
|
|
|
|
2019-10-20 11:42:56 +08:00
|
|
|
|
<script>
|
2020-04-25 10:14:17 +08:00
|
|
|
|
const path = process.env.VUE_APP_BASE_API;
|
|
|
|
|
import { mapGetters } from "vuex";
|
2020-10-29 22:06:09 +08:00
|
|
|
|
import infoList from "@/mixins/infoList";
|
2020-04-25 10:14:17 +08:00
|
|
|
|
import { getFileList, deleteFile } from "@/api/fileUploadAndDownload";
|
|
|
|
|
import { downloadImage } from "@/utils/downloadImg";
|
2020-10-26 18:33:45 +08:00
|
|
|
|
import { formatTimeToStr } from "@/utils/date";
|
2020-10-09 13:22:28 +08:00
|
|
|
|
import CustomPic from "@/components/customPic";
|
|
|
|
|
import UploadImage from "@/components/upload/image.vue";
|
2019-10-20 11:42:56 +08:00
|
|
|
|
export default {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
name: "Upload",
|
2019-10-25 17:34:36 +08:00
|
|
|
|
mixins: [infoList],
|
2020-08-26 17:16:49 +08:00
|
|
|
|
components: {
|
2020-10-09 13:22:28 +08:00
|
|
|
|
CustomPic,
|
2020-12-27 17:20:54 +08:00
|
|
|
|
UploadImage
|
2020-10-09 13:22:28 +08:00
|
|
|
|
},
|
2019-10-25 17:34:36 +08:00
|
|
|
|
data() {
|
|
|
|
|
return {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
fullscreenLoading: false,
|
2019-10-26 23:25:43 +08:00
|
|
|
|
listApi: getFileList,
|
2019-12-15 15:10:40 +08:00
|
|
|
|
path: path,
|
2020-04-25 10:14:17 +08:00
|
|
|
|
tableData: [],
|
2020-12-27 17:20:54 +08:00
|
|
|
|
imageUrl: ""
|
2020-04-25 10:14:17 +08:00
|
|
|
|
};
|
2019-10-25 17:34:36 +08:00
|
|
|
|
},
|
|
|
|
|
computed: {
|
2020-12-27 17:20:54 +08:00
|
|
|
|
...mapGetters("user", ["userInfo", "token"])
|
2019-10-25 17:34:36 +08:00
|
|
|
|
},
|
2019-11-24 11:13:55 +08:00
|
|
|
|
filters: {
|
2020-12-27 17:20:54 +08:00
|
|
|
|
formatDate: function(time) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
if (time != null && time != "") {
|
|
|
|
|
var date = new Date(time);
|
|
|
|
|
return formatTimeToStr(date, "yyyy-MM-dd hh:mm:ss");
|
2019-11-24 11:13:55 +08:00
|
|
|
|
} else {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
return "";
|
2019-11-24 11:13:55 +08:00
|
|
|
|
}
|
2020-12-27 17:20:54 +08:00
|
|
|
|
}
|
2019-11-24 11:13:55 +08:00
|
|
|
|
},
|
2019-10-25 17:34:36 +08:00
|
|
|
|
methods: {
|
2019-11-24 11:13:55 +08:00
|
|
|
|
async deleteFile(row) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.$confirm("此操作将永久文件, 是否继续?", "提示", {
|
|
|
|
|
confirmButtonText: "确定",
|
|
|
|
|
cancelButtonText: "取消",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
type: "warning"
|
2019-11-24 11:13:55 +08:00
|
|
|
|
})
|
|
|
|
|
.then(async () => {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
const res = await deleteFile(row);
|
2020-04-05 16:19:34 +08:00
|
|
|
|
if (res.code == 0) {
|
2019-11-24 11:13:55 +08:00
|
|
|
|
this.$message({
|
2020-04-25 10:14:17 +08:00
|
|
|
|
type: "success",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
message: "删除成功!"
|
2020-04-25 10:14:17 +08:00
|
|
|
|
});
|
2021-05-17 15:38:31 +08:00
|
|
|
|
if (this.tableData.length == 1 && this.page > 1 ) {
|
2020-12-27 17:20:54 +08:00
|
|
|
|
this.page--;
|
|
|
|
|
}
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.getTableData();
|
2019-11-24 11:13:55 +08:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
this.$message({
|
2020-04-25 10:14:17 +08:00
|
|
|
|
type: "info",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
message: "已取消删除"
|
2020-04-25 10:14:17 +08:00
|
|
|
|
});
|
|
|
|
|
});
|
2019-11-24 11:13:55 +08:00
|
|
|
|
},
|
2019-10-25 17:34:36 +08:00
|
|
|
|
checkFile(file) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.fullscreenLoading = true;
|
|
|
|
|
const isJPG = file.type === "image/jpeg";
|
|
|
|
|
const isPng = file.type === "image/png";
|
|
|
|
|
const isLt2M = file.size / 1024 / 1024 < 2;
|
2019-11-24 11:13:55 +08:00
|
|
|
|
if (!isJPG && !isPng) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.$message.error("上传头像图片只能是 JPG或png 格式!");
|
|
|
|
|
this.fullscreenLoading = false;
|
2019-10-25 17:34:36 +08:00
|
|
|
|
}
|
|
|
|
|
if (!isLt2M) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.$message.error("上传头像图片大小不能超过 2MB!");
|
|
|
|
|
this.fullscreenLoading = false;
|
2019-10-25 17:34:36 +08:00
|
|
|
|
}
|
2020-04-25 10:14:17 +08:00
|
|
|
|
return (isPng || isJPG) && isLt2M;
|
2019-10-25 17:34:36 +08:00
|
|
|
|
},
|
2019-10-30 17:21:11 +08:00
|
|
|
|
uploadSuccess(res) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.fullscreenLoading = false;
|
2020-04-05 16:19:34 +08:00
|
|
|
|
if (res.code == 0) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
this.$message({
|
|
|
|
|
type: "success",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
message: "上传成功"
|
2020-04-25 10:14:17 +08:00
|
|
|
|
});
|
|
|
|
|
if (res.code == 0) {
|
|
|
|
|
this.getTableData();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.$message({
|
|
|
|
|
type: "warning",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
message: res.msg
|
2020-04-25 10:14:17 +08:00
|
|
|
|
});
|
2019-10-30 17:21:11 +08:00
|
|
|
|
}
|
2019-10-26 23:25:43 +08:00
|
|
|
|
},
|
2019-10-30 17:21:11 +08:00
|
|
|
|
uploadError() {
|
2019-10-26 23:25:43 +08:00
|
|
|
|
this.$message({
|
2020-04-25 10:14:17 +08:00
|
|
|
|
type: "error",
|
2020-12-27 17:20:54 +08:00
|
|
|
|
message: "上传失败"
|
2020-04-25 10:14:17 +08:00
|
|
|
|
});
|
|
|
|
|
this.fullscreenLoading = false;
|
2019-10-26 23:25:43 +08:00
|
|
|
|
},
|
|
|
|
|
downloadFile(row) {
|
2020-04-25 10:14:17 +08:00
|
|
|
|
downloadImage(row.url, row.name);
|
2020-12-27 17:20:54 +08:00
|
|
|
|
}
|
2020-04-25 10:14:17 +08:00
|
|
|
|
},
|
|
|
|
|
created() {
|
|
|
|
|
this.getTableData();
|
2020-12-27 17:20:54 +08:00
|
|
|
|
}
|
2020-04-25 10:14:17 +08:00
|
|
|
|
};
|
|
|
|
|
</script>
|