import uploader from '@/plugins/uploader/index.js'
const globalUrl = process.uniEnv || {}
export default {
	name: 'upload',
	components: {},
	data() {
		return {
			imgList:[]
		};
	},
	created() {
		console.log(uploader,'uploader')
		// this.initQiniu()
	},
	methods: {	
		async uploadPath(type){
			// 可以直接上传path
			const self = this
			let imgList = []
			let options = {
				files:[] // 文件路径
			}
			if(type==='qiniu'){
				let res = await uploader.qnFileUpload(options)
				return
			}
			let res = await uploader.urlFileUpload(options)
		},
		async chooseImage(type){
			const self = this
			let imgList = []
			if(type==='qiniu'){
				// 七牛上传
				let options = {
					onProgressUpdate(res){
						let item = {
							path:res.path, // 图片临时缓存路径
							key:'', // 图片上传成功后相对路径
							src:'', // 访问图片的绝对路径
							progress:res.progress // 图片上传进度
						}
						if(res.fileIndex<imgList.length){
							imgList[res.fileIndex].progress = res.progress
						}else{
							imgList.push(item)
						}
						imgList[res.fileIndex]
						self.imgList = imgList
						// 多文件上传每个文件进度条
						console.log(res,'res---onProgressUpdate')
					},
					onEachUpdate(res){
						// 多文件上传每个文件上传成功触发
						if(imgList.length>res.fileIndex){
							imgList[res.fileIndex].key = res.url
							imgList[res.fileIndex].src = uploader.qiniuTokenObj.visitPrefix + res.url
						}
						self.imgList = imgList
						console.log(res,'res---onEachUpdate')
					}
				}
				
				// 不需要进度条等可直接获取
				let res = await uploader.qnImgUpload(options)
				console.log(self.imgList,'self.imgList')
				// console.log(uploader,uploader.qiniuTokenObj,'qiniuTokenObj')
				// let imgList = res.map(item=>{
				// 	return {
				// 		key:item,
				// 		src:uploader.qiniuTokenObj.visitPrefix + item
				// 	}
				// })
				// this.imgList = imgList
				return
			}
			// 本地服务上传
			let res = await uploader.urlUpload()
			this.imgList = res
		},
		perviewImg(index) {
			let urls = []
			this.imgList.map(item => {
				urls.push(item.src)
			})
			uni.previewImage({
				current: index,
				urls: urls
			});
		},
		removeImg(index) {
			this.imgList.splice(index, 1)
			// this.$emit('image-change', this.imgList) //将图片回传父页面
		},
	
	}
}
