hesabixCore/webUI/src/hesabixConfig.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2025-03-21 14:20:43 +03:30
import axios from 'axios';
2025-03-22 15:55:28 +03:30
// ثابت‌ها
const KEYS = {
DEV_API_URL: 'dev_api_url',
DEV_ALERT_SHOWN: 'dev_mode_alert_shown',
SITE_NAME: 'hesabix_site_name',
SITE_SLOGON: 'hesabix_site_slogon'
};
const DEFAULTS = {
SITE_NAME: 'حسابیکس',
SITE_SLOGON: 'حسابیکس سامانه جامع مدیریت کسب‌و‌کار'
};
2025-03-21 14:20:43 +03:30
export const name = "hesabixConfig";
2025-03-22 15:55:28 +03:30
// کش برای API URL
let cachedApiUrl = null;
2025-03-21 14:20:43 +03:30
export function getApiUrl() {
2025-03-22 15:55:28 +03:30
if (cachedApiUrl) return cachedApiUrl;
2025-03-21 14:20:43 +03:30
2025-03-22 15:55:28 +03:30
const devApiUrl = localStorage.getItem(KEYS.DEV_API_URL);
const alertShown = localStorage.getItem(KEYS.DEV_ALERT_SHOWN);
if (devApiUrl) {
if (!alertShown) {
alert(`شما در حالت توسعه هستید و به آدرس زیر متصل می‌شوید:\n${devApiUrl}`);
localStorage.setItem(KEYS.DEV_ALERT_SHOWN, 'true');
}
cachedApiUrl = devApiUrl;
return devApiUrl;
}
2025-03-21 14:20:43 +03:30
2025-03-22 15:55:28 +03:30
const origin = window.location.origin;
const pathParts = window.location.pathname.split('/').filter(part => part !== '');
2025-03-21 22:35:12 +03:30
const uIndex = pathParts.indexOf('u');
2025-03-22 15:55:28 +03:30
2025-03-21 22:35:12 +03:30
if (uIndex !== -1) {
2025-03-22 15:55:28 +03:30
const basePath = pathParts.slice(0, uIndex).join('/');
cachedApiUrl = basePath ? `${origin}/${basePath}` : origin;
} else {
cachedApiUrl = origin;
2025-03-21 14:20:43 +03:30
}
2025-03-22 15:55:28 +03:30
return cachedApiUrl;
2025-03-21 14:20:43 +03:30
}
2025-03-22 15:55:28 +03:30
// تابع کمکی برای گرفتن داده از سرور و کش کردن
async function fetchAndCache(url, localStorageKey, defaultValue) {
const storedValue = localStorage.getItem(localStorageKey);
if (storedValue) return storedValue;
2025-03-21 14:20:43 +03:30
try {
2025-03-22 15:55:28 +03:30
const response = await axios.get(url);
if (response.status === 200) {
const data = response.data;
localStorage.setItem(localStorageKey, data);
return data;
}
throw new Error('پاسخ نامعتبر از سرور');
2025-03-21 14:20:43 +03:30
} catch (error) {
2025-03-22 15:55:28 +03:30
console.error(`خطا در گرفتن داده از ${url}:`, error);
return defaultValue;
2025-03-21 14:20:43 +03:30
}
}
2025-03-22 15:55:28 +03:30
export async function getSiteName() {
return fetchAndCache(
`${getApiUrl()}/system/getname`,
KEYS.SITE_NAME,
DEFAULTS.SITE_NAME
);
}
2025-03-23 01:00:34 +03:30
export async function getSiteSlogan() {
2025-03-22 15:55:28 +03:30
return fetchAndCache(
`${getApiUrl()}/system/getslogon`,
KEYS.SITE_SLOGON,
DEFAULTS.SITE_SLOGON
);
}
export function getBasePath() {
const fullPath = window.location.pathname;
const uIndex = fullPath.indexOf('/u');
return uIndex !== -1 ? fullPath.substring(0, uIndex + '/u'.length) + '/' : '/u/';
}
2025-03-21 14:20:43 +03:30
export function getVersionCheckerUrl() {
return 'https://api.hesabix.ir/clude/last-version';
}