feat: 新增银行卡检查插件及相关依赖与配置文件
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
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<PluginNoteEntity>;
|
||||
|
||||
/** 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);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { CoolController, BaseController } from '@cool-midway/core';
|
||||
import { Body, Get, Headers, Inject, Post, Provide, Query } from '@midwayjs/core';
|
||||
import { InjectEntityModel } from '@midwayjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { PluginNoteEntity } from '../../entity/runtime';
|
||||
import { CenterAuthService, ApiError } from '../../service/auth';
|
||||
|
||||
/**
|
||||
* 客户端插件备注(账号 × 插件 唯一):
|
||||
* - GET /api/plugin-notes?account= 取该账号全部备注(客户端打开/切换插件时合并到本机缓存)
|
||||
* - POST /api/plugin-notes 保存备注(note 为空 = 清空云端记录)
|
||||
* 身份口径与 /api/plugins 一致:Bearer cloud_token 优先,兜底 account + X-API-Token。
|
||||
* 备注是「当前最新值」,每次执行时的快照另存 run_logs.note(改备注不影响历史追溯)。
|
||||
*/
|
||||
@Provide()
|
||||
@CoolController('/api')
|
||||
export class ApiPluginNotesController extends BaseController {
|
||||
@Inject()
|
||||
centerAuthService: CenterAuthService;
|
||||
|
||||
@InjectEntityModel(PluginNoteEntity)
|
||||
noteRepo: Repository<PluginNoteEntity>;
|
||||
|
||||
/** 解析账号身份;无有效身份抛 400 */
|
||||
private async resolveAcct(
|
||||
account: string,
|
||||
xApiToken: string,
|
||||
authorization: string
|
||||
): Promise<string> {
|
||||
let acct = await this.centerAuthService.cloudAccount(authorization);
|
||||
if (!acct) {
|
||||
this.centerAuthService.checkApiToken(xApiToken);
|
||||
acct = (account || '').trim();
|
||||
}
|
||||
if (!acct) throw new ApiError(400, '缺少有效会话或 account');
|
||||
return acct;
|
||||
}
|
||||
|
||||
/** GET /api/plugin-notes?account= — 该账号全部备注 { plugin_id: {note, at} } */
|
||||
@Get('/plugin-notes')
|
||||
async notesOfAccount(
|
||||
@Query('account') account: string = '',
|
||||
@Headers('X-API-Token') xApiToken: string = '',
|
||||
@Headers('Authorization') authorization: string = ''
|
||||
) {
|
||||
const acct = await this.resolveAcct(account, xApiToken, authorization);
|
||||
const rows = await this.noteRepo.findBy({ account: acct });
|
||||
const notes: Record<string, { note: string; at: number }> = {};
|
||||
for (const r of rows) {
|
||||
const note = String(r.note || '').trim();
|
||||
if (!note) continue;
|
||||
const t = r.updatedAt ? new Date(r.updatedAt).getTime() : 0;
|
||||
notes[r.pluginId] = { note, at: t ? Math.floor(t / 1000) : 0 };
|
||||
}
|
||||
return { ok: true, account: acct, notes };
|
||||
}
|
||||
|
||||
/** POST /api/plugin-notes — 保存/清空备注 */
|
||||
@Post('/plugin-notes')
|
||||
async save(
|
||||
@Body() b: any,
|
||||
@Headers('X-API-Token') xApiToken: string = '',
|
||||
@Headers('Authorization') authorization: string = ''
|
||||
) {
|
||||
const acct = await this.resolveAcct(b?.account, xApiToken, authorization);
|
||||
const pid = String(b?.plugin_id || '').trim();
|
||||
if (!pid) throw new ApiError(400, 'plugin_id 不能为空');
|
||||
const note = String(b?.note === undefined || b?.note === null ? '' : b.note).trim().slice(0, 500);
|
||||
if (!note) {
|
||||
await this.noteRepo.delete({ account: acct, pluginId: pid });
|
||||
return { ok: true, plugin_id: pid, note: '' };
|
||||
}
|
||||
await this.noteRepo.manager.query(
|
||||
`INSERT INTO plugin_notes(account, plugin_id, note, device_id, created_at, updated_at)
|
||||
VALUES (?,?,?,?,NOW(),NOW())
|
||||
ON DUPLICATE KEY UPDATE note = VALUES(note), device_id = VALUES(device_id), updated_at = NOW()`,
|
||||
[acct, pid, note, String(b?.device_id || '')]
|
||||
);
|
||||
return { ok: true, plugin_id: pid, note };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user