250 lines
6.5 KiB
Vue
250 lines
6.5 KiB
Vue
<template>
|
|
<div class="upload-container">
|
|
<el-upload
|
|
class="upload-demo"
|
|
:class="{ uploadDisabled: disabled ? true : false }"
|
|
action
|
|
multiple
|
|
:http-request="uploadFile"
|
|
:before-upload="beforeUpload"
|
|
:file-list="fileList"
|
|
:on-preview="handlePreview"
|
|
:on-remove="remove"
|
|
:on-exceed="handleExceed"
|
|
accept=".png,.jpg,.jpeg"
|
|
list-type="picture-card"
|
|
>
|
|
<i slot="default" class="el-icon-plus"></i>
|
|
<div slot="file" slot-scope="{ file }" style="width: 100%; height: 100%">
|
|
<viewer
|
|
:ref="file.url"
|
|
:images="fileList"
|
|
style="
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 100%;
|
|
height: 100%;
|
|
"
|
|
>
|
|
<img
|
|
class="el-upload-list__item-thumbnail"
|
|
:src="`${file.url}`"
|
|
alt=""
|
|
crossorigin="anonymous"
|
|
style="max-width: 100%; max-height: 100%"
|
|
/>
|
|
<span class="el-upload-list__item-actions" v-if="!isUpload">
|
|
<span
|
|
class="el-upload-list__item-preview"
|
|
@click="handlePictureCardPreview(file)"
|
|
>
|
|
<i class="el-icon-zoom-in"></i>
|
|
</span>
|
|
<span
|
|
class="el-upload-list__item-delete"
|
|
v-if="!disabled"
|
|
@click="handleRemove(file)"
|
|
>
|
|
<i class="el-icon-delete"></i>
|
|
</span>
|
|
</span>
|
|
<div class="loadingBox" v-else>
|
|
<i class="el-icon-loading" style="color: #fff; margin: auto"></i>
|
|
</div>
|
|
</viewer>
|
|
</div>
|
|
</el-upload>
|
|
</div>
|
|
</template>
|
|
<script>
|
|
const type = 'Statement of Work'
|
|
export default {
|
|
name: 'UploadImage',
|
|
props: {
|
|
path: {
|
|
required: true,
|
|
default: () => {
|
|
return []
|
|
},
|
|
},
|
|
disabled: {
|
|
required: true,
|
|
default: false,
|
|
},
|
|
isUpload: {
|
|
required: true,
|
|
default: false,
|
|
},
|
|
trialId: {
|
|
type: String,
|
|
default: '',
|
|
},
|
|
},
|
|
data() {
|
|
return {
|
|
fileList: [],
|
|
btnDisabled: false,
|
|
}
|
|
},
|
|
methods: {
|
|
remove(file, fileList) {
|
|
// this.$emit("update:path", null);
|
|
},
|
|
fileToBlob(file) {
|
|
// 创建 FileReader 对象
|
|
const reader = new FileReader()
|
|
return new Promise((resolve) => {
|
|
// FileReader 添加 load 事件
|
|
reader.addEventListener('load', (e) => {
|
|
let blob
|
|
if (typeof e.target.result === 'object') {
|
|
blob = new Blob([e.target.result])
|
|
} else {
|
|
blob = e.target.result
|
|
}
|
|
resolve(blob)
|
|
})
|
|
// FileReader 以 ArrayBuffer 格式 读取 File 对象中数据
|
|
reader.readAsArrayBuffer(file)
|
|
})
|
|
},
|
|
// 上传oss
|
|
async uploadToOSS(name, file) {
|
|
try {
|
|
let defaultPath = '/System/FeedBack/'
|
|
if (this.trialId) {
|
|
defaultPath = `/${this.trialId}/FeedBack/`
|
|
}
|
|
let res = await this.OSSclient.put(`${defaultPath}${name}`, file)
|
|
return res
|
|
} catch (err) {
|
|
console.log(err)
|
|
return false
|
|
}
|
|
},
|
|
initFileList(list) {
|
|
if (list && list.length > 0) {
|
|
this.fileList = []
|
|
list.forEach((item, index) => {
|
|
let name = item.split('/')
|
|
this.fileList.push({
|
|
name: name[name.length - 1],
|
|
path: item,
|
|
fullPath: this.OSSclientConfig.basePath + item,
|
|
url: this.OSSclientConfig.basePath + item,
|
|
uid: `${name[name.length - 1]}${index}`,
|
|
})
|
|
})
|
|
}
|
|
},
|
|
async uploadFile(param) {
|
|
var fileName = param.file.name
|
|
this.$emit('update:isUpload', true)
|
|
this.btnDisabled = true
|
|
this.fileList.push({
|
|
url: param.file.url,
|
|
path: fileName,
|
|
uid: param.file.uid,
|
|
})
|
|
let file = await this.fileToBlob(param.file)
|
|
let res = await this.uploadToOSS(fileName, file)
|
|
this.btnDisabled = false
|
|
let index = this.fileList.findIndex((item) => item.uid === param.file.uid)
|
|
if (!res) {
|
|
if (index >= 0) {
|
|
this.fileList.splice(index, 1)
|
|
}
|
|
return this.$emit('update:isUpload', false)
|
|
}
|
|
let name = this.$getObjectName(res.url)
|
|
this.fileList[index].url = this.OSSclientConfig.basePath + name
|
|
this.fileList[index].path = name
|
|
this.$emit('update:path', [...this.path, res.name])
|
|
this.$emit('update:isUpload', false)
|
|
return false
|
|
},
|
|
beforeUpload(file, fileList) {
|
|
const isValidFile = this.fileValid(file.name, ['png', 'jpg', 'jpeg'])
|
|
if (isValidFile) {
|
|
// this.fileList = [];
|
|
} else {
|
|
this.$alert(this.$t('feedBack:uploadImg:format'))
|
|
return false
|
|
}
|
|
},
|
|
handlePreview(file) {
|
|
file.fullPath ? window.open(file.fullPath, '_blank') : ''
|
|
},
|
|
handleExceed(files, fileList) {
|
|
this.$message.warning(`Upload is currently limited to 1 file`)
|
|
},
|
|
fileValid(fileName, typeArr) {
|
|
var extendName = fileName
|
|
.substring(fileName.lastIndexOf('.') + 1)
|
|
.toLocaleLowerCase()
|
|
if (typeArr.indexOf(extendName) > -1) {
|
|
return true
|
|
} else {
|
|
return false
|
|
}
|
|
},
|
|
handlePictureCardPreview(file) {
|
|
this.$refs[file.url].$viewer.show()
|
|
},
|
|
handleRemove(file) {
|
|
let index = this.fileList.findIndex((item) => item.uid === file.uid)
|
|
this.fileList.splice(index, 1)
|
|
let arr = this.fileList.map((item) => item.path)
|
|
this.$emit('update:path', arr)
|
|
},
|
|
},
|
|
}
|
|
</script>
|
|
<style>
|
|
.upload-container .el-upload--text {
|
|
border: none;
|
|
width: 80px;
|
|
height: 30px;
|
|
}
|
|
.upload-container .el-form-item__label {
|
|
font-size: 12px;
|
|
}
|
|
.upload-container .el-upload-list__item {
|
|
font-size: 12px;
|
|
}
|
|
.logoAMessage {
|
|
font-size: 12px;
|
|
cursor: pointer;
|
|
}
|
|
.upload-container .uploadDisabled .el-upload--picture-card {
|
|
display: none;
|
|
}
|
|
.upload-container .el-upload--picture-card {
|
|
width: 100px;
|
|
height: 100px;
|
|
line-height: 100px;
|
|
}
|
|
.upload-container .el-upload-list--picture-card .el-upload-list__item {
|
|
width: 100px;
|
|
height: 100px;
|
|
line-height: 100px;
|
|
}
|
|
.loadingBox {
|
|
position: absolute;
|
|
top: 0;
|
|
bottom: 0;
|
|
right: 0;
|
|
left: 0;
|
|
width: 100px;
|
|
height: 100px;
|
|
background-color: rgba(0, 0, 0, 0.5);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
.el-upload-list--picture-card .el-upload-list__item-thumbnail {
|
|
width: auto;
|
|
height: auto;
|
|
}
|
|
</style> |