import { URL } from "./requestUrl.js";

// 上传图片方法（单张/多张上传）

const uploadImage = (params) => {
	return new Promise((presolve, preject) => {
		uni.showLoading()
		let uploads = [];
		(params.tempFilePaths).forEach((item, i) => {
			uploads[i] = new Promise((resolve) => {
				// console.log({URL});
				uni.uploadFile({
				  url: `http://${URL}/file/uploadImg`,
				  fileType: 'image',
					filePath: item,
					name: 'file',
					formData: {
						'Filedata': item
					},
					header:{
						token: uni.getStorageSync('token')
          },
					success(res) {
						// console.log('1122',JSON.parse(res.data));
						let data = JSON.parse(res.data);
						// console.log({data});
						if(data.code == 'SUCCESS'){
							resolve(data.data)
						}else{
							resolve('')
						}
					},
					fail(err) {
						console.log('上传图片失败:', err)
						uni.hideLoading()
					}
				})
			})
		})

    Promise.all(uploads).then(res => {
			//图片上传完成
			presolve(res)
			uni.hideLoading()
		}).catch(err => {
			preject(err)
			uni.hideLoading()
			uni.showToast({
				title: '上传失败请重试',
        icon: 'none',
        duration: 1000
			})
		})
	})
}
export default uploadImage;