62 lines
1.7 KiB
JavaScript
62 lines
1.7 KiB
JavaScript
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "/api";
|
|
const TOKEN_KEY = "hyapp-admin.access-token";
|
|
const APP_CODE_KEY = "hyapp-admin.app-code";
|
|
const APP_CODE_HEADER = "X-App-Code";
|
|
|
|
export function getCurrentAppCode() {
|
|
return normalizeAppCode(window.localStorage.getItem(APP_CODE_KEY) || "lalu");
|
|
}
|
|
|
|
export async function fetchStatisticsOverview({ appCode, countryId, endMs, startMs }) {
|
|
const query = new URLSearchParams();
|
|
query.set("app_code", normalizeAppCode(appCode) || "lalu");
|
|
if (startMs) {
|
|
query.set("start_ms", String(startMs));
|
|
}
|
|
if (endMs) {
|
|
query.set("end_ms", String(endMs));
|
|
}
|
|
if (countryId) {
|
|
query.set("country_id", String(countryId));
|
|
}
|
|
|
|
const response = await fetch(`${API_BASE_URL}/v1/statistics/overview?${query.toString()}`, {
|
|
credentials: "include",
|
|
headers: requestHeaders(appCode)
|
|
});
|
|
const payload = await readJSON(response);
|
|
if (!response.ok || payload.code !== 0) {
|
|
throw new Error(payload.message || response.statusText || "请求失败");
|
|
}
|
|
return payload.data;
|
|
}
|
|
|
|
function requestHeaders(appCode) {
|
|
const headers = {};
|
|
const token = window.localStorage.getItem(TOKEN_KEY);
|
|
if (token) {
|
|
headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
const normalized = normalizeAppCode(appCode);
|
|
if (normalized) {
|
|
headers[APP_CODE_HEADER] = normalized;
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
async function readJSON(response) {
|
|
const text = await response.text();
|
|
if (!text) {
|
|
return {};
|
|
}
|
|
try {
|
|
return JSON.parse(text);
|
|
} catch {
|
|
return { code: response.ok ? 0 : response.status, message: text };
|
|
}
|
|
}
|
|
|
|
function normalizeAppCode(value) {
|
|
return String(value || "").trim().toLowerCase();
|
|
}
|