lrj
18 小时以前 7ad9c3c93f0cc103347ae2e2429e0122fb512e24
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
// API配置文件 - 统一管理所有API相关配置
export const API_CONFIG = {
  // 基础URL配置
  BASE_URL: '/api',
  
  // GraphQL端点
  GRAPHQL_ENDPOINT: '/api/graphql',
  
  // 其他API端点
  ENDPOINTS: {
    MEDIA_UPLOAD: '/api/media/upload',
    MEDIA_DOWNLOAD: '/api/media/download',
    WECHAT_LOGIN: '/api/wechat/login',
    WECHAT_PHONE: '/api/wechat/phone'
  }
}
 
// GraphQL请求工具函数
export const graphqlRequest = async (query: string, variables: any = {}) => {
  // 获取JWT token
  const { getToken } = await import('@/utils/auth');
  const token = getToken();
 
  // 构建请求头
  const headers: Record<string, string> = {
    'Content-Type': 'application/json',
  };
 
  if (token) {
    headers['Authorization'] = `Bearer ${token}`;
  }
 
  // 构建请求体
  const requestBody = JSON.stringify({
    query,
    variables,
  });
 
  try {
    // 发送请求
    const response = await fetch(API_CONFIG.GRAPHQL_ENDPOINT, {
      method: 'POST',
      headers,
      body: requestBody,
    });
 
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
 
    const result = await response.json();
 
    if (result.errors) {
      throw new Error(`GraphQL errors: ${JSON.stringify(result.errors)}`);
    }
 
    return result;
  } catch (error) {
    console.error('=== GraphQL请求失败 ===');
    console.error('错误详情:', error);
    throw error;
  }
}
 
// 通用API请求工具函数
export const apiRequest = async (endpoint: string, options: RequestInit = {}) => {
  const url = endpoint.startsWith('http') ? endpoint : `${API_CONFIG.BASE_URL}${endpoint}`
  
  const response = await fetch(url, {
    headers: {
      'Content-Type': 'application/json',
      ...options.headers,
    },
    ...options,
  })
 
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`)
  }
 
  return response.json()
}