uniapp uview upload组件二次封装

js yekong 364℃

uniapp使用uview开发项目时 upload 上传组件很方便,但是如果同一个页面上传组件多了容易混乱,每次使用都需要重复很多代码,有没有办法复用一下呢

使用

<uploadimg :maxCount="3" :widht="220" :height="220" @getdata="getimg">
						<view class="upload-btn">
							<u-icon name="camera-fill" color="#ccc" size="40"></u-icon>
							<view class="upload-tips">
								{{$t('add.photos')}}
							</view>
						</view>
</uploadimg>

封装代码

<template>
	<u-upload :width='width+"rpx"' :height='height+"rpx"' :fileList="fileList1" @afterRead="afterRead($event)"
		@delete="deletePic" name="1" :maxCount="maxCount">
		<slot></slot>
	</u-upload>
</template>

<script>
	import configs from '@/config/config.js'
	export default {
		data() {
			return {
				fileList1: [],
				imgList: []
			}
		},
		props: {
			width: {
				type: Number,
				default () {
					return 200;
				}
			},
			height: {
				type: Number,
				default () {
					return 200;
				}
			},
			maxCount: {
				type: Number,
				default () {
					return 10;
				}
			},
			list: {
				type: Array,
				default () {
					return [];
				}
			}
		},
		methods: {
			getlist() {
				// this.fileList1()
				console.log(this.fileList1)
				var list = []
				this.fileList1.forEach((type) => {
					list.push(type.url.path)
				});
				this.$emit('getdata', list)
			},
			clear() {
				this[`fileList${event.name}`] = []
				this.getlist()
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.imgList.splice(event.index, 1)
				this.getlist()
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].thumb)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				this.getlist()
			},
			uploadFilePromise(url) {
				var that = this;
				return new Promise((resolve, reject) => {
					if (uni.getStorageSync('token')) {
						let a = uni.uploadFile({
							url: configs.upload, // 仅为示例,非真实的接口地址
							filePath: url,
							name: 'image',
							header: {
								'x-token':  uni.getStorageSync('token')
							},
							success: (res) => {
								console.log(res)
								console.log(JSON.parse(res.data))
								console.log(1234)
								if (JSON.parse(res.data).code == 401) {
									uni.redirectTo({
										url: '/pages/login/login'
									})
								}
								setTimeout(() => {
									resolve(JSON.parse(res.data).data)
								}, 1000)
							},
							fail: (res) => {
								if (res.code == 401) {
									uni.redirectTo({
										url: '/pages/login/login'
									})
								}
							}
						});
					} else {
						uni.redirectTo({
							url: '/pages/login/login'
						})
					}

				})
			},

		}
	}
</script>

<style>
</style>
喜欢 (2)