diff --git a/src/utils/appConnector.js b/src/utils/appConnector.js new file mode 100644 index 0000000..0cc7511 --- /dev/null +++ b/src/utils/appConnector.js @@ -0,0 +1,125 @@ +// src/utils/appConnector.js +import { + connectApplication, + parseAccessOrigin, + parseHeader, + setHttpHeaders, + isInApp, +} from '@/utils/appBridge.js' +import { + appConnectionManager, + isAppConnected, + getAppHeaderInfo, +} from '@/utils/appConnectionManager.js' + +/** + * 统一的APP连接工具函数 + * @param {Function} onConnected - 连接成功回调 + * @param {Function} onFailed - 连接失败回调 + * @returns {Promise} 连接结果 + */ +export const connectToApp = async (onConnected, onFailed) => { + console.group('🔗 APP Connection Process') + console.debug('🚀 Starting APP connection...') + + try { + // 检查是否在APP环境中 + if (!isInApp()) { + console.debug('🌐 Browser environment detected') + console.groupEnd() + + // 触发连接成功回调 + if (onConnected) onConnected() + + return { success: true, environment: 'browser' } + } + + // 检查是否已经连接且未过期 + if (isAppConnected()) { + console.debug('✅ APP already connected and valid') + + // 使用缓存的头部信息 + const cachedHeaderInfo = getAppHeaderInfo() + if (cachedHeaderInfo) { + console.debug('🔧 Using cached HTTP headers') + } + + console.groupEnd() + + // 触发连接成功回调 + if (onConnected) onConnected() + + return { success: true, environment: 'app', fromCache: true } + } + + // 检查是否有正在进行的连接 + if (appConnectionManager.isConnecting()) { + console.debug('⏳ Connection already in progress, waiting...') + await appConnectionManager.getConnectionPromise() + console.debug('✅ Connected via existing process') + console.groupEnd() + + // 触发连接成功回调 + if (onConnected) onConnected() + + return { success: true, environment: 'app', fromCache: true } + } + + // 建立新的APP连接 + console.debug('📱 Establishing new APP connection...') + + const connectionResult = await new Promise((resolve, reject) => { + const connectionPromise = new Promise((connectResolve, connectReject) => { + connectApplication(async (access) => { + try { + const result = parseAccessOrigin(access) + + if (result.success) { + // 解析头部信息 + const headerInfo = parseHeader(result.data) + + // 设置HTTP请求头 + console.debug('🔧 Setting HTTP headers...') + await setHttpHeaders(headerInfo) + + // 标记连接成功 + appConnectionManager.setConnected(headerInfo) + + console.debug('🎉 APP connection established successfully') + connectResolve({ success: true, environment: 'app', fromCache: false }) + } else { + console.error('❌ Failed to parse access origin:', result.error) + appConnectionManager.reset() + connectReject(new Error(result.error)) + } + } catch (error) { + console.error('❌ Connection process failed:', error) + appConnectionManager.reset() + connectReject(error) + } + }) + }) + + // 设置连接状态 + appConnectionManager.setConnecting(connectionPromise) + + connectionPromise.then(resolve).catch(reject) + }) + + console.debug('✅ Connection completed successfully') + console.groupEnd() + + // 触发连接成功回调 + if (onConnected) onConnected() + + return connectionResult + } catch (error) { + console.error('❌ Connection failed:', error) + console.groupEnd() + + // 触发连接失败回调 + if (onFailed) onFailed(error) + + return { success: false, error: error.message } + } +} diff --git a/src/views/Invitation/InvitationToRegister.vue b/src/views/Invitation/InvitationToRegister.vue index 543553f..12fc5b4 100644 --- a/src/views/Invitation/InvitationToRegister.vue +++ b/src/views/Invitation/InvitationToRegister.vue @@ -428,23 +428,10 @@