| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- window.ss = {
- dom: {
- TYPE: {
- INPUT: 1,
- OBJP: 2,
- DATE: 3,
- TIME: 4,
- DATETIME: 5,
- ONOFFBTN: 6,
- PIC: 7,
- HTML: 9,
- CCPSINGLE: 51, //只对应一个对象属性的,带编码规则的级联菜单,如“出生地区码”(对应省、市、区)
- CCPMUTIPLE: 52, //对应多个对象属性的级联菜单
- SEARCHINPUT: 31, //查询页用到的文本框
- SEARCHDATE: 33, //查询页用到的日期选择
- SEARCHTIME: 34, //查询页用到的时间选择
- SEARCHDATETIME: 35, //查询页用到的日期时间选择
- },
- },
- config: {},
- };
- window.SS = window.ss; //最终都用小写ss,暂时先兼容大写SS
- (function () {
- const H5_ASSET_VERSION = "20261126-select-overlay-v1";
- // ========== 移动端适配:设置根元素font-size ==========
- // 设计稿宽度430px,分成10份,1rem = 43px(在430px屏幕上)
- function setRootFontSize() {
- const designWidth = 430; // 设计稿宽度
- const rootValue = designWidth / 10; // 43px
- const clientWidth =
- document.documentElement.clientWidth || window.innerWidth;
- // 计算当前屏幕对应的根font-size
- const rootFontSize = (clientWidth / designWidth) * rootValue;
- // 设置根元素font-size
- document.documentElement.style.fontSize = rootFontSize + "px";
- // 调试信息(可选)
- // console.log(`屏幕宽度: ${clientWidth}px, 根font-size: ${rootFontSize}px, 1rem = ${rootFontSize}px`);
- }
- // 立即执行
- setRootFontSize();
- // 监听窗口大小变化
- window.addEventListener("resize", setRootFontSize);
- // 监听屏幕旋转
- window.addEventListener("orientationchange", setRootFontSize);
- // ========== 移动端适配结束 ==========
- const _styleDeps = [
- "/skin/mp_easy/vant.min.css",
- "/skin/mp_easy/base.css",
- "/skin/mp_easy/iconfont.css",
- ];
- // 依赖按层级并行加载,既缩短等待时间,又保证后层脚本执行时基础库已经就绪。
- const _scriptWaves = [
- [
- "/js/mp_jq/jquery1.11.3.min.js",
- "/js/mp_vue/vue.global.prod.js",
- "/js/mp_axios/axios.min.js",
- "/js/mp_jweixin/jweixin-1.6.0.js",
- "/js/mp_dayjs/dayjs.min.js",
- ],
- [
- // Vant 依赖 Vue,必须放在 Vue 完成后的下一批并行加载。
- "/js/mp_vant/vant.min.js",
- "/js/mp_bundle/mp-common.bundle.min.js",
- ],
- ];
- // 仅富文本编辑器、图片裁剪组件使用的重量级依赖,改为组件使用时按需加载。
- const OPTIONAL_ASSETS = {
- jodit: [
- ["style", "/skin/easy/css/jodit.css"],
- ["script", "/js/jodit/jodit.js"],
- ],
- cropper: [
- ["style", "/skin/mp_easy/cropper.min.css"],
- ["script", "/js/mp_cropper/cropper.min.js"],
- ],
- };
- const loadedResources = new Map();
- const optionalAssetPromises = new Map();
- function loadResource(type, src, module) {
- const resourceSrc = src.includes("?")
- ? `${src}&v=${encodeURIComponent(H5_ASSET_VERSION)}`
- : `${src}?v=${encodeURIComponent(H5_ASSET_VERSION)}`;
- const cacheKey = `${type}:${resourceSrc}`;
- if (loadedResources.has(cacheKey)) {
- return loadedResources.get(cacheKey);
- }
- const promise = new Promise((resolve, reject) => {
- const elem = document.createElement(
- type === "script" ? "script" : "link"
- );
- if (type === "script") {
- elem.src = resourceSrc;
- elem.type = module ? "module" : "";
- } else {
- elem.rel = "stylesheet";
- elem.href = resourceSrc;
- }
- elem.onload = () => {
- if (type === "script") {
- // elem.remove();
- }
- resolve();
- };
- elem.onerror = reject;
- document.head.appendChild(elem);
- });
- loadedResources.set(cacheKey, promise);
- promise.catch(() => {
- // 网络抖动时允许后续调用重试,避免一次失败永久缓存 rejected Promise。
- loadedResources.delete(cacheKey);
- });
- return promise;
- }
- function ensureOptionalAssets(name) {
- const assets = OPTIONAL_ASSETS[name];
- if (!assets) {
- return Promise.reject(new Error(`未知可选资源: ${name}`));
- }
- if (!optionalAssetPromises.has(name)) {
- const promise = Promise.all(
- assets.map(([type, src, module]) => loadResource(type, src, module))
- ).catch((error) => {
- optionalAssetPromises.delete(name);
- throw error;
- });
- optionalAssetPromises.set(name, promise);
- }
- return optionalAssetPromises.get(name);
- }
- window.SS.loadResource = loadResource;
- window.SS.loadScript = (src, module = "") =>
- loadResource("script", src, module);
- window.SS.loadStyle = (src) => loadResource("style", src);
- window.SS.ensureOptionalAssets = ensureOptionalAssets;
- // 等待所有依赖加载完成后再执行回调
- window.SS.ready = function (callback) {
- if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
- callback();
- } else {
- document.addEventListener("SSReady", () => {
- // 确保组件初始化完成后再执行回调
- if (window.SS && window.SS.dom && window.SS.dom.initializeFormApp) {
- callback();
- }
- });
- }
- };
- async function loadAll() {
- const startedAt = performance.now();
- const stylePromise = Promise.all(
- _styleDeps.map((src) => loadResource("style", src))
- );
- for (const wave of _scriptWaves) {
- await Promise.all(wave.map((src) => loadResource("script", src)));
- }
- await stylePromise;
- // 所有 H5 页面公共依赖加载完成后,先判断是否需要做消息直达登录分流。
- // 如果这里已经跳去了 login/autoLogin,就直接终止当前页面初始化,避免业务页先渲染再被重定向。
- if (typeof window.handleMessageEntryAuthRedirect === "function") {
- const redirected = window.handleMessageEntryAuthRedirect();
- if (redirected) {
- return;
- }
- }
- const durationMs = Math.round(performance.now() - startedAt);
- const publicAssetPaths = new Set([..._styleDeps, ..._scriptWaves.flat()]);
- const resources = performance
- .getEntriesByType("resource")
- .filter((entry) => {
- try {
- const path = new URL(entry.name, window.location.href).pathname;
- return publicAssetPaths.has(path);
- } catch (error) {
- return false;
- }
- })
- .map((entry) => ({
- path: new URL(entry.name, window.location.href).pathname,
- durationMs: Math.round(entry.duration),
- transferSize: entry.transferSize || 0,
- encodedBodySize: entry.encodedBodySize || 0,
- }));
- window.__PMS_ASSET_PERF__ = {
- version: H5_ASSET_VERSION,
- durationMs,
- scriptWaves: _scriptWaves.map((wave) => wave.length),
- styleCount: _styleDeps.length,
- resourceCount: resources.length,
- resources,
- optionalAssetsLoaded: false,
- };
- console.info("PMS H5公共资源加载完成", window.__PMS_ASSET_PERF__);
- document.dispatchEvent(new Event("SSReady"));
- }
- // 当 DOM 加载完成后执行清理和加载
- if (document.readyState === "loading") {
- document.addEventListener("DOMContentLoaded", loadAll);
- console.log("DOMContentLoaded");
- } else {
- loadAll();
- }
- })();
|