import {
	getOssUploadParams
} from "@/api/api.js"

//预览图片
export const lookImg = (e) => {
	uni.previewImage({
		urls: e
	});
}

//拨打电话
export const call = (e) => uni.makePhoneCall({
	phoneNumber: e
});

// 剪贴板
export const copy = (e) => uni.setClipboardData({
	data: e,
	success: function() {
		console.log('success');
	}
});;

//获取超链接参数
export const urlQuery = (e) => {
	let search = e
	search = search.split('?')[1];
	const pairs = search ? search.split('&') : [];
	const query = {};
	for (let i = 0; i < pairs.length; ++i) {
		const [key, value] = pairs[i].split('=');
		query[key] = query[key] || decodeURIComponent(value);
	}
	return query
};




export const uploadFiles = (urls) => {
	return new Promise(async (resolve, reject) => {
		let res = await getOssUploadParams()
		const fd = {
			policy: res.policy,
			OSSAccessKeyId: res.accessId,
			success_action_status: 200,
			signature: res.signature,
			key: res.dir + Date.now() + Math.floor(100000 + Math.random() *
					900000) + '.' +
				urls.substring(urls.lastIndexOf(".") + 1),
		};
		uni.uploadFile({
			url: res.host, // Example, not a real endpoint
			filePath: urls,
			header: {
				"content-type": 'multipart/form-data'
			},
			name: 'file',
			formData: fd,
			success: (_res) => {
				resolve(fd.key);
			},
			fail: (err) => {
				console.log(err);
				reject(err);
			}
		});
		console.log();
	});
}