import config from '../config.js';
const getDomain = (uri, method, type) => {
	let serverUrl = config.serverUrl;
	if (type && type == 'file') return serverUrl + `/files` + uri;
	else return serverUrl + '/notarization/api/' + uri;
};

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 requestApp = async (uri, method = 'GET', data, type) => {
	let baseUrl = `https://120.48.146.1${uri}`;
	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 = `http://192.168.1.197${uri}`;
	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);
			},
		});
	});
};