lrj
3 天以前 7ba080d35812e6db7bd5aa8f88161c02653eb6c1
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
/**
 * GraphQL客户端工具
 */
 
import { getToken } from './auth'
 
// GraphQL端点
const GRAPHQL_ENDPOINT = '/api/graphql'
 
export interface GraphQLResponse<T = any> {
  data?: T
  errors?: Array<{
    message: string
    locations?: Array<{
      line: number
      column: number
    }>
    path?: string[]
  }>
}
 
/**
 * 发送GraphQL请求
 */
export async function graphqlRequest<T = any>(
  query: string,
  variables?: Record<string, any>
): Promise<T> {
  const token = getToken()
  
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  }
  
  // 如果有token,添加到请求头
  if (token) {
    headers['Authorization'] = `Bearer ${token}`
  }
  
  const response = await fetch(GRAPHQL_ENDPOINT, {
    method: 'POST',
    headers,
    body: JSON.stringify({
      query,
      variables
    })
  })
  
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
  
  const result: GraphQLResponse<T> = await response.json()
  
  if (result.errors && result.errors.length > 0) {
    throw new Error(result.errors[0].message)
  }
  
  if (!result.data) {
    throw new Error('No data returned from GraphQL')
  }
  
  return result.data
}
 
/**
 * 登录API
 */
export interface LoginRequest {
  phone: string
  password: string
}
 
export interface LoginResponse {
  webLogin: {
    token: string
    userInfo: {
      userId: string
      name: string
      phone: string
      userType: string
      employee?: {
        id: string
        name: string
        roleId: string
        description?: string
      }
      judge?: {
        id: string
        name: string
        title?: string
        company?: string
        description?: string
      }
      player?: {
        id: string
        name: string
        phone?: string
        description?: string
        auditState?: number
      }
    }
  }
}
 
export async function loginApi(request: LoginRequest): Promise<LoginResponse['webLogin']> {
  const query = `
    mutation WebLogin($input: LoginRequest!) {
      webLogin(input: $input) {
        token
        userInfo {
          userId
          name
          phone
          userType
          employee {
            id
            name
            roleId
            description
          }
          judge {
            id
            name
            title
            company
            description
          }
          player {
            id
            name
            phone
            description
            auditState
          }
        }
      }
    }
  `
  
  const response = await graphqlRequest<LoginResponse>(query, { input: request })
  return response.webLogin
}