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
import { JUDGE_QUERIES, JUDGE_MUTATIONS } from './graphql'
import type { Judge, JudgeInput, CosCredentials, Tag, Media } from './graphql'
 
import { API_CONFIG, graphqlRequest } from '@/config/api'
 
// 使用统一的GraphQL请求函数
 
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('文件上传失败')
    }
  }
}