lrj
昨天 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 媒体查询 API
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!) {
    mediasByTarget(targetType: $targetType, targetId: $targetId) {
      id
      name
      path
      fileExt
      mediaType
      fullUrl
    }
  }
`;
 
const SAVE_MEDIA_MUTATION = `
  mutation SaveMedia($input: MediaInput!) {
    saveMedia(input: $input) {
      id
      name
      path
      fileExt
      mediaType
      fullUrl
      targetType
      targetId
    }
  }
`;
 
const DELETE_MEDIA_MUTATION = `
  mutation DeleteMedia($id: ID!) {
    deleteMedia(id: $id)
  }
`;
 
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);
  }
  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);
  }
  return result.data.saveMedia;
};
 
export const deleteMedia = async (id) => {
  console.log('=== deleteMedia API调用 ===');
  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: headers,
    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);
  }
  
  const deleteResult = result.data.deleteMedia;
  console.log('返回的删除结果:', deleteResult, '类型:', typeof deleteResult);
  return deleteResult;
};
 
// 上传文件到服务器
export const uploadFile = async (file) => {
  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
  });
  
  const result = await response.json();
  if (!result.success) {
    throw new Error(result.error || '上传失败');
  }
  
  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}`);
  }
};
 
// 新版保存媒体(SaveMediaV2):使用字符串 targetType 与 MediaSaveInput
// 注意:目前后端仅支持 targetType: "player"(学员头像 -> 6)与 "activity_player"(报名资料 -> 5)。
// 字段命名与旧版不同:fileName 替代 name;可选 thumbPath;mediaType: 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;
};