import LoginVue from "../pages/login/Login.vue";
import signMd5Utils from "./signMd5Utils";

// export const sysIp = '127.0.0.1'
export const sysIp ='119.188.66.42'
// export const sysIp ='202.194.98.212'
export const sysPort ='8081'
// export const sysPort = '8080'

//minio弃用
export const minioIP = '202.194.98.212'
export const minioPort = '9000'


// 获取用户考试详情
export function getExamDetail(data) {
	return request("/exam-boot/exam/examPaperDetail", 'GET', data)
}

// 创建用户考试
export function getExamCreateExam(data) {
	return request("/exam-boot/exam/createExamPapers", 'GET', data)
}

// 检查有没有正在进行的考试
export function getExamListExamIn(data) {
	return request("/exam-boot/exam/listExamPapers", 'GET', data)
}

// 获取所有考试详情列表
export function getExamOnlineExam(data) {
	return request("/exam-boot/exam/onlineExamPapers", 'GET', data)
}

// 根据用户考试id获取考试信息
export function getExamQueryById(data) {
	return request("/exam-boot/exam/queryById", 'GET', data)
}

// 登录接口
export function getSysLogin(data) {
	return request("/exam-boot/sys/login", 'POST', data)
}
// 缓存答案
export function cacheExamAnswer(data) {
	return request("/exam-boot/exam/cacheExamPaperAnswer", 'POST', data)
}
// 获取缓存答案
export function getCacheAnswer(data) {
	return request("/exam-boot/exam/getCacheExamPaperAnswer", 'GET', data)
}
// 提交当前试卷
export function submitExamPaper(data) {
	return request("/exam-boot/exam/submitExamPaper", 'POST', data)
}


// 访问后端接口
export function request(url, requestType, requestData) {
	if (url !== "/exam-boot/exam/cacheExamPaperAnswer") {
		uni.showLoading({
			title: '加载中...',
			mask: true,
		});
	}

	let request;
	if (url === "/exam-boot/sys/login") {

		sessionStorage.setItem('sign', signMd5Utils.getSign(url, requestData));
		request = uni.request({
			url: "http://" + sysIp + ":" + sysPort + url,
			method: requestType,
			data: requestData
		}).finally(() => {
			uni.hideLoading();
			uni.stopPullDownRefresh()
		})
	} else {
		request = uni.request({
			url: "http://" + sysIp + ":" + sysPort + url,
			method: requestType,
			header: {
				"x-access-token": sessionStorage.getItem("token"),
				"x-tenant-id": '0',
				"x-Sign": sessionStorage.getItem('sign'),
				"x-timestamp": new Date().getTime() + "",
				"x-version": "v3",
			},
			data: requestData
		}).then(date => {

			console.log(date);
			if (date.data.success === false && date.data.code[0] === 4) {
				sessionStorage.removeItem('examId')
				sessionStorage.removeItem('token')
				sessionStorage.removeItem("user")
				sessionStorage.removeItem("sign")
				uni.redirectTo({
					url: '/pages/login/Login'
				});
			} else {
				return date
			}

		}).finally(() => {
			uni.hideLoading();
			uni.stopPullDownRefresh()
		})
	}

	return request
}
// 退出登录
export function exit() {
	request("/exam-boot/sys/logout", "GET", {
		t: new Date()
	}).then(d => {
		uni.hideLoading();
		uni.stopPullDownRefresh()
	})
	sessionStorage.removeItem('examId')
	sessionStorage.removeItem('token')
	sessionStorage.removeItem("user")
	sessionStorage.removeItem("sign")
	uni.redirectTo({
		url: '/pages/login/Login'
	});
}