Merge branch 'v2-dev' into v2-dev-buy

This commit is contained in:
xiaojunnuo
2025-11-04 23:04:11 +08:00
225 changed files with 5865 additions and 1654 deletions
@@ -47,4 +47,12 @@ export abstract class BaseController {
}
return user;
}
isAdmin() {
const roleIds: number[] = this.ctx?.user?.roles;
if (roleIds?.includes(1)) {
return true;
}
}
}
@@ -8,3 +8,5 @@ export * from './common-exception.js';
export * from './not-found-exception.js';
export * from './param-exception.js';
export * from './site-off-exception.js';
export * from './login-error-exception.js'
export * from './code-error-exception.js'
@@ -3,7 +3,7 @@ import { AppKey, PlusRequestService } from '@certd/plus-core';
import { cache, http, HttpRequestConfig, logger } from '@certd/basic';
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
import { merge } from 'lodash-es';
import fs from 'fs';
@Provide("plusService")
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class PlusService {
@@ -85,12 +85,31 @@ export class PlusService {
async sendEmail(email: any) {
const plusRequestService = await this.getPlusRequestService();
let attachments = email.attachments || [];
if (attachments.length > 0) {
const newAttachments: any[] = [];
attachments.forEach((item: any) => {
const name = item.filename || item.path.split('/').pop();
const body = item.content || fs.readFileSync(item.path);
const bodyBase64 = Buffer.from(body).toString('base64');
item = {
name,
body: bodyBase64,
};
newAttachments.push(item);
});
attachments = newAttachments;
}
await plusRequestService.request({
url: '/activation/emailSend',
data: {
subject: email.subject,
text: email.content,
to: email.receivers,
text: email.content,
html: email.html,
attachments,
},
});
}
@@ -37,6 +37,15 @@ export class SysPublicSettings extends BaseSettings {
//验证码类型
captchaType?: string;
captchaAddonId?:number;
//流水线是否启用有效期
pipelineValidTimeEnabled?: boolean = false;
//证书域名添加到监控
certDomainAddToMonitorEnabled?: boolean = false;
}
export class SysPrivateSettings extends BaseSettings {
@@ -51,6 +60,8 @@ export class SysPrivateSettings extends BaseSettings {
dnsResultOrder? = '';
commonCnameEnabled?: boolean = true;
httpRequestTimeout?: number = 30;
sms?: {
type?: string;
config?: any;
@@ -214,3 +225,4 @@ export class SysSafeSetting extends BaseSettings {
};
}
@@ -1,6 +1,13 @@
import { HttpClient, ILogger, utils } from "@certd/basic";
import {upperFirst} from "lodash-es";
import { FormItemProps, PluginRequestHandleReq, Registrable } from "@certd/pipeline";
import {
accessRegistry,
FormItemProps,
IAccessService,
IServiceGetter,
PluginRequestHandleReq,
Registrable
} from "@certd/pipeline";
export type AddonRequestHandleReqInput<T = any> = {
@@ -48,6 +55,7 @@ export type AddonContext = {
http: HttpClient;
logger: ILogger;
utils: typeof utils;
serviceGetter: IServiceGetter;
};
export abstract class BaseAddon implements IAddon {
@@ -58,8 +66,45 @@ export abstract class BaseAddon implements IAddon {
// eslint-disable-next-line @typescript-eslint/no-empty-function
async onInstance() {}
async getAccess<T = any>(accessId: string | number, isCommon = false) {
if (accessId == null) {
throw new Error("您还没有配置授权");
}
const accessService = await this.ctx.serviceGetter.get<IAccessService>("accessService")
let res: any = null;
if (isCommon) {
res = await accessService.getCommonById(accessId);
} else {
res = await accessService.getById(accessId);
}
if (res == null) {
throw new Error("授权不存在,可能已被删除,请前往任务配置里面重新选择授权");
}
// @ts-ignore
if (this.logger?.addSecret) {
// 隐藏加密信息,不在日志中输出
const type = res._type;
const plugin = accessRegistry.get(type);
const define = plugin.define;
// @ts-ignore
const input = define.input;
for (const key in input) {
if (input[key].encrypt && res[key] != null) {
// @ts-ignore
this.logger.addSecret(res[key]);
}
}
}
return res as T;
}
setCtx(ctx: AddonContext) {
this.ctx = ctx;
this.http = ctx.http;
@@ -1,5 +1,3 @@
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'
@@ -1,18 +0,0 @@
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);
}
}
@@ -1,16 +1,15 @@
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 { AddonDefine, BaseService, PageReq, ValidateException } from "../../../index.js";
import { addonRegistry } from "../api/index.js";
import { AddonEntity } from "../entity/addon.js";
import { http, logger, utils } from "@certd/basic";
/**
* Addon
*/
@Provide()
@Scope(ScopeEnum.Request, {allowDowngrade: true})
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class AddonService extends BaseService<AddonEntity> {
@InjectEntityModel(AddonEntity)
repository: Repository<AddonEntity>;
@@ -30,21 +29,21 @@ export class AddonService extends BaseService<AddonEntity> {
async add(param) {
let oldEntity = null;
if (param._copyFrom){
if (param._copyFrom) {
oldEntity = await this.info(param._copyFrom);
if (oldEntity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
}
if (oldEntity.userId !== param.userId) {
throw new ValidateException('您无权查看该Addon配置');
if (oldEntity.userId !== param.userId) {
throw new ValidateException("您无权查看该Addon配置");
}
}
if (!param.userId){
param.isSystem = true
}else{
param.isSystem = false
if (!param.userId) {
param.isSystem = true;
} else {
param.isSystem = false;
}
delete param._copyFrom
delete param._copyFrom;
return await super.add(param);
}
@@ -56,7 +55,7 @@ export class AddonService extends BaseService<AddonEntity> {
async update(param) {
const oldEntity = await this.info(param.id);
if (oldEntity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
}
return await super.update(param);
}
@@ -64,63 +63,24 @@ export class AddonService extends BaseService<AddonEntity> {
async getSimpleInfo(id: number) {
const entity = await this.info(id);
if (entity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除');
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
}
return {
id: entity.id,
name: entity.name,
userId: entity.userId,
addonType: entity.addonType,
type: entity.type,
type: entity.type
};
}
async getAddonById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
const ctx = {
http: http,
logger: logger,
utils: utils,
};
if (!id){
//使用图片验证码
return await newAddon("captcha", "image", {},ctx);
}
const entity = await this.info(id);
if (entity == null) {
//使用图片验证码
return await newAddon("captcha", "image", {},ctx);
}
if (checkUserId) {
if (userId == null) {
throw new ValidateException('userId不能为空');
}
if (userId !== entity.userId) {
throw new PermissionException('您对该Addon无访问权限');
}
}
const setting = JSON.parse(entity.setting ??"{}")
const input = {
id: entity.id,
...setting,
};
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;
getDefineByType(type: string, prefix?: string) {
return addonRegistry.getDefine(type, prefix) as AddonDefine;
}
@@ -134,31 +94,30 @@ export class AddonService extends BaseService<AddonEntity> {
return await this.repository.find({
where: {
id: In(ids),
userId,
userId
},
select: {
id: true,
name: true,
addonType: true,
type: true,
userId:true,
isSystem: true,
},
userId: true,
isSystem: true
}
});
}
async getDefault(userId: number,addonType: string): Promise<any> {
async getDefault(userId: number, addonType: string): Promise<any> {
const res = await this.repository.findOne({
where: {
userId,
addonType
},
order: {
isDefault: 'DESC',
},
isDefault: "DESC"
}
});
if (!res) {
return null;
@@ -174,16 +133,16 @@ export class AddonService extends BaseService<AddonEntity> {
type: res.type,
name: res.name,
userId: res.userId,
setting,
setting
};
}
async setDefault(id: number, userId: number,addonType:string) {
async setDefault(id: number, userId: number, addonType: string) {
if (!id) {
throw new ValidateException('id不能为空');
throw new ValidateException("id不能为空");
}
if (!userId) {
throw new ValidateException('userId不能为空');
throw new ValidateException("userId不能为空");
}
await this.repository.update(
{
@@ -191,7 +150,7 @@ export class AddonService extends BaseService<AddonEntity> {
addonType
},
{
isDefault: false,
isDefault: false
}
);
await this.repository.update(
@@ -201,22 +160,22 @@ export class AddonService extends BaseService<AddonEntity> {
addonType
},
{
isDefault: true,
isDefault: true
}
);
}
async getOrCreateDefault(opts:{addonType:string,type:string, inputs: any, userId: any}) {
const {addonType,type,inputs,userId} = opts;
async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any }) {
const { addonType, type, inputs, userId } = opts;
const addonDefine = this.getDefineByType( type,addonType)
const addonDefine = this.getDefineByType(type, addonType);
const defaultConfig = await this.getDefault(userId,addonType);
const defaultConfig = await this.getDefault(userId, addonType);
if (defaultConfig) {
return defaultConfig;
}
const setting = {
...inputs,
...inputs
};
const res = await this.repository.save({
userId,
@@ -224,7 +183,7 @@ export class AddonService extends BaseService<AddonEntity> {
type: type,
name: addonDefine.title,
setting: JSON.stringify(setting),
isDefault: true,
isDefault: true
});
return this.buildAddonInstanceConfig(res);
}
@@ -1,17 +0,0 @@
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);
}
}