loader.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var strUrl = window.location.href;
  2. var strIp = '';
  3. strIp = strUrl.split('//')[1].split('/')[0]
  4. if (strIp == 'localhost:8080') {
  5. strIp = '172.16.50.4:18080'
  6. }
  7. if (strIp == '127.0.0.1:8080') {
  8. strIp = '172.16.50.4:18080'
  9. }
  10. console.log("strIp", strIp)
  11. // 众趣公有云域名
  12. // const urlBaseSet = {
  13. // OSS: "https://bucket-model.oss-cn-beijing.aliyuncs.com",
  14. // CDN: "https://modelcdn2.3dnest.cn",
  15. // ICDN: "https://infocdn2.3dnest.cn",
  16. // TCDN: "https://template2.3dnest.cn",
  17. // EDIT: "/manager_house/model/rtiframedata/",
  18. // };
  19. const urlBaseSet = {
  20. OSS: "https://bucket-model.oss-cn-beijing.aliyuncs.com",
  21. //CDN: "http://127.0.0.1:8080",
  22. //ICDN: "http://127.0.0.1:8080",
  23. //TCDN: "http://127.0.0.1:8080",
  24. // CDN: "http://172.16.50.4:18080",
  25. // ICDN: "http://172.16.50.4:18080",
  26. // TCDN: "http://172.16.50.4:18080",
  27. CDN: "http://" + strIp,
  28. ICDN: "http://" + strIp,
  29. TCDN: "http://" + strIp,
  30. EDIT: "/manager_house/model/rtiframedata/",
  31. };
  32. let modelId; // 模型id
  33. let modelVersion; // 模型版本号
  34. let modelSetting; // 模型版本设置
  35. let companyId; // 公司Id
  36. let globalSetting = null;
  37. // 获取页面对应的单个参数 start by ytj
  38. function getParams(key) {
  39. let search = window.location.search.replace(/^\?/, "");
  40. let pairs = search.split("&");
  41. let paramsMap = pairs.map(pair => {
  42. let [key, value] = pair.split("=");
  43. return [decodeURIComponent(key), decodeURIComponent(value)];
  44. }).reduce((res, [key, value]) => Object.assign(res, { [key]: value }), {});
  45. return paramsMap[key] || "";
  46. }
  47. // 获取页面对应的单个参数 end by ytj
  48. // 获取模型版本信息
  49. const getModelVesionUrl = (urlBaseSet) => {
  50. modelId = getParams('m');
  51. console.log(modelId)
  52. let currentDate = new Date().getTime();
  53. return `${urlBaseSet.CDN}/${modelId}/version.txt?f=${currentDate}`
  54. }
  55. // 获取模型配置信息
  56. const getModelSettingUrl = (urlBaseSet, modelVersion, modelSetting) => {
  57. return `${urlBaseSet.ICDN}/${modelId}/${modelVersion}/settings.txt?s=${modelSetting}`
  58. }
  59. // 获取模型公司配置
  60. const getCompanySettingUrl = (companyId) => {
  61. let currentDate = new Date().getTime();
  62. return `${urlBaseSet.TCDN} /company/${companyId}/company.txt?t=${currrentDate}`
  63. }
  64. const fetchData = (url) => {
  65. return new Promise((resolve, reject) => {
  66. const xhr = new XMLHttpRequest();
  67. xhr.open('GET', url);
  68. xhr.onload = () => {
  69. if (xhr.status >= 200 && xhr.status < 300) {
  70. resolve(JSON.parse(xhr.responseText));
  71. } else {
  72. reject(xhr.statusText);
  73. }
  74. };
  75. xhr.onerror = () => reject(xhr.statusText);
  76. xhr.send();
  77. });
  78. }
  79. // 这是我们的包装函数,它返回一个数组
  80. const awaitWrap = (promise) => {
  81. return promise
  82. .then(data => [null, data])
  83. .catch(err => [err, null]);
  84. }
  85. // 这是我们的主函数,它按顺序执行三个请求,并合并返回的数据
  86. const fetchAndCombine = async () => {
  87. console.log('房间加载start', Date.now())
  88. let combinedData = {};
  89. let backgroundBgUrl = `${urlBaseSet.ICDN}/${modelId}/https://infocdn2.3dnest.cn/881f840a_q8X6_b6f9_DAP/background.jpg?`
  90. try {
  91. let modelVersionUrl = getModelVesionUrl(urlBaseSet);
  92. console.log('modelVersionUrl', modelVersionUrl)
  93. const [err1, modelVersionData] = await awaitWrap(fetchData(modelVersionUrl));
  94. if (err1) throw new Error(`Error fetching data from ${modelVersionUrl}: ${err1}`);
  95. console.log('=====模型版本数据========', modelVersionData);
  96. // combinedData = { ...combinedData, ...modelVersionData };
  97. let modelSettingUrl = getModelSettingUrl(urlBaseSet, modelVersionData.version, modelVersionData.setting)
  98. console.log('modelSettingUrl', modelSettingUrl)
  99. const [err2, modelSettingData] = await awaitWrap(fetchData(modelSettingUrl));
  100. if (err2) throw new Error(`Error fetching data from url2: ${err2}`);
  101. // combinedData = { ...combinedData, ...modelSettingData };
  102. console.log('=====模型配置数据========', modelSettingData);
  103. if (!!modelSettingData?.basic?.user?.group && !!modelSettingData?.basic?.user?.group > 1) {
  104. let companySettingUrl = getCompanySettingUrl(modelSettingData.basic.user.group)
  105. console.log('companySettingUrl', companySettingUrl)
  106. const [err3, data3] = await awaitWrap(fetchData(companySettingUrl));
  107. if (err3) throw new Error(`Error fetching data from url3: ${err3}`);
  108. console.log('=====模型公司配置========', data3);
  109. } else {
  110. }
  111. let siteConf = {
  112. "type": "display",
  113. "versionpre": `${urlBaseSet.CDN}/${modelId}/`,
  114. "modelpre": `${urlBaseSet.CDN}/${modelId}/`,
  115. "infopre": `${urlBaseSet.ICDN}/${modelId}/`,
  116. "setpre": `${urlBaseSet.ICDN}/${modelId}/`
  117. }
  118. globalSetting = {
  119. modelId: modelId,
  120. settings: modelSettingData,
  121. version: modelVersionData,
  122. siteConf: siteConf,
  123. enterType: 3
  124. }
  125. return [null, globalSetting];
  126. // combinedData = { ...combinedData, ...data3 };
  127. } catch (error) {
  128. return [error, null];
  129. }
  130. return [null, globalSetting];
  131. }
  132. // 导出主函数
  133. export default fetchAndCombine;