uni-app利用chooseImage方法封装一个图片选择组件

效果如图:在这里插入图片描述
可以预览
在这里插入图片描述
长按可删除
在这里插入图片描述
可以设置最多上传数量
在这里插入图片描述
这里封装的组件有个MaxNumber ,number类型,用的时候在父组件传就行了,这里默认给的8

废话不多说直接上代码
封装好了之后我们用的时候只需要引入直接用就行

<template>
  <u-form-item label="上传照片:" label-position="top">
    <uni-chooseimg @getPhoto="getPhoto"></uni-chooseimg> // 就在这里直接用就行了
  </u-form-item>
</template>
import UniChooseimg from '../../../components/uni-chooseimg/uni-chooseimg.vue'; // 导入
export default {
	data() {
		return {
			urlList: [] // 上传的图片
		}
	},
	methods: {
		getPhoto(value) {
			console.log('上传的图片', value)
			this.urlList = value
		}
	}
}

uni-chooseimg组件

<template>
    <view class="uni-chooseimg">
        <view class="flexEnd" style="width: 84vw">{{ imageList.length }}/{{ MaxNumber }}</view>
        <view class="flexFront">
            <view class="kuangPic flexRowCenter" @tap="chooseImage" style="padding-top: 10rpx;">
                <uni-icons type="camera-filled" color="#606266" size="50"></uni-icons>
            </view>
            <view class="photoStyle">
                <view class="photoOver" :style="{ width: picWidth }">
                    <view class="kuangPic" v-for="(image, index) in imageList" :key="index">
                        <image class="" :src="image" :data-src="image" @tap="previewImage" @longpress="clearImg"></image>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
<script>
// ['拍照', '相册', '拍照或相册'
const sourceType = [
    ['camera'],
    ['album'],
    ['camera', 'album']
]
// ['压缩', '原图', '压缩或原图']
const sizeType = [
    ['compressed'],
    ['original'],
    ['compressed', 'original']
]
export default {
    props: {
        // 最大可上传图片数量
        MaxNumber: {
            type: Number,
            default: 8
        },
        // 使用相册还是相机 1 相机、2 相册、3 二者都有
        sourceTypeIndex: {
            type: Number,
            default: 3
        },
        // 使用原图还是压缩图 1 压缩图、2 原图、3 二者都有
        sizeTypeIndex: {
            type: Number,
            default: 2
        },
    },
    data() {
        return {
            alotPhoto: [], // 图片数组
            imageList: [], //  显示图片数组
            imageSuccess: [], //  成功之前暂存
        }
    },
    methods: {
        chooseImage() {   // 图片选择和上传
            console.log('显示的图片数量', this.imageList.length)
            if (this.imageList.length >= this.MaxNumber) {
                uni.showModal({
                    title: `最多只能上传${this.MaxNumber}张图片`,
                    content: '',
                    showCancel: true,
                    success: ({ confirm, cancel }) => {
                        console.log('tishi', confirm, cancel)
                    }
                })
                return
            }
            let url = '上传文件接口';
            uni.chooseImage({
                sourceType: sourceType[this.sourceTypeIndex],
                sizeType: sizeType[this.sizeTypeIndex],
                count: this.MaxNumber, // 最多可以选择的图片张数 默认是9
                success: (res) => {
                    console.log("图片上传", res)
                    this.imageSuccess = this.imageSuccess.concat(res.tempFilePaths);
                    uni.uploadFile({
                        url: url,
                        filePath: res.tempFilePaths[0],
                        name: 'file',
                        formData: {},
                        header: {
                            "Content-Type": "multipart/form-data"
                        },
                        success: (res) => {
                            console.log(12333333, res, JSON.parse(res.data));
                            let imageInfo = JSON.parse(res.data);
                            if (imageInfo.code == 200) {
                                this.feng.showTips('上传成功', 'success');
                                this.alotPhoto = this.alotPhoto.concat(imageInfo.url);
                                this.imageList = this.imageSuccess;
                                console.log('上传成功', this.alotPhoto);
                                this.$emit('getPhoto', this.alotPhoto)
                            } else if (imageInfo.code == 500) {
                                this.feng.showTips('上传失败', 'loading');
                            }
                        },
                        fail: (err) => {
                            this.feng.showTips('上传失败', 'loading');
                        }
                    })
                },
                fail: (err) => {

                }
            })
        },
        // 预览图片
        previewImage: function (e) {
            console.log("预览", e)
            var current = e.target.dataset.src
            uni.previewImage({
                current: current,
                urls: this.imageList,
            })
        },
        // 清除图片
        clearImg(e) {
            let that = this;
            let id = '';
            let deleteItem = e.target.dataset.src;
            console.log("清除图片", e, that.imageList)
            for (let i in that.imageList) {
                if (that.imageList[i] == deleteItem) {
                    id = i;
                }
            }
            console.log("下标值", id)
            uni.showModal({
                title: '删除提示',
                content: '确认删除此图片吗',
                success: function (res) {
                    if (res.confirm) {
                        console.log('用户点击确定');
                        that.imageList.splice(id, 1);
                        that.alotPhoto.splice(id, 1);
                        console.log(that.imageList);
                        that.feng.showTips("删除成功", 'none');
                        that.$emit('getPhoto', that.alotPhoto)
                    } else if (res.cancel) {
                        console.log('用户点击取消');
                    }
                }
            });
        }
    }
}
</script>
<style lang="scss" scoped>
.uni-chooseimg {
    .photoStyle {
        width: 90vw;
        white-space: nowrap;
        overflow: hidden;
        overflow-y: hidden;
        overflow-x: scroll;
        margin-top: 10px;
    }

    .photoOver {
        display: flex;
        align-items: center;
        flex-wrap: wrap;
    }

    .kuangPic {
        border: 1px solid #DCDFE6;
        margin-right: 10rpx;
        width: 160rpx;
        height: 160rpx;
        display: flex;
        justify-content: center;
        align-items: center;

        image {
            width: 100%;
            height: 100%;
        }
    }

}
</style>

直接复制,开箱即用哦。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jieyucx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值