mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
chore:
This commit is contained in:
@@ -30,6 +30,12 @@ export class SysPublicSettings extends BaseSettings {
|
||||
mpsNo?: string;
|
||||
robots?: boolean = true;
|
||||
aiChatEnabled = true;
|
||||
|
||||
|
||||
//验证码是否开启
|
||||
captchaEnabled = false;
|
||||
//验证码类型
|
||||
captchaType?: string;
|
||||
}
|
||||
|
||||
export class SysPrivateSettings extends BaseSettings {
|
||||
@@ -44,6 +50,9 @@ export class SysPrivateSettings extends BaseSettings {
|
||||
dnsResultOrder? = '';
|
||||
commonCnameEnabled?: boolean = true;
|
||||
|
||||
//验证码配置id
|
||||
captchaAddonId?: number;
|
||||
|
||||
sms?: {
|
||||
type?: string;
|
||||
config?: any;
|
||||
@@ -207,4 +216,3 @@ export class SysSafeSetting extends BaseSettings {
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
96
packages/libs/lib-server/src/user/addon/api/api.ts
Normal file
96
packages/libs/lib-server/src/user/addon/api/api.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { HttpClient, ILogger, utils } from "@certd/basic";
|
||||
import {upperFirst} from "lodash-es";
|
||||
import { FormItemProps, PluginRequestHandleReq, Registrable } from "@certd/pipeline";
|
||||
|
||||
|
||||
export type AddonRequestHandleReqInput<T = any> = {
|
||||
id?: number;
|
||||
title?: string;
|
||||
addon: T;
|
||||
};
|
||||
|
||||
export type AddonRequestHandleReq<T = any> = {
|
||||
addonType: string;
|
||||
} &PluginRequestHandleReq<AddonRequestHandleReqInput<T>>;
|
||||
|
||||
export type AddonInputDefine = FormItemProps & {
|
||||
title: string;
|
||||
required?: boolean;
|
||||
};
|
||||
export type AddonDefine = Registrable & {
|
||||
addonType: string;
|
||||
needPlus?: boolean;
|
||||
input?: {
|
||||
[key: string]: AddonInputDefine;
|
||||
};
|
||||
};
|
||||
|
||||
export type AddonInstanceConfig = {
|
||||
id: number;
|
||||
addonType: string;
|
||||
type: string;
|
||||
name: string;
|
||||
userId: number;
|
||||
setting: {
|
||||
[key: string]: any;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
|
||||
export interface IAddon {
|
||||
ctx: AddonContext;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export type AddonContext = {
|
||||
http: HttpClient;
|
||||
logger: ILogger;
|
||||
utils: typeof utils;
|
||||
};
|
||||
|
||||
export abstract class BaseAddon implements IAddon {
|
||||
define!: AddonDefine;
|
||||
ctx!: AddonContext;
|
||||
http!: HttpClient;
|
||||
logger!: ILogger;
|
||||
|
||||
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||
async onInstance() {}
|
||||
setCtx(ctx: AddonContext) {
|
||||
this.ctx = ctx;
|
||||
this.http = ctx.http;
|
||||
this.logger = ctx.logger;
|
||||
}
|
||||
setDefine = (define:AddonDefine) => {
|
||||
this.define = define;
|
||||
};
|
||||
|
||||
async onRequest(req:AddonRequestHandleReq) {
|
||||
if (!req.action) {
|
||||
throw new Error("action is required");
|
||||
}
|
||||
|
||||
let methodName = req.action;
|
||||
if (!req.action.startsWith("on")) {
|
||||
methodName = `on${upperFirst(req.action)}`;
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const method = this[methodName];
|
||||
if (method) {
|
||||
// @ts-ignore
|
||||
return await this[methodName](req.data);
|
||||
}
|
||||
throw new Error(`action ${req.action} not found`);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
export interface IAddonGetter {
|
||||
getById<T = any>(id: any): Promise<T>;
|
||||
getCommonById<T = any>(id: any): Promise<T>;
|
||||
}
|
||||
65
packages/libs/lib-server/src/user/addon/api/decorator.ts
Normal file
65
packages/libs/lib-server/src/user/addon/api/decorator.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
// src/decorator/memoryCache.decorator.ts
|
||||
import * as _ from "lodash-es";
|
||||
import { merge } from "lodash-es";
|
||||
import { addonRegistry } from "./registry.js";
|
||||
import { AddonContext, AddonDefine, AddonInputDefine } from "./api.js";
|
||||
import { Decorator } from "@certd/pipeline";
|
||||
|
||||
// 提供一个唯一 key
|
||||
export const ADDON_CLASS_KEY = "pipeline:addon";
|
||||
export const ADDON_INPUT_KEY = "pipeline:addon:input";
|
||||
|
||||
export function IsAddon(define: AddonDefine): ClassDecorator {
|
||||
return (target: any) => {
|
||||
target = Decorator.target(target);
|
||||
|
||||
const inputs: any = {};
|
||||
const properties = Decorator.getClassProperties(target);
|
||||
for (const property in properties) {
|
||||
const input = Reflect.getMetadata(ADDON_INPUT_KEY, target, property);
|
||||
if (input) {
|
||||
inputs[property] = input;
|
||||
}
|
||||
}
|
||||
_.merge(define, { input: inputs });
|
||||
Reflect.defineMetadata(ADDON_CLASS_KEY, define, target);
|
||||
target.define = define;
|
||||
const key = `${define.addonType}:${define.name}`;
|
||||
addonRegistry.register(key, {
|
||||
define,
|
||||
target: async () => {
|
||||
return target;
|
||||
},
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
export function AddonInput(input?: AddonInputDefine): PropertyDecorator {
|
||||
return (target, propertyKey) => {
|
||||
target = Decorator.target(target, propertyKey);
|
||||
// const _type = Reflect.getMetadata("design:type", target, propertyKey);
|
||||
Reflect.defineMetadata(ADDON_INPUT_KEY, input, target, propertyKey);
|
||||
};
|
||||
}
|
||||
|
||||
export async function newAddon(addonType:string,type: string, input: any, ctx: AddonContext) {
|
||||
const key = `${addonType}:${type}`
|
||||
const register = addonRegistry.get(key);
|
||||
if (register == null) {
|
||||
throw new Error(`${addonType} ${type} not found`);
|
||||
}
|
||||
// @ts-ignore
|
||||
const pluginCls = await register.target();
|
||||
// @ts-ignore
|
||||
const plugin = new pluginCls();
|
||||
merge(plugin, input);
|
||||
if (!ctx) {
|
||||
throw new Error("ctx is required");
|
||||
}
|
||||
plugin.setDefine(register.define);
|
||||
plugin.setCtx(ctx);
|
||||
await plugin.onInstance();
|
||||
return plugin;
|
||||
}
|
||||
|
||||
|
||||
3
packages/libs/lib-server/src/user/addon/api/index.ts
Normal file
3
packages/libs/lib-server/src/user/addon/api/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./api.js";
|
||||
export * from "./registry.js";
|
||||
export * from "./decorator.js";
|
||||
3
packages/libs/lib-server/src/user/addon/api/registry.ts
Normal file
3
packages/libs/lib-server/src/user/addon/api/registry.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { createRegistry } from "@certd/pipeline";
|
||||
|
||||
export const addonRegistry = createRegistry("addon");
|
||||
44
packages/libs/lib-server/src/user/addon/entity/addon.ts
Normal file
44
packages/libs/lib-server/src/user/addon/entity/addon.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
/**
|
||||
*/
|
||||
@Entity('cd_addon')
|
||||
export class AddonEntity {
|
||||
@PrimaryGeneratedColumn()
|
||||
id: number;
|
||||
@Column({ name: 'user_id', comment: '用户id' })
|
||||
userId: number;
|
||||
@Column({ comment: '名称', length: 100 })
|
||||
name: string;
|
||||
|
||||
|
||||
@Column({ comment: 'addon类型', length: 100 })
|
||||
addonType: string;
|
||||
|
||||
|
||||
@Column({ comment: '类型', length: 100 })
|
||||
type: string;
|
||||
|
||||
@Column({ name: 'setting', comment: '设置', length: 10240, nullable: true })
|
||||
setting: string;
|
||||
|
||||
@Column({ name: 'is_system', comment: '是否系统级别', nullable: false, default: false })
|
||||
isSystem: boolean;
|
||||
|
||||
@Column({ name: 'is_default', comment: '是否默认', nullable: false, default: false })
|
||||
isDefault: boolean;
|
||||
|
||||
|
||||
@Column({
|
||||
name: 'create_time',
|
||||
comment: '创建时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
createTime: Date;
|
||||
@Column({
|
||||
name: 'update_time',
|
||||
comment: '修改时间',
|
||||
default: () => 'CURRENT_TIMESTAMP',
|
||||
})
|
||||
updateTime: Date;
|
||||
}
|
||||
5
packages/libs/lib-server/src/user/addon/index.ts
Normal file
5
packages/libs/lib-server/src/user/addon/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './api/index.js'
|
||||
export * from './entity/addon.js'
|
||||
export * from './service/addon-service.js'
|
||||
export * from './service/addon-getter.js'
|
||||
export * from './service/addon-sys-getter.js'
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IAddonGetter } from "../api/index.js";
|
||||
|
||||
export class AddonGetter implements IAddonGetter {
|
||||
userId: number;
|
||||
getter: <T>(id: any, userId?: number) => Promise<T>;
|
||||
constructor(userId: number, getter: (id: any, userId: number) => Promise<any>) {
|
||||
this.userId = userId;
|
||||
this.getter = getter;
|
||||
}
|
||||
|
||||
async getById<T = any>(id: any) {
|
||||
return await this.getter<T>(id, this.userId);
|
||||
}
|
||||
|
||||
async getCommonById<T = any>(id: any) {
|
||||
return await this.getter<T>(id, 0);
|
||||
}
|
||||
}
|
||||
217
packages/libs/lib-server/src/user/addon/service/addon-service.ts
Normal file
217
packages/libs/lib-server/src/user/addon/service/addon-service.ts
Normal file
@@ -0,0 +1,217 @@
|
||||
import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
|
||||
import { InjectEntityModel } from "@midwayjs/typeorm";
|
||||
import { In, Repository } from "typeorm";
|
||||
import { AddonDefine, BaseService, PageReq, PermissionException, ValidateException } from "../../../index.js";
|
||||
import { addonRegistry, newAddon } from "../api/index.js";
|
||||
import { AddonEntity } from "../entity/addon.js";
|
||||
import { http, logger, utils } from "@certd/basic";
|
||||
|
||||
/**
|
||||
* Addon
|
||||
*/
|
||||
@Provide()
|
||||
@Scope(ScopeEnum.Request, {allowDowngrade: true})
|
||||
export class AddonService extends BaseService<AddonEntity> {
|
||||
@InjectEntityModel(AddonEntity)
|
||||
repository: Repository<AddonEntity>;
|
||||
|
||||
//@ts-ignore
|
||||
getRepository() {
|
||||
return this.repository;
|
||||
}
|
||||
|
||||
async page(pageReq: PageReq<AddonEntity>) {
|
||||
const res = await super.page(pageReq);
|
||||
res.records = res.records.map(item => {
|
||||
return item;
|
||||
});
|
||||
return res;
|
||||
}
|
||||
|
||||
async add(param) {
|
||||
let oldEntity = null;
|
||||
if (param._copyFrom){
|
||||
oldEntity = await this.info(param._copyFrom);
|
||||
if (oldEntity == null) {
|
||||
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
|
||||
}
|
||||
if (oldEntity.userId !== param.userId) {
|
||||
throw new ValidateException('您无权查看该Addon配置');
|
||||
}
|
||||
}
|
||||
delete param._copyFrom
|
||||
return await super.add(param);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改
|
||||
* @param param 数据
|
||||
*/
|
||||
async update(param) {
|
||||
const oldEntity = await this.info(param.id);
|
||||
if (oldEntity == null) {
|
||||
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
|
||||
}
|
||||
return await super.update(param);
|
||||
}
|
||||
|
||||
async getSimpleInfo(id: number) {
|
||||
const entity = await this.info(id);
|
||||
if (entity == null) {
|
||||
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
|
||||
}
|
||||
return {
|
||||
id: entity.id,
|
||||
name: entity.name,
|
||||
userId: entity.userId,
|
||||
};
|
||||
}
|
||||
|
||||
async getAddonById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
|
||||
const entity = await this.info(id);
|
||||
if (entity == null) {
|
||||
throw new Error(`该Addon配置不存在,请确认是否已被删除:id=${id}`);
|
||||
}
|
||||
if (checkUserId) {
|
||||
if (userId == null) {
|
||||
throw new ValidateException('userId不能为空');
|
||||
}
|
||||
if (userId !== entity.userId) {
|
||||
throw new PermissionException('您对该Addon无访问权限');
|
||||
}
|
||||
}
|
||||
|
||||
// const access = accessRegistry.get(entity.type);
|
||||
const setting = JSON.parse(entity.setting ??"{}")
|
||||
const input = {
|
||||
id: entity.id,
|
||||
...setting,
|
||||
};
|
||||
const ctx = {
|
||||
http: http,
|
||||
logger: logger,
|
||||
utils: utils,
|
||||
};
|
||||
return await newAddon(entity.addonType, entity.type, input,ctx);
|
||||
}
|
||||
|
||||
async getById(id: any, userId: number): Promise<any> {
|
||||
return await this.getAddonById(id, true, userId);
|
||||
}
|
||||
|
||||
|
||||
getDefineList(addonType: string) {
|
||||
return addonRegistry.getDefineList();
|
||||
}
|
||||
|
||||
getDefineByType(type: string,prefix?: string) {
|
||||
return addonRegistry.getDefine(type,prefix) as AddonDefine;
|
||||
}
|
||||
|
||||
|
||||
async getSimpleByIds(ids: number[], userId: any) {
|
||||
if (ids.length === 0) {
|
||||
return [];
|
||||
}
|
||||
if (!userId) {
|
||||
return [];
|
||||
}
|
||||
return await this.repository.find({
|
||||
where: {
|
||||
id: In(ids),
|
||||
userId,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
addonType: true,
|
||||
type: true,
|
||||
userId:true,
|
||||
isSystem: true,
|
||||
},
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
async getDefault(userId: number,addonType: string): Promise<any> {
|
||||
const res = await this.repository.findOne({
|
||||
where: {
|
||||
userId,
|
||||
addonType
|
||||
},
|
||||
order: {
|
||||
isDefault: 'DESC',
|
||||
},
|
||||
});
|
||||
if (!res) {
|
||||
return null;
|
||||
}
|
||||
return this.buildAddonInstanceConfig(res);
|
||||
}
|
||||
|
||||
private buildAddonInstanceConfig(res: AddonEntity) {
|
||||
const setting = JSON.parse(res.setting);
|
||||
return {
|
||||
id: res.id,
|
||||
addonType: res.addonType,
|
||||
type: res.type,
|
||||
name: res.name,
|
||||
userId: res.userId,
|
||||
setting,
|
||||
};
|
||||
}
|
||||
|
||||
async setDefault(id: number, userId: number,addonType:string) {
|
||||
if (!id) {
|
||||
throw new ValidateException('id不能为空');
|
||||
}
|
||||
if (!userId) {
|
||||
throw new ValidateException('userId不能为空');
|
||||
}
|
||||
await this.repository.update(
|
||||
{
|
||||
userId,
|
||||
addonType
|
||||
},
|
||||
{
|
||||
isDefault: false,
|
||||
}
|
||||
);
|
||||
await this.repository.update(
|
||||
{
|
||||
id,
|
||||
userId,
|
||||
addonType
|
||||
},
|
||||
{
|
||||
isDefault: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async getOrCreateDefault(opts:{addonType:string,type:string, inputs: any, userId: any}) {
|
||||
const {addonType,type,inputs,userId} = opts;
|
||||
|
||||
const addonDefine = this.getDefineByType( type,addonType)
|
||||
|
||||
const defaultConfig = await this.getDefault(userId);
|
||||
if (defaultConfig) {
|
||||
return defaultConfig;
|
||||
}
|
||||
const setting = {
|
||||
...inputs,
|
||||
};
|
||||
const res = await this.repository.save({
|
||||
userId,
|
||||
addonType,
|
||||
type: type,
|
||||
name: addonDefine.title,
|
||||
setting: JSON.stringify(setting),
|
||||
isDefault: true,
|
||||
});
|
||||
return this.buildAddonInstanceConfig(res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { IAccessService } from '@certd/pipeline';
|
||||
import { AddonService } from './addon-service.js';
|
||||
|
||||
export class AddonSysGetter implements IAccessService {
|
||||
addonService: AddonService;
|
||||
constructor(addonService: AddonService) {
|
||||
this.addonService = addonService;
|
||||
}
|
||||
|
||||
async getById<T = any>(id: any) {
|
||||
return await this.addonService.getById(id, 0);
|
||||
}
|
||||
|
||||
async getCommonById<T = any>(id: any) {
|
||||
return await this.addonService.getById(id, 0);
|
||||
}
|
||||
}
|
||||
@@ -1 +1,2 @@
|
||||
export * from './access/index.js';
|
||||
export * from './addon/index.js';
|
||||
|
||||
Reference in New Issue
Block a user