最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

uniapp小程序的圖片(視頻)上傳的組件封裝方法

 更新時(shí)間:2024年02月20日 15:38:35   作者:前端fighter  
這篇文章主要介紹了uniapp做小程序的圖片(視頻)上傳的組件封裝,要求實(shí)現(xiàn)多張圖片的上傳 ,可以限制圖片上傳的數(shù)量,圖片預(yù)覽,多次使用對(duì)圖片的上傳順序排序,需要的朋友可以參考下

最近在做小程序,最后想試試新不同的技術(shù),所以選擇了用uniapp做小程序。

要求實(shí)現(xiàn)多張圖片的上傳 ,可以限制圖片上傳的數(shù)量,圖片預(yù)覽,多次使用對(duì)圖片的上傳順序排序

<template>
	<view>
		<view class="upload">
			<!-- 對(duì)視頻或者圖片進(jìn)行循環(huán) -->
			<block v-for="(upload,index) in uploads" :key="index">
				<view class="uplode-file">
					<!--  判斷是圖片還是視頻 如果是視頻通過(guò) image 進(jìn)行展示 -->
					<image v-if="types == 'image'" class="uploade-img" :src="upload" :data-src="upload" @tap="previewImage"></image>
					<!-- 用于取消的圖片 -->
					<image v-if="types == 'image'" class="clear-one-icon" :src="clearIcon" @tap="delImage(index)"></image>
					<!-- 如果是視頻通過(guò) video進(jìn)行展示   在視頻標(biāo)簽上面添加個(gè)圖片(刪除圖片用于取消視頻) -->
					<video v-if="types == 'video'" class="uploade-img" :src="upload" controls>
						<cover-image v-if="types == 'video'" class="clear-one-icon" :src="clearIcon" @tap="delImage(index)"></cover-image>
					</video>
				</view>
			</block>
			<!-- 這里是對(duì)于沒(méi)有圖片上傳之前的樣式的展示    如果圖片視頻的上傳的數(shù)量大于等于 設(shè)定的圖片上傳的數(shù)量 就對(duì)不在展示 該樣式 這樣的話 就不能繼續(xù)在 上傳圖片了-->
			<view v-if="uploads.length < uploadCount" :class="uploadIcon ? 'uploader-icon' : 'uploader-input-box'">
				<view v-if="!uploadIcon" class="uploader-input" @tap="chooseUploads"></view>
				<image v-else class="image-cion" :src="uploadIcon" @tap="chooseUploads"></image>
				<view style="height: 40upx; height: 25upx;">
				</view>
				<!-- 這里是對(duì)于上傳圖片的提示 -->
				<p class="ziliao" v-for="(item,index) in text" :key="index"> {{item}}</p>
			</view>
		</view>	
		<!--  提交上傳 -->
		<button type="primary" v-if="types == 'image'" @tap="upload" >上傳</button>
	</view>
</template>
<script>
	export default{
		//  props 接受傳遞過(guò)來(lái)的值
		props: {
			// 上傳的圖片的提示
			text: {
				type: Array,
				default: function() {
					return []
				}
			},
			//  用于判斷上傳的是圖片還是視頻
			types: {
				type: String,
				default: 'image'
			},
			// 圖片上傳的路徑
			dataList: {
				type: Array,
				default: function() {
					return []
				}
			},
			//  圖標(biāo) 用于去除已經(jīng)選中的圖片
			clearIcon: {
				type: String,
				default: 'http://img1.imgtn.bdimg.com/it/u=451604666,2295832001&fm=26&gp=0.jpg'
			},
			//  上傳的圖片的樣式  比如說(shuō)是個(gè)相機(jī) 還是加號(hào)
			uploadIcon: {
				type: String,
				default: ''
			},
			//  需要將圖片上傳到服務(wù)器的 地址
			uploadUrl: {
				type: String,
				default: ''
			},
			//  刪除圖片的地址
			deleteUrl: {
				type: String,
				default: ''
			},
			//  設(shè)置上傳圖片或視頻的數(shù)量
			uploadCount: {
				type: Number,
				default: 1
			},
			//上傳圖片大小 默認(rèn)3M
			upload_max: {
				type: Number,
				default: 3
			},
			disable:{
				type: Boolean,
				default: true,
			}
		},
		data(){
			return {
				//上傳的圖片地址
				uploadImages: [],
				//展示的圖片地址
				uploads: [],
				// 超出限制數(shù)組
				exceeded_list: [],
			}
		},
		mounted(){
			// 對(duì)上傳圖片做個(gè)判斷
			this.uploads = this.dataList
		},
		methods:{
			previewImage (e) {
				var current = e.target.dataset.src
				uni.previewImage({
					current: current,
					urls: this.dataList
				})
			},
			chooseUploads(){
				switch (this.types){
					case 'image': 
						uni.chooseImage({
							count: this.uploadCount - this.uploads.length, //默認(rèn)9
							sizeType: ['original', 'compressed'], //可以指定是原圖還是壓縮圖,默認(rèn)二者都有
							sourceType: ['album', 'camera'], //從相冊(cè)選擇
							success: (res) => {
								for(let i = 0; i< res.tempFiles.length; i++){
									if(Math.ceil(res.tempFiles[i].size / 1024) < this.upload_max * 1024){
										this.uploads.push(res.tempFiles[i].path)
										this.uploadImages.push(res.tempFiles[i].path)
									}else {
										this.exceeded_list.push(i === 0 ? 1 : i + 1);
										uni.showModal({
											title: '提示',
											content: `第${[...new Set(this.exceeded_list)].join(',')}張圖片超出限制${this.upload_max}MB,已過(guò)濾`
										});
									}
								}
							},
							fail: (err) => {
								uni.showModal({
									// content: JSON.stringify(err)
									content: '選擇被取消'
								});
							}
						});
					break;
					case 'video' :
						uni.chooseVideo({
							sourceType: ['camera', 'album'],
							success: (res) => {
								if(Math.ceil(res.size / 1024) < this.upload_max * 1024){
									this.uploads.push(res.tempFilePath)
									uni.uploadFile({
										url: this.uploadUrl, //僅為示例,非真實(shí)的接口地址
										filePath: res.tempFilePath,
										name: 'file',
										//請(qǐng)求參數(shù)
										formData: {
											'user': 'test'
										},
										success: (uploadFileRes) => {
											this.$emit('successVideo',uploadFileRes)
										}
									});
								}else {
									uni.showModal({
										title: '提示',
										content: `第${[...new Set(this.exceeded_list)].join(',')}張視頻超出限制${this.upload_max}MB,已過(guò)濾`
									});
								}
							},
							fail: (err) => {
								uni.showModal({
									// content: JSON.stringify(err)
									content: '確認(rèn)取消?'
								});
							}
						});
					break;
				}
			},
			delImage(index){
				//第一個(gè)是判斷app或者h(yuǎn)5的 第二個(gè)是判斷小程序的
				if(this.uploads[index].substring(0,4) !== 'http' || this.uploads[index].substring(0,11) == 'http://tmp/'){
					this.uploads.splice(index,1)
					return;
				};
				if(!this.deleteUrl) {
					uni.showModal({
						content: '請(qǐng)?zhí)顚?xiě)刪除接口'
					});
					return;
				};
				uni.request({
					url: this.deleteUrl,
					method: 'DELETE',
					data: {
						image: this.dataList[index]
					},
					success: res => {
						console.log(123456);
						if(res.data.status == 1) {
							uni.showToast({
								title: '刪除成功'
							})
							this.uploads.splice(index,1)
						}
					},
				});
			},
			upload(){
				var _this = this 
				var j = 0 
				if(!this.disable){
					uni.showModal({
						content: '請(qǐng)先上傳上面的喲'
					});
					return;
				}
				if(!this.uploadUrl) {
					uni.showModal({
						content: '請(qǐng)?zhí)顚?xiě)上傳接口'
					});
					return;
				};
				if(this.uploadImages.length < 1){
					uni.showModal({
						content: '請(qǐng)選擇圖片'
					});
					return;
				}
				if(this.uploadImages.length < this.uploadCount){
					uni.showModal({
						content: '請(qǐng)確認(rèn)上傳的數(shù)量為' + this.uploadCount + '張'
					});
					return;
				}
				for (let i of this.uploadImages) {
					uni.uploadFile({
						url: this.uploadUrl, //僅為示例,非真實(shí)的接口地址
						filePath: i,
						name: 'images[]',
						//請(qǐng)求參數(shù)
						formData: {
							'user': 'test'
						},
						success: (uploadFileRes) => {
							this.$emit('successImage',uploadFileRes)
								j++
							console.log(j)
							if( j == _this.uploadCount){
								this.$emit('successImages',true)
							}
						}
					});
				}
			}
		}
	}
</script>
<style scoped>
	.ziliao{
		margin-top: 15upx;
		text-align: center;
		font-family: MicrosoftYaHei;
			font-size: 20upx;
			font-weight: normal;
			font-stretch: normal;
			line-height: 32upx;
			letter-spacing: 0upx;
			color: #c8c8c8;
	}
	button{
		background-color: #f05a23;
		width: 95%;
	}
	.upload {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}
	.uplode-file {
		margin: 10upx;
		width: 210upx;
		height: 210upx;
		position: relative;
	}
	.uploade-img {
		display: block;
		width: 210upx;
		height: 210upx;
	}
	.clear-one{
		position: absolute;
		top: -10rpx;
		right: 0;
	}
	.clear-one-icon{
		position: absolute;
		width: 20px;
		height: 20px;
		top: 0;
		right: 0;
		z-index: 9;
	}
	.uploader-input-box {
		position: relative;
		margin:10upx;
		width: 208upx;
		height: 208upx;
		border: 2upx solid #D9D9D9;
	}
	.uploader-input-box:before,
	.uploader-input-box:after {
		content: " ";
		position: absolute;
		top: 50%;
		left: 50%;
		-webkit-transform: translate(-50%, -50%);
		transform: translate(-50%, -50%);
		background-color: #D9D9D9;
	}
	.uploader-input-box:before {
		width: 4upx;
		height: 79upx;
	}
	.uploader-input-box:after {
		width: 79upx;
		height: 4upx;
	}
	.uploader-input-box:active {
		border-color: #999999;
	}
	.uploader-input-box:active:before,
	.uploader-input-box:active:after {
		background-color: #999999;
	}
	.uploader-input {
		position: absolute;
		z-index: 1;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		opacity: 0;
	}
	.uploader-icon{
		position: relative;
		margin:10upx;
		width: 208upx;
		height: 208upx;
	}
	.uploader-icon .image-cion{
		width: 100%;
		height: 100%;
	}
</style>

使用方式:

<easyupload
:text="text"   :dataList="imageList" 
uploadUrl="http://sunshine.createnetwork.cn/api/uploadimage" 
:types="category"
deleteUrl='http://sunshine.createnetwork.cn/api/uploadimage' 
:uploadCount="2" 
disable = true
@successImage="successImage" 
@successVideo="successvideo" 
@successImages="successImages"
class="sxzl"
></easyupload>
<easyupload
:text="text1"
:dataList="imageList" 
uploadUrl="http://sunshine.createnetwork.cn/api/uploadimage" 
:types="category"
deleteUrl='http://sunshine.createnetwork.cn/api/uploadimage' 
:uploadCount="2" 
:disable= 'Drivinglicense'
@successImage="successImage" 
@successVideo="successvideo" 
@successImages="successImages"
class="sxzl"
></easyupload>
<script>
// 導(dǎo)入注冊(cè)組件 
	import easyupload from "@/common/easy-upload.vue"
	export default {
		components:{
			easyupload
		},
		data() {
			return {
				tx_details:'',
				Drivinglicense: false,
				data:{
					phone:'',
					plate_number:'',
					type_id:'',
					id_card:[],
					driving_card:[],
					name:''
				},
				news:'',
				right_select_num:0,
				text:["身份證正面","身份證反面"],
				text1:["行駛證正頁(yè)","行駛證副頁(yè)"],
				imageList: [],
				category: 'image',
				car1:"請(qǐng)選擇車品牌",
			};
		},
		watch:{
		// 對(duì)數(shù)據(jù)進(jìn)行深度監(jiān)聽(tīng) 用于設(shè)置使用組件的順序 (多次使用組件并且需要考慮順序的情況下使用)
			'data.id_card':{
				handler(newName, oldName) {
				      // console.log('data.id_card', chnsg.length,oldName);            
					  ( oldName.length >= 2) ? (this.Drivinglicense = true) : (this.Drivinglicense = false)
					 console.log(this.Drivinglicense);
				    },
				    immediate: true,
				    deep: true,
			}
		},
		methods:{
			successImages(e){
				if(e){
					uni.showModal({
						content : '圖片上傳成功'
					})
				}
			},
			successImage(e){
				console.log(JSON.parse(e.data).data)
				this.Drivinglicense ?this.data.driving_card.push(JSON.parse(e.data).data[0]) : this.data.id_card.push(JSON.parse(e.data).data[0])
				console.log(this.data.id_card,this.data.driving_card);
			},
			successvideo(e){
				console.log(e)
			},
		}
	}
</script>

使用效果:
這是圖片預(yù)覽效果:

對(duì)圖片上傳數(shù)量的限制

圖片上傳成功提示

取消選擇選擇圖片提示

對(duì)于圖片上傳順序的限制

沒(méi)有選擇圖片的提示

選擇圖片后效果

到此這篇關(guān)于uniapp做小程序的圖片(視頻)上傳的組件封裝的文章就介紹到這了,更多相關(guān)uniapp小程序上傳的組件封裝內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 小程序hover-class點(diǎn)擊態(tài)效果實(shí)現(xiàn)

    小程序hover-class點(diǎn)擊態(tài)效果實(shí)現(xiàn)

    這篇文章主要介紹了小程序hover-class點(diǎn)擊態(tài)效果實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格功能實(shí)例

    利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格功能實(shí)例

    在現(xiàn)代網(wǎng)頁(yè)開(kāi)發(fā)中,表格是展示數(shù)據(jù)的常用元素,無(wú)論是數(shù)據(jù)統(tǒng)計(jì)、信息展示還是數(shù)據(jù)錄入,表格都能以整齊的格式展示出大量信息,這篇文章主要介紹了利用JavaScript實(shí)現(xiàn)動(dòng)態(tài)表格功能的相關(guān)資料,需要的朋友可以參考下
    2026-03-03
  • JS匿名函數(shù)內(nèi)部this指向問(wèn)題詳析

    JS匿名函數(shù)內(nèi)部this指向問(wèn)題詳析

    這篇文章主要給大家介紹了關(guān)于JS匿名函數(shù)內(nèi)部this指向的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 5款JavaScript代碼壓縮工具推薦

    5款JavaScript代碼壓縮工具推薦

    這篇文章主要介紹了5款JavaScript代碼壓縮工具推薦,代碼壓縮(也稱代碼最小化)是一個(gè)從源代碼中消除所有不必要的字符的過(guò)程,需要的朋友可以參考下
    2014-07-07
  • js百度地圖滾輪縮放所在點(diǎn)偏移問(wèn)題解決

    js百度地圖滾輪縮放所在點(diǎn)偏移問(wèn)題解決

    本文主要介紹了js百度地圖滾輪縮放所在點(diǎn)偏移問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • JS中關(guān)于filter()方法的使用及說(shuō)明

    JS中關(guān)于filter()方法的使用及說(shuō)明

    這篇文章主要介紹了JS中關(guān)于filter()方法的使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 原生js實(shí)現(xiàn)跨瀏覽器獲取鼠標(biāo)按鍵的值

    原生js實(shí)現(xiàn)跨瀏覽器獲取鼠標(biāo)按鍵的值

    e.button W3C是獲取鼠標(biāo)按鍵 0 表示左鍵 1表示中鍵 2表示右鍵 而IE瀏覽器則是 1表示左鍵 4表示中間 2表示右鍵 這里的IE瀏覽器主要是IE8以下的瀏覽器,感興趣的朋友可以參考下哈
    2013-04-04
  • 利用JavaScript在網(wǎng)頁(yè)實(shí)現(xiàn)八數(shù)碼啟發(fā)式A*算法動(dòng)畫(huà)效果

    利用JavaScript在網(wǎng)頁(yè)實(shí)現(xiàn)八數(shù)碼啟發(fā)式A*算法動(dòng)畫(huà)效果

    這篇文章主要介紹了利用JavaScript在網(wǎng)頁(yè)實(shí)現(xiàn)八數(shù)碼啟發(fā)式A*算法動(dòng)畫(huà)效果,需要的朋友可以參考下
    2017-04-04
  • 如何將HTML字符轉(zhuǎn)換為DOM節(jié)點(diǎn)并動(dòng)態(tài)添加到文檔中詳解

    如何將HTML字符轉(zhuǎn)換為DOM節(jié)點(diǎn)并動(dòng)態(tài)添加到文檔中詳解

    這篇文章主要給大家介紹了關(guān)于如何將HTML字符轉(zhuǎn)換為DOM節(jié)點(diǎn)并動(dòng)態(tài)添加到文檔中的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧
    2018-08-08
  • 教你如何通過(guò)JavaScript讀取元素的樣式

    教你如何通過(guò)JavaScript讀取元素的樣式

    這篇文章主要給大家介紹了關(guān)于如何通過(guò)JavaScript讀取元素的樣式,文中通過(guò)實(shí)例代碼以及圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01

最新評(píng)論

太保市| 大同市| 黄平县| 治县。| 绵竹市| 兴隆县| 双牌县| 阜康市| 邓州市| 雅江县| 肇州县| 五原县| 泰宁县| 长沙县| 博罗县| 横山县| 界首市| 含山县| 闽清县| 岳西县| 岑溪市| 寿光市| 栾川县| 尉氏县| 景德镇市| 上栗县| 堆龙德庆县| 英吉沙县| 台江县| 小金县| 浦北县| 安福县| 邯郸县| 什邡市| 崇明县| 伊吾县| 永福县| 勃利县| 古浪县| 商丘市| 阿巴嘎旗|