(step, state, pollPayload = {})
| 2600 | } |
| 2601 | |
| 2602 | async function pollLuckmailVerificationCode(step, state, pollPayload = {}) { |
| 2603 | const purchase = getCurrentLuckmailPurchase(state); |
| 2604 | if (!purchase?.token) { |
| 2605 | throw new Error('LuckMail 当前没有可用 token,请先执行步骤 3 购买邮箱。'); |
| 2606 | } |
| 2607 | |
| 2608 | const client = createLuckmailClient(state); |
| 2609 | const maxAttempts = Math.max(1, Number(pollPayload.maxAttempts) || 5); |
| 2610 | const intervalMs = Math.max(1000, Number(pollPayload.intervalMs) || 3000); |
| 2611 | const filters = { |
| 2612 | afterTimestamp: pollPayload.filterAfterTimestamp || 0, |
| 2613 | senderFilters: pollPayload.senderFilters || [], |
| 2614 | subjectFilters: pollPayload.subjectFilters || [], |
| 2615 | excludeCodes: pollPayload.excludeCodes || [], |
| 2616 | }; |
| 2617 | |
| 2618 | let lastError = null; |
| 2619 | for (let attempt = 1; attempt <= maxAttempts; attempt++) { |
| 2620 | throwIfStopped(); |
| 2621 | await addLog(`步骤 ${step}:正在通过 LuckMail 轮询验证码(${attempt}/${maxAttempts})...`, 'info'); |
| 2622 | |
| 2623 | try { |
| 2624 | const tokenCode = await client.user.getTokenCode(purchase.token); |
| 2625 | const cursor = normalizeLuckmailMailCursor((await getState()).currentLuckmailMailCursor); |
| 2626 | if (tokenCode.verification_code && tokenCode.mail && !isLuckmailMailNewerThanCursor(tokenCode.mail, cursor)) { |
| 2627 | throw new Error(`步骤 ${step}:LuckMail 返回的最新邮件仍是旧验证码。`); |
| 2628 | } |
| 2629 | |
| 2630 | let match = null; |
| 2631 | if (tokenCode.has_new_mail || tokenCode.verification_code) { |
| 2632 | match = await resolveLuckmailVerificationMail(client, purchase.token, filters, tokenCode); |
| 2633 | } |
| 2634 | if (!match) { |
| 2635 | match = await resolveLuckmailVerificationMail(client, purchase.token, filters, null); |
| 2636 | } |
| 2637 | |
| 2638 | if (match?.mail) { |
| 2639 | const cursor = normalizeLuckmailMailCursor((await getState()).currentLuckmailMailCursor); |
| 2640 | if (!isLuckmailMailNewerThanCursor(match.mail, cursor)) { |
| 2641 | throw new Error(`步骤 ${step}:LuckMail 命中的邮件不是新邮件。`); |
| 2642 | } |
| 2643 | |
| 2644 | await setLuckmailMailCursorState(buildLuckmailMailCursor(match.mail)); |
| 2645 | return { |
| 2646 | ok: true, |
| 2647 | code: match.code, |
| 2648 | emailTimestamp: normalizeLuckmailTimestamp(match.mail.received_at) || Date.now(), |
| 2649 | mailId: match.mail.message_id, |
| 2650 | }; |
| 2651 | } |
| 2652 | |
| 2653 | lastError = new Error(`步骤 ${step}:暂未在 LuckMail 邮箱中找到新的匹配验证码。`); |
| 2654 | } catch (err) { |
| 2655 | if (isStopError(err)) { |
| 2656 | throw err; |
| 2657 | } |
| 2658 | lastError = err; |
| 2659 | await addLog(`步骤 ${step}:LuckMail 轮询失败:${err.message}`, 'warn'); |
no test coverage detected