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('文件上传失败')
|
}
|
}
|
}
|