base.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. window.ss = {
  2. dom: {
  3. TYPE: {
  4. INPUT: 1,
  5. OBJP: 2,
  6. DATE: 3,
  7. TIME: 4,
  8. DATETIME: 5,
  9. ONOFFBTN: 6,
  10. PIC: 7,
  11. HTML: 9,
  12. CCPSINGLE: 51, //只对应一个对象属性的,带编码规则的级联菜单,如“出生地区码”(对应省、市、区)
  13. CCPMUTIPLE: 52, //对应多个对象属性的级联菜单
  14. SEARCHINPUT: 31, //查询页用到的文本框
  15. SEARCHDATE: 33, //查询页用到的日期选择
  16. SEARCHTIME: 34, //查询页用到的时间选择
  17. SEARCHDATETIME: 35, //查询页用到的日期时间选择
  18. },
  19. },
  20. config: {},
  21. };
  22. window.SS = window.ss; //最终都用小写ss,暂时先兼容大写SS
  23. (function () {
  24. const H5_ASSET_VERSION = "20261126-select-overlay-v1";
  25. // ========== 移动端适配:设置根元素font-size ==========
  26. // 设计稿宽度430px,分成10份,1rem = 43px(在430px屏幕上)
  27. function setRootFontSize() {
  28. const designWidth = 430; // 设计稿宽度
  29. const rootValue = designWidth / 10; // 43px
  30. const clientWidth =
  31. document.documentElement.clientWidth || window.innerWidth;
  32. // 计算当前屏幕对应的根font-size
  33. const rootFontSize = (clientWidth / designWidth) * rootValue;
  34. // 设置根元素font-size
  35. document.documentElement.style.fontSize = rootFontSize + "px";
  36. // 调试信息(可选)
  37. // console.log(`屏幕宽度: ${clientWidth}px, 根font-size: ${rootFontSize}px, 1rem = ${rootFontSize}px`);
  38. }
  39. // 立即执行
  40. setRootFontSize();
  41. // 监听窗口大小变化
  42. window.addEventListener("resize", setRootFontSize);
  43. // 监听屏幕旋转
  44. window.addEventListener("orientationchange", setRootFontSize);
  45. // ========== 移动端适配结束 ==========
  46. const _styleDeps = [
  47. "/skin/mp_easy/vant.min.css",
  48. "/skin/mp_easy/base.css",
  49. "/skin/mp_easy/iconfont.css",
  50. ];
  51. // 依赖按层级并行加载,既缩短等待时间,又保证后层脚本执行时基础库已经就绪。
  52. const _scriptWaves = [
  53. [
  54. "/js/mp_jq/jquery1.11.3.min.js",
  55. "/js/mp_vue/vue.global.prod.js",
  56. "/js/mp_axios/axios.min.js",
  57. "/js/mp_jweixin/jweixin-1.6.0.js",
  58. "/js/mp_dayjs/dayjs.min.js",
  59. ],
  60. [
  61. // Vant 依赖 Vue,必须放在 Vue 完成后的下一批并行加载。
  62. "/js/mp_vant/vant.min.js",
  63. "/js/mp_bundle/mp-common.bundle.min.js",
  64. ],
  65. ];
  66. // 仅富文本编辑器、图片裁剪组件使用的重量级依赖,改为组件使用时按需加载。
  67. const OPTIONAL_ASSETS = {
  68. jodit: [
  69. ["style", "/skin/easy/css/jodit.css"],
  70. ["script", "/js/jodit/jodit.js"],
  71. ],
  72. cropper: [
  73. ["style", "/skin/mp_easy/cropper.min.css"],
  74. ["script", "/js/mp_cropper/cropper.min.js"],
  75. ],
  76. };
  77. const loadedResources = new Map();
  78. const optionalAssetPromises = new Map();
  79. function loadResource(type, src, module) {
  80. const resourceSrc = src.includes("?")
  81. ? `${src}&v=${encodeURIComponent(H5_ASSET_VERSION)}`
  82. : `${src}?v=${encodeURIComponent(H5_ASSET_VERSION)}`;
  83. const cacheKey = `${type}:${resourceSrc}`;
  84. if (loadedResources.has(cacheKey)) {
  85. return loadedResources.get(cacheKey);
  86. }
  87. const promise = new Promise((resolve, reject) => {
  88. const elem = document.createElement(
  89. type === "script" ? "script" : "link"
  90. );
  91. if (type === "script") {
  92. elem.src = resourceSrc;
  93. elem.type = module ? "module" : "";
  94. } else {
  95. elem.rel = "stylesheet";
  96. elem.href = resourceSrc;
  97. }
  98. elem.onload = () => {
  99. if (type === "script") {
  100. // elem.remove();
  101. }
  102. resolve();
  103. };
  104. elem.onerror = reject;
  105. document.head.appendChild(elem);
  106. });
  107. loadedResources.set(cacheKey, promise);
  108. promise.catch(() => {
  109. // 网络抖动时允许后续调用重试,避免一次失败永久缓存 rejected Promise。
  110. loadedResources.delete(cacheKey);
  111. });
  112. return promise;
  113. }
  114. function ensureOptionalAssets(name) {
  115. const assets = OPTIONAL_ASSETS[name];
  116. if (!assets) {
  117. return Promise.reject(new Error(`未知可选资源: ${name}`));
  118. }
  119. if (!optionalAssetPromises.has(name)) {
  120. const promise = Promise.all(
  121. assets.map(([type, src, module]) => loadResource(type, src, module))
  122. ).catch((error) => {
  123. optionalAssetPromises.delete(name);
  124. throw error;
  125. });
  126. optionalAssetPromises.set(name, promise);
  127. }
  128. return optionalAssetPromises.get(name);
  129. }
  130. window.SS.loadResource = loadResource;
  131. window.SS.loadScript = (src, module = "") =>
  132. loadResource("script", src, module);
  133. window.SS.loadStyle = (src) => loadResource("style", src);
  134. window.SS.ensureOptionalAssets = ensureOptionalAssets;
  135. // 等待所有依赖加载完成后再执行回调
  136. window.SS.ready = function (callback) {
  137. if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
  138. callback();
  139. } else {
  140. document.addEventListener("SSReady", () => {
  141. // 确保组件初始化完成后再执行回调
  142. if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
  143. callback();
  144. }
  145. });
  146. }
  147. };
  148. async function loadAll() {
  149. const startedAt = performance.now();
  150. const stylePromise = Promise.all(
  151. _styleDeps.map((src) => loadResource("style", src))
  152. );
  153. for (const wave of _scriptWaves) {
  154. await Promise.all(wave.map((src) => loadResource("script", src)));
  155. }
  156. await stylePromise;
  157. // 所有 H5 页面公共依赖加载完成后,先判断是否需要做消息直达登录分流。
  158. // 如果这里已经跳去了 login/autoLogin,就直接终止当前页面初始化,避免业务页先渲染再被重定向。
  159. if (typeof window.handleMessageEntryAuthRedirect === "function") {
  160. const redirected = window.handleMessageEntryAuthRedirect();
  161. if (redirected) {
  162. return;
  163. }
  164. }
  165. const durationMs = Math.round(performance.now() - startedAt);
  166. const publicAssetPaths = new Set([..._styleDeps, ..._scriptWaves.flat()]);
  167. const resources = performance
  168. .getEntriesByType("resource")
  169. .filter((entry) => {
  170. try {
  171. const path = new URL(entry.name, window.location.href).pathname;
  172. return publicAssetPaths.has(path);
  173. } catch (error) {
  174. return false;
  175. }
  176. })
  177. .map((entry) => ({
  178. path: new URL(entry.name, window.location.href).pathname,
  179. durationMs: Math.round(entry.duration),
  180. transferSize: entry.transferSize || 0,
  181. encodedBodySize: entry.encodedBodySize || 0,
  182. }));
  183. window.__PMS_ASSET_PERF__ = {
  184. version: H5_ASSET_VERSION,
  185. durationMs,
  186. scriptWaves: _scriptWaves.map((wave) => wave.length),
  187. styleCount: _styleDeps.length,
  188. resourceCount: resources.length,
  189. resources,
  190. optionalAssetsLoaded: false,
  191. };
  192. console.info("PMS H5公共资源加载完成", window.__PMS_ASSET_PERF__);
  193. document.dispatchEvent(new Event("SSReady"));
  194. }
  195. // 当 DOM 加载完成后执行清理和加载
  196. if (document.readyState === "loading") {
  197. document.addEventListener("DOMContentLoaded", loadAll);
  198. console.log("DOMContentLoaded");
  199. } else {
  200. loadAll();
  201. }
  202. })();