baseUrl.ts 2.8 KB

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