From 23b513b644720666c5bda5073b34567cbadbbe4a Mon Sep 17 00:00:00 2001
From: tuqin <tuqin@tuqin.cn>
Date: 星期四, 05 十一月 2026 16:45:03 +0800
Subject: [PATCH] fix(map): 按百度官方规范修正 SDK 加载,并加诊断页定位线上加载失败

---
 src/components/map/src/utils.ts |  115 +++++++++++++++++++---------
 public/bmap-test.html           |  109 +++++++++++++++++++++++++++
 2 files changed, 185 insertions(+), 39 deletions(-)

diff --git a/public/bmap-test.html b/public/bmap-test.html
new file mode 100644
index 0000000..9118654
--- /dev/null
+++ b/public/bmap-test.html
@@ -0,0 +1,109 @@
+<!DOCTYPE html>
+<html lang="zh-CN">
+  <head>
+    <meta charset="UTF-8" />
+    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+    <title>鐧惧害鍦板浘鍔犺浇璇婃柇</title>
+    <style>
+      body {
+        padding: 16px;
+        font-family: -apple-system, 'Microsoft YaHei', sans-serif;
+        font-size: 14px;
+      }
+      h3 { margin: 0 0 12px; }
+      #log {
+        padding: 12px;
+        background: #f5f5f5;
+        border-radius: 6px;
+        font-family: Consolas, Monaco, monospace;
+        font-size: 13px;
+        line-height: 1.9;
+        white-space: pre-wrap;
+        word-break: break-all;
+      }
+      .ok { color: #389e0d; font-weight: 600; }
+      .bad { color: #cf1322; font-weight: 600; }
+      .warn { color: #d46b08; }
+      #map { width: 100%; height: 320px; margin-top: 12px; border: 1px solid #ddd; }
+      .tip { margin-top: 12px; padding: 10px; background: #e6f4ff; border-radius: 6px; }
+    </style>
+  </head>
+  <body>
+    <h3>鐧惧害鍦板浘 SDK 鍔犺浇璇婃柇</h3>
+    <div id="log"></div>
+    <div class="tip">
+      鎶婁笂闈㈢殑鏃ュ織鏁存鎴浘鎴栧鍒跺彂缁欐妧鏈紝灏辫兘瀹氫綅闂銆�
+    </div>
+    <div id="map"></div>
+
+    <script>
+      var logEl = document.getElementById('log');
+      function log(msg, cls) {
+        var time = new Date().toLocaleTimeString('zh-CN', { hour12: false });
+        var div = document.createElement('div');
+        div.className = cls || '';
+        div.textContent = '[' + time + '] ' + msg;
+        logEl.appendChild(div);
+      }
+
+      var KEY = 'Y2aJXiswwPxy6mwFs1z9c7U5gwX9WfUN';
+      var startAt = Date.now();
+
+      log('娴忚鍣�: ' + navigator.userAgent);
+      log('椤甸潰鍦板潃: ' + location.href);
+      log('椤甸潰鍗忚: ' + location.protocol);
+      log('鏄惁鍦ㄧ嚎: ' + navigator.onLine);
+      log('');
+
+      // 涓庢寮忎唬鐮佸畬鍏ㄤ竴鑷寸殑鍔犺浇鏂瑰紡
+      window.BMAP_PROTOCOL = 'https';
+      window.BMapGL_loadScriptTime = Date.now();
+      var url =
+        'https://api.map.baidu.com/getscript?type=webgl&v=1.0&ak=' +
+        KEY +
+        '&services=&t=' +
+        Date.now();
+      log('寮�濮嬪姞杞� SDK: ' + url);
+      log('');
+
+      var script = document.createElement('script');
+      script.src = url;
+
+      script.addEventListener('load', function () {
+        var cost = ((Date.now() - startAt) / 1000).toFixed(2);
+        log('load 浜嬩欢瑙﹀彂锛堣�楁椂 ' + cost + ' 绉掞級', 'ok');
+        log('window.BMapGL = ' + (window.BMapGL ? '瀛樺湪' : '涓嶅瓨鍦�'), window.BMapGL ? 'ok' : 'bad');
+
+        if (!window.BMapGL) {
+          log('SDK 鑴氭湰涓嬭浇浜嗭紝浣嗘病鏈夋寕杞藉湴鍥惧璞� 鈫� Key 鎴栧煙鍚嶇櫧鍚嶅崟闂', 'bad');
+          return;
+        }
+
+        log('鍦板浘 SDK 鍔犺浇鎴愬姛锛屽紑濮嬪垱寤哄湴鍥锯��', 'ok');
+        try {
+          var map = new window.BMapGL.Map(document.getElementById('map'));
+          map.centerAndZoom(new window.BMapGL.Point(87.616848, 43.825592), 14);
+          map.enableScrollWheelZoom(true);
+          log('鍦板浘瀹炰緥鍒涘缓鎴愬姛锛屼笅鏂瑰簲鑳界湅鍒颁箤椴佹湪榻愬湴鍥�', 'ok');
+        } catch (e) {
+          log('鍒涘缓鍦板浘澶辫触: ' + e.message, 'bad');
+        }
+      });
+
+      script.addEventListener('error', function () {
+        log('error 浜嬩欢瑙﹀彂锛氳姹傛牴鏈病鍒拌揪鐧惧害鏈嶅姟鍣�', 'bad');
+        log('鍙兘鍘熷洜锛氬叕鍙哥綉缁�/闃茬伀澧欐嫤鎴�佹祻瑙堝櫒鎻掍欢鎷︽埅銆丏NS 瑙f瀽澶辫触', 'warn');
+        log('璇峰鍒朵笅闈㈣繖涓湴鍧�锛屽湪娴忚鍣ㄥ湴鍧�鏍忕洿鎺ユ墦寮�鍋氬姣旀祴璇曪細', 'warn');
+        log(url);
+      });
+
+      setTimeout(function () {
+        if (!window.BMapGL) {
+          log('宸茬粡杩囦簡 8 绉掞紝鍦板浘浠嶆湭灏辩华锛堝鏋滀笂闈㈡病鏈変换浣曚簨浠舵棩蹇楋紝灏辨槸璇锋眰琚寕浣忎簡锛�', 'bad');
+        }
+      }, 8000);
+
+      document.body.appendChild(script);
+    </script>
+  </body>
+</html>
diff --git a/src/components/map/src/utils.ts b/src/components/map/src/utils.ts
index fb0bcbc..8937315 100644
--- a/src/components/map/src/utils.ts
+++ b/src/components/map/src/utils.ts
@@ -1,28 +1,76 @@
 /**
  * 鐧惧害鍦板浘 SDK 鍔犺浇宸ュ叿
  *
- * 閲嶈锛氳繖閲屽繀椤荤洿杩� getscript锛屼笉鑳界敤 api?... 鍦板潃銆�
- * api?v=1.0&type=webgl&ak=xxx 杩斿洖鐨勬槸涓�娈�
- *   document.write('<script src="https://api.map.baidu.com/getscript?..."></script>')
- * 鑰岄�氳繃 document.body.append(script) 鍔ㄦ�佹彃鍏ョ殑鑴氭湰閲岋紝document.write 浼氳娴忚鍣ㄦ嫤鎴�
- * 锛圕hrome 鐩存帴鎻愮ず "A parser-blocking, cross site script ... invoked via document.write was NOT executed"锛夛紝
- * 瀵艰嚧鐪熸鐨� SDK 浠庢湭鍔犺浇銆亀indow.BMapGL 涓�鐩存槸 undefined/null銆�
- * 浣� api 閲岀殑 callback 浠嶄細琚皟鐢紝璋冪敤鏂硅浠ヤ负鍔犺浇鎴愬姛锛屾帴鐫�鎵ц new BMapGL.Map()
- * 灏辨姏鍑恒�孋annot read properties of null (reading 'Map')銆嶃��
+ * 鍔犺浇鏂瑰紡璇存槑锛堣俯杩囩殑鍧戯級锛�
  *
- * getscript 杩斿洖鐨勬槸 875KB 鐨勭湡瀹� SDK锛堝凡瀹炴祴 HTTP 200 涓斿惈 BMapGL 瀹氫箟锛夛紝
- * 鐩存帴鐢� script.src 鍔犺浇鍗冲彲锛岃剼鏈墽琛屽畬 window.BMapGL 灏卞凡灏辩华銆�
+ * 1. 涓嶈兘鐢� api?v=1.0&type=webgl&ak=xxx 杩欎釜鍦板潃銆�
+ *    瀹冭繑鍥炵殑鏄竴娈� document.write('<script src=".../getscript?...">')锛�
+ *    鑰岄�氳繃 document.body.append(script) 鍔ㄦ�佹彃鍏ョ殑鑴氭湰閲� document.write 浼氳娴忚鍣ㄦ嫤鎴紝
+ *    瀵艰嚧鐪熸鐨� SDK 浠庢湭鎵ц銆亀indow.BMapGL 涓�鐩存槸绌猴紱浣� api 鐨� callback 浠嶄細琚皟鐢紝
+ *    璋冪敤鏂硅鍒ゆ垚鍔燂紝鎺ョ潃 new BMapGL.Map() 灏辨姏
+ *    "Cannot read properties of null (reading 'Map')"銆�
+ *
+ * 2. 蹇呴』鐩磋繛 getscript锛屽畠鏄� 875KB 鐨勭湡瀹� SDK銆�
+ *
+ * 3. 鍔犺浇鍓嶈璁剧疆 window.BMAP_PROTOCOL = 'https'锛堢櫨搴﹀畼鏂硅姹傦級锛�
+ *    鍚﹀垯 SDK 鍐呴儴鎸夐粯璁ゅ崗璁嫾瀛愯祫婧愬湴鍧�锛孒TTPS 椤甸潰涓嬩細琚綋浣滄贩鍚堝唴瀹规嫤鎴��
+ *
+ * 4. 涓嶈缁� script 鍔� async = true锛歋DK 鍐呴儴鐢� document.currentScript 瀹氫綅鑷韩锛�
+ *    async 鑴氭湰鎵ц鏈熼棿 currentScript 涓� null锛屼細璁╁畠璧板埌 fallback 鍒嗘敮鐢氳嚦鎷夸笉鍒拌嚜韬湴鍧�銆�
+ *
+ * 5. 澶辫触閲嶈瘯涓�娆★細鍋跺彂缃戠粶鎶栧姩锛堝挨鍏舵槸璺ㄧ渷/绉诲姩缃戠粶锛夐噸璇曞線寰�灏辫兘杩囥��
  */
 
 // 鎵╁睍 Window 鎺ュ彛浠ュ寘鍚櫨搴﹀湴鍥� GL API
 declare global {
   interface Window {
     BMapGL: any;
+    BMAP_PROTOCOL: any;
+    BMapGL_loadScriptTime: any;
   }
 }
 
 // SDK 鍔犺浇鐘舵��
 let loadPromise: null | Promise<void> = null;
+// 鍔犺浇澶辫触娆℃暟锛岀敤浜庡喅瀹氭槸鍚﹂噸璇�
+let failCount = 0;
+
+/** 鍗曟灏濊瘯鍔犺浇 */
+function loadOnce(accessKey: string, timeout: number): Promise<void> {
+  return new Promise((resolve, reject) => {
+    const timeoutId = setTimeout(() => {
+      reject(new Error('鐧惧害鍦板浘 SDK 鍔犺浇瓒呮椂锛岃妫�鏌ョ綉缁�'));
+    }, timeout);
+
+    const script = document.createElement('script');
+    script.src = `https://api.map.baidu.com/getscript?type=webgl&v=1.0&ak=${accessKey}&services=&t=${Date.now()}`;
+    // 娉ㄦ剰锛氫笉瑕佽 async锛孲DK 渚濊禆 document.currentScript 瀹氫綅鑷韩
+
+    script.addEventListener('load', () => {
+      clearTimeout(timeoutId);
+      if (!window.BMapGL) {
+        // ak 鏃犳晥 / 鍩熷悕鏈巿鏉� / 琚嫤鎴椂锛岃剼鏈兘鍔犺浇浣嗕笉浼氭寕杞� BMapGL
+        reject(
+          new Error('鐧惧害鍦板浘鍔犺浇寮傚父锛氭湭鑾峰彇鍒板湴鍥惧璞★紝璇锋鏌� Key 鎴栧煙鍚嶆巿鏉�'),
+        );
+        return;
+      }
+      resolve();
+    });
+
+    // 浜嬩欢鍚嶆槸 error锛屼笉鏄� onerror锛堝啓 onerror 姘歌繙涓嶄細瑙﹀彂锛�
+    script.addEventListener('error', () => {
+      clearTimeout(timeoutId);
+      reject(
+        new Error(
+          '鐧惧害鍦板浘 SDK 鍔犺浇澶辫触锛堟棤娉曡闂� api.map.baidu.com锛屽彲鑳芥槸缃戠粶闄愬埗鎴栨彃浠舵嫤鎴級',
+        ),
+      );
+    });
+
+    document.body.append(script);
+  });
+}
 
 /**
  * 鍔犺浇鐧惧害鍦板浘 GL SDK
@@ -45,38 +93,27 @@
     return Promise.reject(new Error('鏈厤缃� VITE_BAIDU_MAP_KEY'));
   }
 
-  loadPromise = new Promise((resolve, reject) => {
-    const timeoutId = setTimeout(() => {
-      loadPromise = null;
-      reject(new Error('鐧惧害鍦板浘 SDK 鍔犺浇瓒呮椂锛岃妫�鏌ョ綉缁�'));
-    }, timeout);
+  // 鐧惧害瀹樻柟瑕佹眰锛氬厛澹版槑鍗忚锛岄伩鍏� HTTPS 椤甸潰涓� SDK 鎷煎嚭 http 瀛愯祫婧愬湴鍧�
+  if (typeof window !== 'undefined') {
+    window.BMAP_PROTOCOL = 'https';
+    window.BMapGL_loadScriptTime = Date.now();
+  }
 
-    const script = document.createElement('script');
-    script.src = `https://api.map.baidu.com/getscript?type=webgl&v=1.0&ak=${accessKey}&services=&t=${Date.now()}`;
-    script.async = true;
-
-    script.addEventListener('load', () => {
-      clearTimeout(timeoutId);
-      if (!window.BMapGL) {
-        // ak 鏃犳晥 / 鍩熷悕鏈巿鏉� / 琚嫤鎴椂锛岃剼鏈兘鍔犺浇浣嗕笉浼氭寕杞� BMapGL
+  loadPromise = (async () => {
+    try {
+      await loadOnce(accessKey, timeout);
+    } catch (error) {
+      // 棣栨澶辫触閲嶈瘯涓�娆★紝搴斿鍋跺彂缃戠粶鎶栧姩
+      if (failCount < 1) {
+        failCount += 1;
+        await loadOnce(accessKey, timeout);
+      } else {
+        failCount = 0;
         loadPromise = null;
-        reject(
-          new Error('鐧惧害鍦板浘鍔犺浇寮傚父锛氭湭鑾峰彇鍒板湴鍥惧璞★紝璇锋鏌� Key 鎴栧煙鍚嶆巿鏉�'),
-        );
-        return;
+        throw error;
       }
-      resolve();
-    });
-
-    // 娉ㄦ剰锛氫簨浠跺悕鏄� error锛屼笉鏄� onerror锛堝啓 onerror 姘歌繙涓嶄細瑙﹀彂锛�
-    script.addEventListener('error', () => {
-      clearTimeout(timeoutId);
-      loadPromise = null;
-      reject(new Error('鐧惧害鍦板浘 SDK 鍔犺浇澶辫触锛岃妫�鏌ョ綉缁�'));
-    });
-
-    document.body.append(script);
-  });
+    }
+  })();
 
   return loadPromise;
 };

--
Gitblit v1.9.3