109 lines
4.1 KiB
TypeScript
109 lines
4.1 KiB
TypeScript
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 { PluginEntity, AccountPluginEntity, AccountPlatformEntity } from '../../entity/plugin';
|
||
import { CenterAuthService, ApiError } from '../../service/auth';
|
||
|
||
/**
|
||
* 客户端插件清单接口(对齐旧 FastAPI apis/plugins.py /api/plugins、/api/platforms/allowed)
|
||
* 身份:优先 Authorization: Bearer <cloud_token>,兜底 account + X-API-Token
|
||
*/
|
||
@Provide()
|
||
@CoolController('/api')
|
||
export class ApiPluginsController extends BaseController {
|
||
@Inject()
|
||
centerAuthService: CenterAuthService;
|
||
|
||
@InjectEntityModel(PluginEntity)
|
||
pluginRepo: Repository<PluginEntity>;
|
||
|
||
@InjectEntityModel(AccountPluginEntity)
|
||
accountPluginRepo: Repository<AccountPluginEntity>;
|
||
|
||
@InjectEntityModel(AccountPlatformEntity)
|
||
accountPlatformRepo: Repository<AccountPlatformEntity>;
|
||
|
||
/** 解析账号身份;无有效身份抛 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/plugins — 按账号返回可用插件清单(权限隔离) */
|
||
@Get('/plugins')
|
||
async plugins(
|
||
@Query('account') account: string = '',
|
||
@Headers('X-API-Token') xApiToken: string = '',
|
||
@Headers('Authorization') authorization: string = ''
|
||
) {
|
||
const acct = await this.resolveAcct(account, xApiToken, authorization);
|
||
if (await this.centerAuthService.accountDisabled(acct)) {
|
||
return { account: acct, plugins: [] };
|
||
}
|
||
const rows = await this.accountPluginRepo
|
||
.createQueryBuilder('ap')
|
||
.innerJoin(PluginEntity, 'p', 'p.id = ap.pluginId')
|
||
.select([
|
||
'p.id AS id',
|
||
'p.name AS name',
|
||
'p.latestVersion AS latestVersion',
|
||
'p.testVersion AS testVersion',
|
||
'p.status AS status',
|
||
'p.category AS category',
|
||
])
|
||
// revoked=下架:不下发,执行机会按白名单清理;disabled=停用:仍下发但版本留空(不拉新、本地保留)
|
||
.where('ap.account = :account', { account: acct })
|
||
.andWhere("p.status <> 'revoked'")
|
||
.orderBy('p.id', 'ASC')
|
||
.getRawMany();
|
||
// 测试通道:status=testing 且账号是管理员 → 取在测版本;其他账号继续用正式版(业务不中断)
|
||
const isAdmin = await this.centerAuthService.accountIsAdmin(acct);
|
||
return {
|
||
account: acct,
|
||
plugins: rows.map(r => {
|
||
const testVer = String(r.testVersion || '').trim();
|
||
const disabled = r.status === 'disabled';
|
||
const useTest = !disabled && isAdmin && r.status === 'testing' && !!testVer;
|
||
return {
|
||
id: r.id,
|
||
name: r.name,
|
||
version: disabled ? '' : (useTest ? testVer : String(r.latestVersion || '')),
|
||
category: r.category || '',
|
||
status: r.status,
|
||
channel: useTest ? 'test' : 'prod',
|
||
};
|
||
}),
|
||
};
|
||
}
|
||
|
||
/** GET /api/platforms/allowed — 按账号返回可登录平台白名单 */
|
||
@Get('/platforms/allowed')
|
||
async platformsAllowed(
|
||
@Query('account') account: string = '',
|
||
@Headers('X-API-Token') xApiToken: string = '',
|
||
@Headers('Authorization') authorization: string = ''
|
||
) {
|
||
const acct = await this.resolveAcct(account, xApiToken, authorization);
|
||
if (await this.centerAuthService.accountDisabled(acct)) {
|
||
return { account: acct, allowed: [] };
|
||
}
|
||
const rows = await this.accountPlatformRepo
|
||
.createQueryBuilder('ap')
|
||
.select('ap.platformId', 'platformId')
|
||
.where('ap.account = :account', { account: acct })
|
||
.orderBy('ap.platformId', 'ASC')
|
||
.getRawMany();
|
||
return { account: acct, allowed: rows.map(r => r.platformId) };
|
||
}
|
||
}
|