|
@@ -1,9 +1,18 @@
|
|
<template>
|
|
<template>
|
|
<div>
|
|
<div>
|
|
- <el-upload ref="upload" v-if="type === 'file' ? true : hideUpload" :action="action" :headers="headers"
|
|
|
|
- :data="data" :show-file-list="false" :list-type="type === 'file' ? '':'picture-card'"
|
|
|
|
- :before-upload="beforeUpload" :on-success="onSuccess" :http-request="httpRequest">
|
|
|
|
- <el-button v-if="type === 'file'" type="primary" style="margin:0">上传文件</el-button>
|
|
|
|
|
|
+ <el-upload :disabled="disabledUpload"
|
|
|
|
+ ref="upload"
|
|
|
|
+ v-if="type === 'file' ? true : hideUpload"
|
|
|
|
+ :action="action"
|
|
|
|
+ :headers="headers"
|
|
|
|
+ :data="data"
|
|
|
|
+ :show-file-list="false"
|
|
|
|
+ :list-type="type === 'file' ? '':'picture-card'"
|
|
|
|
+ :before-upload="beforeUpload"
|
|
|
|
+ :on-success="onSuccess"
|
|
|
|
+ :on-progress="onProgress"
|
|
|
|
+ >
|
|
|
|
+ <el-button v-if="type === 'file'" type="primary" style="margin:0">{{ title }}</el-button>
|
|
<i v-else slot="default" class="el-icon-plus" />
|
|
<i v-else slot="default" class="el-icon-plus" />
|
|
</el-upload>
|
|
</el-upload>
|
|
<div class="upload-img" v-if="type === 'file' ? false : hideUpload === false">
|
|
<div class="upload-img" v-if="type === 'file' ? false : hideUpload === false">
|
|
@@ -16,91 +25,106 @@
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script>
|
|
<script>
|
|
-import { client } from '@/api/oss'
|
|
|
|
|
|
+// import { client } from '@/api/oss'
|
|
import { getToken } from "@/utils/auth";
|
|
import { getToken } from "@/utils/auth";
|
|
export default {
|
|
export default {
|
|
props: {
|
|
props: {
|
|
type: {
|
|
type: {
|
|
type: String,
|
|
type: String,
|
|
- default: 'image'
|
|
|
|
|
|
+ default: "image",
|
|
},
|
|
},
|
|
// 图片
|
|
// 图片
|
|
url: {
|
|
url: {
|
|
type: String,
|
|
type: String,
|
|
- default: ''
|
|
|
|
|
|
+ default: "",
|
|
},
|
|
},
|
|
// 隐藏删除按钮
|
|
// 隐藏删除按钮
|
|
disabled: {
|
|
disabled: {
|
|
type: Boolean,
|
|
type: Boolean,
|
|
- default: false
|
|
|
|
- }
|
|
|
|
|
|
+ default: false,
|
|
|
|
+ },
|
|
},
|
|
},
|
|
data() {
|
|
data() {
|
|
return {
|
|
return {
|
|
// 额外参数
|
|
// 额外参数
|
|
data: {
|
|
data: {
|
|
- multipartFile: new FormData()
|
|
|
|
|
|
+ multipartFile: new FormData(),
|
|
},
|
|
},
|
|
|
|
+ // 文件大小
|
|
|
|
+ size: 0,
|
|
// 请求头
|
|
// 请求头
|
|
headers: {
|
|
headers: {
|
|
- Authorization: "Bearer " + getToken()
|
|
|
|
|
|
+ Authorization: "Bearer " + getToken(),
|
|
},
|
|
},
|
|
// 上传地址
|
|
// 上传地址
|
|
- action: '',
|
|
|
|
|
|
+ action: "",
|
|
// 隐藏上传按钮
|
|
// 隐藏上传按钮
|
|
hideUpload: true,
|
|
hideUpload: true,
|
|
|
|
+ // 禁用上传按钮
|
|
|
|
+ disabledUpload: false,
|
|
|
|
+ // 按钮标题
|
|
|
|
+ title: '上传文件',
|
|
// 图片
|
|
// 图片
|
|
- newUrl: ''
|
|
|
|
- }
|
|
|
|
|
|
+ newUrl: "",
|
|
|
|
+ };
|
|
},
|
|
},
|
|
watch: {
|
|
watch: {
|
|
url(val) {
|
|
url(val) {
|
|
if (val) {
|
|
if (val) {
|
|
- this.newUrl = val
|
|
|
|
- this.hideUpload = false
|
|
|
|
|
|
+ this.newUrl = val;
|
|
|
|
+ this.hideUpload = false;
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ },
|
|
},
|
|
},
|
|
mounted() {
|
|
mounted() {
|
|
if (this.url) {
|
|
if (this.url) {
|
|
- this.newUrl = this.url
|
|
|
|
- this.hideUpload = false
|
|
|
|
|
|
+ this.newUrl = this.url;
|
|
|
|
+ this.hideUpload = false;
|
|
}
|
|
}
|
|
|
|
|
|
- this.action = this.type === 'image' ?
|
|
|
|
- `${process.env.VUE_APP_BASE_API}/system/file/picture/upload` :
|
|
|
|
- `${process.env.VUE_APP_BASE_API}/system/file/file/upload`
|
|
|
|
|
|
+ this.action =
|
|
|
|
+ this.type === "image"
|
|
|
|
+ ? `${process.env.VUE_APP_BASE_API}/system/file/picture/upload`
|
|
|
|
+ : `${process.env.VUE_APP_BASE_API}/system/file/file/upload`;
|
|
},
|
|
},
|
|
methods: {
|
|
methods: {
|
|
// 上传服务器之前提交文件
|
|
// 上传服务器之前提交文件
|
|
beforeUpload(file) {
|
|
beforeUpload(file) {
|
|
- this.data.multipartFile = file
|
|
|
|
|
|
+ this.data.multipartFile = file;
|
|
|
|
+ this.size = file.size
|
|
|
|
+ this.disabledUpload = true
|
|
},
|
|
},
|
|
|
|
|
|
// 上传成功
|
|
// 上传成功
|
|
onSuccess(file) {
|
|
onSuccess(file) {
|
|
- this.hideUpload = false
|
|
|
|
- this.$emit('success', this.newUrl = file.data)
|
|
|
|
|
|
+ this.hideUpload = false;
|
|
|
|
+ this.disabledUpload = false
|
|
|
|
+ this.newUrl = file.data
|
|
|
|
+ this.title = '上传成功'
|
|
|
|
+ this.$emit("success", {
|
|
|
|
+ file: this.newUrl,
|
|
|
|
+ size: this.size
|
|
|
|
+ });
|
|
|
|
+ },
|
|
|
|
+
|
|
|
|
+ // 上传时 进度条
|
|
|
|
+ onProgress(file){
|
|
|
|
+ this.title = `${parseInt(file.percent - 1)}%`
|
|
},
|
|
},
|
|
|
|
|
|
// 删除
|
|
// 删除
|
|
handleRemove() {
|
|
handleRemove() {
|
|
- this.hideUpload = true
|
|
|
|
- this.$emit('delete', this.newUrl = '')
|
|
|
|
|
|
+ this.hideUpload = true;
|
|
|
|
+ this.$emit("delete", (this.newUrl = ""));
|
|
},
|
|
},
|
|
|
|
|
|
- async httpRequest(e) {
|
|
|
|
- try {
|
|
|
|
- let file = e.file
|
|
|
|
- let fileName = file.name
|
|
|
|
- const res = client.put(fileName, file)
|
|
|
|
- console.log(res);
|
|
|
|
- } catch(err) {
|
|
|
|
- console.log(err);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+ // httpRequest(e) {
|
|
|
|
+ // client.put(e.file.name, e.file).then(res => {
|
|
|
|
+ // console.log(res);
|
|
|
|
+ // })
|
|
|
|
+ // },
|
|
|
|
+ },
|
|
|
|
+};
|
|
</script>
|
|
</script>
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
<style lang="scss" scoped>
|