uniapp 上传图片(封装)

<template>
	<view class="margin-top">
		<view class="cu-bar bg-white ">
			<view class="action">
				{{label}}
			</view>
			<view class="action">
				{{imgList.length}}/{{maxImg}}
			</view>
		</view>
		<view class="cu-form-group">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(item,index) in imgList" :key="index" @tap="ViewImage" :data-url="imgList[index]">
					<image :src="imgList[index]" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="DelImg" :data-index="index">
						<text class='cuIcon-close'></text>
					</view>
				</view>
				<view class="solids" @tap="ChooseImage" v-if="imgList.length<maxImg">
					<text class='cuIcon-cameraadd'></text>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	
	export default {
		name: 'MyImageUpoad',
		props: {
			value: {type:String,default:''},
			label:{type:String,default:'图片上传'},
			maxImg: {
				type: Number,
				default: 3
			},

		},
		mounted:function(){
			if (this.value.split(',')!=''){
				this.value.split(',').forEach(res=>{
					this.imgList.push(baseurl+res)
				})
			}
		},
		data() {
			return {
				imgList: [],
				pathlist:[],
			}
		},
		methods: {
			ChooseImage() {
				uni.chooseImage({
					count: this.maxImg, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
					sourceType: ['album','camera'], //从相册选择
					success: (res) => {
						uni.uploadFile({
							url: `${baseurl}systemController/filedeal.do?isup=1`,
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (uploadFileRes) => {
								let path = JSON.parse(uploadFileRes.data).obj
								this.pathlist.push(path);
								this.$emit('input',this.pathlist.join(','))
								if (this.imgList.length != 0) {
									this.imgList = this.imgList.concat(res.tempFilePaths)
								} else {
									this.imgList = res.tempFilePaths
								}
							}
						})

					}
				});
			},
			ViewImage(e) {
				uni.previewImage({
					urls: this.imgList,
					current: e.currentTarget.dataset.url
				});
			},
			DelImg(e) {
				uni.showModal({
					title: '提示',
					content: '确认要删除吗',
					cancelText: '取消',
					confirmText: '确认',
					success: res => {
						if (res.confirm) {
							this.pathlist.splice(e.currentTarget.dataset.index,1)
							this.imgList.splice(e.currentTarget.dataset.index, 1)
							this.$emit('input',this.pathlist.join(','))
						}
					}
				})
			},
		}
	}
</script>

css外部引入
css

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值