utils.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. export const isDevelopment = import.meta.env.MODE === "development";
  2. export const getBaseUrl = (url) => {
  3. let cleanUrl = url.split("#")[0];
  4. cleanUrl = cleanUrl.replace(/([^:])\/{2,}/g, "$1/");
  5. const regex = /^(https?:\/\/[^\/]+(?:\/[^\/]+)*)(\/(pc|mobile))\/?.*$/;
  6. const match = cleanUrl.match(regex);
  7. if (match) {
  8. return match[1];
  9. }
  10. return null;
  11. };
  12. export const baseUrl = getBaseUrl(location.href);
  13. export const getEnvImagePath = (path) => {
  14. return isDevelopment
  15. ? "http://192.168.0.18:8080" + path
  16. : `${baseUrl}/base${path}`;
  17. };
  18. export function isMobile() {
  19. const userAgent = navigator.userAgent.toLowerCase();
  20. return /iphone|ipod|android|windows phone|blackberry|mobile/i.test(userAgent);
  21. }
  22. export function checkDeviceAndRedirect() {
  23. const isMobileDevice = isMobile();
  24. const currentPath = window.location.pathname;
  25. const isInMobilePath = currentPath.includes("/mobile/");
  26. const isInPCPath = currentPath.includes("/pc/");
  27. if (isMobileDevice && !isInMobilePath) {
  28. window.location.href =
  29. location.origin + currentPath.replace("/pc/", "/mobile/");
  30. } else if (!isMobileDevice && !isInPCPath) {
  31. window.location.href =
  32. location.origin + currentPath.replace("/mobile/", "/pc/");
  33. }
  34. }