zhaoxiaoqiang
2023-08-31 1e49e17d482e3a3e67108bdffa4ae5f374e26cb8
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!--
 * @Author: zxq
 * @Date: 2022-01-05 18:44:53
 * @LastEditors: zhaoxiaoqiang 287285524@qq.com
 * @LastEditTime: 2023-08-31 14:56:53
 * @Description: Description
 * @FilePath: \sjzl-plat\src\components\upload.vue
-->
<template>
  <div>
    <el-upload
        action="#"
        :limit='limit'
          multiple
        :disabled='disabled'
        list-type="picture-card"
        :on-exceed="masterFileMax"
        :before-upload="beforeAvatarUpload"
        :http-request="uploadFileData"
        :file-list="pictureList"
        >
        <i slot="default" class="el-icon-plus"></i>
        <div slot="file" slot-scope="{ file }">
            <!-- <span>{{ file.url }}</span> -->
            <img class="el-upload-list__item-thumbnail"  :src="file.url"  alt=""/>
            <span class="el-upload-list__item-actions">
            <span
                class="el-upload-list__item-preview"
                @click="handlePictureCardPreview(file)"
            >
                <i class="el-icon-zoom-in"></i>
            </span>
            <span  v-if='!disabled'
                class="el-upload-list__item-delete"
                @click="handleRemoveLoanT(file)"
            >
                <i class="el-icon-delete"></i>
            </span>
            </span>
        </div>
    </el-upload>
    <el-dialog title="查看图片" top="1vh"  :append-to-body='true' :visible.sync="imgVisible" class='dialogImageUrlbox'>
      <img width="80%" :src="dialogImageUrl" alt="" />
    </el-dialog>
  </div>
</template>
 
<script>
import { uploadFile } from "@/api/user";
import { imgShrink } from "@/utils/validate";
 
export default {
  props:{
     limit: {
      type: Number,
      default: 1
     },
     disabled: {
      type: Boolean,
      default: false
     },
     defaultList:{
      type: Array,
      default: ()=>{
          return []
      }
     },
     isProject:{
      type: Boolean,
      default: false
     }
  },
  data() {
      return {
          pictureList:[],
          imgVisible:false,
          dialogImageUrl:'',
          uploadFileForm:{
             suffix:"",
             base64Data:""
          }
      };
  },
  watch:{
      defaultList:{
        handler: function(val) {
          console.log(val)
          this.pictureList = val;
        },
        deep: true
    }
  },
  methods: {
      beforeAvatarUpload(file,type) {
          var testmsg = /^image\/(jpeg|png|jpg)$/.test(file.type);
          const isLt5M = file.size / 1024 / 1024 < 15;
          if (!testmsg) {
              this.$message.error("上传图片格式不对!");
              return false;
          }
          if (!isLt5M) {
              this.$message.error("上传图片大小不能超过 15MB!");
              return false;
          }
      },
      uploadFileData(file,type) {
          //上传图片
          const fileName = file.file.name;
          this.uploadFileForm.suffix = fileName.substr( fileName.lastIndexOf(".") + 1,fileName.length); 
          imgShrink(file.file).then(resBase64 => {
              this.uploadFileForm.base64Data = resBase64.split(",")[1];
              let apiEvent = {uploadFile};
              //直接拿到base64信息
              apiEvent["uploadFile"](this.uploadFileForm).then(res => {
                  this.pictureList.push({
                      filePath: res.body.fileUrl,
                      fileName: fileName,
                      uid: file.file.uid,
                      url: res.body.fileUrl,
                      fileId:res.body.fileId
                  });
                  this.$emit('sendList',this.pictureList);
              });
          });
      },
      handlePictureCardPreview(file) {
          this.dialogImageUrl = file.url;
          this.imgVisible = true;
      },
      handleRemoveLoanT(file) {
        const index = this.pictureList.findIndex(item => {
            return item.uid == file.uid;
        });
        this.pictureList.splice(index,1);
        this.$emit('sendList',this.pictureList);
      },
      masterFileMax(){
        this.$message.warning(`请最多上传 ${this.limit} 个文件。`);
      }
  },
  mounted() {
    if (this.defaultList.length > 0) {
      this.pictureList = this.defaultList;
      this.uploadDisabled = this.pictureList.length >= 1 && this.limit == 1;
    }
  },
};
</script>
 
<style>
.el-upload--picture-card , .el-upload-list__item{
  width: 74px !important;
  height: 74px !important;
  line-height: 76px !important;
}
 
</style>