fix:修复某些登录问题

This commit is contained in:
Jerry 2025-09-30 18:09:53 +08:00
parent 78379f1c94
commit 61cec10da1
3 changed files with 54 additions and 36 deletions

View File

@ -225,24 +225,33 @@ export default class LoginService extends plugin {
}
const qrPath = await loginInstance.login(qq, nickname);
e.reply(segment.image(qrPath), true);
const timerKey = `login:timer:${qq}`;
await redis.set(timerKey, 120, 'pending');
if (qrPath) {
e.reply(segment.image(qrPath), true);
const timerKey = `login:timer:${qq}`;
await redis.set(timerKey, 120, 'pending');
const check = setInterval(async () => {
const status = await loginInstance.checkStatus();
const check = setInterval(async () => {
const status = await loginInstance.checkStatus(qq);
if (status) {
clearInterval(check);
await redis.del(timerKey);
return e.reply(`QQ[${qq}] 登录成功!`, true);
}
const ttl = await redis.ttl(timerKey);
if (ttl <= 0) {
clearInterval(check);
await loginInstance.disconnect(nickname);
return e.reply(`QQ[${qq}] 登录超时,已断开,请重新发起登录..`, true);
}
}, 5000);
} else {
const status = await loginInstance.checkStatus(qq);
if (status) {
clearInterval(check);
await redis.del(timerKey);
return e.reply(`QQ[${qq}] 登录成功!`, true);
return e.reply(`QQ[${qq}] 使用上次登录缓存登录成功!`, true);
} else {
return e.reply(`QQ[${qq}] 登录出现未知错误,请联系管理员操作..`, true);
}
const ttl = await redis.ttl(timerKey);
if (ttl <= 0) {
clearInterval(check);
await loginInstance.disconnect(nickname);
return e.reply(`QQ[${qq}] 登录超时,已断开,请重新发起登录..`, true);
}
}, 5000);
}
} catch (err) {
logger.error('[crystelf-admin]登录流程出现错误', err);
return e.reply(`出了点小问题,过会儿再来试试吧..`);

View File

@ -55,31 +55,36 @@ export default class LgrService {
/**
* 等待qr图片更新
* @param targetDir 目标文件夹
* @param timeout
* @returns {Promise<unknown>}
* @param timeout 最大等待时间 (默认 30s)
* @returns {Promise<string|undefined>}
*/
async waitForQrUpdate(targetDir, timeout = 10000) {
async waitForQrUpdate(targetDir, timeout = 30000) {
const qrPath = path.join(targetDir, 'qr-0.png');
if (!fs.existsSync(qrPath)) {
return 'none';
}
let lastMtime = fs.statSync(qrPath).mtimeMs;
return new Promise((resolve) => {
const timer = setTimeout(() => {
watcher.close();
resolve('none');
}, timeout);
let resolved = false;
const timer = setTimeout(() => {
if (!resolved) {
resolved = true;
watcher.close();
resolve(undefined);
}
}, timeout);
const watcher = fs.watch(qrPath, (eventType) => {
if (eventType === 'change') {
const stat = fs.statSync(qrPath);
if (stat.mtimeMs !== lastMtime) {
lastMtime = stat.mtimeMs;
clearTimeout(timer);
watcher.close();
resolve(qrPath);
if (!resolved) {
resolved = true;
clearTimeout(timer);
watcher.close();
resolve(qrPath);
}
}
}
});

View File

@ -64,27 +64,31 @@ export default class NapcatService {
* @param timeout
* @returns {Promise<unknown>}
*/
async waitForQrUpdate(timeout = 10000) {
async waitForQrUpdate(timeout = 30000) {
if (!fs.existsSync(this.qrPath)) {
return 'none';
}
let lastMtime = fs.statSync(this.qrPath).mtimeMs;
return new Promise((resolve) => {
let resolved = false;
const timer = setTimeout(() => {
watcher.close();
resolve('none');
if (!resolved) {
resolved = true;
watcher.close();
resolve(undefined);
}
}, timeout);
const watcher = fs.watch(this.qrPath, (eventType) => {
if (eventType === 'change') {
const stat = fs.statSync(this.qrPath);
if (stat.mtimeMs !== lastMtime) {
lastMtime = stat.mtimeMs;
clearTimeout(timer);
watcher.close();
resolve(this.qrPath);
if (!resolved) {
resolved = true;
clearTimeout(timer);
watcher.close();
resolve(this.qrPath);
}
}
}
});