优化了qq微信切换

This commit is contained in:
yml2213
2026-04-13 21:48:23 +08:00
parent dc8a014601
commit bc0d6fc8aa
10 changed files with 453 additions and 67 deletions
+147 -15
View File
@@ -6,10 +6,16 @@ import {
createClaimSession,
fetchClaimDetail,
fetchClaimSessionSummary,
refreshClaimSession,
removeClaimSession,
redeemClaim,
} from '@/services/claim'
import type { ClaimDetailData, ClaimTaskStatus } from '@/types/claim'
import type { TencentLoginType } from '@/types/tencent/session'
import type {
TencentBrowserSessionData,
TencentBrowserSessionSummaryData,
TencentLoginType,
} from '@/types/tencent/session'
import { notifyTencentActionError } from './tencent/session-errors'
@@ -163,7 +169,7 @@ export function useClaimPage(token: string) {
)
function applyLoginTypeFromDetail(nextDetail: ClaimDetailData) {
loginType.value = syncLoginTypeFromDetail(nextDetail)
loginType.value = syncLoginTypeFromDetail(nextDetail, loginType.value)
}
watch(
@@ -182,6 +188,10 @@ export function useClaimPage(token: string) {
{ immediate: true },
)
watch(qrImage, () => {
qrImageNaturalWidth.value = 0
})
async function loadDetail() {
detailLoading.value = true
@@ -209,7 +219,9 @@ export function useClaimPage(token: string) {
try {
loginType.value = nextLoginType
const response = await createClaimSession(token, nextLoginType)
const response = await createClaimSession(token, nextLoginType, {
forceRecreate: hasSession.value,
})
if (response.code !== 0) {
throw new Error(response.msg || '创建领取会话失败')
@@ -225,6 +237,75 @@ export function useClaimPage(token: string) {
}
}
async function reloadSessionPage() {
if (!hasSession.value) {
return false
}
sessionLoading.value = true
resetPolling()
resetPollingWarning()
try {
const response = await refreshClaimSession(token)
if (response.code !== 0) {
throw new Error(response.msg || '刷新后端页面失败')
}
detail.value = mergeClaimDetailData(detail.value, response.data)
applyLoginTypeFromDetail(response.data)
restartPollingIfNeeded(response.data)
return true
} catch (error) {
notifyTencentActionError(error, '刷新后端页面失败')
await refreshSessionSummary({ silent: true })
restartPollingIfNeeded()
return false
} finally {
sessionLoading.value = false
}
}
async function closeSessionFlow({ silent = false } = {}) {
if (!hasSession.value) {
return false
}
if (!silent) {
sessionLoading.value = true
}
resetPolling()
resetPollingWarning()
try {
const response = await removeClaimSession(token)
if (response.code !== 0) {
throw new Error(response.msg || '关闭领取会话失败')
}
detail.value = mergeClaimDetailData(detail.value, response.data)
applyLoginTypeFromDetail(response.data)
if (!silent) {
showSuccess(response.msg || '领取会话已关闭')
}
return true
} catch (error) {
if (!silent) {
notifyTencentActionError(error, '关闭领取会话失败')
}
return false
} finally {
if (!silent) {
sessionLoading.value = false
}
}
}
async function refreshSessionSummary({ silent = false } = {}) {
if (!hasSession.value) {
return false
@@ -236,14 +317,28 @@ export function useClaimPage(token: string) {
}
try {
const currentSession = detail.value?.session || null
const response = await fetchClaimSessionSummary(token)
if (response.code !== 0) {
throw new Error(response.msg || '领取会话状态刷新失败')
}
detail.value = mergeClaimDetailData(detail.value, response.data)
applyLoginTypeFromDetail(response.data)
let nextDetail = mergeClaimDetailData(detail.value, response.data)
detail.value = nextDetail
applyLoginTypeFromDetail(nextDetail)
if (shouldRefreshClaimQrImage(currentSession, response.data)) {
const fullResponse = await fetchClaimDetail(token)
if (fullResponse.code !== 0) {
throw new Error(fullResponse.msg || '领取二维码刷新失败')
}
nextDetail = mergeClaimDetailData(nextDetail, fullResponse.data)
detail.value = nextDetail
applyLoginTypeFromDetail(nextDetail)
}
if (silent) {
resetPollingWarning()
@@ -324,11 +419,12 @@ export function useClaimPage(token: string) {
return
}
loginType.value = nextLoginType
if (hasSession.value) {
await createSessionFlow(nextLoginType)
return
}
loginType.value = nextLoginType
}
function startPolling() {
@@ -461,6 +557,8 @@ export function useClaimPage(token: string) {
screenshotUrl,
showScreenshot,
createSessionFlow,
reloadSessionPage,
closeSessionFlow,
refreshSessionSummary,
switchLoginType,
confirmRoleNow,
@@ -469,9 +567,17 @@ export function useClaimPage(token: string) {
}
}
function syncLoginTypeFromDetail(detail: ClaimDetailData) {
function syncLoginTypeFromDetail(detail: ClaimDetailData, fallback: TencentLoginType = DEFAULT_LOGIN_TYPE) {
const nextLoginType = String(detail.session?.loginType || detail.task.loginType || '').trim()
return nextLoginType === 'wx' ? 'wx' : 'qq'
if (nextLoginType === 'wx') {
return 'wx'
}
if (nextLoginType === 'qq') {
return 'qq'
}
return fallback
}
function shouldKeepPolling(detail: ClaimDetailData | null) {
@@ -516,17 +622,17 @@ function resolveTaskStatusLabel(taskStatus?: string, sessionStatus?: string) {
}
function mergeClaimDetailData(current: ClaimDetailData | null, next: ClaimDetailData) {
if (!current?.session) {
return next
}
if (!next.session) {
if (next.session === null) {
return {
...next,
session: current.session,
session: null,
}
}
if (!current?.session) {
return next
}
return {
...next,
session: {
@@ -542,3 +648,29 @@ function mergeClaimDetailData(current: ClaimDetailData | null, next: ClaimDetail
},
}
}
function shouldRefreshClaimQrImage(
currentSession: ClaimDetailData['session'],
nextDetail: ClaimDetailData,
) {
if (!currentSession || !nextDetail.session) {
return false
}
return shouldRefreshQrImage(currentSession, nextDetail.session)
}
function shouldRefreshQrImage(
current: TencentBrowserSessionData | TencentBrowserSessionSummaryData,
next: TencentBrowserSessionData | TencentBrowserSessionSummaryData,
) {
if (!next.artifacts?.hasQrImage) {
return false
}
if (!current.qrImageBase64) {
return true
}
return Boolean(next.qrUpdatedAt && next.qrUpdatedAt !== current.qrUpdatedAt)
}
@@ -210,6 +210,7 @@ export function useTencentBrowserSessionPage() {
return {
activityInfo,
canRedeem,
teardownSession,
createSessionFlow,
hasSession,
initButtonLabel,