const BASE_URL = "/alumnus/alumni_api/alumni/api"

export const myRequest = (options) => {
	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	// 当前时间
	let timestamp = new Date().getTime();
	// 将时间戳添加到URL中
	options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + 'timestamp=' + timestamp;

	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || "GET",
			data: options.data || {},
			header: options.header || {
				'token': uni.getStorageSync('token') || ''
			},
			success: (res) => {
				// console.log(res);
				uni.hideLoading();
				if (res.data.code == 200) {
					resolve(res.data)
				} else if (res.data.code == 558 || res.data.code == 559) {
					uni.showModal({
						title: "提示",
						content: "token失效，请重新授权",
						showCancel: false,
						success: (res) => {
							if (res.confirm) {
								uni.clearStorageSync()
								uni.reLaunch({
									url: "/pages/index/index"
								})
							}
						}
					})
					reject(res.data)
				} else {
					uni.showToast({
						title: res.data.message || "请求失败",
						icon: "none"
					})
					reject(res.data)
				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: "请求数据失败",
					icon: "none"
				})
				reject(err)
			}
		})
	})
}