import axios from 'axios';
import store from '@/store';

const request = axios.create({
	baseURL: import.meta.env.MODE === 'development' ? '/dev-api' : '/prod-api',
});

request.interceptors.request.use((config) => {
	// 是否需要设置 token
	if (config.headers.isToken !== false) {
		config.headers['Authorization'] = 'Bearer ' + store.getters.getToken; // 让每个请求携带自定义token 请根据实际情况自行修改
	}
	return config;
});

request.interceptors.response.use((res) => {
	if (res.data.code === 401) {
		location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx67ca1b8c9816ef28&redirect_uri=${location.href}&response_type=code&scope=snsapi_userinfo&state=LOGIN#wechat_redirect`);
	}
	return res;
});

export default request;
