baseUrl.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { pluginsAll } from '/@/views/plugins/index';
  2. /**
  3. * @description 校验是否为租户模式。租户模式把域名替换成 域名 加端口
  4. */
  5. export const getBaseURL = function () {
  6. var baseURL = import.meta.env.VITE_API_URL as any;
  7. var param = baseURL.split('/')[3] || '';
  8. // @ts-ignore
  9. if (pluginsAll && pluginsAll.indexOf('dvadmin3-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) {
  10. // 1.把127.0.0.1 替换成和前端一样域名
  11. // 2.把 ip 地址替换成和前端一样域名
  12. // 3.把 /api 或其他类似的替换成和前端一样域名
  13. // document.domain
  14. var host = baseURL.split('/')[2];
  15. if (host) {
  16. var port = baseURL.split(':')[2] || 80;
  17. if (port === 80 || port === 443) {
  18. host = document.domain;
  19. } else {
  20. host = document.domain + ':' + port;
  21. }
  22. baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param;
  23. } else {
  24. baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
  25. }
  26. }
  27. if (!baseURL.endsWith('/')) {
  28. baseURL += '/';
  29. }
  30. return baseURL;
  31. };
  32. export const getWsBaseURL = function () {
  33. let baseURL = import.meta.env.VITE_API_URL as any;
  34. let param = baseURL.split('/')[3] || '';
  35. // @ts-ignore
  36. if (pluginsAll && pluginsAll.indexOf('dvadmin3-tenants-web') !== -1 && (!param || baseURL.startsWith('/'))) {
  37. // 1.把127.0.0.1 替换成和前端一样域名
  38. // 2.把 ip 地址替换成和前端一样域名
  39. // 3.把 /api 或其他类似的替换成和前端一样域名
  40. // document.domain
  41. var host = baseURL.split('/')[2];
  42. if (host) {
  43. var port = baseURL.split(':')[2] || 80;
  44. if (port === 80 || port === 443) {
  45. host = document.domain;
  46. } else {
  47. host = document.domain + ':' + port;
  48. }
  49. baseURL = baseURL.split('/')[0] + '//' + baseURL.split('/')[1] + host + '/' + param;
  50. } else {
  51. baseURL = location.protocol + '//' + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
  52. }
  53. } else if (param !== '' || baseURL.startsWith('/')) {
  54. baseURL = (location.protocol === 'https:' ? 'wss://' : 'ws://') + location.hostname + (location.port ? ':' : '') + location.port + baseURL;
  55. }
  56. if (!baseURL.endsWith('/')) {
  57. baseURL += '/';
  58. }
  59. if (baseURL.startsWith('http')) {
  60. // https 也默认会被替换成 wss
  61. baseURL = baseURL.replace('http', 'ws');
  62. }
  63. return baseURL;
  64. };