Initial commit: полный проект my_speaker
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
const DEFAULT_BACKEND_URL = "http://192.268.200.111:8000";
|
||||
let backendUrl = localStorage.getItem("backendUrl") || DEFAULT_BACKEND_URL;
|
||||
|
||||
export function setBackendUrl(url) {
|
||||
backendUrl = url;
|
||||
localStorage.setItem("backendUrl", url);
|
||||
}
|
||||
|
||||
export async function apiFetch(endpoint, options = {}) {
|
||||
const token = localStorage.getItem("token");
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
const response = await fetch(`${backendUrl}/api${endpoint}`, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
localStorage.removeItem("token");
|
||||
window.location.reload(); // Редирект на login
|
||||
}
|
||||
throw new Error(`API error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function login(username, password) {
|
||||
return apiFetch("/auth/login", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
}
|
||||
|
||||
export async function logout() {
|
||||
await apiFetch("/auth/logout", { method: "POST" });
|
||||
localStorage.removeItem("token");
|
||||
window.location.reload();
|
||||
}
|
||||
|
||||
// Другие методы по API_DOCUMENTATION.md
|
||||
export async function getSpeechData() {
|
||||
return apiFetch("/dashboard/speech-data");
|
||||
}
|
||||
|
||||
export async function getSysMonitor() {
|
||||
return apiFetch("/dashboard/sys-monitor");
|
||||
}
|
||||
|
||||
export async function getRecentPhrases(limit = 10) {
|
||||
return apiFetch(`/dashboard/recent-phrases?limit=${limit}`);
|
||||
}
|
||||
|
||||
export async function getSettings() {
|
||||
return apiFetch("/settings");
|
||||
}
|
||||
|
||||
export async function updateSettings(section, data) {
|
||||
return apiFetch(`/settings/${section}`, {
|
||||
method: "PUT",
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
}
|
||||
|
||||
export async function restart() {
|
||||
return apiFetch("/settings/restart", { method: "POST" });
|
||||
}
|
||||
Reference in New Issue
Block a user