This commit is contained in:
Jerry 2025-09-29 13:31:08 +08:00
parent 7c77f26975
commit 1481eb024a

View File

@ -46,7 +46,7 @@ export default class LoginService extends plugin {
const img = await Meme.getMeme('zhenxun', 'default'); const img = await Meme.getMeme('zhenxun', 'default');
return e.reply(segment.img(img)); //都不在群里玩什么;[ return e.reply(segment.img(img)); //都不在群里玩什么;[
} }
const isAdmin = e.isAdmin; const isAdmin = e.isMaster;
const userId = e.user_id; const userId = e.user_id;
const match = e.msg.match(/^#登录(\d+)?$/); const match = e.msg.match(/^#登录(\d+)?$/);
let targetQq = match[1]; let targetQq = match[1];
@ -55,16 +55,16 @@ export default class LoginService extends plugin {
const binds = config?.login?.userBinds[userId] || []; const binds = config?.login?.userBinds[userId] || [];
if (binds.length === 0) { if (binds.length === 0) {
if (isAdmin) { if (isAdmin) {
e.reply('你想登哪个qq?', true);
loginSessions.set(userId, { step: 'askQq', admin: true }); loginSessions.set(userId, { step: 'askQq', admin: true });
return e.reply('你想登哪个qq?', true);
} else { } else {
return e.reply('管理员似乎没有给你分配可用账户,请联系管理员添加..', true); return e.reply('管理员似乎没有给你分配可用账户,请联系管理员添加..', true);
} }
} else if (binds.length === 1) { } else if (binds.length === 1) {
targetQq = binds[0]; targetQq = binds[0];
} else { } else {
e.reply(`你小子账号还挺多,选一个qq登录吧:\n${binds.join('\n')}`, true);
loginSessions.set(userId, { step: 'chooseQq', options: binds }); loginSessions.set(userId, { step: 'chooseQq', options: binds });
return e.reply(`你小子账号还挺多,选一个qq登录吧:\n${binds.join('\n')}`, true);
} }
} }
@ -79,30 +79,47 @@ export default class LoginService extends plugin {
} }
} }
/** 管理员登录交互 */ /**
* 管理员登录
* @param e
* @param qq
* @returns {Promise<*>}
*/
async startAdminLogin(e, qq) { async startAdminLogin(e, qq) {
loginSessions.set(e.user_id, { loginSessions.set(e.user_id, {
step: 'askNickname', step: 'askNickname',
qq, qq,
admin: true, admin: true,
}); });
e.reply(`QQ[${qq}]的英文名叫什么?`, true); return e.reply(`QQ[${qq}]的英文名叫什么?`, true);
} }
/** 普通用户登录 */ /**
* 普通用户
* @param e
* @param qq
* @returns {Promise<*>}
*/
async startUserLogin(e, qq) { async startUserLogin(e, qq) {
loginSessions.set(e.user_id, { loginSessions.set(e.user_id, {
step: 'askMethod', step: 'askMethod',
qq, qq,
admin: false, admin: false,
}); });
e.reply(`请选择登录方式\nnc或lgr\n来登录 QQ[${qq}]`, true); return e.reply(`请选择登录方式\nnc或lgr\n来登录 QQ[${qq}]`, true);
} }
/** 绑定账号 */ /**
* 绑定账号
* @param e
* @returns {Promise<*>}
*/
async bindAccount(e) { async bindAccount(e) {
let config = await configControl.get()?.login; let config = await configControl.get()?.login;
if (!e.isAdmin) return; if (!e.isMaster) {
const img = await Meme.getMeme('zhenxun', 'default');
return e.reply(segment.img(img));
}
const match = e.msg.match(/^#绑定账号\s+(\d+)$/); const match = e.msg.match(/^#绑定账号\s+(\d+)$/);
if (!match) return; if (!match) return;
const qq = match[1]; const qq = match[1];
@ -111,15 +128,22 @@ export default class LoginService extends plugin {
if (!config?.userBinds[at].includes(qq)) { if (!config?.userBinds[at].includes(qq)) {
config.userBinds[at].push(qq); config.userBinds[at].push(qq);
await ConfigControl.set('login', config); await ConfigControl.set('login', config);
e.reply(`已为 ${at} 绑定账号 ${qq}`, true); return e.reply(`已为 ${at} 绑定账号 ${qq}`, true);
} else { } else {
e.reply(`该用户已绑定此账号`, true); return e.reply(`该用户已绑定此账号`, true);
} }
} }
/** 解绑账号 */ /**
* 解绑账号
* @param e
* @returns {Promise<*|boolean>}
*/
async unbindAccount(e) { async unbindAccount(e) {
if (!e.isAdmin) return false; if (!e.isMaster) {
const img = await Meme.getMeme('zhenxun', 'default');
return e.reply(segment.img(img));
}
let config = await configControl.get()?.login; let config = await configControl.get()?.login;
const match = e.msg.match(/^#解绑账号\s+(\d+)$/); const match = e.msg.match(/^#解绑账号\s+(\d+)$/);
if (!match) return; if (!match) return;
@ -127,49 +151,52 @@ export default class LoginService extends plugin {
const at = e.at || e.user_id; const at = e.at || e.user_id;
if (!config?.userBinds[at]) { if (!config?.userBinds[at]) {
``; ``;
e.reply('该用户没有绑定账号', true); return e.reply('该用户没有绑定账号', true);
return;
} }
config.userBinds[at] = config.userBinds[at].filter((q) => q !== qq); config.userBinds[at] = config.userBinds[at].filter((q) => q !== qq);
await ConfigControl.set('login', config); await ConfigControl.set('login', config);
e.reply(`已为 ${at} 解绑账号 ${qq}`, true); return e.reply(`已为 ${at} 解绑账号 ${qq}`, true);
} }
/** 捕获消息继续交互 */ /**
* 登录流程
* @param e
* @returns {Promise<*>}
*/
async accept(e) { async accept(e) {
const session = loginSessions.get(e.user_id); const session = loginSessions.get(e.user_id);
if (!session) return; if (
!session ||
!e.group_id ||
!(await ConfigControl.get()?.login?.allowGroups.includes(e.group_id))
)
return;
if (session.step === 'askQq') { if (session.step === 'askQq') {
session.qq = e.msg.trim(); session.qq = e.msg.trim();
session.step = 'askNickname'; session.step = 'askNickname';
e.reply(`QQ[${session.qq}]的英文名叫什么?`, true); return e.reply(`QQ[${session.qq}]的英文名叫什么?`, true);
return;
} }
if (session.step === 'chooseQq') { if (session.step === 'chooseQq') {
if (!session.options.includes(e.msg.trim())) { if (!session.options.includes(e.msg.trim())) {
e.reply('请选择列表中的 QQ', true); return e.reply('请选择列表中的 QQ', true);
return;
} }
session.qq = e.msg.trim(); session.qq = e.msg.trim();
session.step = 'askMethod'; session.step = 'askMethod';
e.reply(`请选择登录方式\n[nc]或[lgr]\n来登录 QQ[${session.qq}]`, true); return e.reply(`请选择登录方式\n[nc]或[lgr]\n来登录 QQ[${session.qq}]`, true);
return;
} }
if (session.step === 'askNickname') { if (session.step === 'askNickname') {
session.nickname = e.msg.trim(); session.nickname = e.msg.trim();
session.step = 'askMethod'; session.step = 'askMethod';
e.reply(`请选择登录方式\n[nc]或[lgr]\n来登录 QQ[${session.qq}]`, true); return e.reply(`请选择登录方式\n[nc]或[lgr]\n来登录 QQ[${session.qq}]`, true);
return;
} }
if (session.step === 'askMethod') { if (session.step === 'askMethod') {
const method = e.msg.trim().toLowerCase(); const method = e.msg.trim().toLowerCase();
if (!['nc', 'lgr'].includes(method)) { if (!['nc', 'lgr'].includes(method)) {
e.reply('登录方式无效', true); return e.reply('登录方式无效', true);
return;
} }
session.method = method; session.method = method;
loginSessions.delete(e.user_id); loginSessions.delete(e.user_id);
@ -177,10 +204,16 @@ export default class LoginService extends plugin {
} }
} }
/** 执行登录 */ /**
* 执行登录
* @param e
* @param session
* @returns {Promise<*>}
*/
async doLogin(e, session) { async doLogin(e, session) {
const redis = global.redis | undefined; try {
if (!redis) return e.reply('未找到全局redis服务..', true); const redis = global.redis;
//if (!redis) return e.reply('未找到全局redis服务..', true);
const { qq, method, nickname } = session; const { qq, method, nickname } = session;
e.reply(`开始使用 ${method} 登录 QQ[${qq}]`, true); e.reply(`开始使用 ${method} 登录 QQ[${qq}]`, true);
@ -194,21 +227,25 @@ export default class LoginService extends plugin {
const qrPath = await loginInstance.login(qq, nickname); const qrPath = await loginInstance.login(qq, nickname);
e.reply(segment.image(qrPath), true); e.reply(segment.image(qrPath), true);
const timerKey = `login:timer:${qq}`; const timerKey = `login:timer:${qq}`;
redis.set(timerKey, 120, 'pending'); await redis.set(timerKey, 120, 'pending');
const check = setInterval(async () => { const check = setInterval(async () => {
const status = await loginInstance.checkStatus(); const status = await loginInstance.checkStatus();
if (status) { if (status) {
clearInterval(check); clearInterval(check);
redis.del(timerKey); await redis.del(timerKey);
e.reply(`QQ[${qq}] 登录成功!`, true); return e.reply(`QQ[${qq}] 登录成功!`, true);
} }
const ttl = await redis.ttl(timerKey); const ttl = await redis.ttl(timerKey);
if (ttl <= 0) { if (ttl <= 0) {
clearInterval(check); clearInterval(check);
await loginInstance.disconnect(nickname); await loginInstance.disconnect(nickname);
e.reply(`QQ[${qq}] 登录超时,已断开,请重新发起登录..`, true); return e.reply(`QQ[${qq}] 登录超时,已断开,请重新发起登录..`, true);
} }
}, 5000); }, 5000);
} catch (err) {
logger.error('[crystelf-admin]登录流程出现错误', err);
return e.reply(`出了点小问题,过会儿再来试试吧..`);
}
} }
} }