//请求Url数据
function requestData(url, params, method) {
	return new Promise((resolve, reject) => {
		uni.request({
			url: url,
			data: params,
			method: method ? "POST" : "GET",
			header: {
				//application/x-www-form-urlencoded text/html;charset=UTF-8;application/json; accessToken backend_api_context Bearer 
				"Content-Type": "application/x-www-form-urlencoded",
				"token": uni.getStorageSync('token') ? `${uni.getStorageSync('token')}` : '',
				"platform": uni.getStorageSync('platform') || uni.getStorageSync('platform') === 0 ? `${uni.getStorageSync('platform')}` : 2
			},
			success: function(res) {
				console.log("GET URL:", url, ". Params:", params);
				if (res.statusCode == 200) {
					resolve(res.data);
				} else {
					console.error("Error:", res)
					reject("error")
				}
			},
			fail: function(e) {
				console.error("Failed:", e)
				reject(e);
			}
		})
	});
}

function reqUpload(url, params,filePath) {
	console.log("reqUpload",filePath,params);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: url, 
			header:{
				//application/x-www-form-urlencoded text/html;charset=UTF-8;application/json; accessToken backend_api_context Bearer 
				// "Content-Type": "multipart/form-data",
				"token": uni.getStorageSync('token') ? `${uni.getStorageSync('token')}` : '',
				"platform": uni.getStorageSync('platform') || uni.getStorageSync('platform') === 0 ? `${uni.getStorageSync('platform')}` : 2
			},
			filePath: filePath,
			name: 'file',
			formData: params,
			method:"POST",
			success: (uploadFileRes) => {
				console.log(filePath,"上传数据后",uploadFileRes);
				if (uploadFileRes.statusCode == 200) {
					resolve(JSON.parse(uploadFileRes.data));
				} else {
					console.error("Error:", uploadFileRes)
					reject("error")
				}
			},
			fail: function(e) {
				console.error("Failed:", e)
				reject(e);
			}
		})
		
	});
}


function reqUploadFiles(url, params,files) {
	console.log("reqUploadFiles",files,params);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: url, 
			header:{
				"token": uni.getStorageSync('token') ? `${uni.getStorageSync('token')}` : '',
				"platform": uni.getStorageSync('platform') || uni.getStorageSync('platform') === 0 ? `${uni.getStorageSync('platform')}` : 2
			},
			files: files,
			name: 'files',
			formData: params,
			method:"POST",
			success: (uploadFileRes) => {
				console.log(files,"上传数据后",uploadFileRes);
				if (uploadFileRes.statusCode == 200) {
					resolve(JSON.parse(uploadFileRes.data));
				} else {
					console.error("Error:", uploadFileRes)
					reject("error")
				}
			},
			fail: function(e) {
				console.error("Failed:", e)
				reject(e);
			}
		})
		
	});
}

export default {
	requestData,
	reqUpload,
	reqUploadFiles
}
