Skip to content

Commit 4327d08

Browse files
committed
feat(upload-imgs): 支持上传并获取数组件据方法
1 parent 56bd2c8 commit 4327d08

2 files changed

Lines changed: 113 additions & 6 deletions

File tree

src/components/base/upload-imgs/index.vue

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
<div class="imgs-upload-container" v-loading="loading">
4242
<template v-for="(item, i) in itemList">
4343
<template v-if="item.display">
44-
<div class="thumb-item" :key="item.id" :style="boxStyle">
44+
<div class="thumb-item" :key="item.id" :style="boxStyle" v-loading="item.loading">
4545
<el-image
4646
class="thumb-item-img"
4747
:src="item.display"
@@ -400,7 +400,75 @@ export default {
400400
},
401401
methods: {
402402
async getValue() {
403-
// 获取需要上传
403+
const { itemList, isStable, min } = this
404+
405+
// 检查是否有不符合要求的空项
406+
const l = isStable ? itemList.length : (itemList.length - 1)
407+
for (let i = 0; i < l; i += 1) {
408+
if (itemList[i].status === 'input') {
409+
this.$message.error('当前存在未选择图片, 请全部选择')
410+
return false
411+
}
412+
}
413+
if (l < min) {
414+
this.$message.error(`至少选择${min}张图片`)
415+
return false
416+
}
417+
// 提取需要上传文件
418+
const asyncList = []
419+
// 上传文件后获取对应key值
420+
const getAsyncItem = (item) => {
421+
if (!item.file) {
422+
return Promise.resolve(item)
423+
}
424+
// eslint-disable-next-line
425+
item.loading = true
426+
return this.$axios({
427+
method: 'post',
428+
url: '/cms/file/',
429+
data: {
430+
file: item.file,
431+
},
432+
}).then((res) => {
433+
if (!Array.isArray(res) || res.length !== 1) {
434+
throw new Error('upload error')
435+
}
436+
// 释放内存
437+
window.URL.revokeObjectURL(item.display)
438+
// eslint-disable-next-line
439+
item.display = res[0].url
440+
// eslint-disable-next-line
441+
item.src = res[0].key
442+
// eslint-disable-next-line
443+
item.file = null
444+
// eslint-disable-next-line
445+
item.loading = false
446+
return item
447+
}).catch((err) => {
448+
// eslint-disable-next-line
449+
item.loading = false
450+
console.error(err)
451+
this.$message.error('图像上传失败, 请重试')
452+
return false
453+
})
454+
}
455+
for (let i = 0; i < itemList.length; i += 1) {
456+
// 跳过上传组件
457+
if (itemList[i].status !== 'input') {
458+
asyncList.push(getAsyncItem(itemList[i]))
459+
}
460+
}
461+
const imgInfoList = await Promise.all(asyncList)
462+
// 如果有失败的上传, 则返回错误
463+
if (imgInfoList.some(item => !item)) {
464+
return false
465+
}
466+
// 如无错误, 表示图像都以上传, 开始构造数据
467+
return imgInfoList.map(item => ({
468+
id: item.status === 'new' ? '' : item.id,
469+
display: item.display,
470+
src: item.src,
471+
}))
404472
},
405473
/**
406474
* 删除某项

src/plugins/ImgsUpload/views/Demo.vue

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@
33
<div class="lin-title">插件ImgsUpload舞台页面</div>
44
<el-divider></el-divider>
55
<div class="lin-wrap">
6-
<el-form label-width="180px">
7-
<el-form-item label="带初始化">
6+
<el-form label-width="220px">
7+
<el-form-item label="带初始化至少4张, 至多5张">
88
<upload-imgs
9+
ref="uploadEle"
910
:value="initData"
1011
:rules="rules"
1112
:multiple="true"
12-
:max-num="5" />
13+
:max-num="5"
14+
:min-num="4" />
15+
<div>
16+
<el-button @click="getValue">获取数据</el-button>
17+
</div>
1318
</el-form-item>
1419
<el-form-item label="普通示例">
1520
<upload-imgs :rules="rules" :multiple="true" />
@@ -37,6 +42,12 @@
3742
<el-form-item label="定制宽高-排序">
3843
<upload-imgs :rules="rules" :width="200" :height="150" :value="initData" :sortable="true" />
3944
</el-form-item>
45+
<el-form-item label="重新初始化">
46+
<upload-imgs :rules="rules" :width="200" :height="150" :value="initData1" :sortable="true" />
47+
<div>
48+
<el-button @click="changeData">重新初始化</el-button>
49+
</div>
50+
</el-form-item>
4051
<el-form-item label="图像缩略图展示模式">
4152
<upload-imgs :rules="rules" :width="200" :height="150" fit="cover" :value="initData" />
4253
</el-form-item>
@@ -64,6 +75,19 @@ export default {
6475
minWidth: 100,
6576
maxWidth: 2000,
6677
},
78+
initData1: [{
79+
id: '123',
80+
display: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
81+
src: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
82+
}, {
83+
id: '12d3',
84+
display: 'http://img-home.7yue.pro/images/index/Lin_cms_%E5%B0%81%E9%9D%A2.png',
85+
src: 'http://img-home.7yue.pro/images/index/Lin_cms_%E5%B0%81%E9%9D%A2.png',
86+
}, {
87+
id: 'hahah',
88+
display: 'http://img-home.7yue.pro/images/index/Lin_UI_%E5%B0%81%E9%9D%A2.png',
89+
src: 'http://img-home.7yue.pro/images/index/Lin_UI_%E5%B0%81%E9%9D%A2.png',
90+
}],
6791
initData: [{
6892
id: '123',
6993
display: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100',
@@ -91,7 +115,22 @@ export default {
91115
// // this.init()
92116
// // next()
93117
// },
94-
methods: {},
118+
methods: {
119+
async getValue() {
120+
console.log(await this.$refs.uploadEle.getValue())
121+
},
122+
changeData() {
123+
this.initData1 = [{
124+
id: 'hahah',
125+
display: 'http://img-home.7yue.pro/images/index/Lin_UI_%E5%B0%81%E9%9D%A2.png',
126+
src: 'http://img-home.7yue.pro/images/index/Lin_UI_%E5%B0%81%E9%9D%A2.png',
127+
}, {
128+
id: '12d3',
129+
display: 'http://img-home.7yue.pro/images/index/Lin_cms_%E5%B0%81%E9%9D%A2.png',
130+
src: 'http://img-home.7yue.pro/images/index/Lin_cms_%E5%B0%81%E9%9D%A2.png',
131+
}]
132+
},
133+
},
95134
}
96135
</script>
97136

0 commit comments

Comments
 (0)