Files
frontend-miniapp/static/sgs-map-sdk/index.global.js
lyf e473b6a2a5
Some checks failed
CI / verify (push) Has been cancelled
升级 SGS 地图 SDK 至 2.4.1
2026-07-13 11:00:04 +08:00

2 lines
14 KiB
JavaScript

"use strict";(()=>{var y=Object.defineProperty;var f=(d,o)=>{for(var e in o)y(d,e,{get:o[e],enumerable:!0})};var u=class{constructor(){this.events={}}on(o,e){this.events[o]||(this.events[o]=[]),this.events[o].push(e)}off(o,e){this.events[o]&&(this.events[o]=this.events[o].filter(s=>s!==e))}once(o,e){let s=t=>{this.off(o,s),e(t)};this.on(o,s)}emit(o,e){this.events[o]&&[...this.events[o]].forEach(t=>{try{t(e)}catch(r){console.error(`[EventEmitter] Error emitting event ${String(o)}:`,r)}})}removeAllListeners(o){o!==void 0?delete this.events[o]:this.events={}}listenerCount(o){return this.events[o]?.length??0}};var h={};f(h,{isIndoorNavigableFloor:()=>_,sortIndoorFloors:()=>R});function _(d){return d?d.toUpperCase()!=="EXTERIOR":!1}function R(d){let o=["L-2","L-1","B2","B1","L1","L1.5","L2","L3","L4","L5"],e=d.filter(t=>_(t.floorCode)),s=t=>{let r=t.trim().toUpperCase();r.startsWith("F")&&(r="L"+r.substring(1));let i=o.findIndex(a=>a.toUpperCase()===r);if(i!==-1)return i;let n=r.match(/-?\d+(\.\d+)?/);if(n){let a=parseFloat(n[0]);return r.startsWith("B")||r.includes("-")?-Math.abs(a)-100:a}return 0};return[...e].sort((t,r)=>s(t.floorCode)-s(r.floorCode))}var g="2.4.1",S=2,E=3e4,P=5e3,l=class extends u{constructor(e){super();this.iframe=null;this._isReady=!1;this._isDestroyed=!1;this._readyFailed=!1;this._readyFailedError=null;this.promiseQueue={};this._pendingCommands=[];this._pendingFireAndForget=[];this._messageListener=null;this._readyTimer=null;this._helloInterval=null;this.readyPayload=null;this._readyResolve=null;this._readyReject=null;this._iframeDomReadyMs=null;this._engineReadyMs=null;this._sdkCreatedAt=Date.now();let s=typeof e.container=="string"?document.getElementById(e.container):e.container;if(!s)throw new Error(`[SGSMapSDK] Cannot find container: ${e.container}`);this.container=s,this.sdkUrl=e.sdkUrl||"/h5-sdk",this.floorId=e.floorId||1,this.mapId=e.mapId||1,this.defaultTimeout=e.timeout||P,this.readyTimeoutMs=e.readyTimeout||E,this.targetOrigin=e.targetOrigin,this.targetOrigin||(this.sdkUrl.startsWith("http")?this.targetOrigin=new URL(this.sdkUrl).origin:typeof window<"u"?this.targetOrigin=window.location.origin:this.targetOrigin="*"),this._readyPromise=new Promise((t,r)=>{this._readyResolve=t,this._readyReject=r}),this._initIframeBridge()}_initIframeBridge(){let e=document.createElement("iframe"),s=new URL(this.sdkUrl,typeof window<"u"?window.location.href:"http://localhost");typeof window<"u"&&new URLSearchParams(window.location.search).forEach((r,i)=>{s.searchParams.set(i,r)}),s.searchParams.has("floorId")||s.searchParams.set("floorId",String(this.floorId)),e.src=s.toString(),e.style.cssText="width:100%;height:100%;border:none;background:transparent;",this.container.innerHTML="",this.container.appendChild(e),this.iframe=e,this._messageListener=t=>{if(this.targetOrigin!=="*"&&t.origin!==this.targetOrigin||!this.iframe||t.source!==this.iframe.contentWindow)return;let r=t.data;if(r){if(r.type==="SGS_MAP_EVENT"){let{action:i,payload:n}=r;i==="ON_POI_CLICK"?this.emit("poiClick",n):i==="ON_POI_DETAIL"?this.emit("poiDetail",n):i==="ON_POI_NAVIGATE"?this.emit("poiNavigate",n):i==="ON_POI_CLOSE"?this.emit("poiClose",n):i==="ON_SPACE_CLICK"?this.emit("spaceClick",n):i==="ON_SPACE_DETAIL"?this.emit("spaceDetail",n):i==="ON_SPACE_NAVIGATE"?this.emit("spaceNavigate",n):i==="ON_SPACE_CLOSE"?this.emit("spaceClose",n):i==="ENGINE_READY"?this._onEngineReady(n):i==="ON_FLOOR_CHANGED"?this.emit("floorChanged",n):i==="ON_ROUTE_TRANSFER"?this.emit("routeTransfer",n):i==="ON_MODEL_LOADING"?this.emit("modelLoading",n):i==="ON_MODEL_READY"?this.emit("modelReady",n):i==="ON_MODEL_ERROR"?this.emit("modelError",n):i==="ON_FLOOR_BUNDLE_READY"?this.emit("floorBundleReady",n):i==="ON_ROUTE_READY"?this.emit("routeReady",n):i==="ON_ROAMING_PROGRESS"?this.emit("roamingProgress",n):i==="ON_ROAMING_DONE"&&this.emit("roamingDone",n)}if(r.type==="SGS_MAP_RESPONSE"){let{action:i,requestId:n,payload:a,error:m}=r,c=this.promiseQueue[n];if(c){if(a&&a.success!==!1)c.resolve(a);else{let p=m||{code:"ERR_FAILED",message:`${i} command failed`};c.reject(p)}delete this.promiseQueue[n]}}}},window.addEventListener("message",this._messageListener),e.onload=()=>{if(this._iframeDomReadyMs=Date.now()-this._sdkCreatedAt,this._isReady)return;let t=()=>{if(this._isReady||this._isDestroyed){this._helloInterval&&(clearInterval(this._helloInterval),this._helloInterval=null);return}this._postCommand("HELLO",{protocolVersion:S,sdkVersion:g})};t(),this._helloInterval=setInterval(t,500),console.log(`[SGSMapSDK] Sent HELLO handshake (v${g}), will retry every 500ms until ENGINE_READY...`)},e.onerror=t=>{console.error("[SGSMapSDK] Iframe load error",t);let r={code:"ERR_IFRAME_LOAD",message:"Failed to load map engine iframe."};this.emit("loadError",{type:"iframe_error",detail:r.message}),this._rejectReady(r)},this._readyTimer=setTimeout(()=>{if(!this._isReady&&!this._isDestroyed){let t={code:"ERR_READY_TIMEOUT",message:`ENGINE_READY not received within ${this.readyTimeoutMs}ms`};console.error(`[SGSMapSDK] ${t.message}`),this._rejectReady(t)}},this.readyTimeoutMs)}_onEngineReady(e){this._isReady||(this._isReady=!0,this.readyPayload=e,this._engineReadyMs=Date.now()-this._sdkCreatedAt,this._helloInterval&&(clearInterval(this._helloInterval),this._helloInterval=null),this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._readyResolve&&(this._readyResolve(e),this._readyResolve=null,this._readyReject=null),this.emit("ready",e),console.log("[SGSMapSDK] Handshake complete: Engine Ready received \u2705"),this._flushPendingCommands())}_rejectReady(e){this._readyFailed=!0,this._readyFailedError=e,this._readyReject&&(this._readyReject(e),this._readyResolve=null,this._readyReject=null),this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._rejectAllPending(e.code,e.message),this._pendingFireAndForget.splice(0)}_flushPendingCommands(){let e=this._pendingFireAndForget.splice(0);for(let r of e)this._postCommand(r.action,r.payload);let s=this._pendingCommands.splice(0);for(let r of s)this._postCommandAsyncInner(r.action,r.payload,r.timeout).then(r.resolve).catch(r.reject);let t=e.length+s.length;t>0&&console.log(`[SGSMapSDK] Flushed ${t} pending commands after ready (${e.length} fire-and-forget, ${s.length} async)`)}_rejectAllPending(e,s){Object.keys(this.promiseQueue).forEach(r=>{let i=this.promiseQueue[r];i&&i.reject({code:e,message:s}),delete this.promiseQueue[r]});let t=this._pendingCommands.splice(0);for(let r of t)r.reject({code:e,message:s})}whenReady(){return this._readyPromise}ready(){return this._readyPromise}getIsReady(){return this._isReady}getCurrentFloor(){return this.floorId}getMapId(){return this.mapId}getVersion(){return g}_generateRequestId(){return`req_sdk_${Date.now()}_${Math.floor(Math.random()*1e5)}`}_postCommand(e,s,t){if(!this.iframe||!this.iframe.contentWindow){console.error("[SGSMapSDK] SDK Iframe is not available!");return}this.iframe.contentWindow.postMessage({type:"SGS_MAP_COMMAND",action:e,requestId:t,payload:s},this.targetOrigin)}_postCommandAsync(e,s,t=this.defaultTimeout){return this._isDestroyed?Promise.reject({code:"ERR_DESTROYED",message:"SGSMapSDK has been destroyed. Cannot send commands."}):this._readyFailed?Promise.reject(this._readyFailedError||{code:"ERR_NOT_READY",message:"SDK ready failed."}):this._isReady?this._postCommandAsyncInner(e,s,t):new Promise((r,i)=>{this._pendingCommands.push({action:e,payload:s,timeout:t,resolve:r,reject:i}),console.log(`[SGSMapSDK] Command "${e}" queued (waiting for ready). Queue size: ${this._pendingCommands.length}`)})}_postCommandSafe(e,s){if(this._isDestroyed){console.warn(`[SGSMapSDK] Command "${e}" ignored: SDK destroyed.`);return}if(this._readyFailed){console.warn(`[SGSMapSDK] Command "${e}" ignored: SDK ready failed.`);return}if(!this._isReady){this._pendingFireAndForget.push({action:e,payload:s}),console.log(`[SGSMapSDK] Command "${e}" queued (fire-and-forget, waiting for ready). Queue size: ${this._pendingFireAndForget.length}`);return}this._postCommand(e,s)}_postCommandAsyncInner(e,s,t=this.defaultTimeout){let r=this._generateRequestId();return new Promise((i,n)=>{let a=setTimeout(()=>{this.promiseQueue[r]&&(this.promiseQueue[r].reject({code:"ERR_TIMEOUT",message:`Command "${e}" timed out after ${t}ms`}),delete this.promiseQueue[r])},t);this.promiseQueue[r]={resolve:m=>{clearTimeout(a),i(m)},reject:m=>{clearTimeout(a),n(m)}},this._postCommand(e,s,r)})}focusTo(e){typeof e=="string"?this._postCommandSafe("FOCUS_TO",{nodeName:e}):this._postCommandSafe("FOCUS_TO",e)}changeFloor(e,s,t){return this._postCommandAsync("CHANGE_FLOOR",{floorId:e,...t},s).then(r=>(this.floorId=e,r))}addMarker(e){this._postCommandSafe("ADD_MARKER",e)}removeMarker(e){this._postCommandSafe("REMOVE_MARKER",{markerId:e})}clearMarkers(){this._postCommandSafe("CLEAR_MARKERS")}highlightPolygon(e){typeof e=="string"?this._postCommandSafe("HIGHLIGHT_POLYGON",{nodeName:e}):this._postCommandSafe("HIGHLIGHT_POLYGON",e)}planRoute(e,s,t,r=5e3){let i=typeof e=="string"?e:void 0,n=typeof s=="string"?s:void 0,a={};return i?a.startNode=i:a.from=e,n?a.endNode=n:a.to=s,t&&(a.options=t),this._postCommandAsync("PLAN_ROUTE",a,r)}planRouteV2(e,s,t,r=5e3){let i={from:e,to:s,options:t};return this._postCommandAsync("PLAN_ROUTE",i,r)}clearRoute(){this._postCommandSafe("CLEAR_ROUTE")}renderRoutePoints(e,s,t=12e3){return this._postCommandAsync("RENDER_ROUTE_POINTS",{...s,pathPoints:e},t)}startRoaming(e=[],s,t=12e3){return this._postCommandAsync("START_ROAMING",{...s,pathPoints:e},t)}stopRoaming(e){return this._postCommandAsync("STOP_ROAMING",{},e)}toggleRoamingPause(e){return this._postCommandAsync("TOGGLE_ROAMING_PAUSE",{},e)}planRouteToPoi(e,s,t,r=5e3){let n={from:typeof e=="string"||typeof e=="number"?{poiId:e}:e,toPoiId:s,wheelchair:t?.wheelchair?1:0,preferElevator:t?.preferElevator};return this._postCommandAsync("PLAN_ROUTE_TO_POI",n,r)}planRouteBetweenPois(e,s,t,r=5e3){let i={fromPoiId:e,toPoiId:s,wheelchair:t?.wheelchair?1:0,preferElevator:t?.preferElevator};return this._postCommandAsync("PLAN_ROUTE_BETWEEN_POIS",i,r)}getState(){return this._postCommandAsync("GET_STATE")}resetView(e){return this._postCommandAsync("RESET_VIEW",{},e)}setViewMode(e,s){return this._postCommandAsync("SET_VIEW_MODE",{mode:e},s)}toggleLayer(e,s,t){return this._postCommandAsync("TOGGLE_LAYER",{types:e,visible:s},t)}setVisibleLayers(e,s){return this._postCommandAsync("SET_VISIBLE_TYPES",{types:e},s)}setRouteViewActive(e,s){return this._postCommandAsync("SET_ROUTE_VIEW_ACTIVE",{active:e},s)}getManifest(e){return this._postCommandAsync("GET_MANIFEST",{},e)}loadFloorBundle(e,s){return this._postCommandAsync("LOAD_FLOOR_BUNDLE",{floorId:e},s)}getFloorPois(e,s){return this._postCommandAsync("GET_FLOOR_POIS",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.pois||[])}getNavigablePlaces(e,s){return this._postCommandAsync("GET_NAVIGABLE_PLACES",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.navigablePlaces||t?.places||[])}getPois(e,s){return this._postCommandAsync("GET_POIS",{floorCode:e},s)}getSpaces(e,s){return this._postCommandAsync("GET_SPACES",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.spaces||[])}getGuideStops(e,s){return this._postCommandAsync("GET_GUIDE_STOPS",{floorId:e},s).then(t=>Array.isArray(t)?t:t?.guideStops||[])}getGuideStopsByHall(e,s){return this._postCommandAsync("GET_GUIDE_STOPS_BY_HALL",{hallId:e},s).then(t=>Array.isArray(t)?t:t?.guideStops||[])}preloadFloor(e,s){return this._postCommandAsync("PRELOAD_FLOOR",{floorId:e},s)}getPerformanceStats(e){return this._postCommandAsync("GET_PERFORMANCE_STATS",{},e).then(s=>({sdkCreatedAt:this._sdkCreatedAt,iframeDomReadyMs:this._iframeDomReadyMs??void 0,engineReadyMs:this._engineReadyMs??void 0,...s}))}getDiagnostics(e){return this._postCommandAsync("GET_DIAGNOSTICS",{},e)}getFloorDiagnostics(e,s){return this._postCommandAsync("GET_FLOOR_DIAGNOSTICS",{floorId:e},s)}clearModelCache(){this._postCommandSafe("CLEAR_MODEL_CACHE")}getBusinessPois(e,s,t){return this._postCommandAsync("GET_BUSINESS_POIS",{floorId:e,options:s},t).then(r=>Array.isArray(r)?r:r?.pois||[])}queryPois(e,s){return this._postCommandAsync("QUERY_POIS",e,s).then(t=>Array.isArray(t)?t:t?.pois||[])}getFeaturedRoutes(e,s,t){return this._postCommandAsync("GET_FEATURED_ROUTES",{mapId:e,options:s},t).then(r=>Array.isArray(r)?r:r?.routes||[])}getFeaturedRoute(e,s){return this._postCommandAsync("GET_FEATURED_ROUTE",{routeId:e},s).then(t=>t?.route||t)}planFeaturedRoutePreview(e,s=15e3){return this._postCommandAsync("PLAN_FEATURED_ROUTE_PREVIEW",e,s)}showFlowline(e,s,t){return this._postCommandAsync("SHOW_FLOWLINE",{routeId:e,options:s},t)}clearFlowline(){this._postCommandSafe("CLEAR_FLOWLINE")}getReleaseManifest(e){return console.warn("[SGSMapSDK] getReleaseManifest() is deprecated. Use getManifest() instead."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getReleaseManifest() is deprecated. Use getManifest() instead."})}getModelResources(e){return console.warn("[SGSMapSDK] getModelResources() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getModelResources() is deprecated."})}getLayerTree(e){return console.warn("[SGSMapSDK] getLayerTree() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"getLayerTree() is deprecated."})}loadMockReleaseRoute(e){return console.warn("[SGSMapSDK] loadMockReleaseRoute() is deprecated."),Promise.reject({code:"ERR_UNSUPPORTED",message:"loadMockReleaseRoute() is deprecated."})}destroy(){this._isDestroyed||(this._isDestroyed=!0,this._readyTimer&&(clearTimeout(this._readyTimer),this._readyTimer=null),this._helloInterval&&(clearInterval(this._helloInterval),this._helloInterval=null),this._readyReject&&(this._readyReject({code:"ERR_DESTROYED",message:"SGSMapSDK has been destroyed before ready."}),this._readyResolve=null,this._readyReject=null),this._rejectAllPending("ERR_DESTROYED","SGSMapSDK has been destroyed. Pending promises were rejected."),this.iframe&&(this.iframe.parentNode&&this.iframe.parentNode.removeChild(this.iframe),this.iframe=null),this._messageListener&&(window.removeEventListener("message",this._messageListener),this._messageListener=null),this.removeAllListeners(),this._isReady=!1,console.log("[SGSMapSDK] Destroyed successfully. Cleaned up iframe, listeners and pending queues."))}};l.utils=h;typeof window<"u"&&(window.SGSMapSDK=l);})();
//# sourceMappingURL=index.global.js.map