| |
| |
|
|
| class ApiStatsClient { |
| constructor() { |
| this.baseURL = window.location.origin |
| |
| this.isDev = import.meta.env.DEV |
| } |
|
|
| async request(url, options = {}) { |
| try { |
| |
| if (this.isDev && url.startsWith('/admin')) { |
| url = '/webapi' + url |
| } |
|
|
| const response = await fetch(`${this.baseURL}${url}`, { |
| headers: { |
| 'Content-Type': 'application/json', |
| ...options.headers |
| }, |
| ...options |
| }) |
|
|
| const data = await response.json() |
|
|
| if (!response.ok) { |
| throw new Error(data.message || `请求失败: ${response.status}`) |
| } |
|
|
| return data |
| } catch (error) { |
| console.error('API Stats request error:', error) |
| throw error |
| } |
| } |
|
|
| |
| async getKeyId(apiKey) { |
| return this.request('/apiStats/api/get-key-id', { |
| method: 'POST', |
| body: JSON.stringify({ apiKey }) |
| }) |
| } |
|
|
| |
| async getUserStats(apiId) { |
| return this.request('/apiStats/api/user-stats', { |
| method: 'POST', |
| body: JSON.stringify({ apiId }) |
| }) |
| } |
|
|
| |
| async getUserModelStats(apiId, period = 'daily') { |
| return this.request('/apiStats/api/user-model-stats', { |
| method: 'POST', |
| body: JSON.stringify({ apiId, period }) |
| }) |
| } |
|
|
| |
| async getOemSettings() { |
| try { |
| return await this.request('/admin/oem-settings') |
| } catch (error) { |
| console.error('Failed to load OEM settings:', error) |
| return { |
| success: true, |
| data: { |
| siteName: 'Claude Relay Service', |
| siteIcon: '', |
| siteIconData: '' |
| } |
| } |
| } |
| } |
|
|
| |
| async getBatchStats(apiIds) { |
| return this.request('/apiStats/api/batch-stats', { |
| method: 'POST', |
| body: JSON.stringify({ apiIds }) |
| }) |
| } |
|
|
| |
| async getBatchModelStats(apiIds, period = 'daily') { |
| return this.request('/apiStats/api/batch-model-stats', { |
| method: 'POST', |
| body: JSON.stringify({ apiIds, period }) |
| }) |
| } |
| } |
|
|
| export const apiStatsClient = new ApiStatsClient() |
|
|