From c82c2ec8df2bcdd120cfdac72fc6234ba350dc7a Mon Sep 17 00:00:00 2001 From: liangguodong Date: Wed, 11 Mar 2026 13:38:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0PyJWT=E4=BE=9D?= =?UTF-8?q?=E8=B5=96=EF=BC=8C=E4=BF=9D=E7=95=99=E6=9C=AC=E5=9C=B0http?= =?UTF-8?q?=E5=8F=8C=E7=8E=AF=E5=A2=83=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/js/utils/http.js | 89 ++++++----------------------------------- server/requirements.txt | 1 + 2 files changed, 14 insertions(+), 76 deletions(-) diff --git a/client/js/utils/http.js b/client/js/utils/http.js index 27c4cd1..1bce1d3 100644 --- a/client/js/utils/http.js +++ b/client/js/utils/http.js @@ -9,7 +9,7 @@ const ENV = 'cloud'; // 'local' = 本地后端, 'cloud' = 微信云托管 const CONFIG = { local: { - baseUrl: 'http://172.20.10.8:8000/api' // 局域网IP,真机测试用 + baseUrl: 'http://localhost:8000/api' }, cloud: { env: 'prod-6gjx1rd4c40f5884', @@ -17,18 +17,6 @@ const CONFIG = { } }; -/** - * 获取存储的 Token - */ -function getToken() { - try { - const userInfo = wx.getStorageSync('userInfo'); - return userInfo?.token || ''; - } catch (e) { - return ''; - } -} - /** * 发送HTTP请求 */ @@ -45,43 +33,16 @@ export function request(options) { */ function requestLocal(options) { return new Promise((resolve, reject) => { - const timeoutMs = options.timeout || 30000; - - // 自动添加 Token 到请求头 - const token = getToken(); - const header = { - 'Content-Type': 'application/json', - ...options.header - }; - - if (token) { - header['Authorization'] = `Bearer ${token}`; - } - - // 处理 URL 查询参数 - let url = CONFIG.local.baseUrl + options.url; - if (options.params) { - const queryString = Object.entries(options.params) - .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) - .join('&'); - url += (url.includes('?') ? '&' : '?') + queryString; - } - wx.request({ - url, + url: CONFIG.local.baseUrl + options.url, method: options.method || 'GET', data: options.data || {}, - timeout: timeoutMs, - header, + timeout: options.timeout || 30000, + header: { + 'Content-Type': 'application/json', + ...options.header + }, success(res) { - // 处理 401 未授权错误 - if (res.statusCode === 401) { - // Token 过期或无效,清除本地存储 - wx.removeStorageSync('userInfo'); - reject(new Error('登录已过期,请重新登录')); - return; - } - if (res.data && res.data.code === 0) { resolve(res.data.data); } else { @@ -101,43 +62,19 @@ function requestLocal(options) { */ function requestCloud(options) { return new Promise((resolve, reject) => { - // 自动添加 Token 到请求头 - const token = getToken(); - const header = { - 'X-WX-SERVICE': CONFIG.cloud.serviceName, - 'Content-Type': 'application/json', - ...options.header - }; - - if (token) { - header['Authorization'] = `Bearer ${token}`; - } - - // 处理 URL 查询参数 - let path = '/api' + options.url; - if (options.params) { - const queryString = Object.entries(options.params) - .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`) - .join('&'); - path += (path.includes('?') ? '&' : '?') + queryString; - } - wx.cloud.callContainer({ config: { env: CONFIG.cloud.env }, - path, + path: '/api' + options.url, method: options.method || 'GET', data: options.data || {}, - header, + header: { + 'X-WX-SERVICE': CONFIG.cloud.serviceName, + 'Content-Type': 'application/json', + ...options.header + }, success(res) { - // 处理 401 未授权错误 - if (res.statusCode === 401) { - wx.removeStorageSync('userInfo'); - reject(new Error('登录已过期,请重新登录')); - return; - } - if (res.data && res.data.code === 0) { resolve(res.data.data); } else if (res.data) { diff --git a/server/requirements.txt b/server/requirements.txt index e1952f7..09c5045 100644 --- a/server/requirements.txt +++ b/server/requirements.txt @@ -9,3 +9,4 @@ pydantic-settings==2.1.0 python-dotenv==1.0.0 python-multipart==0.0.6 httpx==0.27.0 +PyJWT==2.8.0