2025-11-10 21:22:58 +00:00
|
|
|
import axios from 'axios'
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
const API_URL =
|
|
|
|
|
import.meta.env.VITE_API_URL ||
|
2025-11-10 21:22:58 +00:00
|
|
|
(import.meta.env.PROD ? '/api' : 'http://localhost:3000/api')
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
const api = axios.create({
|
|
|
|
|
baseURL: API_URL,
|
2025-11-10 21:56:36 +00:00
|
|
|
withCredentials: true
|
2025-11-10 21:22:58 +00:00
|
|
|
})
|
2025-11-10 20:13:22 +00:00
|
|
|
|
2025-11-10 22:19:37 +00:00
|
|
|
api.interceptors.request.use((config) => {
|
|
|
|
|
const initData = window.Telegram?.WebApp?.initData;
|
|
|
|
|
if (initData) {
|
|
|
|
|
config.headers = config.headers || {};
|
|
|
|
|
if (!config.headers.Authorization) {
|
|
|
|
|
config.headers.Authorization = `tma ${initData}`;
|
|
|
|
|
}
|
2025-11-10 22:48:18 +00:00
|
|
|
if (!config.headers['x-telegram-init-data']) {
|
|
|
|
|
config.headers['x-telegram-init-data'] = initData;
|
|
|
|
|
}
|
2025-11-10 22:19:37 +00:00
|
|
|
}
|
|
|
|
|
return config;
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-10 23:16:43 +00:00
|
|
|
// Response interceptor для обработки устаревших токенов
|
|
|
|
|
api.interceptors.response.use(
|
|
|
|
|
(response) => response,
|
|
|
|
|
(error) => {
|
|
|
|
|
const status = error?.response?.status;
|
|
|
|
|
const errorMessage = error?.response?.data?.error || '';
|
|
|
|
|
|
|
|
|
|
// Если токен устарел или невалиден - перезагрузить приложение
|
|
|
|
|
if (status === 401 && (
|
|
|
|
|
errorMessage.includes('устарели') ||
|
|
|
|
|
errorMessage.includes('expired') ||
|
|
|
|
|
errorMessage.includes('Неверная подпись')
|
|
|
|
|
)) {
|
|
|
|
|
console.warn('[Moderation API] Auth token expired or invalid, reloading app...');
|
|
|
|
|
|
|
|
|
|
// Показать уведомление пользователю
|
|
|
|
|
const tg = window.Telegram?.WebApp;
|
|
|
|
|
if (tg?.showAlert) {
|
|
|
|
|
tg.showAlert('Сессия устарела. Перезагрузка...', () => {
|
|
|
|
|
window.location.reload();
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
setTimeout(() => window.location.reload(), 1000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-11-10 21:56:36 +00:00
|
|
|
export const verifyAuth = () => api.post('/mod-app/auth/verify').then((res) => res.data.user)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const fetchUsers = (params = {}) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.get('/mod-app/users', { params }).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const banUser = (userId, data) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.put(`/mod-app/users/${userId}/ban`, data).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const fetchPosts = (params = {}) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.get('/mod-app/posts', { params }).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const updatePost = (postId, data) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.put(`/mod-app/posts/${postId}`, data).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const deletePost = (postId) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.delete(`/mod-app/posts/${postId}`).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const removePostImage = (postId, index) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.delete(`/mod-app/posts/${postId}/images/${index}`).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const banPostAuthor = (postId, data) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.post(`/mod-app/posts/${postId}/ban`, data).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const fetchReports = (params = {}) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.get('/mod-app/reports', { params }).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const updateReportStatus = (reportId, data) =>
|
2025-11-10 21:22:58 +00:00
|
|
|
api.put(`/mod-app/reports/${reportId}`, data).then((res) => res.data)
|
2025-11-10 20:13:22 +00:00
|
|
|
|
|
|
|
|
export const publishToChannel = (formData) =>
|
|
|
|
|
api.post('/mod-app/channel/publish', formData, {
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'multipart/form-data'
|
|
|
|
|
}
|
2025-11-10 21:22:58 +00:00
|
|
|
})
|
2025-11-10 20:13:22 +00:00
|
|
|
|
2025-11-11 00:40:29 +00:00
|
|
|
export const fetchAdmins = () =>
|
|
|
|
|
api.get('/mod-app/admins').then((res) => res.data)
|
|
|
|
|
|
|
|
|
|
export const initiateAddAdmin = (userId, adminNumber) =>
|
|
|
|
|
api.post('/mod-app/admins/initiate-add', { userId, adminNumber }).then((res) => res.data)
|
|
|
|
|
|
|
|
|
|
export const confirmAddAdmin = (userId, code) =>
|
|
|
|
|
api.post('/mod-app/admins/confirm-add', { userId, code }).then((res) => res.data)
|
|
|
|
|
|
|
|
|
|
export const initiateRemoveAdmin = (adminId) =>
|
|
|
|
|
api.post('/mod-app/admins/initiate-remove', { adminId }).then((res) => res.data)
|
|
|
|
|
|
|
|
|
|
export const confirmRemoveAdmin = (adminId, code) =>
|
|
|
|
|
api.post('/mod-app/admins/confirm-remove', { adminId, code }).then((res) => res.data)
|
|
|
|
|
|
2025-12-04 21:45:02 +00:00
|
|
|
export const getPostComments = (postId) =>
|
|
|
|
|
api.get(`/posts/${postId}`).then((res) => res.data.post)
|
|
|
|
|
|
|
|
|
|
export const deleteComment = (postId, commentId) =>
|
|
|
|
|
api.delete(`/posts/${postId}/comments/${commentId}`).then((res) => res.data)
|
|
|
|
|
|
2025-11-10 21:22:58 +00:00
|
|
|
export default api
|
2025-11-10 20:13:22 +00:00
|
|
|
|