|
@@ -1,9 +1,24 @@
|
|
|
<template>
|
|
|
<div class="upload">
|
|
|
- <el-upload v-if="isBtn()" :action="action" :headers="headers" :multiple="multiple" :data="data" :name="name"
|
|
|
- :show-file-list="showFileList" :drag="drag" :accept="accept" :listType="listType" :autoUpload="autoUpload"
|
|
|
- :disabled="disabled" :before-upload="beforeUpload" :on-progress="onProgress" :on-success="onSuccess"
|
|
|
- :on-error="onError">
|
|
|
+ <el-upload
|
|
|
+ v-if="isBtn()"
|
|
|
+ :platformId="platformId"
|
|
|
+ :action="action"
|
|
|
+ :headers="headers"
|
|
|
+ :multiple="multiple"
|
|
|
+ :data="data"
|
|
|
+ :name="name"
|
|
|
+ :show-file-list="showFileList"
|
|
|
+ :drag="drag"
|
|
|
+ :accept="accept"
|
|
|
+ :listType="listType"
|
|
|
+ :autoUpload="autoUpload"
|
|
|
+ :disabled="disabled"
|
|
|
+ :before-upload="beforeUpload"
|
|
|
+ :on-progress="onProgress"
|
|
|
+ :on-success="onSuccess"
|
|
|
+ :on-error="onError"
|
|
|
+ >
|
|
|
<el-button v-if="listType !== 'picture-card'" :type="type" ref="upload">
|
|
|
<slot v-if="percentage <= 0 && !form.file">点击上传</slot>
|
|
|
<span v-if="percentage > 0 && !form.file">{{ title }}</span>
|
|
@@ -13,8 +28,14 @@
|
|
|
<i class="el-icon-plus" />
|
|
|
</div>
|
|
|
</el-upload>
|
|
|
- <el-progress v-if="isPercentage()" type="circle" :percentage="percentage" :status="status"
|
|
|
- @click.native="handleUpload" :width="146" />
|
|
|
+ <el-progress
|
|
|
+ v-if="isPercentage()"
|
|
|
+ type="circle"
|
|
|
+ :percentage="percentage"
|
|
|
+ :status="status"
|
|
|
+ @click.native="handleUpload"
|
|
|
+ :width="146"
|
|
|
+ />
|
|
|
<div class="img" v-if="isImg()">
|
|
|
<i class="el-icon-delete" v-if="!disabled" @click="onDelete" />
|
|
|
<el-image :src="form.file" />
|
|
@@ -23,8 +44,8 @@
|
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
-const baseUrl = process.env.VUE_APP_BASE_API
|
|
|
-import { getToken } from '@/utils/auth'
|
|
|
+const baseUrl = process.env.VUE_APP_BASE_API;
|
|
|
+import { getToken } from "@/utils/auth";
|
|
|
export default {
|
|
|
props: {
|
|
|
// 回显图片
|
|
@@ -32,197 +53,235 @@ export default {
|
|
|
// 是否支持多选文件
|
|
|
multiple: {
|
|
|
type: Boolean,
|
|
|
- default: false
|
|
|
+ default: false,
|
|
|
},
|
|
|
// 是否显示文件列表
|
|
|
showFileList: {
|
|
|
type: Boolean,
|
|
|
- default: false
|
|
|
+ default: false,
|
|
|
},
|
|
|
// 是否支持拖拽上传
|
|
|
drag: {
|
|
|
type: Boolean,
|
|
|
- default: false
|
|
|
+ default: false,
|
|
|
},
|
|
|
// 上传类型
|
|
|
listType: {
|
|
|
type: String,
|
|
|
- default: 'text'
|
|
|
+ default: "text",
|
|
|
},
|
|
|
+ // platformId: {
|
|
|
+ // type: Number,
|
|
|
+ // default: "",
|
|
|
+ // },
|
|
|
// 是否支持自动上传
|
|
|
autoUpload: {
|
|
|
type: Boolean,
|
|
|
- default: true
|
|
|
+ default: true,
|
|
|
},
|
|
|
// 是否禁用
|
|
|
disabled: {
|
|
|
type: Boolean,
|
|
|
- default: false
|
|
|
+ default: false,
|
|
|
},
|
|
|
// 最大允许上传个数
|
|
|
limit: Number,
|
|
|
// 文件类型
|
|
|
accept: {
|
|
|
type: String,
|
|
|
- default: ''
|
|
|
+ default: "",
|
|
|
},
|
|
|
// 文件名
|
|
|
name: {
|
|
|
type: String,
|
|
|
- default: 'multipartFile'
|
|
|
+ default: "multipartFile",
|
|
|
},
|
|
|
// 图片尺寸
|
|
|
width: {
|
|
|
type: Number,
|
|
|
- default: null
|
|
|
+ default: null,
|
|
|
},
|
|
|
height: {
|
|
|
type: Number,
|
|
|
- default: null
|
|
|
+ default: null,
|
|
|
},
|
|
|
// 文件大小
|
|
|
size: {
|
|
|
type: Number,
|
|
|
- default: null
|
|
|
- }
|
|
|
+ default: null,
|
|
|
+ },
|
|
|
},
|
|
|
+
|
|
|
data() {
|
|
|
return {
|
|
|
// 上传类型
|
|
|
obj: {
|
|
|
- 'text': `${baseUrl}/system/file/file/upload`, // 文件上传
|
|
|
- 'picture-card': `${baseUrl}/system/file/picture/upload`, // 图片上传
|
|
|
- 'audio': `${baseUrl}/system/file/mp3/upload`, // 音频上传
|
|
|
- 'zip': `${baseUrl}/system/file/file/uploadFolderZip`, // 压缩包上传
|
|
|
- 'mp3': `${baseUrl}/admin/program/batch/upload` // 歌曲批量上传
|
|
|
+ text: `${baseUrl}/system/file/file/upload`, // 文件上传
|
|
|
+ "picture-card": `${baseUrl}/system/file/picture/upload`, // 图片上传
|
|
|
+ audio: `${baseUrl}/system/file/mp3/upload`, // 音频上传
|
|
|
+ zip: `${baseUrl}/system/file/file/uploadFolderZip`, // 压缩包上传
|
|
|
+ mp3: `${baseUrl}/admin/program/batch/upload`, // 歌曲批量上传
|
|
|
},
|
|
|
// 上传地址
|
|
|
- action: '#',
|
|
|
+ action: "#",
|
|
|
+ platformId: 0,
|
|
|
// 请求头部
|
|
|
headers: {
|
|
|
- Authorization: "Bearer " + getToken()
|
|
|
+ Authorization: "Bearer " + getToken(),
|
|
|
},
|
|
|
// 额外参数
|
|
|
- data: {},
|
|
|
+ data: { platformId: 6 },
|
|
|
// 进度条
|
|
|
percentage: 0,
|
|
|
- title: '',
|
|
|
+ title: "",
|
|
|
// 进度条状态
|
|
|
status: null,
|
|
|
- type: 'primary',
|
|
|
+ type: "primary",
|
|
|
// 表单
|
|
|
form: {
|
|
|
size: 0,
|
|
|
- file: ''
|
|
|
- }
|
|
|
- }
|
|
|
+ file: "",
|
|
|
+ },
|
|
|
+ };
|
|
|
},
|
|
|
+
|
|
|
watch: {
|
|
|
url(val) {
|
|
|
- this.form.file = val
|
|
|
+ this.form.file = val;
|
|
|
if (!val) {
|
|
|
- this.percentage = 0
|
|
|
+ this.percentage = 0;
|
|
|
}
|
|
|
},
|
|
|
listType(val) {
|
|
|
- this.action = this.obj[val]
|
|
|
- }
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==xxxxx==" + val);
|
|
|
+ this.action = this.obj[val];
|
|
|
+ },
|
|
|
+
|
|
|
+ platformId(val) {
|
|
|
+ this.form.platformId = val;
|
|
|
+ },
|
|
|
},
|
|
|
+
|
|
|
created() {
|
|
|
// 根据上传类型 变更 上传地址
|
|
|
- this.action = this.obj[this.listType]
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==yyyy==" + this.listType);
|
|
|
+ this.action = this.obj[this.listType];
|
|
|
// 回显图片
|
|
|
- this.form.file = this.url
|
|
|
+ this.form.file = this.url;
|
|
|
+ this.form.platformId = this.platformId;
|
|
|
+ this.data.platformId = this.platformId;
|
|
|
},
|
|
|
methods: {
|
|
|
// 上传之前
|
|
|
async beforeUpload(file) {
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==111==" + JSON.stringify(file));
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==aaa==" + JSON.stringify(this.form));
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==bbb==" + JSON.stringify(this.data));
|
|
|
if (this.width && this.height) {
|
|
|
- const data = await this.readerImg(file)
|
|
|
- let _width = data.width % this.width === 0 || this.width % data.width === 0 ? true : false
|
|
|
- let _height = data.height % this.height === 0 || this.height % data.height === 0 ? true : false
|
|
|
+ const data = await this.readerImg(file);
|
|
|
+ let _width =
|
|
|
+ data.width % this.width === 0 || this.width % data.width === 0
|
|
|
+ ? true
|
|
|
+ : false;
|
|
|
+ let _height =
|
|
|
+ data.height % this.height === 0 || this.height % data.height === 0
|
|
|
+ ? true
|
|
|
+ : false;
|
|
|
if (!_width || !_height) {
|
|
|
- this.$message.error(`请上传${this.width}x${this.height}尺寸或同比例倍数的图片`)
|
|
|
- return Promise.reject(false)
|
|
|
+ this.$message.error(
|
|
|
+ `请上传${this.width}x${this.height}尺寸或同比例倍数的图片`
|
|
|
+ );
|
|
|
+ return Promise.reject(false);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (this.size && (file.size / 1024 / 1024 > this.size)) {
|
|
|
- this.$message.error(`文件大小不得超过${this.size}MB`)
|
|
|
- return Promise.reject(false)
|
|
|
+ if (this.size && file.size / 1024 / 1024 > this.size) {
|
|
|
+ this.$message.error(`文件大小不得超过${this.size}MB`);
|
|
|
+ return Promise.reject(false);
|
|
|
}
|
|
|
|
|
|
- this.form.size = file.size
|
|
|
+ this.form.size = file.size;
|
|
|
},
|
|
|
|
|
|
readerImg(file) {
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==222==" + file);
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- const reader = new FileReader()
|
|
|
- reader.readAsDataURL(file)
|
|
|
+ const reader = new FileReader();
|
|
|
+ reader.readAsDataURL(file);
|
|
|
reader.onload = () => {
|
|
|
- const img = new Image()
|
|
|
- img.src = reader.result
|
|
|
+ const img = new Image();
|
|
|
+ img.src = reader.result;
|
|
|
img.onload = () => {
|
|
|
resolve({
|
|
|
width: img.width,
|
|
|
- height: img.height
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
+ height: img.height,
|
|
|
+ });
|
|
|
+ };
|
|
|
+ };
|
|
|
+ });
|
|
|
},
|
|
|
|
|
|
// 上传过程中
|
|
|
onProgress(file) {
|
|
|
- this.percentage = parseInt(file.percent - 1)
|
|
|
- this.title = `已上传 ${parseInt(file.percent - 1)}%`
|
|
|
+ this.percentage = parseInt(file.percent - 1);
|
|
|
+ this.title = `已上传 ${parseInt(file.percent - 1)}%`;
|
|
|
},
|
|
|
|
|
|
// 上传成功
|
|
|
onSuccess(file) {
|
|
|
+ console.log("gasdfqwerqwerqwerwqr==333==" + JSON.stringify(file));
|
|
|
if (file.code === 0) {
|
|
|
- this.form.file = file.data
|
|
|
- this.$emit('upload', this.form)
|
|
|
- this.title = '上传成功'
|
|
|
- this.type = 'success'
|
|
|
+ this.form.file = file.data;
|
|
|
+ this.$emit("upload", this.form);
|
|
|
+ this.title = "上传成功";
|
|
|
+ this.type = "success";
|
|
|
} else {
|
|
|
- this.onError()
|
|
|
+ this.onError();
|
|
|
}
|
|
|
},
|
|
|
|
|
|
// 上传失败
|
|
|
onError() {
|
|
|
- this.status = 'exception'
|
|
|
- this.type = 'danger'
|
|
|
- this.title = '上传失败'
|
|
|
+ this.status = "exception";
|
|
|
+ this.type = "danger";
|
|
|
+ this.title = "上传失败";
|
|
|
},
|
|
|
|
|
|
// 删除
|
|
|
onDelete() {
|
|
|
- this.form.file = ''
|
|
|
- this.percentage = 0
|
|
|
- this.$emit('upload', this.form)
|
|
|
+ this.form.file = "";
|
|
|
+ this.percentage = 0;
|
|
|
+ this.$emit("upload", this.form);
|
|
|
},
|
|
|
|
|
|
// 如果上传失败 重新上传
|
|
|
handleUpload() {
|
|
|
- this.percentage = 0
|
|
|
+ this.percentage = 0;
|
|
|
},
|
|
|
|
|
|
// 上传按钮
|
|
|
isBtn() {
|
|
|
- return ['text', 'audio', 'zip', 'mp3'].includes(this.listType) || (this.listType === 'picture-card' && this.percentage == 0 && !this.form.file)
|
|
|
+ return (
|
|
|
+ ["text", "audio", "zip", "mp3"].includes(this.listType) ||
|
|
|
+ (this.listType === "picture-card" &&
|
|
|
+ this.percentage == 0 &&
|
|
|
+ !this.form.file)
|
|
|
+ );
|
|
|
},
|
|
|
// 显示进度条
|
|
|
isPercentage() {
|
|
|
- return this.listType === 'picture-card' && this.percentage !== 0 && !this.form.file
|
|
|
+ return (
|
|
|
+ this.listType === "picture-card" &&
|
|
|
+ this.percentage !== 0 &&
|
|
|
+ !this.form.file
|
|
|
+ );
|
|
|
},
|
|
|
// 显示图片
|
|
|
isImg() {
|
|
|
- return this.listType === 'picture-card' && this.form.file
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
+ return this.listType === "picture-card" && this.form.file;
|
|
|
+ },
|
|
|
+ },
|
|
|
+};
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|