v2
xiaojunnuo 2025-09-28 11:29:57 +08:00
parent 8f6e5bd24b
commit 9c854f727f
7 changed files with 108 additions and 90 deletions

View File

@ -3,7 +3,7 @@ on:
push: push:
branches: ['v2-dev'] branches: ['v2-dev']
paths: paths:
- "trigger/build.trigger" - "trigger/build1.trigger"
# schedule: # schedule:
# - # 国际时间 19:17 执行北京时间3:17 ↙↙↙ 改成你想要每天自动执行的时间 # - # 国际时间 19:17 执行北京时间3:17 ↙↙↙ 改成你想要每天自动执行的时间

View File

@ -22,12 +22,14 @@ import { sp } from "./util.sp.js";
import { hashUtils } from "./util.hash.js"; import { hashUtils } from "./util.hash.js";
import { promises } from "./util.promise.js"; import { promises } from "./util.promise.js";
import { fileUtils } from "./util.file.js"; import { fileUtils } from "./util.file.js";
import * as _ from "lodash-es";
import { cache } from "./util.cache.js"; import { cache } from "./util.cache.js";
import dayjs from "dayjs"; import dayjs from "dayjs";
import { domainUtils } from "./util.domain.js"; import { domainUtils } from "./util.domain.js";
export * from "./util.domain.js";
import { optionsUtils } from "./util.options.js"; import { optionsUtils } from "./util.options.js";
export * from "./util.options.js";
import { amountUtils } from "./util.amount.js"; import { amountUtils } from "./util.amount.js";
export * from "./util.amount.js";
import { nanoid } from "nanoid"; import { nanoid } from "nanoid";
import * as id from "./util.id.js"; import * as id from "./util.id.js";
import { locker } from "./util.lock.js"; import { locker } from "./util.lock.js";
@ -43,7 +45,6 @@ export const utils = {
hash: hashUtils, hash: hashUtils,
promises, promises,
file: fileUtils, file: fileUtils,
_,
mergeUtils, mergeUtils,
cache, cache,
nanoid, nanoid,

View File

@ -1,24 +1,19 @@
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core"; import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { InjectEntityModel } from "@midwayjs/typeorm"; import { InjectEntityModel } from "@midwayjs/typeorm";
import { In, Repository } from "typeorm"; import { In, Repository } from "typeorm";
import { AddonDefine, BaseService, PageReq, PermissionException, ValidateException } from "../../../index.js"; import { AddonDefine, BaseService, PageReq, ValidateException } from "../../../index.js";
import { addonRegistry, newAddon } from "../api/index.js"; import { addonRegistry } from "../api/index.js";
import { AddonEntity } from "../entity/addon.js"; import { AddonEntity } from "../entity/addon.js";
import { http, logger, utils } from "@certd/basic";
import { TaskServiceBuilder } from "@certd/ui-server";
/** /**
* Addon * Addon
*/ */
@Provide() @Provide()
@Scope(ScopeEnum.Request, {allowDowngrade: true}) @Scope(ScopeEnum.Request, { allowDowngrade: true })
export class AddonService extends BaseService<AddonEntity> { export class AddonService extends BaseService<AddonEntity> {
@InjectEntityModel(AddonEntity) @InjectEntityModel(AddonEntity)
repository: Repository<AddonEntity>; repository: Repository<AddonEntity>;
@Inject()
private taskServiceBuilder: TaskServiceBuilder;
//@ts-ignore //@ts-ignore
getRepository() { getRepository() {
return this.repository; return this.repository;
@ -34,21 +29,21 @@ export class AddonService extends BaseService<AddonEntity> {
async add(param) { async add(param) {
let oldEntity = null; let oldEntity = null;
if (param._copyFrom){ if (param._copyFrom) {
oldEntity = await this.info(param._copyFrom); oldEntity = await this.info(param._copyFrom);
if (oldEntity == null) { if (oldEntity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除'); throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
} }
if (oldEntity.userId !== param.userId) { if (oldEntity.userId !== param.userId) {
throw new ValidateException('您无权查看该Addon配置'); throw new ValidateException("您无权查看该Addon配置");
} }
} }
if (!param.userId){ if (!param.userId) {
param.isSystem = true param.isSystem = true;
}else{ } else {
param.isSystem = false param.isSystem = false;
} }
delete param._copyFrom delete param._copyFrom;
return await super.add(param); return await super.add(param);
} }
@ -60,7 +55,7 @@ export class AddonService extends BaseService<AddonEntity> {
async update(param) { async update(param) {
const oldEntity = await this.info(param.id); const oldEntity = await this.info(param.id);
if (oldEntity == null) { if (oldEntity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除'); throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
} }
return await super.update(param); return await super.update(param);
} }
@ -68,64 +63,24 @@ export class AddonService extends BaseService<AddonEntity> {
async getSimpleInfo(id: number) { async getSimpleInfo(id: number) {
const entity = await this.info(id); const entity = await this.info(id);
if (entity == null) { if (entity == null) {
throw new ValidateException('该Addon配置不存在,请确认是否已被删除'); throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
} }
return { return {
id: entity.id, id: entity.id,
name: entity.name, name: entity.name,
userId: entity.userId, userId: entity.userId,
addonType: entity.addonType, addonType: entity.addonType,
type: entity.type, type: entity.type
}; };
} }
async getAddonById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
const serviceGetter = this.taskServiceBuilder.create({userId:userId??0})
const ctx = {
http: http,
logger: logger,
utils: utils,
serviceGetter
};
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) { getDefineList(addonType: string) {
return addonRegistry.getDefineList(); return addonRegistry.getDefineList();
} }
getDefineByType(type: string,prefix?: string) { getDefineByType(type: string, prefix?: string) {
return addonRegistry.getDefine(type,prefix) as AddonDefine; return addonRegistry.getDefine(type, prefix) as AddonDefine;
} }
@ -139,31 +94,30 @@ export class AddonService extends BaseService<AddonEntity> {
return await this.repository.find({ return await this.repository.find({
where: { where: {
id: In(ids), id: In(ids),
userId, userId
}, },
select: { select: {
id: true, id: true,
name: true, name: true,
addonType: true, addonType: true,
type: true, type: true,
userId:true, userId: true,
isSystem: 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({ const res = await this.repository.findOne({
where: { where: {
userId, userId,
addonType addonType
}, },
order: { order: {
isDefault: 'DESC', isDefault: "DESC"
}, }
}); });
if (!res) { if (!res) {
return null; return null;
@ -179,16 +133,16 @@ export class AddonService extends BaseService<AddonEntity> {
type: res.type, type: res.type,
name: res.name, name: res.name,
userId: res.userId, userId: res.userId,
setting, setting
}; };
} }
async setDefault(id: number, userId: number,addonType:string) { async setDefault(id: number, userId: number, addonType: string) {
if (!id) { if (!id) {
throw new ValidateException('id不能为空'); throw new ValidateException("id不能为空");
} }
if (!userId) { if (!userId) {
throw new ValidateException('userId不能为空'); throw new ValidateException("userId不能为空");
} }
await this.repository.update( await this.repository.update(
{ {
@ -196,7 +150,7 @@ export class AddonService extends BaseService<AddonEntity> {
addonType addonType
}, },
{ {
isDefault: false, isDefault: false
} }
); );
await this.repository.update( await this.repository.update(
@ -206,22 +160,22 @@ export class AddonService extends BaseService<AddonEntity> {
addonType addonType
}, },
{ {
isDefault: true, isDefault: true
} }
); );
} }
async getOrCreateDefault(opts:{addonType:string,type:string, inputs: any, userId: any}) { async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any }) {
const {addonType,type,inputs,userId} = opts; 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) { if (defaultConfig) {
return defaultConfig; return defaultConfig;
} }
const setting = { const setting = {
...inputs, ...inputs
}; };
const res = await this.repository.save({ const res = await this.repository.save({
userId, userId,
@ -229,7 +183,7 @@ export class AddonService extends BaseService<AddonEntity> {
type: type, type: type,
name: addonDefine.title, name: addonDefine.title,
setting: JSON.stringify(setting), setting: JSON.stringify(setting),
isDefault: true, isDefault: true
}); });
return this.buildAddonInstanceConfig(res); return this.buildAddonInstanceConfig(res);
} }

View File

@ -2,3 +2,4 @@ export * from "./api.js";
export * from "./registry.js"; export * from "./registry.js";
export * from "./decorator.js"; export * from "./decorator.js";
export * from "./base.js"; export * from "./base.js";
export * from "./domain-parser.js";

View File

@ -1,7 +1,8 @@
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core"; import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { AddonService, SysSettingsService } from "@certd/lib-server"; import { SysSettingsService } from "@certd/lib-server";
import { logger } from "@certd/basic"; import { logger } from "@certd/basic";
import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js"; import { ICaptchaAddon } from "../../../plugins/plugin-captcha/api.js";
import { AddonGetterService } from "../../pipeline/service/addon-getter-service.js";
@Provide() @Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true }) @Scope(ScopeEnum.Request, { allowDowngrade: true })
@ -9,7 +10,7 @@ export class CaptchaService {
@Inject() @Inject()
sysSettingsService: SysSettingsService; sysSettingsService: SysSettingsService;
@Inject() @Inject()
addonService: AddonService; addonGetterService: AddonGetterService;
async getCaptcha(captchaAddonId?:number){ async getCaptcha(captchaAddonId?:number){
@ -17,7 +18,7 @@ export class CaptchaService {
const settings = await this.sysSettingsService.getPublicSettings() const settings = await this.sysSettingsService.getPublicSettings()
captchaAddonId = settings.captchaAddonId ?? 0 captchaAddonId = settings.captchaAddonId ?? 0
} }
const addon:ICaptchaAddon = await this.addonService.getAddonById(captchaAddonId,true,0) const addon:ICaptchaAddon = await this.addonGetterService.getAddonById(captchaAddonId,true,0)
if (!addon) { if (!addon) {
throw new Error('验证码插件还未配置') throw new Error('验证码插件还未配置')
} }
@ -30,7 +31,7 @@ export class CaptchaService {
const settings = await this.sysSettingsService.getPublicSettings() const settings = await this.sysSettingsService.getPublicSettings()
opts.captchaAddonId = settings.captchaAddonId ?? 0 opts.captchaAddonId = settings.captchaAddonId ?? 0
} }
const addon = await this.addonService.getById(opts.captchaAddonId,0) const addon = await this.addonGetterService.getById(opts.captchaAddonId,0)
if (!addon) { if (!addon) {
if (opts.must) { if (opts.must) {
throw new Error('请先配置验证码插件'); throw new Error('请先配置验证码插件');

View File

@ -0,0 +1,61 @@
import { Inject, Provide, Scope, ScopeEnum } from "@midwayjs/core";
import { http, logger, utils } from "@certd/basic";
import { TaskServiceBuilder } from "./getter/task-service-getter.js";
import { AddonService, newAddon, PermissionException, ValidateException } from "@certd/lib-server";
/**
* Addon
*/
@Provide()
@Scope(ScopeEnum.Request, { allowDowngrade: true })
export class AddonGetterService {
@Inject()
taskServiceBuilder: TaskServiceBuilder;
@Inject()
addonService: AddonService;
async getAddonById(id: any, checkUserId: boolean, userId?: number): Promise<any> {
const serviceGetter = this.taskServiceBuilder.create({
userId
});
const ctx = {
http,
logger,
utils,
serviceGetter
}
if (!id) {
//使用图片验证码
return await newAddon("captcha", "image", {}, ctx);
}
const entity = await this.addonService.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);
}
}