diff --git a/src/utils/appBridge.js b/src/utils/appBridge.js index 42242fd..f360c0a 100644 --- a/src/utils/appBridge.js +++ b/src/utils/appBridge.js @@ -8,19 +8,11 @@ import { COMMON_HEADERS } from '@/utils/http.js' // ==================== 工具函数 ==================== /** - * 检测是否为iOS系统 - * @returns {boolean} - */ -export function ios() { - return /iPad|iPhone|iPod/.test(navigator.userAgent) -} - -/** - * 检测是否为Android系统 + * 检测是否为设备(Android、ios)系统 * @returns {boolean} */ export function android() { - return /Android/.test(navigator.userAgent) + return /Android|iPad|iPhone|iPod/.test(navigator.userAgent) } /** @@ -41,45 +33,9 @@ export function isInApp() { function createSafeLogger() { return function safeConsoleLog(level, ...args) { if (process.env.NODE_ENV === 'production') return - try { - if (ios()) { - // 对 iOS 设备进行特殊处理 - const processedArgs = args.map((arg) => { - if (typeof arg === 'object' && arg !== null) { - try { - // 序列化对象,避免循环引用 - return JSON.stringify( - arg, - (key, value) => { - // 处理特殊对象类型 - if (value instanceof HTMLElement) return '[HTMLElement]' - if (typeof value === 'function') return '[Function]' - if (value instanceof Date) return value.toISOString() - if (value instanceof RegExp) return value.toString() - if (value instanceof Error) return { message: value.message, stack: value.stack } - - if (this.__visited__) { - if (this.__visited__.has(value)) return '[Circular Reference]' - } else { - this.__visited__ = new Set() - } - this.__visited__.add(value) - return value - }, - 2, - ) - } catch (e) { - return '[Object with serialization error]' - } - } - return arg - }) - console[level](...processedArgs) - } else { - // 非 iOS 设备正常使用 - console[level](...args) - } + // 非 iOS 设备正常使用 + console[level](...args) } catch (e) { if (process.env.NODE_ENV === 'development') { console.error('Console logging failed:', e) @@ -185,85 +141,6 @@ function handleAndroidPlatform(renderFun) { } } -/** - * iOS 平台处理逻辑 - * @param {Function} renderFun - 回调函数 - */ -function handleIOSPlatform(renderFun) { - debugLog('iOS access detected') - - // iOS WebKit 消息处理器检测和等待 - const waitForIOSInterface = () => { - let attempts = 0 - const maxAttempts = 50 // 最多等待5秒 - - const checkIOSInterface = () => { - attempts++ - debugLog(`尝试第 ${attempts} 次检查iOS接口...`) - - if ( - window.webkit && - window.webkit.messageHandlers && - window.webkit.messageHandlers.getAccessOrigin - ) { - debugLog('iOS接口检测成功,调用 getAccessOrigin') - - try { - // iOS的postMessage是异步的,需要通过回调处理 - window.webkit.messageHandlers.getAccessOrigin.postMessage({}) - - // 设置全局回调函数来接收iOS返回的数据 - window.receiveAccessOrigin = function (accessData) { - debugLog('收到iOS返回的access数据:', accessData) - renderFun(accessData) // 接收数据后传递给回调函数 - } - } catch (error) { - errorLog('调用 getAccessOrigin 出错:', error) - // 使用模拟数据作为降级处理 - const mockAccess = JSON.stringify({ - Authorization: 'Bearer IOS_MOCK_TOKEN', - 'Req-Lang': 'en', - 'Req-App-Intel': 'build=1.0;version=2.1;channel=ios;Req-Imei=ios-device', - 'Req-Sys-Origin': 'origin=LIKEI;originChild=IOS', - }) - renderFun(mockAccess) - } - } else if (attempts < maxAttempts) { - // 没找到,继续查询 - setTimeout(checkIOSInterface, 100) - } else { - // 查询次数用完 - errorLog('iOS接口注入超时,使用默认数据') - const mockAccess = JSON.stringify({ - Authorization: 'Bearer IOS_MOCK_TOKEN', - 'Req-Lang': 'en', - 'Req-App-Intel': 'build=1.0;version=2.1;channel=ios;Req-Imei=ios-device', - 'Req-Sys-Origin': 'origin=LIKEI;originChild=IOS', - }) - renderFun(mockAccess) - } - } - - checkIOSInterface() - } - - // window.app 存在(某些情况下iOS也可能注入window.app) - if (window.app && typeof window.app.getAccessOrigin === 'function') { - try { - const result = window.app.getAccessOrigin() - debugLog('window.app.getAccessOrigin 返回结果:', result) - renderFun(result) - } catch (error) { - errorLog('调用window.app.getAccessOrigin出错:', error) - waitForIOSInterface() // 尝试WebKit方式 - } - } else { - // window.app不存在,等待注入 - debugLog('等待iOS WebKit接口注入...') - waitForIOSInterface() - } -} - /** * Web 浏览器环境处理逻辑 * @param {Function} renderFun - 回调函数 @@ -297,10 +174,10 @@ export function connectApplication(renderFun) { try { if (android()) { + console.log('设备为设备环境') handleAndroidPlatform(renderFun) - } else if (ios()) { - handleIOSPlatform(renderFun) } else { + console.log('设备为Web浏览器环境') handleWebPlatform(renderFun) } } catch (error) {