彻底重构-3
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
createTencentBrowserSession,
|
||||
getTencentBrowserSession,
|
||||
getTencentBrowserSessionSummary,
|
||||
getTencentBrowserSessionScreenshotPath,
|
||||
redeemTencentBrowserSession,
|
||||
} from '../session/session.js'
|
||||
@@ -36,7 +37,7 @@ export async function createClaimSession(token, payload = {}) {
|
||||
|
||||
if (context.task.browser_session_id) {
|
||||
const existingSession = await getTencentBrowserSession(context.task.browser_session_id)
|
||||
const syncedTask = syncTaskWithSession(context.task, existingSession)
|
||||
const syncedTask = await syncTaskWithSession(context.task, existingSession)
|
||||
|
||||
return buildClaimDetailPayload({
|
||||
claimToken: context.claimToken,
|
||||
@@ -50,10 +51,11 @@ export async function createClaimSession(token, payload = {}) {
|
||||
const session = await createTencentBrowserSession({
|
||||
loginType: payload.loginType,
|
||||
})
|
||||
const updatedTask = updateTask(context.task.id, {
|
||||
const updatedTask = await updateTask(context.task.id, {
|
||||
task_status: 'claimed',
|
||||
browser_session_id: session.sessionId,
|
||||
login_type: session.loginType,
|
||||
user_action_status: 'claimed',
|
||||
claimed_at: context.task.claimed_at || nowIso(),
|
||||
updated_at: nowIso(),
|
||||
})
|
||||
@@ -78,7 +80,7 @@ export async function getClaimSessionSummary(token) {
|
||||
}
|
||||
|
||||
const session = await getTencentBrowserSession(context.task.browser_session_id)
|
||||
const syncedTask = syncTaskWithSession(context.task, session)
|
||||
const syncedTask = await syncTaskWithSession(context.task, session)
|
||||
|
||||
return buildClaimDetailPayload({
|
||||
claimToken: context.claimToken,
|
||||
@@ -109,13 +111,14 @@ export async function confirmClaimRole(token) {
|
||||
})
|
||||
}
|
||||
|
||||
const updatedTask = updateTask(context.task.id, {
|
||||
const updatedTask = await updateTask(context.task.id, {
|
||||
task_status: 'role_confirmed',
|
||||
nickname: String(activityInfo.nickname || ''),
|
||||
role_id: String(activityInfo.role.roleId || ''),
|
||||
role_name: String(activityInfo.role.roleName || ''),
|
||||
area: String(activityInfo.role.area || ''),
|
||||
partition_name: String(activityInfo.role.partition || ''),
|
||||
user_action_status: 'role_confirmed',
|
||||
role_confirmed_at: nowIso(),
|
||||
updated_at: nowIso(),
|
||||
last_error: '',
|
||||
@@ -148,7 +151,7 @@ export async function redeemClaimTask(token) {
|
||||
})
|
||||
}
|
||||
|
||||
const cdk = context.task.reserved_cdk_id ? getCdkById(context.task.reserved_cdk_id) : null
|
||||
const cdk = context.task.primary_inventory_item_id ? await getCdkById(context.task.primary_inventory_item_id) : null
|
||||
|
||||
if (!cdk || !String(cdk.cdk_code || '').trim()) {
|
||||
throw createHttpError('当前任务没有可用的预占 CDK', {
|
||||
@@ -157,8 +160,9 @@ export async function redeemClaimTask(token) {
|
||||
})
|
||||
}
|
||||
|
||||
updateTask(context.task.id, {
|
||||
await updateTask(context.task.id, {
|
||||
task_status: 'redeeming',
|
||||
delivery_status: 'processing',
|
||||
updated_at: nowIso(),
|
||||
last_error: '',
|
||||
})
|
||||
@@ -168,8 +172,10 @@ export async function redeemClaimTask(token) {
|
||||
code: cdk.cdk_code,
|
||||
})
|
||||
const finalRedeem = session.redeem?.final?.redeem || null
|
||||
const updatedTask = updateTask(context.task.id, {
|
||||
const updatedTask = await updateTask(context.task.id, {
|
||||
task_status: 'redeemed',
|
||||
inventory_status: 'consumed',
|
||||
delivery_status: 'delivered',
|
||||
result_code: String(finalRedeem?.iRet || finalRedeem?.ret || '0'),
|
||||
result_message: String(finalRedeem?.sMsg || finalRedeem?.msg || session.notice || '兑换完成'),
|
||||
screenshot_path: session.artifacts?.hasScreenshot ? await getTencentBrowserSessionScreenshotPath(session.sessionId) : '',
|
||||
@@ -179,7 +185,7 @@ export async function redeemClaimTask(token) {
|
||||
last_error: '',
|
||||
})
|
||||
|
||||
markCdkDelivered(cdk.id, nowIso())
|
||||
await markCdkDelivered(cdk.id, nowIso())
|
||||
|
||||
return buildClaimDetailPayload({
|
||||
claimToken: context.claimToken,
|
||||
@@ -189,10 +195,11 @@ export async function redeemClaimTask(token) {
|
||||
session,
|
||||
})
|
||||
} catch (error) {
|
||||
const nextRetryCount = Number(context.task.retry_count || 0) + 1
|
||||
const failedTask = updateTask(context.task.id, {
|
||||
const nextRetryCount = Number(context.task.attempt_count || 0) + 1
|
||||
const failedTask = await updateTask(context.task.id, {
|
||||
task_status: 'retry_pending',
|
||||
retry_count: nextRetryCount,
|
||||
delivery_status: 'pending',
|
||||
attempt_count: nextRetryCount,
|
||||
last_error: error instanceof Error ? error.message : String(error || ''),
|
||||
updated_at: nowIso(),
|
||||
})
|
||||
@@ -230,7 +237,7 @@ async function getClaimContext(token) {
|
||||
})
|
||||
}
|
||||
|
||||
const claimToken = findClaimTokenByToken(normalized)
|
||||
const claimToken = await findClaimTokenByToken(normalized)
|
||||
|
||||
if (!claimToken) {
|
||||
throw createHttpError('领取链接无效或不存在', {
|
||||
@@ -239,7 +246,7 @@ async function getClaimContext(token) {
|
||||
})
|
||||
}
|
||||
|
||||
const task = findTaskByClaimTokenId(claimToken.id)
|
||||
const task = await findTaskByClaimTokenId(claimToken.id)
|
||||
|
||||
if (!task) {
|
||||
throw createHttpError('领取任务不存在', {
|
||||
@@ -256,7 +263,7 @@ async function getClaimContext(token) {
|
||||
}
|
||||
|
||||
if (claimToken.expired_at && new Date(claimToken.expired_at).getTime() <= Date.now()) {
|
||||
const expiredContext = expireClaimContext(claimToken, task)
|
||||
const expiredContext = await expireClaimContext(claimToken, task)
|
||||
|
||||
throw createHttpError('领取链接已过期', {
|
||||
statusCode: 410,
|
||||
@@ -265,8 +272,10 @@ async function getClaimContext(token) {
|
||||
})
|
||||
}
|
||||
|
||||
const order = getOrderById(task.order_id)
|
||||
const orderItem = getOrderItemById(task.order_item_id)
|
||||
const [order, orderItem] = await Promise.all([
|
||||
getOrderById(task.order_id),
|
||||
getOrderItemById(task.order_item_id),
|
||||
])
|
||||
|
||||
if (!order || !orderItem) {
|
||||
throw createHttpError('领取任务关联订单不完整', {
|
||||
@@ -292,9 +301,9 @@ function assertTaskCanProceed(task) {
|
||||
}
|
||||
}
|
||||
|
||||
function expireClaimContext(claimToken, task) {
|
||||
async function expireClaimContext(claimToken, task) {
|
||||
const now = nowIso()
|
||||
const nextClaimToken = updateClaimToken(claimToken.id, {
|
||||
const nextClaimToken = await updateClaimToken(claimToken.id, {
|
||||
status: 'expired',
|
||||
updated_at: now,
|
||||
})
|
||||
@@ -302,13 +311,14 @@ function expireClaimContext(claimToken, task) {
|
||||
let nextTask = task
|
||||
|
||||
if (!CLAIM_TERMINAL_STATUSES.has(String(task.task_status || '')) && task.task_status !== 'redeemed') {
|
||||
if (task.reserved_cdk_id) {
|
||||
releaseReservedCdk(task.reserved_cdk_id, now)
|
||||
if (task.primary_inventory_item_id) {
|
||||
await releaseReservedCdk(task.primary_inventory_item_id, now)
|
||||
}
|
||||
|
||||
nextTask = updateTask(task.id, {
|
||||
nextTask = await updateTask(task.id, {
|
||||
task_status: 'expired',
|
||||
reserved_cdk_id: null,
|
||||
inventory_status: 'pending',
|
||||
user_action_status: 'expired',
|
||||
last_error: '领取链接已过期,预占 CDK 已释放',
|
||||
updated_at: now,
|
||||
})
|
||||
@@ -330,7 +340,7 @@ async function loadTaskSession(task, { includeQrImage = false } = {}) {
|
||||
: getTencentBrowserSessionSummary(task.browser_session_id)
|
||||
}
|
||||
|
||||
function syncTaskWithSession(task, session) {
|
||||
async function syncTaskWithSession(task, session) {
|
||||
const activityInfo = session.activityInfo || null
|
||||
const patch = {
|
||||
login_type: String(session.loginType || task.login_type || ''),
|
||||
@@ -350,6 +360,7 @@ function syncTaskWithSession(task, session) {
|
||||
|
||||
if (task.task_status === 'link_generated') {
|
||||
patch.task_status = 'claimed'
|
||||
patch.user_action_status = 'claimed'
|
||||
patch.claimed_at = task.claimed_at || nowIso()
|
||||
}
|
||||
|
||||
@@ -372,7 +383,7 @@ function buildClaimDetailPayload({ claimToken, task, order, orderItem, session }
|
||||
taskId: task.id,
|
||||
taskNo: task.task_no,
|
||||
status: task.task_status,
|
||||
expiresAt: task.expires_at,
|
||||
expiresAt: claimToken.expired_at,
|
||||
claimedAt: task.claimed_at,
|
||||
roleConfirmedAt: task.role_confirmed_at,
|
||||
redeemedAt: task.redeemed_at,
|
||||
|
||||
Reference in New Issue
Block a user