import config from "../common/config";
const getDomain = (uri, type) => {
	const {
		serverUrl,
		wechatUrl
	} = config;
	// 自定义
	if (uri.includes("http")) return uri;
	// 微信
	if (uri.startsWith("/wechat/api")) return wechatUrl + uri;
	// 地址前缀
	else if (type) return serverUrl + `/projectadmin/${type}/api/` + uri;
	// 常规
	return serverUrl + "/projectadmin/api/" + uri;
};
// 常规请求
export const requestBase = async (uri, method = "GET", data, type) => {
	// 请求地址
	let baseUrl = getDomain(uri, type);
	// 请求头
	let header = {};
	// 用户信息
	let token = "";
	if (token) header.token = token;
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl,
			method,
			data,
			header,
			success: (res) => toResolve(resolve, res),
			fail: (err) => {
				console.log(err);
			},
		});
	});
};
// 图片请求
export const requestFile = (uri, method, data, type) => {
	// 请求地址
	let baseUrl = getDomain(uri, type);
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: `https://broadcast.waityou24.cn/${uri}`,
			filePath: data,
			name: 'file',
			formData: {},
			success: (res) => toResolve(resolve, res),
			error: (e) => toReject(reject, e),
		})
	});
};
const toResolve = (resolve, result) => {
	if (result.statusCode === 200) resolve(result.data);
};
const toReject = (reject, result) => {
	reject(result);
};
