import config from '../config.js';
const getDomain = (uri, method, type) => {
	let Jcyjdtglpturl = config.jcyjdtglpt;
	let FreeLabelurl = config.freeLabel;
	let Fileurl = config.serverFile;
	if (type && type == 'file') return Fileurl + `/files` + uri;
	else if (type && type == 'jcyjdtglpt') return Jcyjdtglpturl + `/jcyjdtglpt/v1/api` + uri;
	else if (type && type == 'freeLabel') return FreeLabelurl + `/freeLabel/api` + uri;
};
// 获取token
const getToken = () => {
	try {
		const value = uni.getStorageSync('token');
		if (value) return value;
	} catch {}
};
export const requestBase = async (uri, method = 'GET', data, type) => {
	let baseUrl = getDomain(uri, method, type);
	let header = {};
	const token = uni.getStorageSync('token');
	if (token) header.token = token;
	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);
			},
		});
	});
};
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);
			},
		});
	});
};