fix api url

This commit is contained in:
genadis 2026-09-10 10:44:19 +00:00
parent 91e225ff2f
commit 5f878047fd

View File

@ -4,6 +4,9 @@ let events = { biot: [], ctpsr_main: [], gloves: [] };
let currentFilter = 'все'; let currentFilter = 'все';
let currentUser = null; let currentUser = null;
// URL бэкенда — меняется при каждом запуске
const API_URL = 'https://code.vibe42.kz/vscode/genadis/kak-ya-mogu-skopirovat-chuzh/proxy/3000';
// Проверка авторизации при загрузке // Проверка авторизации при загрузке
async function checkAuth() { async function checkAuth() {
const token = localStorage.getItem('token'); const token = localStorage.getItem('token');
@ -36,8 +39,7 @@ function showApp() {
async function login(username, password) { async function login(username, password) {
try { try {
// Используем GET с query параметрами — proxy пропускает GET const res = await fetch(API_URL + '/service/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), {
const res = await fetch('/service/login?username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password), {
method: 'GET' method: 'GET'
}); });
const data = await res.json(); const data = await res.json();
@ -47,6 +49,8 @@ async function login(username, password) {
localStorage.setItem('userName', data.name); localStorage.setItem('userName', data.name);
localStorage.setItem('userId', username); localStorage.setItem('userId', username);
showApp(); showApp();
await loadEvents();
render();
} else { } else {
alert(data.error || 'Ошибка входа'); alert(data.error || 'Ошибка входа');
} }
@ -54,6 +58,10 @@ async function login(username, password) {
alert('Ошибка соединения: ' + e.message); alert('Ошибка соединения: ' + e.message);
} }
} }
} catch (e) {
alert('Ошибка соединения: ' + e.message);
}
}
function logout() { function logout() {
localStorage.removeItem('token'); localStorage.removeItem('token');
@ -88,7 +96,7 @@ function closeAdmin() {
async function loadUsers() { async function loadUsers() {
try { try {
const res = await fetch('/service/users', { const res = await fetch(API_URL + '/service/users', {
headers: { 'Authorization': 'Bearer ' + currentUser.token } headers: { 'Authorization': 'Bearer ' + currentUser.token }
}); });
if (!res.ok) { if (!res.ok) {
@ -121,7 +129,7 @@ document.getElementById('add-user-form').addEventListener('submit', async functi
const password = document.getElementById('new-user-password').value.trim(); const password = document.getElementById('new-user-password').value.trim();
try { try {
const res = await fetch('/service/users', { const res = await fetch(API_URL + '/service/users', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@ -145,7 +153,7 @@ document.getElementById('add-user-form').addEventListener('submit', async functi
async function deleteUser(id) { async function deleteUser(id) {
if (!confirm('Удалить пользователя ' + id + '?')) return; if (!confirm('Удалить пользователя ' + id + '?')) return;
try { try {
const res = await fetch('/service/users/' + id, { const res = await fetch(API_URL + '/service/users/' + id, {
method: 'DELETE', method: 'DELETE',
headers: { 'Authorization': 'Bearer ' + currentUser.token } headers: { 'Authorization': 'Bearer ' + currentUser.token }
}); });
@ -166,9 +174,9 @@ async function loadEvents() {
try { try {
const headers = { 'Authorization': 'Bearer ' + currentUser.token }; const headers = { 'Authorization': 'Bearer ' + currentUser.token };
const [biotRes, ctpsrRes, glovesRes] = await Promise.all([ const [biotRes, ctpsrRes, glovesRes] = await Promise.all([
fetch('/service/events?tab=biot', { headers }), fetch(API_URL + '/service/events?tab=biot', { headers }),
fetch('/service/events?tab=ctpsr_main', { headers }), fetch(API_URL + '/service/events?tab=ctpsr_main', { headers }),
fetch('/service/events?tab=gloves', { headers }) fetch(API_URL + '/service/events?tab=gloves', { headers })
]); ]);
events.biot = await biotRes.json(); events.biot = await biotRes.json();
events.ctpsr_main = await ctpsrRes.json(); events.ctpsr_main = await ctpsrRes.json();
@ -436,7 +444,7 @@ function addNew() {
async function deleteEvent(id) { async function deleteEvent(id) {
if (!confirm('Вы уверены, что хотите удалить эту запись?')) return; if (!confirm('Вы уверены, что хотите удалить эту запись?')) return;
var key = currentSubtab || currentTab; var key = currentSubtab || currentTab;
await fetch('/service/events/' + id + '?tab=' + key, { await fetch(API_URL + '/service/events/' + id + '?tab=' + key, {
method: 'DELETE', method: 'DELETE',
headers: { 'Authorization': 'Bearer ' + currentUser.token } headers: { 'Authorization': 'Bearer ' + currentUser.token }
}); });
@ -487,7 +495,7 @@ document.getElementById('form').addEventListener('submit', async function(e) {
console.log('Data to save:', data); console.log('Data to save:', data);
var url = id ? '/service/events/' + id + '?tab=' + key : '/service/events?tab=' + key; var url = id ? API_URL + '/service/events/' + id + '?tab=' + key : API_URL + '/service/events?tab=' + key;
console.log('POST to:', url); console.log('POST to:', url);
try { try {
@ -545,7 +553,7 @@ async function testApi() {
return; return;
} }
try { try {
const res = await fetch('/service/events?tab=biot', { const res = await fetch(API_URL + '/service/events?tab=biot', {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',