long
2021-12-08 bf97988edfb77f5436d4ef20575e9f34477f36a7
提交 | 用户 | age
ccad61 1 <template>
C 2   <div>
3     <!-- 上传图片组件(多图) -->
4     <el-upload
30a172 5       id="UploadMultipleImg"
ccad61 6       ref="refUploadImg"
C 7       :accept="accept"
8       :auto-upload="false"
9       list-type="picture-card"
10       :class="{disabled:uploadDisabled}"
11       action="#"
12       :limit="limit"
30a172 13       drag
bf9798 14       multiple
ccad61 15       :file-list="uploadImgs"
C 16       :on-change="addUploadImg"
17       :on-remove="delUploadImg"
18       :http-request="uploadImg"
19       :on-preview="uploadPreview"
20       :before-upload="beforeUploadImg"
21       size="mini"
22     >
23       <i class="el-icon-plus" />
24     </el-upload>
25     <!-- upload放大图片 -->
26     <el-dialog :visible.sync="uploadPreviewVisible" append-to-body style="text-align:center">
27       <img style="max-width:100%" :src="uploadPreviewUrl" alt="">
28     </el-dialog>
29   </div>
30 </template>
31
32 <script>
33 import mixin_upload from '@/mixins/upload.js' // 通用上传图片预览
34 export default {
35   mixins: [mixin_upload],
36   model: {
37     prop: 'uploadImgs',
38     event: 'change'
39   },
40   props: {
41     uploadImgs: {
42       type: Array,
43       default: () => {
44         return []
45       }
46     },
47     accept: {
48       type: String,
49       default: () => {
50         return 'image/jpeg,image/jpg,image/gif,image/png'
51       }
52     },
53     limit: {
54       type: Number,
55       default: 9
56     }
57   },
58
59   data() {
60     return {
61       callback: null
62     }
63   },
64   computed: {
65     uploadDisabled() {
66       return this.uploadImgs.length >= this.limit
67     }
68   },
69
70   methods: {
71     // 上传图片 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
72     // 上传前确认格式
73     beforeUploadImg(file) {
74       const isImg = file.type.indexOf('image/') > -1
75       const isTooLarge = file.size / 1024 / 1024 > 4
76       let flag = true
77       if (!isImg) {
78         this.$message.error('必须是选择图片文件')
79         flag = false
80       } else if (isTooLarge) {
81         this.$message.error('请勿上传大于4MB的图片')
82         flag = false
83       }
84       return flag
85     },
86     // 增加图片
87     addUploadImg(file, fileList) {
88       this.$emit('change', fileList)
89     },
90     // 删除图片
91     delUploadImg(file, fileList) {
92       this.$emit('change', fileList)
93     },
94     // 执行上传商品图
95     runUploadImg(suc_cb) {
96       console.log('runUploadImg!!!!!')
97       if (this.checkNeedUpload(this.uploadImgs)) {
98         this.callback = suc_cb
99         this.$refs.refUploadImg.submit()
100       } else {
d4d2d4 101         suc_cb && suc_cb(this.uploadImgs)
ccad61 102       }
C 103     },
104     // 上传商品图
105     uploadImg(res) {
106       console.log(res)
107       const file = res.file
108       const formData = new FormData()
109       formData.append('file', file)
110       console.log(formData)
111       this.postFN({
112         url: 'admin/image/upload?folderCode=IMG',
113         header: { 'Content-Type': 'multipart/form-data' },
114         params: formData,
115         mockData: {
116           code: 100,
117           msg: '',
118           data: {
119             imgUrl: 'xxxx'
120           }
121         }
122       }, (inf) => {
123         console.log('上传图片成功')
124
125         // 根据uid 替换掉未上传的图片
126         var uploadImgs = this.uploadImgs
127         uploadImgs.forEach((o, i) => {
128           if (o.uid === res.file.uid) uploadImgs[i] = { url: inf.imgUrl, status: 'success' }
129         })
130         this.uploadImgs = JSON.parse(JSON.stringify(uploadImgs))
131
132         // 触发change时间激活v-model
133         this.$emit('change', uploadImgs)
134
135         console.log('上传图片结束')
136
137         // 上传结束
138         if (!this.checkNeedUpload(this.uploadImgs)) {
139           console.log('上传轮播图结束')
140           this.callback && this.callback(this.uploadImgs)
141         }
142       })
143     }
144     // 上传图片 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
145   }
146 }
147 </script>
148
149 <style>
150 .disabled .el-upload--picture-card {
151     display: none;
152 }
153 .el-upload-list--picture-card{
154   line-height: 1;
155 }
156 .el-upload-list--picture-card .el-upload-list__item{
157   margin-bottom: 0
158 }
30a172 159 #UploadMultipleImg .el-upload-dragger{
L 160   width: auto;
161   height: auto;
162 }
ccad61 163 </style>