import { CoolController, BaseController } from '@cool-midway/core'; import { Get, Headers, Inject, Provide, Query } from '@midwayjs/core'; import { InjectEntityModel } from '@midwayjs/typeorm'; import { Repository } from 'typeorm'; import { PluginNoteEntity } from '../../entity/runtime'; import { CenterAuthService } from '../../service/auth'; import { pageBounds } from '../../comm/util'; /** * 插件备注管理端(账号 × 插件 唯一): * - GET /api/admin/plugin-notes 分页 + 账号/插件/关键字筛选,供管理台查看「谁给哪个插件写了什么备注」 * 注:每次执行时的备注快照看 run_logs.note(运行日志页),这里是当前最新值。 */ @Provide() @CoolController('/api/admin') export class AdminPluginNotesController extends BaseController { @Inject() centerAuthService: CenterAuthService; @InjectEntityModel(PluginNoteEntity) noteRepo: Repository; /** GET /api/admin/plugin-notes */ @Get('/plugin-notes') async notesList( @Query('page') page: number = 1, @Query('page_size') pageSize: number = 20, @Query('account') account: string = '', @Query('plugin') plugin: string = '', @Query('q') q: string = '', @Headers('X-Admin-Token') xAdminToken: string = '', @Headers('X-API-Token') xApiToken: string = '' ) { await this.centerAuthService.requirePerms( ['center:plugin:list', 'center:log:list'], xAdminToken, xApiToken ); const [limit, offset] = pageBounds(page, pageSize); const cond: string[] = []; const args: any[] = []; if ((account || '').trim()) { cond.push('n.account LIKE ?'); args.push(`%${account.trim()}%`); } if ((plugin || '').trim()) { cond.push('n.plugin_id LIKE ?'); args.push(`%${plugin.trim()}%`); } if ((q || '').trim()) { cond.push('(n.note LIKE ? OR n.account LIKE ? OR n.plugin_id LIKE ? OR p.name LIKE ?)'); const kw = `%${q.trim()}%`; args.push(kw, kw, kw, kw); } const where = cond.length ? ' WHERE ' + cond.join(' AND ') : ''; const m = this.noteRepo.manager; const totalRow: any = (await m.query( `SELECT COUNT(*) AS c FROM plugin_notes n LEFT JOIN plugins p ON p.id = n.plugin_id${where}`, args ))[0]; const rows: any[] = await m.query( `SELECT n.id, n.account, a.nickname, n.plugin_id, p.name AS plugin_name, n.note, n.device_id, n.created_at, n.updated_at FROM plugin_notes n LEFT JOIN accounts a ON a.account = n.account LEFT JOIN plugins p ON p.id = n.plugin_id ${where} ORDER BY n.updated_at DESC LIMIT ? OFFSET ?`, [...args, limit, offset] ); return { items: rows.map((r: any) => ({ id: Number(r.id), account: r.account || '', nickname: r.nickname || '', plugin_id: r.plugin_id || '', plugin_name: r.plugin_name || '', note: r.note || '', device_id: r.device_id || '', created_at: fmtStr(r.created_at), updated_at: fmtStr(r.updated_at), })), total: Number(totalRow.c || 0), }; } } function fmtStr(d: any): string { if (!d) return ''; if (d instanceof Date) { const p = (n: number) => String(n).padStart(2, '0'); return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())} ${p(d.getHours())}:${p(d.getMinutes())}:${p(d.getSeconds())}`; } return String(d).replace('T', ' ').slice(0, 19); }