requestUrl.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-04-19 15:46:08
  4. * @LastEditTime: 2025-12-02 17:00:17
  5. * @LastEditors: xieyunhui awesomedema@gmail.com
  6. * @Description: In User Settings Edit
  7. * @FilePath: /web_TracerMethodology/utils/requestUrl.js
  8. */
  9. // const base = '192.168.51.80:8801/imed/pfm/';
  10. export const networkType = 1; //网络类型:内网=0,外网=1
  11. // 运行时可更新的网关域名,由授权查询接口返回的 region 动态设置
  12. export function setRuntimeRegionDomain(region) {
  13. try {
  14. if (region && typeof region === 'string') {
  15. uni.setStorageSync('runtimeRegion', region);
  16. }
  17. } catch (e) {}
  18. }
  19. export function getRuntimeRegionDomain() {
  20. try {
  21. const r = uni.getStorageSync('runtimeRegion');
  22. return r || '';
  23. } catch (e) {
  24. return '';
  25. }
  26. }
  27. // 计算当前基础URL(无兜底,必须授权设置后才可用)
  28. export function getBaseURL() {
  29. // #ifdef H5
  30. try {
  31. if (typeof process !== 'undefined' && process.env && process.env.NODE_ENV === 'production') {
  32. // H5 生产:直连真实网关
  33. return 'http://47.97.190.5:8806/imed/pfm/';
  34. // return 'http://120.27.235.181:8806/imed/pfm/';
  35. // return 'http://120.27.235.181:8802/imed/pfm/';
  36. }
  37. } catch (e) {}
  38. // H5 开发:走本地代理前缀(由 devServer 重写到 /imed/pfm)
  39. return '/TracerMethodology/api/';
  40. // #endif
  41. // 非 H5(例如 APP)走授权下发的网关域名
  42. const region = getRuntimeRegionDomain();
  43. if (!region) {
  44. try { uni.showToast({ title: '接口域名缺失,请重新授权', icon: 'none' }); } catch (e) {}
  45. throw new Error('Missing runtime region domain');
  46. }
  47. const hasProtocol = /^https?:\/\//i.test(region);
  48. // 默认拼接 /imed/pfm/ 路径,可按需调整
  49. const origin = hasProtocol ? region : `http://${region}`;
  50. const normalizedOrigin = origin.replace(/\/$/, '');
  51. return `${normalizedOrigin}/imed/pfm/`;
  52. }
  53. // 兼容旧导出:提供一个按需计算的 URL 常量访问器
  54. export function getURL() { return getBaseURL(); }
  55. // WebSocket地址生成:基于当前HTTP基础地址替换协议
  56. export const wsURL = (hiId, user, permission) => {
  57. const httpBase = getBaseURL();
  58. const wsBase = httpBase.replace(/^http:/i, 'ws:').replace(/^https:/i, 'wss:');
  59. return `${wsBase}websocket/${hiId}/${user}/${permission}`;
  60. }