(function (window) { 'use strict'; /** * ColorMapRun JSBridge SDK (Compatible Version) * 用于 H5 页面与 Flutter App 进行交互 * 兼容性说明: * - 优先检测 uni.webView 标准通道 * - 降级适配旧版 App 的 action:// 协议拦截和 window.share_wx 注入对象 */ var Bridge = { version: '1.0.2', /** * 内部核心发送方法 */ _post: function (action, data) { data = data || {}; console.log('[Bridge] Call:', action, data); // 1. 优先尝试标准 uni 通道 (新版 App) if (window.uni && window.uni.postMessage) { window.uni.postMessage({ data: { action: action, data: data } }); return; } // 2. 降级适配 (旧版 App) this._fallback(action, data); }, /** * 旧版 App 适配逻辑 */ _fallback: function (action, data) { var url = ''; switch (action) { case 'back': // 尝试关闭页面或返回 window.history.back(); break; case 'toHome': // 协议: action://to_home/ url = 'action://to_home/'; break; case 'toLogin': // 协议: action://to_login/ url = 'action://to_login/'; break; case 'openMap': // 协议: action://to_map_app?title=xxx&latitude=xxx&longitude=xxx url = 'action://to_map_app?title=' + encodeURIComponent(data.name || '') + '&latitude=' + data.latitude + '&longitude=' + data.longitude; break; case 'openMatch': // 协议: action://to_detail/?id=xxx&matchType=xxx url = 'action://to_detail/?id=' + data.id + '&matchType=' + (data.type || 1); break; case 'openActivityList': // 协议: action://to_activity_list/?id=xxx&mapName=xxx url = 'action://to_activity_list/?id=' + data.id + '&mapName=' + encodeURIComponent(data.mapName || ''); break; case 'shareWx': // 旧版使用注入对象 share_wx if (window.share_wx && window.share_wx.postMessage) { window.share_wx.postMessage(JSON.stringify(data)); } else { console.error('[Bridge] share_wx injection not found'); alert('微信分享功能不可用(环境不支持)'); } return; // shareWx 不需要走 URL 拦截 case 'launchWxMini': // 旧版使用注入对象 wx_launch_mini if (window.wx_launch_mini && window.wx_launch_mini.postMessage) { window.wx_launch_mini.postMessage(JSON.stringify(data)); } else { console.error('[Bridge] wx_launch_mini injection not found'); } return; case 'saveImage': // 旧版使用注入对象 save_base64 if (window.save_base64 && window.save_base64.postMessage) { window.save_base64.postMessage(data.base64); } else { console.error('[Bridge] save_base64 injection not found'); } return; default: console.warn('[Bridge] No legacy fallback for action:', action); } if (url) { console.log('[Bridge] Legacy URL jump:', url); // 触发 URL 拦截 window.location.href = url; } }, // ============================== // 公开 API // ============================== back: function () { this._post('back'); }, toHome: function() { this._post('toHome'); }, toLogin: function() { this._post('toLogin'); }, setTitle: function (title) { this._post('setTitle', { title: title }); }, openMap: function (latitude, longitude, name) { this._post('openMap', { latitude: latitude, longitude: longitude, name: name }); }, openMatch: function (id, type) { this._post('openMatch', { id: id, type: type }); }, openActivityList: function(id, mapName) { this._post('openActivityList', { id: id, mapName: mapName }); }, shareWx: function (options) { this._post('shareWx', options); }, launchWxMini: function (username, path) { this._post('launchWxMini', { username: username, path: path }); }, saveImage: function (base64Str) { this._post('saveImage', { base64: base64Str }); }, getToken: function () { this._post('getToken'); }, _tokenCallback: null, onToken: function(callback) { this._tokenCallback = callback; }, receiveToken: function(token) { console.log('[Bridge] Received token:', token); if (this._tokenCallback) { this._tokenCallback(token); } } }; window.Bridge = Bridge; })(window);