| | |
| | | uni.showToast({ title: message, icon: "none" }); |
| | | } |
| | | }; |
| | | console.log(baseURL,'baseURL'); |
| | | console.log(baseURL,'baseURL1'); |
| | | |
| | | const config = { |
| | | baseURL: baseURL, |
| | |
| | | return data; |
| | | }; |
| | | |
| | | // 挂起请求队列:认证未完成时暂存请求,完成后批量重试 |
| | | const pendingQueue = [] |
| | | let pendingTimer = null |
| | | |
| | | function flushPending() { |
| | | clearInterval(pendingTimer) |
| | | pendingTimer = null |
| | | const q = pendingQueue.splice(0) |
| | | q.forEach(({ options, resolve, reject }) => { |
| | | request(options).then(resolve).catch(reject) |
| | | }) |
| | | } |
| | | |
| | | function pendRequest(options) { |
| | | return new Promise((resolve, reject) => { |
| | | pendingQueue.push({ options, resolve, reject }) |
| | | if (!pendingTimer) { |
| | | const maxWait = 15000 |
| | | const startTime = Date.now() |
| | | pendingTimer = setInterval(() => { |
| | | if (!uni.__authPending || Date.now() - startTime > maxWait) { |
| | | flushPending() |
| | | } |
| | | }, 100) |
| | | } |
| | | }) |
| | | } |
| | | |
| | | const request = async (options) => { |
| | | try { |
| | | // ✅ 认证挂起检查必须在 URL 拼接之前 |
| | | // 保存原始 options,重试时避免 baseURL 叠加 |
| | | const token = uni.getStorageSync("token"); |
| | | if (!token && uni.__authPending && !isInWhiteList(options.url)) { |
| | | return pendRequest({ ...options }); // 传入原始 options 的副本 |
| | | } |
| | | |
| | | options = { |
| | | ...config, |
| | | ...options, |
| | | url: config.baseURL + options.url, |
| | | header: { ...config.header, ...options.header }, |
| | | }; |
| | | |
| | | options = await requestInterceptor(options); |
| | | return new Promise((resolve, reject) => { |
| | | uni.request({ |