184 lines
6.5 KiB
JavaScript
184 lines
6.5 KiB
JavaScript
/**
|
||
* 检测是否为Android系统
|
||
* @returns {boolean}
|
||
*/
|
||
export function android() {
|
||
return /Android/.test(navigator.userAgent)
|
||
}
|
||
|
||
/**
|
||
* 上传图片
|
||
* @param {Function} renderFun 回调函数,接收APP传递的access参数
|
||
* @param {Function} filterFun 可选的过滤函数,用于判断是否接受该数据
|
||
*/
|
||
export function uploadImgFile(renderFun, filterFun) {
|
||
// 设置全局回调函数,供原生App调用
|
||
window.renderData = renderFun
|
||
// iOS平台兼容
|
||
window.getIosAccessOriginParam = renderFun
|
||
|
||
try {
|
||
if (window.FlutterPageControl) {
|
||
console.log('使用window.FlutterPageControl.postMessage(`uploadImgFile`)')
|
||
// 直接使用Flutter通道
|
||
window.FlutterPageControl.postMessage(`uploadImgFile`)
|
||
|
||
try {
|
||
if (android()) {
|
||
console.debug('Flutter Android WebView 检测')
|
||
console.debug('window.app 对象:', window.app)
|
||
|
||
// 检查Flutter注入的接口
|
||
if (window.app) {
|
||
console.debug('window.app 的所有方法:')
|
||
console.debug(Object.keys(window.app))
|
||
|
||
// 检查具体的方法
|
||
console.debug('getImagePath 方法存在?', typeof window.app.getImagePath)
|
||
console.debug('getImagePath 是函数?', typeof window.app.getImagePath === 'function')
|
||
}
|
||
|
||
// 等待Flutter注入完成
|
||
const waitForFlutterInterface = () => {
|
||
let attempts = 0
|
||
const maxAttempts = 6000 // 最多等待60秒
|
||
|
||
// 清除之前的定时器(如果存在)
|
||
if (window._flutterCheckTimeout) {
|
||
clearTimeout(window._flutterCheckTimeout)
|
||
window._flutterCheckTimeout = null
|
||
}
|
||
|
||
const checkInterface = () => {
|
||
attempts++
|
||
console.debug(`尝试第 ${attempts} 次检查Flutter接口...`)
|
||
|
||
if (
|
||
window.app &&
|
||
window.app.getImagePath &&
|
||
typeof window.app.getImagePath === 'function'
|
||
) {
|
||
console.debug('Flutter接口检测成功,调用 getImagePath')
|
||
try {
|
||
const result = window.app.getImagePath()
|
||
console.debug('getImagePath 返回结果:', result)
|
||
|
||
// 如果提供了过滤函数,则使用它来判断是否接受该数据
|
||
if (filterFun && typeof filterFun === 'function') {
|
||
// 如果过滤函数返回false,则继续检查
|
||
if (!filterFun(result)) {
|
||
console.debug('数据被过滤函数拒绝,继续检查...')
|
||
if (attempts < maxAttempts) {
|
||
// 保存timeout ID以便下次调用时清除
|
||
window._flutterCheckTimeout = setTimeout(checkInterface, 100)
|
||
return
|
||
} else {
|
||
console.error('Flutter接口注入超时,使用默认数据')
|
||
const mockAccess = JSON.stringify({})
|
||
renderFun(mockAccess)
|
||
return
|
||
}
|
||
}
|
||
}
|
||
|
||
// 清除定时器
|
||
if (window._flutterCheckTimeout) {
|
||
clearTimeout(window._flutterCheckTimeout)
|
||
window._flutterCheckTimeout = null
|
||
}
|
||
renderFun(result)
|
||
} catch (error) {
|
||
console.error('调用 getImagePath 出错:', error)
|
||
}
|
||
} else if (attempts < maxAttempts) {
|
||
// 保存timeout ID以便下次调用时清除
|
||
window._flutterCheckTimeout = setTimeout(checkInterface, 100)
|
||
} else {
|
||
console.error('Flutter接口注入超时,使用默认数据')
|
||
// 清除定时器
|
||
if (window._flutterCheckTimeout) {
|
||
clearTimeout(window._flutterCheckTimeout)
|
||
window._flutterCheckTimeout = null
|
||
}
|
||
// 使用默认的开发数据
|
||
const mockAccess = JSON.stringify({})
|
||
renderFun(mockAccess)
|
||
}
|
||
}
|
||
|
||
checkInterface()
|
||
}
|
||
|
||
// 如果window.app已存在,直接检查,否则等待
|
||
if (window.app) {
|
||
waitForFlutterInterface()
|
||
} else {
|
||
console.debug('等待Flutter注入window.app...')
|
||
// 监听window.app的创建
|
||
let checkCount = 0
|
||
const checkApp = () => {
|
||
checkCount++
|
||
if (window.app) {
|
||
console.debug('检测到window.app,开始连接')
|
||
waitForFlutterInterface()
|
||
} else if (checkCount < 50) {
|
||
setTimeout(checkApp, 100)
|
||
} else {
|
||
console.error('等待window.app超时')
|
||
// 手动通知Flutter注入接口
|
||
if (window.FlutterApp && window.FlutterApp.postMessage) {
|
||
window.FlutterApp.postMessage('requestAccessOrigin')
|
||
}
|
||
}
|
||
}
|
||
checkApp()
|
||
}
|
||
} else {
|
||
console.debug('非移动设备环境')
|
||
// 非APP环境,可能是浏览器调试
|
||
console.warn('Not in APP environment, using mock data for development')
|
||
// 开发环境可以提供模拟数据
|
||
if (process.env.NODE_ENV === 'development') {
|
||
const mockAccess = JSON.stringify({})
|
||
setTimeout(() => renderFun(mockAccess), 100)
|
||
}
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to connect to APP:', error)
|
||
}
|
||
} else {
|
||
console.log('window.FlutterPageControl不存在')
|
||
// 降级处理
|
||
const mockAccess = JSON.stringify('')
|
||
renderFun(mockAccess)
|
||
}
|
||
} catch (error) {
|
||
console.error('调出图片选择界面失败:', error)
|
||
// 异常情况下的兜底处理
|
||
const mockAccess = JSON.stringify('')
|
||
renderFun(mockAccess)
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 解析APP传递的访问参数
|
||
* @param {string} access JSON字符串格式的访问参数
|
||
* @returns {Object} 解析后的参数对象
|
||
*/
|
||
export function parseAccessOrigin(access) {
|
||
try {
|
||
const accessParam = JSON.parse(access)
|
||
return {
|
||
success: true,
|
||
data: accessParam,
|
||
}
|
||
} catch (error) {
|
||
console.error('Failed to parse access origin:', error)
|
||
return {
|
||
success: false,
|
||
error: 'accessOrigin Error.',
|
||
data: null,
|
||
}
|
||
}
|
||
}
|