From 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b Mon Sep 17 00:00:00 2001 From: lrj <owen.stl@gmail.com> Date: 星期二, 30 九月 2025 17:38:04 +0800 Subject: [PATCH] feat: 完善比赛晋级功能并清理测试文件 --- web/src/api/media.js | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 111 insertions(+), 4 deletions(-) diff --git a/web/src/api/media.js b/web/src/api/media.js index 2ca732a..6c7afdc 100644 --- a/web/src/api/media.js +++ b/web/src/api/media.js @@ -1,5 +1,7 @@ // 濯掍綋鏌ヨ API -const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql'; +import { graphqlRequest, API_CONFIG } from '../config/api.ts'; + +const GRAPHQL_ENDPOINT = API_CONFIG.GRAPHQL_ENDPOINT; const MEDIAS_BY_TARGET_QUERY = ` query MediasByTarget($targetType: Int!, $targetId: ID!) { @@ -36,9 +38,17 @@ `; 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: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify({ query: MEDIAS_BY_TARGET_QUERY, variables: { targetType, targetId } @@ -52,9 +62,17 @@ }; 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: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify({ query: SAVE_MEDIA_MUTATION, variables: { input } @@ -72,9 +90,17 @@ console.log('瑕佸垹闄ょ殑濯掍綋ID:', id); console.log('GraphQL鏌ヨ:', DELETE_MEDIA_MUTATION); + // 鑾峰彇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: { 'Content-Type': 'application/json' }, + headers: headers, body: JSON.stringify({ query: DELETE_MEDIA_MUTATION, variables: { id: id.toString() } @@ -99,8 +125,17 @@ const formData = new FormData(); formData.append('file', file); + // 鑾峰彇JWT token + const { getToken } = await import('@/utils/auth'); + const token = getToken(); + const headers = {}; + if (token) { + headers['Authorization'] = `Bearer ${token}`; + } + const response = await fetch('http://localhost:8080/api/upload/image', { method: 'POST', + headers: headers, body: formData }); @@ -110,4 +145,76 @@ } return result; +}; + +// 涓婁紶瑙嗛鏂囦欢骞惰嚜鍔ㄧ敓鎴愮缉鐣ュ浘 +export const uploadVideoWithThumbnail = async (videoFile) => { + const { extractVideoFrame, generateThumbnailFileName } = await import('@/utils/video.js'); + + try { + console.log('寮�濮嬪鐞嗚棰戞枃浠�:', videoFile.name); + + // 1. 涓婁紶鍘熻棰戞枃浠� + console.log('涓婁紶瑙嗛鏂囦欢...'); + const videoUploadResult = await uploadFile(videoFile); + console.log('瑙嗛涓婁紶鎴愬姛:', videoUploadResult); + + // 2. 鎻愬彇瑙嗛绗竴甯� + console.log('鎻愬彇瑙嗛绗竴甯�...'); + const thumbnailBlob = await extractVideoFrame(videoFile); + console.log('瑙嗛甯ф彁鍙栨垚鍔燂紝澶у皬:', thumbnailBlob.size); + + // 3. 鍒涘缓缂╃暐鍥炬枃浠跺璞� + const thumbnailFileName = generateThumbnailFileName(videoFile.name); + const thumbnailFile = new File([thumbnailBlob], thumbnailFileName, { + type: 'image/jpeg' + }); + + // 4. 涓婁紶缂╃暐鍥� + console.log('涓婁紶缂╃暐鍥�...'); + const thumbnailUploadResult = await uploadFile(thumbnailFile); + console.log('缂╃暐鍥句笂浼犳垚鍔�:', thumbnailUploadResult); + + // 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 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); + } + return result.data.saveMediaV2; }; \ No newline at end of file -- Gitblit v1.8.0