File size: 6,426 Bytes
2d4ab0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57864b1
 
 
 
 
 
 
 
 
 
 
 
2d4ab0a
 
 
 
 
 
 
 
 
 
 
57864b1
 
 
 
 
 
 
 
 
 
 
 
2d4ab0a
 
 
 
 
 
 
 
 
 
19b1032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8288938
 
 
57864b1
 
8288938
a7623eb
 
57864b1
 
 
 
 
 
 
 
 
87d4f50
 
 
 
 
 
 
 
 
 
 
2d4ab0a
 
57864b1
19b1032
6b5137b
 
19b1032
 
 
6b5137b
57864b1
2d4ab0a
 
 
 
 
 
57864b1
2d4ab0a
 
 
 
 
 
19b1032
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57864b1
 
 
 
 
 
 
 
 
 
 
 
 
692172e
8c42c9b
57864b1
 
692172e
57864b1
 
 
9945a5d
57864b1
8288938
 
 
 
 
 
 
 
 
 
 
 
 
 
c7f5be9
8288938
 
 
c7f5be9
8288938
01fa57b
 
 
 
 
 
87d4f50
be8b6db
 
 
 
 
 
 
87d4f50
 
 
 
2d4ab0a
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/**
 * API 客户端
 */

const API_BASE = process.env.NEXT_PUBLIC_API_URL || '';

export interface KLine {
  date: string;
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
  amount?: number;
  pct_chg?: number;
  volume_ratio?: number; // 量比
  turnover_rate?: number; // 换手率
}

export interface GameStartResponse {
  code: string | null;
  real_code: string;
  real_name: string;
  start_date: string;
  klines: KLine[];
  total_candles: number;
  initial_price: number;
  initial_index: number;
}

export interface HealthResponse {
  status: string;
  database: string;
  stocks_count: number;
}

export class ApiError extends Error {
  status: number;
  data: any;

  constructor(message: string, status: number, data: any) {
    super(message);
    this.name = 'ApiError';
    this.status = status;
    this.data = data;
  }
}

async function fetchAPI<T>(path: string, options?: RequestInit): Promise<T> {
  const url = `${API_BASE}${path}`;
  const response = await fetch(url, {
    ...options,
    headers: {
      'Content-Type': 'application/json',
      ...options?.headers,
    },
  });

  if (!response.ok) {
    const errorData = await response.json().catch(() => ({ detail: 'Unknown error' }));
    // 尝试提取错误信息
    let message = 'API request failed';
    if (typeof errorData.detail === 'string') {
      message = errorData.detail;
    } else if (errorData.detail && errorData.detail.message) {
      message = errorData.detail.message;
    } else if (errorData.error) {
      message = errorData.error;
    }

    throw new ApiError(message, response.status, errorData);
  }

  return response.json();
}

export interface IndexData {
  date: string;
  close: number;
}

export interface AuthResponse {
  token: string;
  user_id: number;
  username: string;
}

export interface UserInfoResponse {
  user_id: number;
  username: string;
  vip_expire_at: string | null;
  is_vip: boolean;
}

export interface VipStatusResponse {
  is_vip: boolean;
  vip_expire_at: string | null;
}

export interface AdminUserItem {
  user_id: number;
  username: string;
  is_vip: boolean;
  vip_expire_at: string | null;
  created_at: string;
  has_payment: boolean;  // 是否有支付记录
  last_login: string | null;  // 最近登录时间
}

export interface CreatePaymentResponse {
  order_id: string;
  price: number;
  type: number;
}


export interface AdminOrderItem {
  order_id: string;
  amount: number;
  pay_type: number;
  status: string;
  created_at: string;
  paid_at: string | null;
  months: number;
}


export const api = {
  health: () => fetchAPI<HealthResponse>('/api/health'),

  startGame: (mode = 'random', market?: string, token?: string) => {
    const params = new URLSearchParams({ mode });
    if (market) params.append('market', market);
    const headers: Record<string, string> = {};
    if (token) headers['Authorization'] = `Bearer ${token}`;
    return fetchAPI<GameStartResponse>(`/api/game/start?${params}`, { headers });
  },

  getKline: (code: string, start?: string, end?: string) => {
    const params = new URLSearchParams({ code });
    if (start) params.append('start', start);
    if (end) params.append('end', end);
    return fetchAPI<KLine[]>(`/api/kline?${params}`);
  },

  getHS300Index: (start?: string, end?: string) => {
    const params = new URLSearchParams();
    if (start) params.append('start', start);
    if (end) params.append('end', end);
    return fetchAPI<IndexData[]>(`/api/index/hs300?${params}`);
  },

  register: (username: string, password: string) =>
    fetchAPI<AuthResponse>('/api/v1/auth/register', {
      method: 'POST',
      body: JSON.stringify({ username, password }),
    }),

  login: (username: string, password: string) =>
    fetchAPI<AuthResponse>('/api/v1/auth/login', {
      method: 'POST',
      body: JSON.stringify({ username, password }),
    }),

  me: (token: string) =>
    fetchAPI<UserInfoResponse>('/api/v1/auth/me', {
      headers: { Authorization: `Bearer ${token}` },
    }),

  vipStatus: (token: string) =>
    fetchAPI<VipStatusResponse>('/api/v1/vip/status', {
      headers: { Authorization: `Bearer ${token}` },
    }),

  logout: (token: string) =>
    fetchAPI<{ status: string }>('/api/v1/auth/logout', {
      method: 'POST',
      headers: { Authorization: `Bearer ${token}` },
    }),

  getUsageToday: (token: string) =>
    fetchAPI<{ used: number; limit: number | null; remaining: number | null; is_vip: boolean }>(
      '/api/v1/usage/today',
      { headers: { Authorization: `Bearer ${token}` } }
    ),

  createPayment: (type: number, months: number, token: string) =>
    fetchAPI<CreatePaymentResponse>('/api/v1/payment/create', {
      method: 'POST',
      headers: { Authorization: `Bearer ${token}` },
      body: JSON.stringify({ type, months }),
    }),

  async checkPaymentStatus(orderId: string): Promise<{ code: number; data: { status: number } }> {
    return fetchAPI(`/api/v1/payment/check/${orderId}`);
  },

  // 管理员接口
  adminGetUsers: (token: string) =>
    fetchAPI<AdminUserItem[]>('/api/v1/admin/users', {
      headers: { Authorization: `Bearer ${token}` },
    }),

  adminUpdatePassword: (userId: number, newPassword: string, token: string) =>
    fetchAPI<{ status: string }>(`/api/v1/admin/user/${userId}/password`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${token}` },
      body: JSON.stringify({ new_password: newPassword }),
    }),

  adminUpdateVip: (userId: number, months: number, token: string) =>
    fetchAPI<{ status: string }>(`/api/v1/admin/user/${userId}/vip`, {
      method: 'POST',
      headers: { Authorization: `Bearer ${token}` },
      body: JSON.stringify({ months }),
    }),

  adminDeleteUser: (userId: number, token: string) =>
    fetchAPI<{ status: string }>(`/api/v1/admin/user/${userId}`, {
      method: 'DELETE',
      headers: { Authorization: `Bearer ${token}` },
    }),

  adminUpdateOrderStatus: (orderId: string, status: string, token: string) =>
    fetchAPI<{ status: string }>(`/api/v1/admin/order/${orderId}/status`, {
      method: 'PUT',
      headers: { Authorization: `Bearer ${token}` },
      body: JSON.stringify({ status }),
    }),

  adminUserOrders: (userId: number, token: string) =>
    fetchAPI<AdminOrderItem[]>(`/api/v1/admin/user/${userId}/orders`, {
      headers: { Authorization: `Bearer ${token}` },
    }),
};