feat(排行榜): 更新请求头
This commit is contained in:
parent
304e3e8c55
commit
98bb616338
@ -273,7 +273,21 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { isInApp, closePage, viewUserInfo, gotoRoom } from '@/utils/appBridge.js'
|
import {
|
||||||
|
connectApplication,
|
||||||
|
parseAccessOrigin,
|
||||||
|
parseHeader,
|
||||||
|
setHttpHeaders,
|
||||||
|
isInApp,
|
||||||
|
closePage,
|
||||||
|
viewUserInfo,
|
||||||
|
gotoRoom,
|
||||||
|
} from '@/utils/appBridge.js'
|
||||||
|
import {
|
||||||
|
appConnectionManager,
|
||||||
|
isAppConnected,
|
||||||
|
getAppHeaderInfo,
|
||||||
|
} from '@/utils/appConnectionManager.js'
|
||||||
import { computed, onMounted, ref, watch, watchEffect } from 'vue'
|
import { computed, onMounted, ref, watch, watchEffect } from 'vue'
|
||||||
import TopUser from '@/components/Ranking/topUser.vue'
|
import TopUser from '@/components/Ranking/topUser.vue'
|
||||||
import { getWealthRanking, getCharmRanking, getRoomRanking, getMyRanking } from '@/api/ranking'
|
import { getWealthRanking, getCharmRanking, getRoomRanking, getMyRanking } from '@/api/ranking'
|
||||||
@ -498,7 +512,128 @@ const getMyRankingInfo = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const appConnected = ref(false)
|
||||||
|
const headerInfo = ref({})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接APP并获取认证信息(优化版 - 仅专注连接)
|
||||||
|
*/
|
||||||
|
const connectToApp = async () => {
|
||||||
|
console.group('🔗 APP Connection Process')
|
||||||
|
console.debug('🚀 Starting APP connection...')
|
||||||
|
|
||||||
|
try {
|
||||||
|
// 检查是否在APP环境中
|
||||||
|
if (!isInApp()) {
|
||||||
|
console.debug('🌐 Browser environment detected')
|
||||||
|
appConnected.value = true
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// getUserInfo()
|
||||||
|
|
||||||
|
return { success: true, environment: 'browser' }
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否已经连接且未过期
|
||||||
|
if (isAppConnected()) {
|
||||||
|
console.debug('✅ APP already connected and valid')
|
||||||
|
appConnected.value = true
|
||||||
|
|
||||||
|
// 使用缓存的头部信息
|
||||||
|
const cachedHeaderInfo = getAppHeaderInfo()
|
||||||
|
if (cachedHeaderInfo) {
|
||||||
|
headerInfo.value = cachedHeaderInfo
|
||||||
|
console.debug('🔧 Using cached HTTP headers')
|
||||||
|
}
|
||||||
|
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// getUserInfo()
|
||||||
|
|
||||||
|
return { success: true, environment: 'app', fromCache: true }
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否有正在进行的连接
|
||||||
|
if (appConnectionManager.isConnecting()) {
|
||||||
|
console.debug('⏳ Connection already in progress, waiting...')
|
||||||
|
await appConnectionManager.getConnectionPromise()
|
||||||
|
appConnected.value = true
|
||||||
|
console.debug('✅ Connected via existing process')
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// getUserInfo()
|
||||||
|
|
||||||
|
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) {
|
||||||
|
// 解析头部信息
|
||||||
|
headerInfo.value = parseHeader(result.data)
|
||||||
|
|
||||||
|
// 设置HTTP请求头
|
||||||
|
console.debug('🔧 Setting HTTP headers...')
|
||||||
|
await setHttpHeaders(headerInfo.value)
|
||||||
|
|
||||||
|
// 标记连接成功
|
||||||
|
appConnected.value = true
|
||||||
|
appConnectionManager.setConnected(headerInfo.value)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// getUserInfo()
|
||||||
|
|
||||||
|
return connectionResult
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Connection failed:', error)
|
||||||
|
appConnected.value = false
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 连接失败的特殊处理
|
||||||
|
if (error.message && error.message.includes('parse access')) {
|
||||||
|
router.push({ path: '/not_app', query: { message: error.message } })
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: false, error: error.message }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
|
connectToApp()
|
||||||
isInAppEnvironment.value = isInApp()
|
isInAppEnvironment.value = isInApp()
|
||||||
|
|
||||||
observer.observe(sentinel.value)
|
observer.observe(sentinel.value)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user