import { BASE_URL, TIMEOUT } from "./config.js";

import Cache from "@/utils/cache.js";
//  引入请求库
import Request from "@/utils/luch-request/luch-request/index.js";
const http = new Request({
	baseURL: BASE_URL,
	timeout: TIMEOUT
});

//  请求拦截器
http.interceptors.request.use(
	(config)=>{
		// 根据请求信息里携带的自定义属性，判断当前请求是否需要携带token
		const needToken = config.custom.needToken || false;
		if (needToken) {
			const token = Cache.getCache('token');
			config.header['Auth-Token'] = token;
		}
		return config;
	},
	//  失败拦截
	(error)=>{
		return Promise.reject(error)
	}
)

//  响应拦截器
http.interceptors.response.use(
	// 响应成功拦截
	(response)=>{
		const { data } = response;
		if(data.data == "token已过期"){
			uni.showToast({
				title: "账号已过期，请重新登陆",
				icon:"none",
				duration: 1200
			});
		}else if(data.statusCode == 50000){
			uni.showToast({
				title: "网络异常！！！！",
				icon:"none",
				duration: 1200
			});
		}
		return data;
	},
	//  响应失败拦截
	(err)=>{
		const { msg } = err
		if(msg){
			this.$showToast(msg)
		}
		console.log(err)
	}
	
)

export default http;