import axios from "axios"
import baseURL from "../utils/app"
import { Message, Notification } from "element-ui"
axios.defaults.timeout = 10000
// axios.defaults.baseURL = baseURL.SVC_API_PDMS + "/api/v2";
import { getToken } from "../utils/auth"
//请求拦截器
axios.interceptors.request.use(
	config => {
		const token = getToken()
		//配置token
		if (token) {
			config.headers["Authorization"] = token
		}
		return config
	},
	error => {
		return Promise.reject(error)
	}
)

//响应拦截器即异常处理
axios.interceptors.response.use(
	response => {
		// 根据后端接口code执行操作
		if (response.data.code !== 200) {
			Message.warning(response.data.message)
		}
		return response.data
	},
	error => {
		if (error && error.response) {
			switch (error.response.status) {
				case 400:
					Notification.error({
						title: "通知",
						message: "错误请求",
						duration: 2000
					})
					break
				case 401:
					break
				case 403:
					Notification.error({
						title: "通知",
						message: "拒绝访问",
						duration: 2000
					})
					break
				case 404:
					Notification.error({
						title: "通知",
						message: "请求错误,地址不存在",
						duration: 2000
					})
					break
				case 405:
					Notification.error({
						title: "通知",
						message: "请求方法未允许",
						duration: 2000
					})
					break
				case 408:
					Notification.error({
						title: "通知",
						message: "请求超时",
						duration: 2000
					})
					break
				case 500:
					if (error.response.data.code === 1000 || error.response.data.code === 401) {
						Notification.error({
							title: "通知",
							message: "无效token,请重新登录",
							duration: 2000
						})
						setTimeout(() => {
							window.location.href = baseURL.VUE_APP_PORTAL_PATH + "/login"
						}, 1000)
					} else {
						Notification.error({
							title: "通知",
							message: "服务器端出错",
							duration: 2000
						})
					}
					break
				case 501:
					Notification.error({
						title: "通知",
						message: "网络未实现",
						duration: 2000
					})
					break
				case 502:
					Notification.error({
						title: "服务异常",
						message: "服务重启中..."
					})
					break
				case 503:
					Notification.error({
						title: "服务异常",
						message: "服务重启中..."
					})
					break
				case 504:
					Notification.error({
						title: "通知",
						message: "网络超时",
						duration: 2000
					})
					break
				case 505:
					Notification.error({
						title: "通知",
						message: "http版本不支持该请求",
						duration: 2000
					})
					break
				default:
					Notification.error({
						title: "通知",
						message: `连接错误`,
						duration: 2000
					})
			}
		} else {
			Notification.error({
				title: "通知",
				message: "连接到服务器失败",
				duration: 2000
			})
		}
		return Promise.resolve(error.response)
	}
)

export default {
	// get请求
	get(url, params = {}, headers = {}) {
		return new Promise((resolve, reject) => {
			axios
				.get(url, {
					params,
					headers
				})
				.then(
					res => {
						resolve(res)
					},
					err => {
						reject(err)
					}
				)
		})
	},
	// post请求
	post(url, params = {}, headers = {}) {
		console.log("debug log --> ", headers)

		return new Promise((resolve, reject) => {
			axios
				.post(url, params, {
					headers
				})
				.then(
					res => {
						resolve(res)
					},
					err => {
						reject(err)
					}
				)
		})
	},
	// put请求
	put(url, params = {}, headers = {}) {
		return new Promise((resolve, reject) => {
			axios
				.put(url, params, {
					headers
				})
				.then(
					response => {
						resolve(response)
					},
					err => {
						reject(err)
					}
				)
		})
	},
	// patch请求
	patch(url, params = {}, headers = {}) {
		return new Promise((resolve, reject) => {
			axios
				.patch(url, params, {
					headers
				})
				.then(
					response => {
						resolve(response)
					},
					err => {
						reject(err)
					}
				)
		})
	},
	// delete
	delete(url, params = {}, headers = {}) {
		return new Promise((resolve, reject) => {
			axios
				.delete(url, {
					params,
					headers
				})
				.then(
					response => {
						resolve(response)
					},
					err => {
						reject(err)
					}
				)
		})
	},
	// petch请求
	petch(url, params = {}) {
		return new Promise((resolve, reject) => {
			axios.petch(url, params).then(
				response => {
					resolve(response)
				},
				err => {
					reject(err)
				}
			)
		})
	},
	//FormData请求
	postFile(url, params = {}) {
		return new Promise((resolve, reject) => {
			let form = new FormData()
			if (Object.keys(params).length > 0) {
				for (const key in params) {
					form.append(key, params[key])
				}
			}
			axios
				.post(url, form, {
					headers: {
						"Content-Type": "multipart/form-data"
					}
				})
				.then(
					res => {
						resolve(res)
					},
					err => {
						reject(err)
					}
				)
		})
	}
}
