mrr.sj.front/components/dynamic-form/FormItemImage.vue

123 lines
2.5 KiB
Vue
Raw Normal View History

2026-06-09 17:49:15 +08:00
<template>
<view class="form-image">
<view class="image-list">
<view v-for="(item, index) in imageList" :key="index" class="image-item">
<image :src="item" mode="aspectFill" class="preview-image" @click="previewImage(index)" />
<view class="delete-btn" @click.stop="deleteImage(index)">
<text class="delete-icon">&times;</text>
</view>
</view>
<view v-if="imageList.length < maxCount" class="image-item add-btn" @click="chooseImage">
<text class="add-icon">+</text>
</view>
</view>
</view>
</template>
<script>
export default {
2026-06-10 10:15:10 +08:00
name: 'FormItemImage',
2026-06-09 17:49:15 +08:00
props: {
2026-06-10 10:15:10 +08:00
value: { type: Array, default: function () { return []; } },
2026-06-09 17:49:15 +08:00
maxCount: { type: Number, default: 9 }
},
2026-06-10 10:15:10 +08:00
data: function () {
2026-06-09 17:49:15 +08:00
return {
imageList: []
};
},
watch: {
value: {
2026-06-10 10:15:10 +08:00
handler: function (val) {
2026-06-09 17:49:15 +08:00
this.imageList = Array.isArray(val) ? val.slice() : [];
},
immediate: true
}
},
methods: {
2026-06-10 10:15:10 +08:00
chooseImage: function () {
2026-06-09 17:49:15 +08:00
var remaining = this.maxCount - this.imageList.length;
if (remaining <= 0) return;
var self = this;
uni.chooseImage({
count: remaining,
2026-06-10 10:15:10 +08:00
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: function (res) {
2026-06-09 17:49:15 +08:00
self.imageList = self.imageList.concat(res.tempFilePaths);
2026-06-10 10:15:10 +08:00
self.$emit('input', self.imageList);
2026-06-09 17:49:15 +08:00
}
});
},
2026-06-10 10:15:10 +08:00
deleteImage: function (index) {
2026-06-09 17:49:15 +08:00
this.imageList.splice(index, 1);
2026-06-10 10:15:10 +08:00
this.$emit('input', this.imageList);
2026-06-09 17:49:15 +08:00
},
2026-06-10 10:15:10 +08:00
previewImage: function (index) {
2026-06-09 17:49:15 +08:00
uni.previewImage({
urls: this.imageList,
current: index
});
}
}
};
</script>
<style scoped>
.form-image {
width: 100%;
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.image-list {
display: flex;
flex-wrap: wrap;
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.image-item {
width: 200rpx;
height: 200rpx;
2026-06-10 10:15:10 +08:00
margin-right: 10rpx;
margin-bottom: 20rpx;
2026-06-09 17:49:15 +08:00
border-radius: 8rpx;
overflow: hidden;
position: relative;
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.preview-image {
2026-06-10 10:15:10 +08:00
width: 200rpx;
height: 200rpx;
border-radius: 8rpx;
2026-06-09 17:49:15 +08:00
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.delete-btn {
position: absolute;
2026-06-10 10:15:10 +08:00
top: -20rpx;
right: -20rpx;
2026-06-09 17:49:15 +08:00
width: 40rpx;
height: 40rpx;
2026-06-10 10:15:10 +08:00
background-color: rgba(0, 0, 0, 0.5);
border-radius: 50%;
2026-06-09 17:49:15 +08:00
display: flex;
align-items: center;
justify-content: center;
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.delete-icon {
color: #ffffff;
2026-06-10 10:15:10 +08:00
font-size: 32rpx;
2026-06-09 17:49:15 +08:00
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.add-btn {
2026-06-10 10:15:10 +08:00
background-color: #ffffff;
2026-06-09 17:49:15 +08:00
display: flex;
align-items: center;
justify-content: center;
2026-06-10 10:15:10 +08:00
border: 2rpx dashed #dddddd;
2026-06-09 17:49:15 +08:00
}
2026-06-10 10:15:10 +08:00
2026-06-09 17:49:15 +08:00
.add-icon {
font-size: 60rpx;
color: #999999;
}
</style>