import axios from 'axios';
import {ElNotification} from 'element-plus';
import sConfig from "@/config";
import tools from '@/utils/tools';
import router from '@/router';

// 请求地址
axios.defaults.baseURL = sConfig.API_URL + sConfig.API_PREFIX
// 超时时间
axios.defaults.timeout = sConfig.TIMEOUT
// 请求拦截
axios.interceptors.request.use((config) => {
	let token = tools.data.get("TOKEN")
	if (token) {
		config.headers[sConfig.TOKEN_NAME] = sConfig.TOKEN_PREFIX + token
	}
	// get请求添加时间戳
	if (!sConfig.REQUEST_CACHE && config.method === 'get') {
		config.params = config.params || {};
		config.params['_'] = new Date().getTime();
	}
	// 多语言
	const lang = tools.data.get("APP_LANG") || 'zh-cn'
	config.headers['Accept-Language'] = lang
	return config;
})
//响应拦截
axios.interceptors.response.use((response) => {
	// 续签逻辑
	const newToken = response.headers['x-token-refresh']
	const ttl = response.headers['x-token-expire']
	if (newToken && ttl > 0) {
		tools.data.set("TOKEN", newToken, ttl)
	}
	let res = response.data
	if (res.code == 0) {
		return Promise.resolve(res)
	} else if (res.code == 1) { // 操作失败拦截
		ElNotification.error({title: '操作失败', message: res.msg});
		return Promise.reject(res)
	} else if (res.code == 2) { // 权限不足拦截
		ElNotification.error({title: '权限不足', message: res.msg});
		return Promise.reject(res)
	} else if (res.code == 3) { // 登录失效拦截
		ElNotification.error({title: '登录失效', message: res.msg});
		router.replace({path: '/login'}).then(r => {
		});
	}else {
		ElNotification.error({
			title: '请求错误',
			message: `Status:${response.status}，未知错误！`
		});
	}
}, (error) => {
	if (error.response) {
		if (error.response.status === 404) {
			ElNotification.error({title: '请求错误', message: "Status:404，正在请求不存在的资源！"});
		} else if (error.response.status === 500) {
			ElNotification.error({
				title: '请求错误',
				message: error.response.data.message || "Status:500，服务器发生错误！"
			});
		} else {
			ElNotification.error({
				title: '请求错误',
				message: error.message || `Status:${error.response.status}，未知错误！`
			});
		}
	} else {
		ElNotification.error({title: '请求错误', message: "请求服务器无响应！"});
	}
	return Promise.reject(error.response);
})

const http = {
	/** get 请求
	 * @param url 请求地址
	 * @param params 参数
	 * @param config 配置
	 */
	get: function (url: string, params = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'get',
				url: url,
				params: params,
				...config
			}).then((response) => {
				resolve(response);
			}).catch((error) => {
				reject(error);
			})
		})
	},

	/** post 请求
	 * @param url 请求地址
	 * @param data 参数
	 * @param config 配置
	 */
	post: function (url: string, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'post',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response);
			}).catch((error) => {
				reject(error);
			})
		})
	},

	/** put 请求
	 * @param url 请求地址
	 * @param data 参数
	 * @param config 配置
	 */
	put: function (url: string, data = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'put',
				url: url,
				data: data,
				...config
			}).then((response) => {
				resolve(response);
			}).catch((error) => {
				reject(error);
			})
		})
	},

	/** delete 请求
	 * @param url 请求地址
	 * @param params 参数
	 * @param config 配置
	 */
	delete: function (url: string, params = {}, config = {}) {
		return new Promise((resolve, reject) => {
			axios({
				method: 'delete',
				url: url,
				params: params,
				...config
			}).then((response) => {
				resolve(response);
			}).catch((error) => {
				reject(error);
			})
		})
	}
}

export default http;
