From 3714621173c606c4c58439ed8941100ce9ddea14 Mon Sep 17 00:00:00 2001
From: Codex Assistant <codex@example.com>
Date: 星期三, 05 十一月 2025 15:10:49 +0800
Subject: [PATCH] bug
---
web/src/api/media.js | 188 ++++++++++++++++++++++++++++++++--------------
1 files changed, 129 insertions(+), 59 deletions(-)
diff --git a/web/src/api/media.js b/web/src/api/media.js
index 2ca732a..91f522b 100644
--- a/web/src/api/media.js
+++ b/web/src/api/media.js
@@ -1,5 +1,8 @@
// 濯掍綋鏌ヨ API
-const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql';
+import { graphqlRequest, API_CONFIG } from '../config/api.ts';
+import { serverUrl } from '../utils/appConfig.js';
+
+const GRAPHQL_ENDPOINT = API_CONFIG.GRAPHQL_ENDPOINT;
const MEDIAS_BY_TARGET_QUERY = `
query MediasByTarget($targetType: Int!, $targetId: ID!) {
@@ -36,78 +39,145 @@
`;
export const getMediasByTarget = async (targetType, targetId) => {
- const res = await fetch(GRAPHQL_ENDPOINT, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- 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) => {
- const res = await fetch(GRAPHQL_ENDPOINT, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- 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;
};
export const deleteMedia = async (id) => {
- console.log('=== deleteMedia API璋冪敤 ===');
- console.log('瑕佸垹闄ょ殑濯掍綋ID:', id);
- console.log('GraphQL鏌ヨ:', DELETE_MEDIA_MUTATION);
-
- const res = await fetch(GRAPHQL_ENDPOINT, {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- query: DELETE_MEDIA_MUTATION,
- variables: { id: id.toString() }
- })
- });
- const result = await res.json();
- console.log('GraphQL鍝嶅簲:', result);
- console.log('deleteMedia缁撴灉:', result.data?.deleteMedia);
-
- if (result.errors) {
- console.error('GraphQL閿欒:', result.errors);
- throw new Error(result.errors[0].message);
+ try {
+ const variables = { id: parseInt(id) };
+
+ // 鍙戦�丟raphQL璇锋眰
+ const result = await graphqlRequest(DELETE_MEDIA_MUTATION, variables);
+
+ // 妫�鏌ヨ繑鍥炵粨鏋�
+ const deleteResult = result.data?.deleteMedia;
+
+ return deleteResult;
+ } catch (error) {
+ throw new Error(`鍒犻櫎濯掍綋澶辫触: ${error.message}`);
}
-
- const deleteResult = result.data.deleteMedia;
- console.log('杩斿洖鐨勫垹闄ょ粨鏋�:', deleteResult, '绫诲瀷:', typeof deleteResult);
- return deleteResult;
};
-// 涓婁紶鏂囦欢鍒版湇鍔″櫒
-export const uploadFile = async (file) => {
+// 涓婁紶鏂囦欢鍒版湇鍔″櫒锛堝甫閲嶈瘯鏈哄埗锛�
+export const uploadFile = async (file, maxRetries = 3) => {
const formData = new FormData();
formData.append('file', file);
- const response = await fetch('http://localhost:8080/api/upload/image', {
- method: 'POST',
- body: formData
- });
-
- const result = await response.json();
- if (!result.success) {
- throw new Error(result.error || '涓婁紶澶辫触');
+ // 鑾峰彇JWT token
+ const { getToken } = await import('@/utils/auth');
+ const token = getToken();
+ const headers = {};
+ if (token) {
+ headers['Authorization'] = `Bearer ${token}`;
}
- return result;
+ let lastError;
+
+ for (let attempt = 1; attempt <= maxRetries; attempt++) {
+ try {
+ const response = await fetch(`${serverUrl}/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));
+ }
+ }
+
+ // 鎻愪緵鏇村弸濂界殑閿欒淇℃伅
+ 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}`);
+ }
+};
+
+// 涓婁紶瑙嗛鏂囦欢骞惰嚜鍔ㄧ敓鎴愮缉鐣ュ浘
+export const uploadVideoWithThumbnail = async (videoFile) => {
+ const { extractVideoFrame, generateThumbnailFileName } = await import('@/utils/video.js');
+
+ try {
+ // 1. 涓婁紶瑙嗛鏂囦欢
+ const videoUploadResult = await uploadFile(videoFile);
+
+ // 2. 鎻愬彇瑙嗛绗竴甯�
+ const thumbnailBlob = await extractVideoFrame(videoFile);
+
+ // 3. 鍒涘缓缂╃暐鍥炬枃浠跺璞�
+ const thumbnailFileName = generateThumbnailFileName(videoFile.name);
+ const thumbnailFile = new File([thumbnailBlob], thumbnailFileName, {
+ type: 'image/jpeg'
+ });
+
+ // 4. 涓婁紶缂╃暐鍥�
+ const thumbnailUploadResult = await uploadFile(thumbnailFile);
+
+ // 5. 杩斿洖鍖呭惈瑙嗛鍜岀缉鐣ュ浘淇℃伅鐨勭粨鏋�
+ return {
+ video: videoUploadResult,
+ thumbnail: thumbnailUploadResult,
+ success: true
+ };
+
+ } catch (error) {
+ console.error('瑙嗛澶勭悊澶辫触:', error);
+ throw new Error(`瑙嗛澶勭悊澶辫触: ${error.message}`);
+ }
+};
+
+// 鏂扮増淇濆瓨濯掍綋锛圫aveMediaV2锛夛細浣跨敤瀛楃涓� targetType 涓� MediaSaveInput
+// 娉ㄦ剰锛氱洰鍓嶅悗绔粎鏀寔 targetType: "player"锛堝鍛樺ご鍍� -> 6锛変笌 "activity_player"锛堟姤鍚嶈祫鏂� -> 5锛夈��
+// 瀛楁鍛藉悕涓庢棫鐗堜笉鍚岋細fileName 鏇夸唬 name锛涘彲閫� thumbPath锛沵ediaType: 1鍥剧墖銆�2瑙嗛銆�3闊抽銆�4鏂囨。銆�
+const SAVE_MEDIA_V2_MUTATION = `
+ mutation SaveMediaV2($input: MediaSaveInput!) {
+ saveMediaV2(input: $input) {
+ success
+ message
+ mediaId
+ }
+ }
+`;
+
+// 缁熶竴鐨� 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 result = await graphqlRequest(SAVE_MEDIA_V2_MUTATION, { input });
+ return result.data.saveMediaV2;
};
\ No newline at end of file
--
Gitblit v1.8.0