pms-v1/web/src/view/example/upload/upload.vue

171 lines
5.1 KiB
Vue
Raw Normal View History

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">
<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>
2021-08-26 12:45:41 +08:00
<template #tip>
<div class="el-upload__tip">只能上传jpg/png文件且不超过500kb</div>
</template>
</el-upload>
</el-col>
<el-col :span="12">
带压缩的上传, (512(k)为压缩限制)
2021-07-10 12:08:28 +08:00
<upload-image v-model="imageUrl" :file-size="512" :max-w-h="1080" @on-success="getTableData" />
已上传文件 {{ imageUrl }}
</el-col>
</el-row>
<el-table :data="tableData" border stripe>
<el-table-column label="预览" width="100">
2021-08-26 12:45:41 +08:00
<template #default="scope">
2021-06-02 14:11:45 +08:00
<CustomPic pic-type="file" :pic-src="scope.row.url" />
</template>
</el-table-column>
<el-table-column label="日期" prop="UpdatedAt" width="180">
2021-08-26 12:45:41 +08:00
<template #default="scope">
<div>{{ formatDate(scope.row.UpdatedAt) }}</div>
</template>
</el-table-column>
2021-06-02 14:11:45 +08:00
<el-table-column label="文件名" prop="name" width="180" />
<el-table-column label="链接" prop="url" min-width="300" />
<el-table-column label="标签" prop="tag" width="100">
2021-08-26 12:45:41 +08:00
<template #default="scope">
<el-tag
:type="scope.row.tag === 'jpg' ? 'primary' : 'success'"
disable-transitions
>{{ scope.row.tag }}</el-tag>
</template>
</el-table-column>
2020-05-18 13:29:48 +08:00
<el-table-column label="操作" width="160">
2021-08-26 12:45:41 +08:00
<template #default="scope">
2021-06-02 14:11:45 +08:00
<el-button size="small" type="text" @click="downloadFile(scope.row)">下载</el-button>
<el-button size="small" type="text" @click="deleteFile(scope.row)">删除</el-button>
</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"
2021-06-02 14:11:45 +08:00
layout="total, sizes, prev, pager, next, jumper"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
2021-06-02 14:11:45 +08:00
/>
2020-02-03 10:58:29 +08:00
</div>
</div>
2019-10-20 11:42:56 +08:00
</template>
2019-10-20 11:42:56 +08:00
<script>
2021-09-04 16:04:07 +08:00
const path = import.meta.env.VITE_BASE_API
2021-06-02 14:11:45 +08:00
import { mapGetters } from 'vuex'
import infoList from '@/mixins/infoList'
import { getFileList, deleteFile } from '@/api/fileUploadAndDownload'
import { downloadImage } from '@/utils/downloadImg'
import CustomPic from '@/components/customPic'
import UploadImage from '@/components/upload/image.vue'
2019-10-20 11:42:56 +08:00
export default {
2021-06-02 14:11:45 +08:00
name: 'Upload',
components: {
CustomPic,
UploadImage
},
2021-06-02 14:11:45 +08:00
mixins: [infoList],
data() {
return {
fullscreenLoading: false,
2019-10-26 23:25:43 +08:00
listApi: getFileList,
path: path,
tableData: [],
2021-06-02 14:11:45 +08:00
imageUrl: ''
}
},
computed: {
2021-06-02 14:11:45 +08:00
...mapGetters('user', ['userInfo', 'token'])
},
2021-06-02 14:11:45 +08:00
created() {
this.getTableData()
},
methods: {
async deleteFile(row) {
2021-06-02 14:11:45 +08:00
this.$confirm('此操作将永久文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
2021-06-02 14:11:45 +08:00
.then(async() => {
const res = await deleteFile(row)
if (res.code === 0) {
this.$message({
2021-06-02 14:11:45 +08:00
type: 'success',
message: '删除成功!'
})
if (this.tableData.length === 1 && this.page > 1) {
this.page--
}
2021-06-02 14:11:45 +08:00
this.getTableData()
}
})
.catch(() => {
this.$message({
2021-06-02 14:11:45 +08:00
type: 'info',
message: '已取消删除'
})
})
},
checkFile(file) {
2021-06-02 14:11:45 +08:00
this.fullscreenLoading = true
const isJPG = file.type === 'image/jpeg'
const isPng = file.type === 'image/png'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG && !isPng) {
2021-06-02 14:11:45 +08:00
this.$message.error('上传头像图片只能是 JPG或png 格式!')
this.fullscreenLoading = false
}
if (!isLt2M) {
2021-06-02 14:11:45 +08:00
this.$message.error('上传头像图片大小不能超过 2MB!')
this.fullscreenLoading = false
}
2021-06-02 14:11:45 +08:00
return (isPng || isJPG) && isLt2M
},
2019-10-30 17:21:11 +08:00
uploadSuccess(res) {
2021-06-02 14:11:45 +08:00
this.fullscreenLoading = false
if (res.code === 0) {
this.$message({
2021-06-02 14:11:45 +08:00
type: 'success',
message: '上传成功'
})
if (res.code === 0) {
this.getTableData()
}
} else {
this.$message({
2021-06-02 14:11:45 +08:00
type: 'warning',
message: res.msg
2021-06-02 14:11:45 +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({
2021-06-02 14:11:45 +08:00
type: 'error',
message: '上传失败'
})
this.fullscreenLoading = false
2019-10-26 23:25:43 +08:00
},
downloadFile(row) {
2021-06-02 14:11:45 +08:00
downloadImage(row.url, row.name)
}
}
2021-06-02 14:11:45 +08:00
}
</script>