feat(新工具类): 将重新获取请求头的方法封装成工具类

This commit is contained in:
hzj 2025-11-03 14:11:01 +08:00
parent cc29b63c79
commit 1f00654a14
2 changed files with 126 additions and 19 deletions

125
src/utils/appConnector.js Normal file
View File

@ -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<Object>} 连接结果
*/
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 }
}
}

View File

@ -428,23 +428,10 @@
</template>
<script setup>
import {
connectApplication,
parseAccessOrigin,
parseHeader,
setHttpHeaders,
isInApp,
gotoPrivateChat,
} from '@/utils/appBridge.js'
import {
appConnectionManager,
isAppConnected,
getAppHeaderInfo,
} from '@/utils/appConnectionManager.js'
import { gotoPrivateChat } from '@/utils/appBridge.js'
import { computed, onMounted, onUnmounted, ref } from 'vue'
import GeneralHeader from '@/components/GeneralHeader.vue'
import maskLayer from '../../components/MaskLayer.vue'
import { getMemberProfile } from '@/api/wallet'
import { showError, showWarning, showInfo, showSuccess } from '@/utils/toast.js'
import { useClipboard } from '@vueuse/core'
import {
@ -472,10 +459,6 @@ const images = Object.fromEntries(
const { copy, isSupported } = useClipboard()
const isInAppEnvironment = ref(false) // APP
const appConnected = ref(false)
const headerInfo = ref({})
const helpInfoShow = ref(false) //
const enterCodeShow = ref(false) //
const tipShow = ref(false) //
@ -634,7 +617,6 @@ const connectToAppHandler = async () => {
onMounted(() => {
connectToAppHandler()
isInAppEnvironment.value = isInApp()
})
</script>