<template>
<view>
<view class="content">
<view class="imageBigBox" v-for="(item,i) in imageList" :key="i">
<image style="width: 100%;height: 160rpx;" @click="previewImg(item)" :src="item"></image>
<u-icon class="delIcon" name="close-circle" color="#999" size="25" @click="delectImg(i)"></u-icon>
</view>
<view class="flex-center upImg" v-if="imageList.length<maxCount" @click="chooseFile">
<u-icon name="camera-fill" color="#c1c1c1" size="30"></u-icon>
</view>
</view>
</view>
</template>
<script>
export default {
name: "rc-upload-img",
props: {
maxCount: {
type: Number,
default: 1
},
},
data() {
return {
BASE_URL: getApp().globalData.BASE_URL,
imageList: [],
list: [],
};
},
methods: {
async chooseFile() {
uni.chooseImage({
count: this.maxCount - this.imageList.length,
sourceType: ['album'],
success: (res) => {
this.list=[]
res.tempFilePaths.forEach(item => {
this.imageList.push(item)
})
this.imageList.forEach(item => {
this.upImg(item)
})
}
});
},
upImg(path) {
uni.uploadFile({
url: `${this.BASE_URL}/api/common/upload`,
filePath: path,
name: 'file',
success: (res) => {
const data=eval('('+res.data+')')
if(data.code==1){
this.list.push(data.data.url)
this.$emit('list', this.list);
}
}
});
},
delectImg(i) {
uni.showModal({
title: '提示',
content: '确定要删除照片吗?',
cancelText: '取消',
confirmText: '确定',
success: res => {
if (res.confirm) {
this.imageList.splice(i, 1)
this.list.splice(i,1)
this.$emit('list', this.list);
}
}
})
},
previewImg(current) {
uni.previewImage({
current,
urls: this.imageList,
loop: true,
indicator: "default"
})
},
}
}
</script>
<style lang="scss" scoped>
.upImg {
width: 100%;
height: 160rpx;
background: #f4f5f7;
}
.content {
margin-right: 10rpx;
display: grid;
grid-template-columns: repeat(4, 24%);
grid-gap: 20rpx;
}
.imageBigBox {
position: relative;
}
.delIcon {
position: absolute;
right: 0;
top: -16rpx;
}
</style>
<rc-upload-img :maxCount="1" @list="getImgList"></rc-upload-img>
<script>
data(){
return{
photoList :[]
}},
methods: {
getImgList(e) {
this.photoList = e
},
}
</script>