import Config from "@/config";
import Sign from "./Sign";
import to from "await-to-js";
import { YcStorage } from "@ycxxkj/uniapp";
/**
 * 网络请求帮助类
 */
const http = {
	SUCCESS_CODE: 1000,
	async get(url: string, param: any = {}, header: object = {}, isShowLoading: boolean = true): Promise<any> {
		let [err, res] = await to(http.request(url, param, header, isShowLoading, "GET"));
		if (err) {
			return err;
		}
		return res;
	},
	async post(url: string, param: any = {}, header: object = {}, isShowLoading: boolean = true) {
		let [err, res] = await to(http.request(url, param, header, isShowLoading, "POST"));
		if (err) {
			return err;
		}
		return res;
	},
	async patch(url: string, param: any = {}, header: object = {}, isShowLoading: boolean = true) {
		let [err, res] = await to(http.request(url, param, header, isShowLoading, "PATCH"));
		if (err) {
			return err;
		}
		return res;
	},
	async delete(url: string, param: any = {}, header: object = {}, isShowLoading: boolean = true) {
		let [err, res] = await to(http.request(url, param, header, isShowLoading, "DELETE"));
		if (err) {
			return err;
		}
		return res;
	},
	async upload(url: string, filePath: string, name: string = "file", param: any = {}, isShowLoading: boolean = true) {
		let [err, res] = await to(http.uploadFile(url, filePath, name, param, isShowLoading));
		if (err) {
			return err;
		}
		return res;
	},

	uploadFile(url: string, filePath: string, name: string = "file", param: any = {}, isShowLoading: boolean = true): Promise<any> {
		if (isShowLoading) {
			uni.showLoading({
				title: "正在上传..."
			});
		}
		let combineObj = http.combimeData(param);
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: Config.apiHost + url,
				filePath: filePath,
				name: name,
				formData: combineObj.param,
				header: combineObj.header,
				success: (res: any) => {
					let result = res.data;
					if (typeof res.data == "string") {
						result = JSON.parse(res.data);
					}
					resolve(result);
				},
				fail: (res: any) => {
					let result = res.data;
					if (typeof res.data == "string") {
						result = JSON.parse(res.data);
					}
					reject(result);
				},
				complete() {
					uni.hideLoading();
				}
			});
		});
	},
	request(url: string, param: object, header: object = {}, isShowLoading: boolean = true, method: any = "POST"): Promise<any> {
		if (isShowLoading) {
			uni.showLoading({
				title: "正在加载..."
			});
		}
		let combineObj = http.combimeData(param, header);
		return new Promise((resolve, reject) => {
			uni.request({
				url: Config.apiHost + url,
				data: combineObj.param,
				method: method,
				header: combineObj.header,
				success(response: any) {
					// if (response.statusCode == 600) {
					// 	//处理登录流程
					// 	toLogin();
					// 	return;
					// }
					if (response.statusCode != 200) {
						reject({ code: response.statusCode, desc: response.data.error, data: response.data });
						return;
					}
					console.log("request success", response);
					let res = response.data;
					if (res.code == 600) {
						//登录
						//这里写未登录的逻辑
						toLogin();
						reject(res);
						return;
					}
					if (res.code == 602) {
						return;
					}
					resolve(res);
				},
				fail(err) {
					uni.hideLoading();
					let error = {
						code: 9999,
						msg: "网络请求错误",
						data: err
					};
					reject(error);
				},
				complete() {
					uni.hideLoading();
				}
			});
		});
	},
	/**
	 * 组合header和bodydata参数
	 * @param {*} param
	 * @returns
	 */
	combimeData(param: any = {}, header: any = {}) {
		let token = YcStorage.get("token"); // uni.getStorageSync("token"); //从缓存取token
		if (!header["Content-Type"]) {
			header["Content-Type"] = "application/json";
		}
		if (token) {
			header["Authorization"] = token;
		}
		// param = sign(param);//签名
		return { param, header };
	}
};

const toLogin = () => {
	YcStorage.clear();
	let pages = getCurrentPages();
	console.log("lzj500🚀 ~ file: Http.ts:153 ~ toLogin ~ pages:", pages);
	let callback = "/pages/index/index";
	if (pages.length > 0) {
		let curPage: any = pages[pages.length - 1];
		console.log("lzj500🚀 ~ file: Http.ts:156 ~ toLogin ~ curPage:", curPage);
		callback = curPage.$page.fullPath;
	}
	YcStorage.set("callback", callback);
	uni.reLaunch({
		url: "/pages/index/index"
	});
	//let curPage = pages[pages.length - 1];
};
export default http;
