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