import Request from '@/service/request/index.js' import {useAuth} from "@/store/auth"; import { createApp } from 'vue'; import XMessage from '@/components/x-message/index.vue'; // 创建消息提示实例 const messageInstance = (() => { const messageNode = document.createElement('div'); document.body.appendChild(messageNode); const app = createApp(XMessage); const instance = app.mount(messageNode); return { warning: (msg) => instance.showMessage({ type: 'warning', message: msg }), error: (msg) => instance.showMessage({ type: 'error', message: msg }), success: (msg) => instance.showMessage({ type: 'success', message: msg }), info: (msg) => instance.showMessage({ type: 'info', message: msg }) }; })(); const { token ,refreshToken,userInfo}=useAuth() let isRefreshing = false; let refreshSubscribers = []; const request = new Request({ baseURL: import.meta.env.VITE_BASEURL, timeout: 1000 * 60 * 5, interceptors: { //实例的请求拦截器 requestInterceptors: (config) => { config.headers["Content-Type"] = config.method === "get" ? "application/x-www-form-urlencoded" : config.contentType ? config.contentType : "application/json"; config.headers['Authorization'] = token.value || ''; if (config.isFormData) { config.headers['Content-Type'] = 'multipart/form-data'; } return config; }, responseInterceptors: async (res) => { if(res.data.status===1){ message.warning(res.data.msg) } if (res.data.status === 401) { return getRefreshToken(res); // uni.navigateTo({ // url:'/pages/login/index' // }) } if ([200, 201, 204].includes(res.status)) { return res.config.responseType === 'blob' ? res : res; } else { /* message.error(res.data.msg || 'An error occurred.');*/ messageInstance.error(res.data.msg || 'An error occurred.'); return Promise.reject(new Error(res.data.msg || 'An error occurred.')); } } } }) async function getRefreshToken(response) { if (!isRefreshing) { isRefreshing = true; const refreshTokenT = refreshToken.value; if (refreshTokenT) { try { const data = { refreshToken:refreshTokenT }; const res = await request.instance.post('/user/refresh/token', data); if (res.code === 200) { token.value = res.data.Token; refreshToken.value = res.data.RefreshToken; userInfo.value = res.data.AccountInfo response.config.headers['Authorization'] = res.data.Token; uni.navigateTo({ url:'/pages/index/index' }) return request.request(response.config); } else { messageInstance.error(res.message || res.msg); throw new Error(res.message || res.msg); } } catch (error) { uni.navigateTo({ url:'/pages/login/index' }) throw error } finally { isRefreshing = false; refreshSubscribers.forEach(callback => callback()); refreshSubscribers = []; } } else { uni.navigateTo({ url:'/pages/login/index' }) throw new Error('No refresh token available.'); } } else { return new Promise(resolve => { refreshSubscribers.push(() => resolve(request.request(response.config))); }); } } const fontRequest = (config) => { if (['get', 'GET'].includes(config.method)) { config.params = config.data; } return request.request(config); }; export default fontRequest;