Browse Source

swiper点击传递item

tongshangming 2 years ago
parent
commit
b929474b1b
2 changed files with 135 additions and 133 deletions
  1. 2 2
      components/fs-swiper/fs-swiper.vue
  2. 133 131
      utils/utils.js

+ 2 - 2
components/fs-swiper/fs-swiper.vue

@@ -18,7 +18,7 @@
 		:style="{ height: height }"
 	>
 		<template v-if="mode === 'card'">
-			<swiper-item class="fs-swiper-item-box" v-for="(item, index) in list" :key="index" @click="handleClick">
+			<swiper-item class="fs-swiper-item-box" v-for="(item, index) in list" :key="index" @click="handleClick(item)">
 				<view class="fs-swiper-item" :class="{ 'card-cur': index === curIndex }">
 					<fs-avatar shape="square" radius width="100%" height="100%" :src="item[keyMap.src]"></fs-avatar>
 					<view class="fs-swiper-item-text line1" v-if="showTitle">{{ item[keyMap.title] }}</view>
@@ -26,7 +26,7 @@
 			</swiper-item>
 		</template>
 		<template v-else>
-			<swiper-item class="fs-swiper-item-box" v-for="(item, index) in list" :key="index" @click="handleClick">
+			<swiper-item class="fs-swiper-item-box" v-for="(item, index) in list" :key="index" @click="handleClick(item)">
 				<view class="fs-swiper-item">
 					<fs-avatar shape="square" width="100%" height="100%" :src="item[keyMap.src]"></fs-avatar>
 					<view class="fs-swiper-item-text line1" v-if="showTitle">{{ item[keyMap.title] }}</view>

+ 133 - 131
utils/utils.js

@@ -1,140 +1,142 @@
-import config from './config'
-
-export default {
-	isHttp(path) {
-		return path.startsWith('http') || path.startsWith('https')
-	},
-	escapeHTML(a) {
-		a = "" + a;  
-		return a.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;")
-	},  
-	/** 
-	 * @function unescapeHTML 还原html脚本 < > & " ' 
-	 * @param a - 字符串 
-	 */  
-	unescapeHTML(a){  
-		a = "" + a;  
-		return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(/&apos;/g, "'")
-	},
-	uuid(len = 16) {
-		const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
-		const uuid = []
-	
-		if (len) {
-			// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
-			for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * chars.length]
-		} else {
-			let r
-			// rfc4122标准要求返回的uuid中,某些位为固定的字符
-			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
-			uuid[14] = '4'
-	
-			for (let i = 0; i < 36; i++) {
-				if (!uuid[i]) {
-					r = 0 | Math.random() * 16
-					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
-				}
-			}
-		}
-		
-		return uuid.join('')
-	},
+import config from './config'
+
+export default {
+	isHttp(path) {
+		return path.startsWith('http') || path.startsWith('https')
+	},
+	escapeHTML(a) {
+		a = "" + a;
+		return a.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(
+			/'/g, "&apos;")
+	},
+	/** 
+	 * @function unescapeHTML 还原html脚本 < > & " ' 
+	 * @param a - 字符串 
+	 */
+	unescapeHTML(a) {
+		a = "" + a;
+		return a.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "&").replace(/&quot;/g, '"').replace(
+			/&apos;/g, "'")
+	},
+	uuid(len = 16) {
+		const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
+		const uuid = []
+
+		if (len) {
+			// 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
+			for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * chars.length]
+		} else {
+			let r
+			// rfc4122标准要求返回的uuid中,某些位为固定的字符
+			uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
+			uuid[14] = '4'
+
+			for (let i = 0; i < 36; i++) {
+				if (!uuid[i]) {
+					r = 0 | Math.random() * 16
+					uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r]
+				}
+			}
+		}
+
+		return uuid.join('')
+	},
 	upload(data) {
 		return uni.uploadFile({
 			url: this.isHttp(data.url) ? data.url : config.apiBaseUrl + data.url,
 			name: data.name || 'file',
-			formData: {
+			formData: {
 				...data.formData,
-				token: uni.getStorageSync('token')
-			},
+				token: uni.getStorageSync('token')
+			},
 			header: data.header || {},
 			filePath: data.filePath
 		}).then(res => {
 			return JSON.parse(res)
-		})
-	},
-	uploadCloud(data) {
-		return uniCloud.uploadFile(data).then(res => {
-			return res
-		})
-	},
-	chooseAndUpload(methods, chooseParam, uploadParam, isCloud) {
-		const uploadArr = []
-		return this[methods](chooseParam).then(file => {
-			file.filePaths.forEach((filePath, index) => {
-				if (isCloud) {
-					const fileArr = filePath.split('.')
-					// 只在h5可用
-					const fileType = file.tempFiles[index].type || 'image/png'
-					const suffix = fileArr.length >= 2 ? fileArr[fileArr.length - 1] : fileType.split('/')[1]
-					
-					uploadArr.push(this.uploadCloud({
-						filePath,
-						cloudPath: this.uuid() + '.' + suffix,
-						fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + suffix
-					}).then(res => {
-						if (res.success) {
-							return {
-								filePath: res.fileID
-							}
-						} else {
-							uni.showToast({
-								title: res.msg,
-								icon: 'none'
-							})
-						}
-					}))
-				} else{
-					uploadArr.push(this.upload({
-						...uploadParam,
-						filePath
-					}).then(res => {
-						if (res.code === 200 || res.success) {
-							return res.data
-						} else {
-							uni.showToast({
-								title: res.msg,
-								icon: 'none'
-							})
-						}
-					}))
-				}
-			})
-			uni.showLoading({
-				title: '上传中...'
-			})
-			return Promise.all(uploadArr).then(res => {
-				uni.hideLoading()
-				return res.filter(item => item)
-			})
-		})
-	},
-	chooseImage(data) {
-		return uni.chooseImage({
-			count: 1,
-			...data
-		}).then(res => {
-			console.log(res);
-			return {
-				filePaths: res.tempFilePaths,
-				tempFiles: res.tempFiles
-			}
-		})
-	},
-	chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
-		return this.chooseAndUpload('chooseImage',chooseParam, uploadParam, isCloud)
-	},
-	chooseVideo(data) {
-		return uni.chooseVideo({
-			...data
-		}).then(res => {
-			return {
-				filePaths: [res.tempFilePath],
-				tempFiles: [res.tempFile]
-			}
-		})
-	},
-	chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
-		return this.chooseAndUpload('chooseVideo',chooseParam, uploadParam, isCloud)
-	},
-}
+		})
+	},
+	uploadCloud(data) {
+		return uniCloud.uploadFile(data).then(res => {
+			return res
+		})
+	},
+	chooseAndUpload(methods, chooseParam, uploadParam, isCloud) {
+		const uploadArr = []
+		return this[methods](chooseParam).then(file => {
+			file.filePaths.forEach((filePath, index) => {
+				if (isCloud) {
+					const fileArr = filePath.split('.')
+					// 只在h5可用
+					const fileType = file.tempFiles[index].type || 'image/png'
+					const suffix = fileArr.length >= 2 ? fileArr[fileArr.length - 1] : fileType.split(
+						'/')[1]
+
+					uploadArr.push(this.uploadCloud({
+						filePath,
+						cloudPath: this.uuid() + '.' + suffix,
+						fileType: (methods === 'chooseImage' ? 'image/' : 'video/') + suffix
+					}).then(res => {
+						if (res.success) {
+							return {
+								filePath: res.fileID
+							}
+						} else {
+							uni.showToast({
+								title: res.msg,
+								icon: 'none'
+							})
+						}
+					}))
+				} else {
+					uploadArr.push(this.upload({
+						...uploadParam,
+						filePath
+					}).then(res => {
+						if (res.code === 200 || res.success) {
+							return res.data
+						} else {
+							uni.showToast({
+								title: res.msg,
+								icon: 'none'
+							})
+						}
+					}))
+				}
+			})
+			uni.showLoading({
+				title: '上传中...'
+			})
+			return Promise.all(uploadArr).then(res => {
+				uni.hideLoading()
+				return res.filter(item => item)
+			})
+		})
+	},
+	chooseImage(data) {
+		return uni.chooseImage({
+			count: 1,
+			...data
+		}).then(res => {
+			return {
+				filePaths: res.tempFilePaths,
+				tempFiles: res.tempFiles
+			}
+		})
+	},
+	chooseAndUploadImage(chooseParam, uploadParam, isCloud) {
+		return this.chooseAndUpload('chooseImage', chooseParam, uploadParam, isCloud)
+	},
+	chooseVideo(data) {
+		return uni.chooseVideo({
+			...data
+		}).then(res => {
+			return {
+				filePaths: [res.tempFilePath],
+				tempFiles: [res.tempFile]
+			}
+		})
+	},
+	chooseAndUploadVideo(chooseParam, uploadParam, isCloud) {
+		return this.chooseAndUpload('chooseVideo', chooseParam, uploadParam, isCloud)
+	},
+}