163 lines
3.5 KiB
JavaScript
163 lines
3.5 KiB
JavaScript
import axios from 'axios';
|
|
|
|
import { useAuthStore } from "../store/authStore";
|
|
|
|
const BASE_URL = import.meta.env.DEV ? "/" : "https://khalijpay-core.qaserver.ir";
|
|
|
|
// ساخت instance از axios
|
|
const api = axios.create({
|
|
baseURL: BASE_URL,
|
|
withCredentials: true, // ارسال و دریافت cookie/session
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
// -----------------------------
|
|
// Interceptor پاسخها
|
|
// -----------------------------
|
|
api.interceptors.response.use(
|
|
(response) => response,
|
|
(error) => {
|
|
if (error.response?.status === 401) {
|
|
// session منقضی شده → هدایت به login
|
|
const setLoggedIn = useAuthStore.getState().setLoggedIn;
|
|
setLoggedIn(false);
|
|
window.location.href = "/";
|
|
}
|
|
return Promise.reject(error.response?.data || error);
|
|
}
|
|
);
|
|
|
|
// -----------------------------
|
|
// توابع API
|
|
// -----------------------------
|
|
|
|
// Login
|
|
export async function login(username, password) {
|
|
try {
|
|
const res = await api.post("/api/v1/Auth/SignIn", {
|
|
// include common variants to satisfy different backends
|
|
userName: username,
|
|
username: username,
|
|
email: username,
|
|
password: password,
|
|
});
|
|
return res.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// خروج از سیستم
|
|
export async function signOut() {
|
|
try {
|
|
const res = await api.post("/api/v1/Auth/SignOut");
|
|
// پاک کردن وضعیت login در Zustand
|
|
const setLoggedIn = useAuthStore.getState().setLoggedIn;
|
|
setLoggedIn(false);
|
|
window.location.href = "/";
|
|
return res.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// فراموشی رمز عبور
|
|
export async function forgotPassword(email) {
|
|
try {
|
|
const res = await api.post("/api/v1/Auth/ForgotPassword", { email });
|
|
return res.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
// گرفتن دادههای محافظتشده
|
|
export async function fetchProtectedData(endpoint) {
|
|
try {
|
|
const res = await api.get(endpoint);
|
|
return res.data;
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Mock data for development
|
|
const mockData = {
|
|
stats: {
|
|
total: 1247,
|
|
success: 1189,
|
|
failed: 58
|
|
},
|
|
payments: [
|
|
{
|
|
id: 'TXN-001',
|
|
user: 'John Doe',
|
|
amount: 299.99,
|
|
status: 'success',
|
|
date: '2024-01-15T10:30:00Z',
|
|
currency: 'USD'
|
|
},
|
|
{
|
|
id: 'TXN-002',
|
|
user: 'Jane Smith',
|
|
amount: 150.00,
|
|
status: 'pending',
|
|
date: '2024-01-15T11:45:00Z',
|
|
currency: 'USD'
|
|
},
|
|
{
|
|
id: 'TXN-003',
|
|
user: 'Bob Johnson',
|
|
amount: 75.50,
|
|
status: 'failed',
|
|
date: '2024-01-15T12:15:00Z',
|
|
currency: 'USD'
|
|
},
|
|
{
|
|
id: 'TXN-004',
|
|
user: 'Alice Brown',
|
|
amount: 450.00,
|
|
status: 'success',
|
|
date: '2024-01-15T13:20:00Z',
|
|
currency: 'USD'
|
|
},
|
|
{
|
|
id: 'TXN-005',
|
|
user: 'Charlie Wilson',
|
|
amount: 89.99,
|
|
status: 'success',
|
|
date: '2024-01-15T14:30:00Z',
|
|
currency: 'USD'
|
|
}
|
|
],
|
|
chartData: [
|
|
{ date: '2024-01-09', amount: 1200 },
|
|
{ date: '2024-01-10', amount: 1900 },
|
|
{ date: '2024-01-11', amount: 3000 },
|
|
{ date: '2024-01-12', amount: 2800 },
|
|
{ date: '2024-01-13', amount: 1890 },
|
|
{ date: '2024-01-14', amount: 2390 },
|
|
{ date: '2024-01-15', amount: 3490 }
|
|
]
|
|
};
|
|
|
|
// Expose mock payment API for dashboard until real endpoints are integrated
|
|
export const paymentsAPI = {
|
|
async getStats() {
|
|
return mockData.stats;
|
|
},
|
|
async getChartData() {
|
|
return mockData.chartData;
|
|
},
|
|
};
|