uniapp uview upload上传图片后获取图片拼接字符串

uniapp yekong

uniapp 开发app时,需要上传图片后,将图片拼接成一个字符串。穿给接口。

<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'
	import {
		getFileName
	} from '../utils/utils.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)
				});
				this.$emit('getdata', list.join(","))
			},
			// 删除图片
			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;
				console.log(url)
				console.log(uni.getStorageSync('token'))
				console.log(getFileName(url))
				return new Promise((resolve, reject) => {
					if (uni.getStorageSync('token')) {
						let a = uni.uploadFile({
							url: configs.upload, // 仅为示例,非真实的接口地址
							filePath: url,
							name: 'image',
							header: {
								Authorization: 'Bearer ' + uni.getStorageSync('token')
							},
							formData: {
								upload_name: 'image'
							},
							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>

使用

			<upload @getdata="getimg"></upload>
						getimg(e) {
				console.log(e)
				this.data.pic = e;
			},
喜欢