fix: improve LuckMail verification resend flow

This commit is contained in:
zhangkun
2026-04-28 13:12:20 +08:00
parent 5c5296afec
commit 96e6fa9242
3 changed files with 137 additions and 1 deletions
+67 -1
View File
@@ -675,6 +675,68 @@
throw lastError || new Error(`步骤 ${step}:无法获取新的${getVerificationCodeLabel(step)}验证码。`);
}
function shouldRequestLuckmailResendBeforeRetry(error) {
const message = String(error?.message || error || '');
if (!message) {
return true;
}
return !/没有可用 token|token 对应邮箱与当前邮箱不一致/i.test(message);
}
async function pollLuckmailVerificationCodeWithResend(step, state, pollOverrides = {}) {
const {
onResendRequestedAt,
maxRounds: _ignoredMaxRounds,
maxResendRequests: _ignoredMaxResendRequests,
...cleanPollOverrides
} = pollOverrides;
const basePayload = {
...getVerificationPollPayload(step, state),
...cleanPollOverrides,
};
const maxAttempts = Math.max(1, Number(basePayload.maxAttempts) || 1);
const intervalMs = Math.max(15000, Number(basePayload.intervalMs) || 15000);
let lastError = null;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
throwIfStopped();
try {
return await pollLuckmailVerificationCode(step, state, {
...basePayload,
maxAttempts: 1,
intervalMs,
});
} catch (err) {
if (isStopError(err)) {
throw err;
}
lastError = err;
const canRetry = attempt < maxAttempts;
if (!canRetry) {
break;
}
if (shouldRequestLuckmailResendBeforeRetry(err)) {
try {
await requestVerificationCodeResend(step, pollOverrides);
} catch (resendError) {
if (isStopError(resendError)) {
throw resendError;
}
await addLog(`步骤 ${step}:LuckMail 点击重新发送验证码失败:${resendError.message},仍将在 ${Math.ceil(intervalMs / 1000)} 秒后继续轮询 /code 接口。`, 'warn');
}
}
await addLog(`步骤 ${step}LuckMail 暂未获取到新的${getVerificationCodeLabel(step)}验证码,等待 ${Math.ceil(intervalMs / 1000)} 秒后继续轮询 /code 接口(${attempt + 1}/${maxAttempts}...`, 'warn');
await sleepWithStop(intervalMs);
}
}
throw lastError || new Error(`步骤 ${step}:无法获取新的${getVerificationCodeLabel(step)}验证码。`);
}
async function pollFreshVerificationCode(step, state, mail, pollOverrides = {}) {
const {
onResendRequestedAt,
@@ -697,7 +759,11 @@
...getVerificationPollPayload(step, state),
...cleanPollOverrides,
}, cleanPollOverrides, `轮询${getVerificationCodeLabel(step)}验证码邮箱`);
return pollLuckmailVerificationCode(step, state, timedPoll.payload);
return pollLuckmailVerificationCodeWithResend(step, state, {
...cleanPollOverrides,
...timedPoll.payload,
onResendRequestedAt,
});
}
if (mail.provider === CLOUDFLARE_TEMP_EMAIL_PROVIDER) {
const timedPoll = await applyMailPollingTimeBudget(step, {