import uploadFile from "@/plugins/upload.js";
import {
	checkPic
} from "@/utils/checkPic.js"

export function uploadImg(uploadUrl, photo, tempImgs, baseUrl, callback) {
	uni.chooseImage({
		count: 3,
		success: async (img) => {
			let bool = await checkPic(img.tempFiles[0]);
			if (bool) {
				uploadFile(uploadUrl, img.tempFilePaths[0]).then((res) => {
					let fileName = JSON.parse(res).fileName;
					if (photo) {
						photo += "," + fileName
					} else {
						photo = fileName
					}
					tempImgs.push({
						path: baseUrl + fileName
					});
					// 调用回调函数  
					callback && callback(null, {
						photo,
						tempImgs
					});
				})
			}
		},
		fail: (error) => {}
	});
}

export function previewImg(imgs,index=0) {
	let urls = [];
	urls = imgs.map(img => img.path);
	uni.previewImage({
		urls: urls,
		current:index
	})
}
export function removeImg(index,path,photo,tempImgs,baseUrl,callback) {
	tempImgs.splice(index, 1);
	let newImgs = tempImgs.filter(fileName => fileName.path !== path);
	newImgs = newImgs.map(img => img.path.replace(baseUrl, ''))
	newImgs = newImgs.join(",");
	photo = newImgs;
	// 调用回调函数
	callback && callback(null, {
		photo
	});
}