From 80efe5bdce1c7795315ea1b6edf947cc66dedc3c Mon Sep 17 00:00:00 2001 From: taiyi Date: Wed, 22 Jul 2026 10:08:01 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E5=8D=A1=E5=AF=86=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/api.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/web/src/api.js b/web/src/api.js index 16ea023..e4139a9 100644 --- a/web/src/api.js +++ b/web/src/api.js @@ -1,10 +1,27 @@ -const BASE = "" +import { clearStoredAuth, getAccessToken, getStoredAuth, saveStoredAuth } from "./auth"; + +const BASE = "" async function request(path, opts = {}) { + const token = getAccessToken(); + const headers = { "Content-Type": "application/json", ...opts.headers }; + if (token) headers.Authorization = `Bearer ${token}`; + const res = await fetch(`${BASE}${path}`, { - headers: { "Content-Type": "application/json", ...opts.headers }, + headers, ...opts, }); + + const renewedToken = res.headers.get("x-license-token"); + if (renewedToken) { + const current = getStoredAuth() || {}; + saveStoredAuth({ ...current, token: renewedToken }); + } + + if (res.status === 401) { + clearStoredAuth(); + } + if (!res.ok) { const err = await res.json().catch(() => ({ error: res.statusText })); throw new Error(err.error || "请求失败");