// 线上地址
// const BASE_URL = "https://chtech.ncjti.edu.cn/jiaofei/jiaofei-api/tuitionpayment"
// 本地地址
const BASE_URL = "https://www.web-server.top"
export const myRequest = (options) => {
	uni.showLoading({
		title: "加载中",
		mask: true,
	});
	return new Promise((resolve, reject) => {
		uni.request({
			url: BASE_URL + options.url,
			method: options.method || "GET",
			data: options.data || {},
			header: options.header || {
				'content-type': 'application/x-www-form-urlencoded',
				'Authorization': uni.getStorageSync("token") ||
					'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1MDQ2LCJ1c2VyX3V1aWQiOjEzNDc3NzE0NzM1NTY5NzE1MiwibmJmIjoxNjcxMTU1ODQzfQ.u4-N762Ijfb9RkuuFOFkeMiJQI9uCi0IaheJlGwi5Ms'
			},
			success: (res) => {
				uni.hideLoading();
				resolve(res.data)
				if (res.data.code != 200) {
					if (res.data.code == 498) {
						uni.request({
							url: 'https://www.web-server.top/attendance/api/home/permissions',
							header: {
								'content-type': 'application/x-www-form-urlencoded',
								'Authorization': uni.getStorageSync("token") ||
									'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjo1MDQ2LCJ1c2VyX3V1aWQiOjEzNDc3NzE0NzM1NTY5NzE1MiwibmJmIjoxNjcxMTU1ODQzfQ.u4-N762Ijfb9RkuuFOFkeMiJQI9uCi0IaheJlGwi5Ms'
							},
							success: (res) => {
								if (res.data.code == 200) {
									uni.removeStorageSync("manager")
									uni.removeStorageSync("sub-administrator")
									uni.showModal({
										title: "提示",
										content: "用户权限变更，请重新授权",
										showCancel: false,
										success: function(res) {
											if (res.confirm) {
												uni.reLaunch({
													url: "/pages/home/home",
												});
											}
										},
									});
								} else {
									uni.showToast({
										title: res.data.message,
										icon: "error",
										mask: true,
									});
								}
							}
						});
					} else if (res.data.code == 401) {
						uni.showModal({
							title: "提示",
							content: res.data.message,
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									uni.reLaunch({
										url: "/pages/index/index",
									});
								}
							},
						});
					} else {
						uni.showToast({
							title: res.data.message,
							icon: "error",
							mask: true,
						});
					}
				}
			},
			fail: (err) => {
				uni.hideLoading();
				uni.showToast({
					title: "请求数据失败",
					icon: "none",
					mask: true
				})
				reject(err)
			}
		})
	})
}
