fix(请求头): 设置获取请求头
This commit is contained in:
parent
080c7ac8dd
commit
3db467c67b
@ -638,7 +638,18 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { isInApp } from '../../utils/appBridge.js'
|
import {
|
||||||
|
connectApplication,
|
||||||
|
parseAccessOrigin,
|
||||||
|
parseHeader,
|
||||||
|
setHttpHeaders,
|
||||||
|
isInApp,
|
||||||
|
} from '@/utils/appBridge.js'
|
||||||
|
import {
|
||||||
|
appConnectionManager,
|
||||||
|
isAppConnected,
|
||||||
|
getAppHeaderInfo,
|
||||||
|
} from '@/utils/appConnectionManager.js'
|
||||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||||
import borderImg from '../../components/TopList/borderImg.vue'
|
import borderImg from '../../components/TopList/borderImg.vue'
|
||||||
import maskLayer from '../../components/MaskLayer.vue'
|
import maskLayer from '../../components/MaskLayer.vue'
|
||||||
@ -661,6 +672,139 @@ const images = Object.fromEntries(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'browser' })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'app', fromCache: true })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'app', fromCache: true })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess(connectionResult)
|
||||||
|
// }
|
||||||
|
|
||||||
|
return connectionResult
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Connection failed:', error)
|
||||||
|
appConnected.value = false
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 触发连接失败回调
|
||||||
|
if (onConnectionFailed) {
|
||||||
|
onConnectionFailed(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接失败的特殊处理
|
||||||
|
if (error.message && error.message.includes('parse access')) {
|
||||||
|
router.push({ path: '/not_app', query: { message: error.message } })
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: false, error: error.message }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const userInfo = ref({})
|
const userInfo = ref({})
|
||||||
|
|
||||||
const visibleKingList = ref(true)
|
const visibleKingList = ref(true)
|
||||||
@ -941,6 +1085,7 @@ const updatePageData = () => {
|
|||||||
// 组件挂载时检测环境
|
// 组件挂载时检测环境
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
isInAppEnvironment.value = isInApp()
|
isInAppEnvironment.value = isInApp()
|
||||||
|
connectToApp()
|
||||||
|
|
||||||
getCountdown()
|
getCountdown()
|
||||||
timer = setInterval(getCountdown, 1000) //每秒更新
|
timer = setInterval(getCountdown, 1000) //每秒更新
|
||||||
|
|||||||
@ -742,7 +742,6 @@ import itemCenter from '@/components/WeeklyStar/itemCenter.vue'
|
|||||||
import TopUser from '@/components/WeeklyStar/topUser.vue'
|
import TopUser from '@/components/WeeklyStar/topUser.vue'
|
||||||
import maskLayer from '../../components/MaskLayer.vue'
|
import maskLayer from '../../components/MaskLayer.vue'
|
||||||
import { useDebounce, useThrottle } from '@/utils/useDebounce'
|
import { useDebounce, useThrottle } from '@/utils/useDebounce'
|
||||||
import { usePageInitializationWithConfig } from '@/utils/pageConfig.js'
|
|
||||||
import { getMemberProfile } from '@/api/wallet'
|
import { getMemberProfile } from '@/api/wallet'
|
||||||
import { showError, showWarning, showInfo, showSuccess } from '@/utils/toast.js'
|
import { showError, showWarning, showInfo, showSuccess } from '@/utils/toast.js'
|
||||||
import {
|
import {
|
||||||
@ -765,6 +764,139 @@ const images = Object.fromEntries(
|
|||||||
})
|
})
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'browser' })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'app', fromCache: true })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess({ environment: 'app', fromCache: true })
|
||||||
|
// }
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
// 触发连接成功回调
|
||||||
|
// if (onConnectionSuccess) {
|
||||||
|
// onConnectionSuccess(connectionResult)
|
||||||
|
// }
|
||||||
|
|
||||||
|
return connectionResult
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Connection failed:', error)
|
||||||
|
appConnected.value = false
|
||||||
|
console.groupEnd()
|
||||||
|
|
||||||
|
// 触发连接失败回调
|
||||||
|
if (onConnectionFailed) {
|
||||||
|
onConnectionFailed(error)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 连接失败的特殊处理
|
||||||
|
if (error.message && error.message.includes('parse access')) {
|
||||||
|
router.push({ path: '/not_app', query: { message: error.message } })
|
||||||
|
}
|
||||||
|
|
||||||
|
return { success: false, error: error.message }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const userInfo = ref({})
|
const userInfo = ref({})
|
||||||
|
|
||||||
const isInAppEnvironment = ref(false) // 检测是否在APP环境中
|
const isInAppEnvironment = ref(false) // 检测是否在APP环境中
|
||||||
@ -1087,7 +1219,7 @@ const observer = new IntersectionObserver((entries) => {
|
|||||||
|
|
||||||
// 组件挂载时检测环境
|
// 组件挂载时检测环境
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
usePageInitializationWithConfig('APPLY')
|
connectToApp()
|
||||||
isInAppEnvironment.value = isInApp()
|
isInAppEnvironment.value = isInApp()
|
||||||
|
|
||||||
getCountdown()
|
getCountdown()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user