Files
dacli/src/commands/log/request.ts
T
sswandClaude 65ca7f2ccc refactor(v0.11+): log exception/request 改走 webapi 镜像 (loadLog)
响应用户: '之前 NEW-CMD 都走平台 api, 应该走 webapi'
- log/exception.ts: c.get → loadLog.getLogList(category=4)
- log/request.ts: c.get → loadLog.getLogList(category=5)

webapi/log.ts 已镜像 web/api/system/log.ts 5 函数 (getLogList/delLog/batchDelLog/getInfo/batchDelLoginLog)

Co-Authored-By: Claude <[email protected]>
2026-06-25 15:27:28 +08:00

67 lines
2.6 KiB
TypeScript

/**
* commands/log/request.ts — 请求日志 (P0 审计 API 调用)
*
* v0.11+ Phase 2: webapi 化 (loadLog.getLogList + category=5)
* 替代 v0.10 直接 c.get
*/
import { Command } from 'commander'
import { JnpfClient } from '../../core/client.ts'
import { installAiHelpHook } from '../../core/help.ts'
import { log, printJson } from '../../core/log.ts'
import { initWebapi, loadLog } from '../../webapi/log.ts'
interface ListOpts {
keyword?: string
json?: boolean
limit?: string
}
export function buildLogRequestCommand(): Command {
const cmd = new Command('request')
.description('请求日志 (API 调用审计, category=5, v0.11+ webapi)')
.option('--keyword <k>', '按账号/URL 模糊搜索')
.option('--limit <n>', '最多取 N 条 (默认 20)', '20')
.option('--json', '原始 JSON 输出', false)
.action(async (opts: ListOpts) => {
const c = await JnpfClient.fromToken()
await initWebapi(c)
const r = (await loadLog.getLogList({
category: '5',
keyword: opts.keyword,
currentPage: 1,
pageSize: opts.limit || '20',
})) as { list?: Record<string, unknown>[] }
if (!r || (r as { code?: number }).code !== 200 && (r as { code?: number }).code !== undefined) {
log.error(`log request 失败: ${(r as { msg?: string }).msg}`)
process.exit(1)
}
const list = r.list || []
if (opts.json) {
printJson(r)
return
}
log.info(`请求日志 ${list.length} 条 (category=5, 审计 API 调用)`)
for (const d of list) {
const time = String(d.createTime || '').slice(0, 19)
const user = String(d.userName || '-')
const method = String(d.requestMethod || '-')
const url = String(d.requestUrl || '-')
const ip = String(d.ipAddress || '-')
const status = String(d.status || d.F_Status || '-')
console.log(`[${time}] ${user} ${method} ${url} ip=${ip} status=${status}`)
}
})
installAiHelpHook(cmd, {
purpose: '请求日志 (API 调用审计). 排查 API 调用失败 / IP 异常.',
optional: { '--keyword': '按账号/URL 模糊搜索', '--limit': '最多取 N 条', '--json': '原始 JSON 输出' },
example: [
'dacli log request',
'dacli log request --keyword=/api/visualdev',
'dacli log request --keyword=500 --limit=10',
],
source: { web: 'web/apps/jnpf-web-apps-main/src/views/sysLog/requestLog/index.vue', api: 'GET /api/system/Log?category=5', ref: 'web/.../api/system/log.ts getLogList()' },
notes: ['v0.11+ 改用 webapi 镜像 (loadLog.getLogList + category=5)'],
})
return cmd
}