1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<template>
<div>
<el-row :gutter="20">
<el-col :span="12">
<el-form ref="form" label-width="80px">
<el-form-item label="标题">
<el-input v-model="formData.title"/>
</el-form-item>
<el-form-item label="封面">
<div style="margin: 10px">
<el-button @click="uploadType = true" v-if="!uploadType">地址上传</el-button>
<el-button @click="uploadType = false" v-if="uploadType">文件上传</el-button>
</div>
<el-upload
v-if="!uploadType"
class="avatar-uploader"
:http-request="uploadFileImage"
:before-upload="beforeAvatarUpload"
action="https://jsonplaceholder.typicode.com/posts/"
:on-success="uploadImgSuccess"
:show-file-list="false">
<img v-if="formData.cover" :src="formData.cover" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
<el-input v-model="formData.cover" v-if="uploadType"></el-input>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</template>
<script>
import {uploadFileApi} from "../../service/api";
export default {
name: "baseBlock",
props:[
'formData'
],
data(){
return{
uploadType:false
}
},
methods:{
beforeAvatarUpload(file){
const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png' );
const isLt2M = file.size / 1024 < 200;
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG 或 PNG 格式!');
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 200KB!');
}
return isJPG && isLt2M;
},
uploadFileImage(a){
uploadFileApi({file:a.file,type:'local',obj:a}).then(res=>{
this.formData.cover = process.env.IMAGE_URL_HEAD + res.url;
this.$message({
type: 'success',
message: '上传成功!'
});
a.onSuccess('上传成功')
});
},
uploadImgSuccess(a,b,c){
}
}
}
</script>
<style scoped>
</style>