Your Name
2022-04-06 37b718547bc441c7502f0bfcf86209efe253851b
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<template>
  <div >
      <el-upload
          action=""
          :disabled="disabled"
          list-type="picture-card"
          :file-list="fileList"
          :http-request="handleUpload"
          :beforeUpload="beforeUploadImg"
          :on-success="handleSuccess"
          :on-error="handleError"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
      >
          <i  class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible" :modal="false">
          <img width="100%" :src="dialogImageUrl" alt="">
      </el-dialog>
  </div>
</template>
 
 
<script>
 
    import {emergencyPlanUpload} from "@/api/emergencyplan.js";
 
    export default {
        props: {
            disabled: {//是否可用
                type: Boolean,
                default:false
            },
            imgList: {//存储路径
                type: Array,
                default:[]
            },
        },
        data() {
            return {
                dialogImageUrl: '',
                dialogVisible: false,
 
                fileList:[],
                fileUrl:"",
                fileName:"",
 
            };
        },
        watch:{},
        created:function () {},
        mounted(){
 
            if (this.imgList!=null){
                for(let i = 0 ;i < this.imgList.length ; i++){
                    this.imgList[i].url = process.env.IMG_API + 'emergencyPlan/'+this.imgList[i].fileName
                    this.fileList.push(this.imgList[i])
                }
            }
        },
        methods: {
            handleRemove(file, fileList) {
                for (let a = 0;a<this.fileList.length;a++){
                    if(file.uid == this.fileList[a].uid){
                        this.fileList.splice(a,1);
                    }
                }
                this.$nextTick(() => {
                    this.$emit('removeImgSuccess', {
                        fileList:this.fileList
                    });
                });
            },
            handlePreview(file) {
                this.dialogImageUrl = file.url;
                this.dialogVisible = true;
            },
            beforeUploadImg(file) {
                const isJPG = file.type === 'image/jpeg';
                const isPNG = file.type === 'image/png';
                if (!isJPG && !isPNG) {
                    this.$message.error('上传图片只能是 JPG 或者 PNG 格式!');
                }
                return (isJPG || isPNG) ;
            },
            handleError(err, file, fileList){
                this.$message.error('上传失败,系统未知错误!错误码为【500】');
            },
            handleUpload(param){
                let formData = new FormData();
                formData.append('file', param.file); //添加键值
                emergencyPlanUpload(formData).then(res=>{
                    if (res.data.code==200){
                        this.fileUrl = res.data.result.fileUrl
                        this.fileName = res.data.result.fileName
                        this.$nextTick(() => {
                            this.$emit('uploadImgSuccess', {
                                fileName: this.fileName,
                                fileUrl: this.fileUrl,
                                url:process.env.IMG_API + 'emergencyPlan/'+res.data.result.fileName
                            });
                        });
                        this.$message({
                            type:'success',
                            message:'上传成功',
                            duration:2000,
                        })
                    } else{
                        this.$message.error('上传失败,系统未知错误!');
                    }
                })
            },
            handleSuccess(response, file, fileList) {
            },
        }
    }
</script>