From 67e41a40fa4e68f2ab72d4211704a0e1e4a66254 Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 15 Jun 2026 10:17:55 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=20=E6=96=B0=E5=A2=9E=20online-kick?= =?UTF-8?q?=20=E5=91=BD=E4=BB=A4=20(admin/self=20=E5=BC=BA=E5=88=B6?= =?UTF-8?q?=E4=B8=8B=E7=BA=BF)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commands/user/online-kick.ts | 49 +++++++++++++++++++++++++++----- src/core/client.ts | 5 ++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/commands/user/online-kick.ts b/src/commands/user/online-kick.ts index 758c2c2..3ee8c18 100644 --- a/src/commands/user/online-kick.ts +++ b/src/commands/user/online-kick.ts @@ -1,16 +1,51 @@ /** - * commands/user/online-kick.ts — 占位, Task 13 实现 + * commands/user/online-kick.ts — admin/self 踢下线某 token + * + * API: DELETE /api/system/OnlineUser/{token} + * + * web 端触发: + * - 个人中心 Account/index.vue: handleExit(token) — 自己退出其他设备 + * - 在线用户 monitor/userOnline/index.vue — admin 强制下线 */ import { Command } from 'commander' -import { log } from '../../core/log.ts' +import { JnpfClient } from '../../core/client.ts' import { installAiHelpHook } from '../../core/help.ts' +import { log } from '../../core/log.ts' + +interface OnlineKickOpts { + token: string +} export function buildUserOnlineKickCommand(): Command { - const cmd = new Command('online-kick').description('TODO: implement in Task 13') - cmd.action(async () => { - log.warn('TODO: implement in Task 13') - process.exit(1) + const cmd = new Command('online-kick') + .description('强制下线某 token (admin/self 操作)') + .requiredOption('--token ', '要踢的 token (从 onlineUser 列表获取)') + .action(async (opts: OnlineKickOpts) => { + const c = await JnpfClient.fromToken() + // 防自踢: 当前登录 token 不能踢自己 + if (opts.token === c.getCurrentToken()) { + log.error('不能踢自己下线 (当前登录 token), 会立即断连') + process.exit(1) + } + const r = await c.del(`/system/OnlineUser/${opts.token}`) + if (r.code !== 200) { + log.error(`online-kick 失败 (code=${r.code}): ${r.msg}`) + process.exit(1) + } + log.info(`✓ token=${opts.token.slice(0, 8)}... 已踢下线`) + }) + installAiHelpHook(cmd, { + purpose: '强制下线某 token (admin/self 操作, 防自踢)', + required: { '--token': '要踢的 token (从 onlineUser 列表获取)' }, + example: 'dacli user online-kick --token=eyJhbGciOi...', + source: { + web: 'web/apps/jnpf-web-apps-main/src/views/_core/profile/components/Account/index.vue', + api: 'DELETE /api/system/OnlineUser/{token}', + }, + notes: [ + '**防自踢**: 当前登录 token 触发时拒绝 + exit(1)', + '**admin 用**: 配合 `dacli monitor online-user list --token=...` 查 token', + ], }) - installAiHelpHook(cmd, { purpose: 'TODO' }) return cmd } \ No newline at end of file diff --git a/src/core/client.ts b/src/core/client.ts index 43d07fd..8c41105 100644 --- a/src/core/client.ts +++ b/src/core/client.ts @@ -129,6 +129,11 @@ export class JnpfClient { saveToken(t) } + /** 当前登录 token (供 online-kick 等需防自踢/防自调的子命令读) */ + getCurrentToken(): string | undefined { + return this.token || undefined + } + // ----- HTTP ----- async get(path: string, query?: Record): Promise> {