index.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import type { GlobConfig } from '@jeesite/types/config';
  2. import { getAppEnvConfig } from '@jeesite/core/utils/env';
  3. let globCache: Readonly<GlobConfig>;
  4. export const useGlobSetting = (): Readonly<GlobConfig> => {
  5. if (globCache) return globCache;
  6. const {
  7. VITE_GLOB_APP_TITLE,
  8. VITE_GLOB_API_URL,
  9. VITE_GLOB_APP_SHORT_NAME,
  10. VITE_GLOB_API_URL_PREFIX,
  11. // VITE_GLOB_UPLOAD_URL,
  12. VITE_GLOB_ADMIN_PATH,
  13. VITE_FILE_PREVIEW,
  14. } = getAppEnvConfig();
  15. const ctxPath = ((): string => {
  16. let ctx = VITE_GLOB_API_URL + VITE_GLOB_API_URL_PREFIX;
  17. let idx = ctx.indexOf('://');
  18. if (idx != -1) {
  19. ctx = ctx.substring(idx + 3);
  20. }
  21. idx = ctx.indexOf('/');
  22. if (idx != -1) {
  23. ctx = ctx.substring(idx);
  24. } else {
  25. ctx = '';
  26. }
  27. return ctx;
  28. })();
  29. const adminPath = VITE_GLOB_ADMIN_PATH as string;
  30. const ctxAdminPath = ctxPath + adminPath;
  31. // Take global configuration
  32. const glob: Readonly<GlobConfig> = {
  33. title: VITE_GLOB_APP_TITLE,
  34. apiUrl: VITE_GLOB_API_URL,
  35. shortName: VITE_GLOB_APP_SHORT_NAME,
  36. urlPrefix: VITE_GLOB_API_URL_PREFIX,
  37. // uploadUrl: VITE_GLOB_UPLOAD_URL,
  38. ctxPath: ctxPath,
  39. adminPath: adminPath,
  40. ctxAdminPath: ctxAdminPath,
  41. filePreview: VITE_FILE_PREVIEW || 'true',
  42. };
  43. globCache = glob;
  44. return glob as Readonly<GlobConfig>;
  45. };