From 49c254ed5bdc8348551d808ee72579a6d2221e3b Mon Sep 17 00:00:00 2001 From: lrj <owen.stl@gmail.com> Date: 星期六, 04 十月 2025 18:43:12 +0800 Subject: [PATCH] fix(miniprogram): 切换 GraphQL 端点为 /graphql,修复 review/index 400 --- web/src/api/media.js | 115 ++++++++++++++++++++++++++------------------------------- 1 files changed, 52 insertions(+), 63 deletions(-) diff --git a/web/src/api/media.js b/web/src/api/media.js index 7bec028..c351e39 100644 --- a/web/src/api/media.js +++ b/web/src/api/media.js @@ -38,50 +38,12 @@ `; export const getMediasByTarget = async (targetType, targetId) => { - // 鑾峰彇JWT token - const { getToken } = await import('@/utils/auth'); - const token = getToken(); - const headers = { 'Content-Type': 'application/json' }; - if (token) { - headers['Authorization'] = `Bearer ${token}`; - } - - const res = await fetch(GRAPHQL_ENDPOINT, { - method: 'POST', - headers: headers, - body: JSON.stringify({ - query: MEDIAS_BY_TARGET_QUERY, - variables: { targetType, targetId } - }) - }); - const result = await res.json(); - if (result.errors) { - throw new Error(result.errors[0].message); - } + const result = await graphqlRequest(MEDIAS_BY_TARGET_QUERY, { targetType, targetId }); return result.data.mediasByTarget || []; }; export const saveMedia = async (input) => { - // 鑾峰彇JWT token - const { getToken } = await import('@/utils/auth'); - const token = getToken(); - const headers = { 'Content-Type': 'application/json' }; - if (token) { - headers['Authorization'] = `Bearer ${token}`; - } - - const res = await fetch(GRAPHQL_ENDPOINT, { - method: 'POST', - headers: headers, - body: JSON.stringify({ - query: SAVE_MEDIA_MUTATION, - variables: { input } - }) - }); - const result = await res.json(); - if (result.errors) { - throw new Error(result.errors[0].message); - } + const result = await graphqlRequest(SAVE_MEDIA_MUTATION, { input }); return result.data.saveMedia; }; @@ -101,8 +63,8 @@ } }; -// 涓婁紶鏂囦欢鍒版湇鍔″櫒 -export const uploadFile = async (file) => { +// 涓婁紶鏂囦欢鍒版湇鍔″櫒锛堝甫閲嶈瘯鏈哄埗锛� +export const uploadFile = async (file, maxRetries = 3) => { const formData = new FormData(); formData.append('file', file); @@ -114,18 +76,56 @@ headers['Authorization'] = `Bearer ${token}`; } - const response = await fetch('http://localhost:8080/api/upload/image', { - method: 'POST', - headers: headers, - body: formData - }); + let lastError; - const result = await response.json(); - if (!result.success) { - throw new Error(result.error || '涓婁紶澶辫触'); + for (let attempt = 1; attempt <= maxRetries; attempt++) { + try { + const response = await fetch('http://localhost:8080/api/upload/image', { + method: 'POST', + headers: headers, + body: formData, + // 娣诲姞瓒呮椂璁剧疆 + signal: AbortSignal.timeout(30000) // 30绉掕秴鏃� + }); + + if (!response.ok) { + throw new Error(`HTTP ${response.status}: ${response.statusText}`); + } + + const result = await response.json(); + if (!result.success) { + throw new Error(result.error || '涓婁紶澶辫触'); + } + + return result; + } catch (error) { + lastError = error; + console.warn(`鏂囦欢涓婁紶绗�${attempt}娆″皾璇曞け璐�:`, error.message); + + // 濡傛灉鏄渶鍚庝竴娆″皾璇曪紝鎴栬�呮槸闈炵綉缁滈敊璇紝鐩存帴鎶涘嚭 + if (attempt === maxRetries || + (!error.message.includes('Failed to fetch') && + !error.message.includes('ERR_CONNECTION_RESET') && + !error.message.includes('ERR_NETWORK'))) { + break; + } + + // 绛夊緟涓�娈垫椂闂村悗閲嶈瘯锛堟寚鏁伴��閬匡級 + const delay = Math.min(1000 * Math.pow(2, attempt - 1), 5000); + await new Promise(resolve => setTimeout(resolve, delay)); + } } - return result; + // 鎻愪緵鏇村弸濂界殑閿欒淇℃伅 + if (lastError.message.includes('Failed to fetch') || + lastError.message.includes('ERR_CONNECTION_RESET') || + lastError.message.includes('ERR_NETWORK')) { + throw new Error('缃戠粶杩炴帴澶辫触锛岃妫�鏌ョ綉缁滆繛鎺ユ垨绋嶅悗閲嶈瘯'); + } else if (lastError.message.includes('timeout')) { + throw new Error('涓婁紶瓒呮椂锛岃妫�鏌ョ綉缁滆繛鎺ユ垨绋嶅悗閲嶈瘯'); + } else { + throw new Error(`涓婁紶澶辫触: ${lastError.message}`); + } }; // 涓婁紶瑙嗛鏂囦欢骞惰嚜鍔ㄧ敓鎴愮缉鐣ュ浘 @@ -177,17 +177,6 @@ // 缁熶竴鐨� V2 淇濆瓨鎺ュ彛锛堣繑鍥� { success, message, mediaId }锛夛紝绀轰緥锛� // await saveMediaV2({ targetType: 'player', targetId: 123, path: 'avatar/xxx.jpg', fileName: 'avatar.jpg', fileExt: 'jpg', fileSize: 2048, mediaType: 1 }) export const saveMediaV2 = async (input) => { - const res = await fetch(GRAPHQL_ENDPOINT, { - method: 'POST', - headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify({ - query: SAVE_MEDIA_V2_MUTATION, - variables: { input } - }) - }); - const result = await res.json(); - if (result.errors) { - throw new Error(result.errors[0].message); - } + const result = await graphqlRequest(SAVE_MEDIA_V2_MUTATION, { input }); return result.data.saveMediaV2; }; \ No newline at end of file -- Gitblit v1.8.0