feat(app连接文件): 新增ios设备判断
This commit is contained in:
parent
6e87bf1f47
commit
9ce0c65315
@ -167,19 +167,68 @@ export function connectApplication(renderFun) {
|
|||||||
// 兼容历史版本
|
// 兼容历史版本
|
||||||
window.getIosAccessOriginParam = renderFun
|
window.getIosAccessOriginParam = renderFun
|
||||||
|
|
||||||
|
// 安全日志函数
|
||||||
|
const safeConsoleLog = (level, ...args) => {
|
||||||
|
if (process.env.NODE_ENV === 'production') return
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (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 {
|
||||||
|
console[level](...args)
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
console.error('Console logging failed:', e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const debugLog = (...args) => safeConsoleLog('debug', ...args)
|
||||||
|
const errorLog = (...args) => safeConsoleLog('error', ...args)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// 安卓设备环境
|
||||||
if (android()) {
|
if (android()) {
|
||||||
console.debug('Flutter Android WebView 检测')
|
debugLog('Flutter Android WebView 检测')
|
||||||
console.debug('window.app 对象:', window.app)
|
debugLog('window.app 对象:', window.app)
|
||||||
|
|
||||||
// 检查Flutter注入的接口
|
// 检查Flutter注入的接口
|
||||||
if (window.app) {
|
if (window.app) {
|
||||||
console.debug('window.app 的所有方法:')
|
debugLog('window.app 的所有方法:')
|
||||||
console.debug(Object.keys(window.app))
|
debugLog(Object.keys(window.app))
|
||||||
|
|
||||||
// 检查具体的方法
|
// 检查具体的方法
|
||||||
console.debug('getAccessOrigin 方法存在?', typeof window.app.getAccessOrigin)
|
debugLog('getAccessOrigin 方法存在?', typeof window.app.getAccessOrigin)
|
||||||
console.debug('getAccessOrigin 是函数?', typeof window.app.getAccessOrigin === 'function')
|
debugLog('getAccessOrigin 是函数?', typeof window.app.getAccessOrigin === 'function')
|
||||||
}
|
}
|
||||||
|
|
||||||
// 等待Flutter注入完成
|
// 等待Flutter注入完成
|
||||||
@ -189,26 +238,26 @@ export function connectApplication(renderFun) {
|
|||||||
|
|
||||||
const checkInterface = () => {
|
const checkInterface = () => {
|
||||||
attempts++
|
attempts++
|
||||||
console.debug(`尝试第 ${attempts} 次检查Flutter接口...`)
|
debugLog(`尝试第 ${attempts} 次检查Flutter接口...`)
|
||||||
|
|
||||||
if (
|
if (
|
||||||
window.app &&
|
window.app &&
|
||||||
window.app.getAccessOrigin &&
|
window.app.getAccessOrigin &&
|
||||||
typeof window.app.getAccessOrigin === 'function'
|
typeof window.app.getAccessOrigin === 'function'
|
||||||
) {
|
) {
|
||||||
console.debug('Flutter接口检测成功,调用 getAccessOrigin')
|
debugLog('Flutter接口检测成功,调用 getAccessOrigin')
|
||||||
try {
|
try {
|
||||||
const result = window.app.getAccessOrigin()
|
const result = window.app.getAccessOrigin()
|
||||||
console.debug('getAccessOrigin 返回结果:', result)
|
debugLog('getAccessOrigin 返回结果:', result)
|
||||||
renderFun(result)
|
renderFun(result)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('调用 getAccessOrigin 出错:', error)
|
errorLog('调用 getAccessOrigin 出错:', error)
|
||||||
}
|
}
|
||||||
} else if (attempts < maxAttempts) {
|
} else if (attempts < maxAttempts) {
|
||||||
// 继续等待
|
// 继续等待
|
||||||
setTimeout(checkInterface, 100)
|
setTimeout(checkInterface, 100)
|
||||||
} else {
|
} else {
|
||||||
console.error('Flutter接口注入超时,使用默认数据')
|
errorLog('Flutter接口注入超时,使用默认数据')
|
||||||
// 使用默认的开发数据
|
// 使用默认的开发数据
|
||||||
const mockAccess = JSON.stringify({
|
const mockAccess = JSON.stringify({
|
||||||
Authorization: 'Bearer FLUTTER_MOCK_TOKEN',
|
Authorization: 'Bearer FLUTTER_MOCK_TOKEN',
|
||||||
@ -227,18 +276,18 @@ export function connectApplication(renderFun) {
|
|||||||
if (window.app) {
|
if (window.app) {
|
||||||
waitForFlutterInterface()
|
waitForFlutterInterface()
|
||||||
} else {
|
} else {
|
||||||
console.debug('等待Flutter注入window.app...')
|
debugLog('等待Flutter注入window.app...')
|
||||||
// 监听window.app的创建
|
// 监听window.app的创建
|
||||||
let checkCount = 0
|
let checkCount = 0
|
||||||
const checkApp = () => {
|
const checkApp = () => {
|
||||||
checkCount++
|
checkCount++
|
||||||
if (window.app) {
|
if (window.app) {
|
||||||
console.debug('检测到window.app,开始连接')
|
debugLog('检测到window.app,开始连接')
|
||||||
waitForFlutterInterface()
|
waitForFlutterInterface()
|
||||||
} else if (checkCount < 50) {
|
} else if (checkCount < 50) {
|
||||||
setTimeout(checkApp, 100)
|
setTimeout(checkApp, 100)
|
||||||
} else {
|
} else {
|
||||||
console.error('等待window.app超时')
|
errorLog('等待window.app超时')
|
||||||
// 手动通知Flutter注入接口
|
// 手动通知Flutter注入接口
|
||||||
if (window.FlutterApp && window.FlutterApp.postMessage) {
|
if (window.FlutterApp && window.FlutterApp.postMessage) {
|
||||||
window.FlutterApp.postMessage('requestAccessOrigin')
|
window.FlutterApp.postMessage('requestAccessOrigin')
|
||||||
@ -247,17 +296,85 @@ export function connectApplication(renderFun) {
|
|||||||
}
|
}
|
||||||
checkApp()
|
checkApp()
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
/*else if (ios()) {
|
|
||||||
console.debug('ios access detected')
|
// 苹果设备环境
|
||||||
// iOS可能需要特殊处理,这里先使用通用方法
|
else if (ios()) {
|
||||||
if (window.webkit && window.webkit.messageHandlers && window.webkit.messageHandlers.getAccessOrigin) {
|
debugLog('iOS access detected')
|
||||||
window.webkit.messageHandlers.getAccessOrigin.postMessage({})
|
|
||||||
} else if (window.app && window.app.getAccessOrigin) {
|
// iOS WebKit 消息处理器检测和等待
|
||||||
renderFun(window.app.getAccessOrigin())
|
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()
|
||||||
}
|
}
|
||||||
}*/
|
|
||||||
console.debug('非移动设备环境')
|
// 检查 window.app 是否存在(某些情况下iOS也可能注入window.app)
|
||||||
|
if (window.app && typeof window.app.getAccessOrigin === 'function') {
|
||||||
|
debugLog('检测到window.app,尝试调用getAccessOrigin')
|
||||||
|
try {
|
||||||
|
const result = window.app.getAccessOrigin()
|
||||||
|
debugLog('window.app.getAccessOrigin 返回结果:', result)
|
||||||
|
renderFun(result)
|
||||||
|
} catch (error) {
|
||||||
|
errorLog('调用window.app.getAccessOrigin出错:', error)
|
||||||
|
waitForIOSInterface() // 尝试WebKit方式
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
debugLog('等待iOS WebKit接口注入...')
|
||||||
|
waitForIOSInterface()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 浏览器环境
|
||||||
|
else {
|
||||||
|
debugLog('非移动设备环境')
|
||||||
// 非APP环境,可能是浏览器调试
|
// 非APP环境,可能是浏览器调试
|
||||||
console.warn('Not in APP environment, using mock data for development')
|
console.warn('Not in APP environment, using mock data for development')
|
||||||
// 开发环境可以提供模拟数据
|
// 开发环境可以提供模拟数据
|
||||||
@ -272,7 +389,7 @@ export function connectApplication(renderFun) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to connect to APP:', error)
|
errorLog('Failed to connect to APP:', error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user