feat(抽奖活动页): 使用新的获取请求头工具类
This commit is contained in:
parent
c1897088db
commit
5c142dd374
@ -822,21 +822,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import {
|
import { isInApp } from '@/utils/appBridge.js'
|
||||||
connectApplication,
|
import { connectToApp } from '@/utils/appConnector.js'
|
||||||
parseAccessOrigin,
|
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||||
parseHeader,
|
|
||||||
setHttpHeaders,
|
|
||||||
isInApp,
|
|
||||||
} from '@/utils/appBridge.js'
|
|
||||||
import {
|
|
||||||
appConnectionManager,
|
|
||||||
isAppConnected,
|
|
||||||
getAppHeaderInfo,
|
|
||||||
} from '@/utils/appConnectionManager.js'
|
|
||||||
import { computed, nextTick, onMounted, onUnmounted, ref } from 'vue'
|
|
||||||
import maskLayer from '@/components/MaskLayer.vue'
|
import maskLayer from '@/components/MaskLayer.vue'
|
||||||
import { useDebounce, useThrottle } from '@/utils/useDebounce'
|
|
||||||
import { formatUTCCustom } from '@/utils/utcFormat.js'
|
import { formatUTCCustom } from '@/utils/utcFormat.js'
|
||||||
import { getMemberProfile, getUserIdentity } from '@/api/wallet'
|
import { getMemberProfile, getUserIdentity } from '@/api/wallet'
|
||||||
import { getSelectedPayee, clearSelectedPayee } from '@/utils/payeeStore.js'
|
import { getSelectedPayee, clearSelectedPayee } from '@/utils/payeeStore.js'
|
||||||
@ -1224,128 +1213,6 @@ const getUserInfo = async () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 连接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()
|
|
||||||
|
|
||||||
// 触发连接成功回调
|
|
||||||
initData() // 页面初始化数据
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
// 触发连接成功回调
|
|
||||||
initData() // 页面初始化数据
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
// 触发连接成功回调
|
|
||||||
initData() // 页面初始化数据
|
|
||||||
|
|
||||||
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()
|
|
||||||
|
|
||||||
// 触发连接成功回调
|
|
||||||
initData() // 页面初始化数据
|
|
||||||
|
|
||||||
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 getRanking = async () => {
|
const getRanking = async () => {
|
||||||
const resRanking = await ranklist()
|
const resRanking = await ranklist()
|
||||||
@ -1451,9 +1318,16 @@ const initData = () => {
|
|||||||
getTaskList() //获取任务列表
|
getTaskList() //获取任务列表
|
||||||
getWinners() //获取历史中奖用户
|
getWinners() //获取历史中奖用户
|
||||||
}
|
}
|
||||||
|
// 使用工具函数连接APP
|
||||||
|
const connectToAppHandler = async () => {
|
||||||
|
await connectToApp(() => {
|
||||||
|
// 连接成功回调
|
||||||
|
initData()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
connectToApp()
|
connectToAppHandler()
|
||||||
isInAppEnvironment.value = isInApp()
|
isInAppEnvironment.value = isInApp()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user