lrj
2 天以前 c61d4fe27c97d2ecc907756aa571a4ef14a7b9b6
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
import { JUDGE_QUERIES, JUDGE_MUTATIONS } from './graphql'
import type { Judge, JudgeInput, CosCredentials, Tag, Media } from './graphql'
 
const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql'
 
// GraphQL请求函数
async function graphqlRequest(query: string, variables: any = {}) {
  const response = await fetch(GRAPHQL_ENDPOINT, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      query,
      variables,
    }),
  })
 
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
 
  const result = await response.json()
  
  if (result.errors) {
    throw new Error(result.errors[0]?.message || 'GraphQL error')
  }
 
  return result.data
}
 
export class JudgeApi {
  // 获取所有评委
  static async getJudges(): Promise<Judge[]> {
    const data = await graphqlRequest(JUDGE_QUERIES.GET_JUDGES)
    return data?.judges || []
  }
 
  // 根据姓名搜索评委
  static async searchJudges(name: string): Promise<Judge[]> {
    const data = await graphqlRequest(JUDGE_QUERIES.SEARCH_JUDGES, { name })
    return data?.judgesByName || []
  }
 
  // 获取评委详情
  static async getJudge(id: string): Promise<Judge | null> {
    const data = await graphqlRequest(JUDGE_QUERIES.GET_JUDGE, { id: parseInt(id) })
    return data?.judge || null
  }
 
  // 保存评委
  static async saveJudge(input: JudgeInput): Promise<Judge> {
    const data = await graphqlRequest(JUDGE_MUTATIONS.SAVE_JUDGE, { input })
    return data?.saveJudge
  }
 
  // 删除评委
  static async deleteJudge(id: string): Promise<boolean> {
    const data = await graphqlRequest(JUDGE_MUTATIONS.DELETE_JUDGE, { id: parseInt(id) })
    return data?.deleteJudge || false
  }
 
  // 获取上传凭证
  static async getUploadCredentials(): Promise<CosCredentials> {
    const data = await graphqlRequest(JUDGE_QUERIES.GET_UPLOAD_CREDENTIALS)
    return data?.getUploadCredentials
  }
 
  // 获取所有标签
  static async getTags(): Promise<Tag[]> {
    const data = await graphqlRequest(JUDGE_QUERIES.GET_TAGS)
    return data?.tags || []
  }
 
  // 根据分类获取标签
  static async getTagsByCategory(category: string): Promise<Tag[]> {
    const data = await graphqlRequest(JUDGE_QUERIES.GET_TAGS_BY_CATEGORY, { category })
    return data?.tagsByCategory || []
  }
 
  // 保存媒体文件信息
  static async saveMedia(input: {
    name: string
    path: string
    fileSize: number
    fileExt: string
    mediaType: number
    targetType: number
    targetId: number
  }): Promise<Media> {
    const data = await graphqlRequest(JUDGE_MUTATIONS.SAVE_MEDIA, { input })
    return data?.saveMedia
  }
}
 
// 腾讯云COS上传服务
export class CosUploadService {
  // 上传文件到腾讯云COS
  static async uploadFile(file: File): Promise<string> {
    try {
      // 1. 获取上传凭证
      const credentials = await JudgeApi.getUploadCredentials()
      
      // 2. 构建上传URL
      const uploadUrl = `https://${credentials.bucket}.cos.${credentials.region}.myqcloud.com/${credentials.key}`
      
      // 3. 创建FormData
      const formData = new FormData()
      formData.append('file', file)
      
      // 4. 上传文件
      const response = await fetch(uploadUrl, {
        method: 'PUT',
        headers: {
          'Authorization': credentials.authorization
        },
        body: file
      })
      
      if (!response.ok) {
        throw new Error('上传失败')
      }
      
      // 5. 返回文件URL
      return uploadUrl
    } catch (error) {
      console.error('文件上传失败:', error)
      throw new Error('文件上传失败')
    }
  }
}