Files
onboard/resources/js/bootstrap.js
brusnitsyn 0a882b0cb2 Добавлена страница штаба
Добавлены графики
Добавлены события отчетов
2026-05-31 21:57:21 +09:00

149 lines
4.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios from 'axios';
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
import {useAuthStore} from "./Stores/auth.js";
import {startGlobalLoading, stopGlobalLoading} from "./Composables/useGlobalLoading.js";
window.axios = axios;
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT ?? 8080,
wssPort: import.meta.env.VITE_REVERB_PORT ?? 8080,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'http') === 'https',
enabledTransports: ['ws', 'wss'],
withCredentials: true,
});
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.axios.defaults.withCredentials = true
window.axios.defaults.withXSRFToken = true;
// Добавляем токен авторизации к запросам
window.axios.interceptors.request.use(
(config) => {
startGlobalLoading()
const authStore = useAuthStore()
// Если токен есть, добавляем его в заголовки
if (authStore.token) {
config.headers.Authorization = `Bearer ${authStore.token}`
}
// Для форм добавляем заголовок Content-Type
if (config.data instanceof FormData) {
config.headers['Content-Type'] = 'multipart/form-data'
} else {
config.headers['Content-Type'] = 'application/json'
}
return config
},
(error) => {
stopGlobalLoading()
return Promise.reject(error)
}
)
// Интерцептор ответов для обработки ошибок авторизации
window.axios.interceptors.response.use(
(response) => {
stopGlobalLoading()
return response
},
(error) => {
stopGlobalLoading()
const authStore = useAuthStore()
if (error.response?.status === 401) {
if (authStore.isAuthenticated) {
authStore.logout()
}
}
return Promise.reject(error)
}
)
// Вспомогательные методы для работы с API
const api = {
// GET запрос
get: async (url, config = {}) => {
try {
const response = await axios.get(url, config)
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// POST запрос
post: async (url, data = {}, config = {}) => {
try {
const response = await axios.post(url, data, config)
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// PUT запрос
put: async (url, data = {}, config = {}) => {
try {
const response = await axios.put(url, data, config)
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// PATCH запрос
patch: async (url, data = {}, config = {}) => {
try {
const response = await axios.patch(url, data, config)
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// DELETE запрос
delete: async (url, config = {}) => {
try {
const response = await axios.delete(url, config)
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// Загрузка файлов
upload: async (url, formData, config = {}) => {
try {
const response = await axios.post(url, formData, {
...config,
headers: {
'Content-Type': 'multipart/form-data'
}
})
return response.data
} catch (error) {
throw error.response?.data || error
}
},
// Проверка токена
checkToken: async () => {
try {
const response = await axios.get('/auth/check-token')
return response.data.valid
} catch {
return false
}
}
}
export default api