// 线上地址
const BASE_URL = "https://chtech.ncjti.edu.cn/campusclock"
// 本地地址
// const BASE_URL = "https://www.web-server.top"
export const myRequest_clockIn = (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"),
				'platform': 2,
				'Accept-Language': 'zh-CN,zh;q=0.9'
			},
			success: (res) => {
				uni.hideLoading();
				resolve(res.data)
				if (res.data.code != 200) {
					if (res.data.code == 498) {
						uni.request({
							url: 'https://chtech.ncjti.edu.cn/campusclock/attendance/api/home/permissions',
							header: {
								'content-type': 'application/x-www-form-urlencoded',
								'Authorization': uni.getStorageSync("token"),
								'platform': 2
							},
							success: (res) => {
								if (res.data.code == 200) {
									uni.removeStorageSync("manager")
									uni.removeStorageSync("sub-administrator")
									uni.removeStorageSync("userInfo")
									uni.showModal({
										title: "提示",
										content: "用户权限变更，请重新授权",
										showCancel: false,
										success: function(res) {
											if (res.confirm) {
												uni.reLaunch({
													url: "/pagesClockIn/index/index",
												});
											}
										},
									});
								} else {
									uni.showToast({
										title: res.data.message,
										icon: "error",
										mask: true,
									});
								}
							}
						});
					} else if (res.data.code == 401 || res.data.code == 499) {
						uni.showModal({
							title: "提示",
							content: res.data.message,
							showCancel: false,
							success: function(res) {
								if (res.confirm) {
									uni.removeStorageSync("userInfo")
									uni.reLaunch({
										url: "/pagesClockIn/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)
			}
		})
	})
}
