| | |
| | | const GRAPHQL_ENDPOINT = '/api/graphql' |
| | | import { API_CONFIG, graphqlRequest } from '@/config/api' |
| | | |
| | | async function graphqlRequest(query, variables = {}) { |
| | | const response = await fetch(GRAPHQL_ENDPOINT, { |
| | | method: 'POST', |
| | | headers: { 'Content-Type': 'application/json' }, |
| | | body: JSON.stringify({ query, variables }) |
| | | }) |
| | | if (!response.ok) { |
| | | const text = await response.text() |
| | | throw new Error(`HTTP ${response.status}: ${text}`) |
| | | // 使用统一的graphqlRequest函数 |
| | | |
| | | // GraphQL查询语句 |
| | | const GET_PLAYERS_QUERY = ` |
| | | query GetPlayers($page: Int!, $size: Int!, $name: String) { |
| | | players(page: $page, size: $size, name: $name) { |
| | | content { |
| | | id |
| | | name |
| | | phone |
| | | regionId |
| | | region { |
| | | id |
| | | name |
| | | } |
| | | createTime |
| | | updateTime |
| | | } |
| | | totalElements |
| | | page |
| | | size |
| | | } |
| | | } |
| | | const result = await response.json() |
| | | if (result.errors) throw new Error(result.errors.map(e => e.message).join('\\n')) |
| | | return result.data |
| | | ` |
| | | |
| | | const GET_PLAYER_QUERY = ` |
| | | query GetPlayer($id: ID!) { |
| | | player(id: $id) { |
| | | id |
| | | name |
| | | phone |
| | | regionId |
| | | region { |
| | | id |
| | | name |
| | | } |
| | | createTime |
| | | updateTime |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const SAVE_PLAYER_MUTATION = ` |
| | | mutation SavePlayer($input: PlayerInput!) { |
| | | savePlayer(input: $input) { |
| | | id |
| | | name |
| | | phone |
| | | regionId |
| | | region { |
| | | id |
| | | name |
| | | } |
| | | createTime |
| | | updateTime |
| | | } |
| | | } |
| | | ` |
| | | |
| | | const DELETE_PLAYER_MUTATION = ` |
| | | mutation DeletePlayer($id: ID!) { |
| | | deletePlayer(id: $id) |
| | | } |
| | | ` |
| | | |
| | | // API函数 |
| | | export const getPlayers = async (page = 0, size = 10, name = '') => { |
| | | try { |
| | | const data = await graphqlRequest(GET_PLAYERS_QUERY, { page, size, name }) |
| | | return data.players |
| | | } catch (error) { |
| | | throw new Error(error.message || '获取学员列表失败') |
| | | } |
| | | } |
| | | |
| | | export const getPlayer = async (id) => { |
| | | try { |
| | | const data = await graphqlRequest(GET_PLAYER_QUERY, { id }) |
| | | return data.player |
| | | } catch (error) { |
| | | throw new Error(error.message || '获取学员详情失败') |
| | | } |
| | | } |
| | | |
| | | export const savePlayer = async (playerData) => { |
| | | try { |
| | | const data = await graphqlRequest(SAVE_PLAYER_MUTATION, { input: playerData }) |
| | | return data.savePlayer |
| | | } catch (error) { |
| | | throw new Error(error.message || '保存学员失败') |
| | | } |
| | | } |
| | | |
| | | export const deletePlayer = async (id) => { |
| | | try { |
| | | const data = await graphqlRequest(DELETE_PLAYER_MUTATION, { id }) |
| | | return data.deletePlayer |
| | | } catch (error) { |
| | | throw new Error(error.message || '删除学员失败') |
| | | } |
| | | } |
| | | |
| | | const GET_APPLICATIONS = ` |
| | | query GetApplications($name: String, $activityId: ID, $page: Int, $size: Int) { |
| | | activityPlayerApplications(name: $name, activityId: $activityId, page: $page, size: $size) { |
| | | query GetApplications($name: String, $activityId: ID, $state: Int, $page: Int, $size: Int) { |
| | | activityPlayerApplications(name: $name, activityId: $activityId, state: $state, page: $page, size: $size) { |
| | | id playerName activityName phone applyTime state |
| | | } |
| | | } |
| | | ` |
| | | |
| | | export const PlayerApi = { |
| | | getApplications: async (name, activityId, page, size) => { |
| | | const data = await graphqlRequest(GET_APPLICATIONS, { name, activityId, page, size }) |
| | | getApplications: async (name, activityId, state, page, size) => { |
| | | const data = await graphqlRequest(GET_APPLICATIONS, { name, activityId, state, page, size }) |
| | | return data.activityPlayerApplications || [] |
| | | } |
| | | } |