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