app.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { md5 } from 'js-md5';
  2. import { parseHrefParam } from '../utils/utils';
  3. // domain
  4. export function getOrgCode() {
  5. let domain;
  6. const paramCode =
  7. (parseHrefParam(window.location.href, 'code') as string) ||
  8. window.sessionStorage.getItem('paramDomainCode');
  9. if (paramCode) {
  10. domain = paramCode;
  11. window.sessionStorage.setItem('paramDomainCode', domain);
  12. return domain;
  13. }
  14. domain = window.localStorage.getItem('domain_in_url');
  15. if (domain) return domain;
  16. const ipFormat = new RegExp(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/);
  17. const { hostname } = window.location;
  18. if (ipFormat.test(hostname) || hostname.includes('localhost')) return '';
  19. domain = hostname.split('.')[0];
  20. return domain;
  21. }
  22. export function getOrgCodeFromDomain(domain: string) {
  23. let code = '';
  24. if (!domain) return '';
  25. console.log('domain', domain);
  26. const hostname = domain.split('://')[1];
  27. if (!hostname) return '';
  28. const ipFormat = new RegExp(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/);
  29. if (ipFormat.test(hostname) || hostname.includes('localhost')) return '';
  30. code = hostname.split('.')[0];
  31. return code;
  32. }
  33. export const PLATFORM = 'WEB';
  34. if (!localStorage.getItem('deviceId')) {
  35. localStorage.setItem('deviceId', md5(`${Math.random()}-${Date.now()}`));
  36. }
  37. export const DEVICE_ID = localStorage.getItem('deviceId') as string;