diff --git a/lib/channel/weixin.js b/lib/channel/weixin.js index 1b7beb0..40fda01 100644 --- a/lib/channel/weixin.js +++ b/lib/channel/weixin.js @@ -20,7 +20,7 @@ const crypto = require('crypto'); const fs = require('fs'); const path = require('path'); -const { execFileSync } = require('child_process'); +const { execFileSync, spawnSync } = require('child_process'); const log = require('../logger'); // ── Constants (from reference script) ──────────────────────────────────────── @@ -69,6 +69,29 @@ function _ensureWeixinStateOwnership() { } } + +function _restartGateway() { + try { + const result = spawnSync('openclaw gateway restart', { shell: true, stdio: 'inherit' }); + if (result.status !== 0) { + return { + ok: false, + code: result.status, + message: `openclaw gateway restart exited with code ${result.status}`, + }; + } + log.info('weixin', 'gateway restarted after login success'); + return { ok: true }; + } catch (err) { + log.warn('weixin', `gateway restart after login success failed: ${err.message}`); + return { + ok: false, + code: 1, + message: err.message, + }; + } +} + function _resolveWeixinStateDir() { return path.join(_resolveStateDir(), 'openclaw-weixin'); } function _resolveAccountIndexPath(){ return path.join(_resolveWeixinStateDir(), 'accounts.json'); } function _resolveAccountsDir() { return path.join(_resolveWeixinStateDir(), 'accounts'); } @@ -443,8 +466,23 @@ async function _runLogin({ callId, timeoutMs, botType, emit, isAborted }) { _registerAccountId(accountId); if (status.ilink_user_id) _clearStaleAccountsForUserId(accountId, status.ilink_user_id); _bumpOpenClawConfigTimestamp(); + _ensureWeixinStateOwnership(); log.info('weixin', `callId=${callId} login success accountId=${accountId} file=${filePath}`); emit({ action: 'finish', event: 'success', data: { accountId } }); + emit({ + action: 'progress', + event: 'gateway_restarting', + data: { + status: 'restarting', + message: 'OpenClaw 正在重启,请稍候。', + }, + }); + const gatewayRestart = _restartGateway(); + emit({ + action: 'event', + event: gatewayRestart.ok ? 'gateway_restart_succeeded' : 'gateway_restart_failed', + data: gatewayRestart, + }); return; }