refactor: remove verify-code/reply support (not needed)

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
stswangzhiping
2026-05-14 22:20:10 +08:00
parent 80e1c97000
commit eeb984ebfe
2 changed files with 23 additions and 89 deletions

View File

@@ -7,18 +7,14 @@
* Requires Node.js >= 18 (global fetch).
*
* method: login
* params : { callId, timeout, emit, onReplyRef }
* returns: { abort, onReply }
* params : { callId, timeout, emit }
* returns: { abort }
*
* emit(payload) sends a sys-call reply upstream:
* { action:'event', event:'qrcode', data:{ url, expire:30, index } }
* { action:'event', event:'need_verifycode', data:{ retry } }
* { action:'progress', event:'scanned', data:{ status:'waiting_confirm' } }
* { action:'finish', event:'success', data:{ accountId } }
* { action:'finish', event:'failed', code, message }
*
* To provide a verify code after 'need_verifycode', the upstream dispatcher
* should call the returned onReply({ data: { code: '1234' } }).
* { action:'event', event:'qrcode', data:{ url, expire:30, index } }
* { action:'progress', event:'scanned', data:{ status:'waiting_confirm' } }
* { action:'finish', event:'success', data:{ accountId } }
* { action:'finish', event:'failed', code, message }
*/
const crypto = require('crypto');
@@ -227,10 +223,9 @@ async function _fetchQRCode(botType) {
return data;
}
async function _pollQRStatus(apiBaseUrl, qrcode, verifyCode) {
async function _pollQRStatus(apiBaseUrl, qrcode) {
try {
let endpoint = `ilink/bot/get_qrcode_status?qrcode=${encodeURIComponent(qrcode)}`;
if (verifyCode) endpoint += `&verify_code=${encodeURIComponent(verifyCode)}`;
const endpoint = `ilink/bot/get_qrcode_status?qrcode=${encodeURIComponent(qrcode)}`;
const data = await _getJson(apiBaseUrl, endpoint, QR_LONG_POLL_TIMEOUT_MS);
if (data.ret !== undefined && data.ret !== 0) {
throw new Error(`get_qrcode_status ret=${data.ret} errmsg=${data.errmsg || ''}`);
@@ -279,47 +274,15 @@ function login({ callId, timeout = 180, botType = DEFAULT_BOT_TYPE, emit }) {
let aborted = false;
let finished = false;
// Pending verify-code: resolve/reject
let _pendingVerifyResolve = null;
let _pendingVerifyReject = null;
function _waitForVerifyCode() {
return new Promise((resolve, reject) => {
_pendingVerifyResolve = resolve;
_pendingVerifyReject = reject;
});
}
function onReply(msg) {
const event = (msg.event || '').toString();
const code = (msg.data && msg.data.code) ? String(msg.data.code).trim() : '';
if (event === 'verify_code' && _pendingVerifyResolve) {
log.info('weixin', `callId=${callId} verify_code received`);
const resolve = _pendingVerifyResolve;
_pendingVerifyResolve = null;
_pendingVerifyReject = null;
resolve(code);
}
}
function abort() {
aborted = true;
if (_pendingVerifyReject) {
const reject = _pendingVerifyReject;
_pendingVerifyResolve = null;
_pendingVerifyReject = null;
reject(new Error('aborted'));
}
log.info('weixin', `callId=${callId} aborted`);
}
const timeoutMs = Math.max(timeout * 1000, 30_000);
// Run async without blocking caller
_runLogin({ callId, timeoutMs, botType, emit,
isAborted: () => aborted,
waitForVerifyCode: _waitForVerifyCode,
})
_runLogin({ callId, timeoutMs, botType, emit, isAborted: () => aborted })
.then(() => { finished = true; })
.catch((err) => {
if (finished) return;
@@ -328,10 +291,10 @@ function login({ callId, timeout = 180, botType = DEFAULT_BOT_TYPE, emit }) {
emit({ action: 'finish', event: 'failed', code: 500, message: err.message });
});
return { abort, onReply };
return { abort };
}
async function _runLogin({ callId, timeoutMs, botType, emit, isAborted, waitForVerifyCode }) {
async function _runLogin({ callId, timeoutMs, botType, emit, isAborted }) {
let qrRefreshCount = 1;
let scannedEmitted = false;
let activeLogin = null;
@@ -344,8 +307,7 @@ async function _runLogin({ callId, timeoutMs, botType, emit, isAborted, waitForV
qrcode: qrData.qrcode,
qrcodeUrl: qrData.qrcode_img_content,
startedAt: Date.now(),
apiBaseUrl: FIXED_BASE_URL,
pendingVerify: null,
apiBaseUrl: FIXED_BASE_URL,
};
emit({
action: 'event',
@@ -366,18 +328,12 @@ async function _runLogin({ callId, timeoutMs, botType, emit, isAborted, waitForV
emit({ action: 'finish', event: 'failed', code: 1003, message: 'QR expired too many times' });
return;
}
await startOrRefreshQr();
scannedEmitted = false;
activeLogin.pendingVerify = null;
continue;
await startOrRefreshQr();
scannedEmitted = false;
continue;
}
const status = await _pollQRStatus(
activeLogin.apiBaseUrl,
activeLogin.qrcode,
activeLogin.pendingVerify
);
activeLogin.pendingVerify = null; // consumed
const status = await _pollQRStatus(activeLogin.apiBaseUrl, activeLogin.qrcode);
if (isAborted()) return;
@@ -399,18 +355,10 @@ async function _runLogin({ callId, timeoutMs, botType, emit, isAborted, waitForV
}
case 'need_verifycode': {
const retry = !!activeLogin.pendingVerify;
emit({ action: 'event', event: 'need_verifycode', data: { retry } });
log.info('weixin', `callId=${callId} need_verifycode (retry=${retry})`);
try {
const code = await waitForVerifyCode();
if (isAborted()) return;
activeLogin.pendingVerify = code;
} catch (_) {
// aborted while waiting
return;
}
break;
// Verify code entry not supported; treat as failure
log.warn('weixin', `callId=${callId} need_verifycode not supported`);
emit({ action: 'finish', event: 'failed', code: 1005, message: 'verify code required (not supported)' });
return;
}
case 'expired': {