import config from '../config.js';
const getDomain = (uri, method, type) => {
	let url = config.serverUrl;
	if (type && type == 'file') return url + `/files` + uri;
	if (type && type == 'integral') {
		let integral = `/point/two/zr/v1/api` + uri;
		if (config.wx_dev) integral = url + `${config.wx_dev}` + integral;
		else integral = url + integral;
		return integral
	}
	if (type && type == 'group') {
		let group = `/point/two/group/v1/api` + uri;
		if (config.wx_dev) group = url + `${config.wx_dev}` + group;
		else group = url + group;
		return group
	}
	if (type && type == 'chat') {
		let chat = `/point/two/chat/v1/api` + uri;
		if (config.wx_dev) chat = url + `${config.wx_dev}` + chat;
		else chat = url + chat;
		return chat
	}
	if (uri.startsWith('/wechat/api')) return url + uri;
	let general = `/point/two/v1/api` + uri;
	if (config.wx_dev) general = url + `${config.wx_dev}` + general;
	else general = url + general;
	return general;
};
// 获取token
const getToken = () => {
	try {
		const value = uni.getStorageSync('token');
		if (value) return value;
	} catch {}
};
// 获取请求key
const getRequestKey = (views) => {
	let baseUrl = getDomain('/util/crk');
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl,
			method: 'POST',
			header: {
				views
			},
			success: (res) => {
				if (res.data && res.data.data) resolve(res.data.data);
				else reject();
			},
			fail: (err) => {},
		});
	});
};
export const requestBase = async (uri, method = 'GET', data, type) => {
	let baseUrl = getDomain(uri, method, type);
	// let token = getToken();
	let header = {};
	let views = JSON.stringify({
		uri,
		method
	});
	views = encodeURIComponent(views);
	const token = uni.getStorageSync('token');
	if (token) header.token = token;
	if (method === 'GET') {
		return new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl,
				method: method || 'GET',
				data: data || {},
				header,
				success: (res) => {
					resolve(res.data);
				},
				fail: (err) => {
					uni.showToast({
						title: '请求接口失败',
						icon: 'fail',
					});
					reject(err.data);
				},
			});
		});
	} else if (method === 'POST' || method === 'Delete') {
		header.views = views;
		return new Promise((resolve, reject) => {
			// rk配置
			let rkbaseUrl = getDomain('/util/crk');
			let pages = getCurrentPages();
			let route = pages[pages.length - 1].$page.fullPath;
			uni.request({
				url: rkbaseUrl,
				method: 'POST',
				header: {
					views,
					Referer: route,
				},
				success: (rkres) => {
					if (rkres.data && rkres.data.data) header.rk = rkres.data.data;
					header.Referer = route;
					uni.request({
						url: baseUrl,
						method,
						data: data || {},
						header,
						success: (res) => {
							resolve(res.data);
						},
						fail: (err) => {
							uni.showToast({
								title: '请求接口失败',
								icon: 'fail',
							});
							reject(err.data);
						},
					});
				},
			});
			// uni.request({
			// 	url: baseUrl,
			// 	method: method || 'GET',
			// 	data: data || {},
			// 	header,
			// 	success: (res) => {
			// 		resolve(res.data);
			// 	},
			// 	fail: (err) => {
			// 		uni.showToast({
			// 			title: '请求接口失败',
			// 			icon: 'fail',
			// 		});
			// 		reject(err.data);
			// 	},
			// });
		});
	}
};
export const requestFile = (uri, method, data, type) => {
	let baseUrl = getDomain(uri, method, type);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: baseUrl,
			filePath: data,
			name: method || 'file',
			formData: {},
			success: (res) => {
				let data = JSON.parse(res.data);
				if (data.errcode != '0') {
					return uni.showToast({
						title: data.errmsg,
						icon: 'error',
					});
				}
				resolve(data);
			},
			fail: (err) => {
				let data = JSON.parse(err.data);
				uni.showToast({
					title: '请求接口失败',
					icon: 'fail',
				});
				reject(data);
			},
		});
	});
};
