import axios from "axios";
import store from '../store/index'
import JSONbig from 'json-bigint'
// import { getToken } from "@/utils/storage";
const service = axios.create();
service.defaults.timeout = 50000;
service.defaults.baseURL = `${process.env.VUE_APP_API}`
service.interceptors.request.use(
	config => {
		config.headers = {
			// 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
			Authorization: store.state.token,
		}
		if(config.url.indexOf('/alarm/history/page') != -1 ){
			config.transformResponse =  [
				function (data) {
					const json = JSONbig({
						storeAsString: true
					})
					const res = json.parse(data)
					return res
				}
			]
		}
		
		return config;
	},
	error => {
		return Promise.reject(error);
	}
);

service.interceptors.response.use(
	// Loading.close(),
	response => {
		// 处理正常返回数据
		const { status } = response;
		if (
			status === 200
		) {
			// if (!data.errorCode || data.data === "fail") {
			// 	// Message.success('qqq')
			// 	return data ? data : true;
			// } else {
			// 	return false;
			// }
			return response;
		} else {
			// tips[data.code](data);
			return false;
		}
	},
	error => {
		console.log(error);
		// 处理错误并给出相应的错误处理
		if (error.response) {
			// const { status } = error.response;
			// tips[status](error);
			return error.response.data
		} else {
			return false;
		}
	}
);

export default service;