Files
certd/packages/libs/lib-server/src/user/addon/service/addon-service.ts

201 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-09-28 11:29:57 +08:00
import { Provide, Scope, ScopeEnum } from "@midwayjs/core";
2025-09-11 00:19:38 +08:00
import { InjectEntityModel } from "@midwayjs/typeorm";
import { In, Repository } from "typeorm";
2025-09-28 11:29:57 +08:00
import { AddonDefine, BaseService, PageReq, ValidateException } from "../../../index.js";
import { addonRegistry } from "../api/index.js";
2025-09-11 00:19:38 +08:00
import { AddonEntity } from "../entity/addon.js";
/**
* Addon
*/
@Provide()
2025-09-28 11:29:57 +08:00
@Scope(ScopeEnum.Request, { allowDowngrade: true })
2025-09-11 00:19:38 +08:00
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;
2025-09-28 11:29:57 +08:00
if (param._copyFrom) {
2025-09-11 00:19:38 +08:00
oldEntity = await this.info(param._copyFrom);
if (oldEntity == null) {
2025-09-28 11:29:57 +08:00
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
2025-09-11 00:19:38 +08:00
}
2025-09-28 11:29:57 +08:00
if (oldEntity.userId !== param.userId) {
throw new ValidateException("您无权查看该Addon配置");
2025-09-11 00:19:38 +08:00
}
}
2025-09-28 11:29:57 +08:00
if (!param.userId) {
param.isSystem = true;
} else {
param.isSystem = false;
2025-09-11 23:47:05 +08:00
}
2025-09-28 11:29:57 +08:00
delete param._copyFrom;
2025-09-11 00:19:38 +08:00
return await super.add(param);
}
/**
*
* @param param
*/
async update(param) {
const oldEntity = await this.info(param.id);
if (oldEntity == null) {
2025-09-28 11:29:57 +08:00
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
2025-09-11 00:19:38 +08:00
}
return await super.update(param);
}
async getSimpleInfo(id: number) {
const entity = await this.info(id);
if (entity == null) {
2025-09-28 11:29:57 +08:00
throw new ValidateException("该Addon配置不存在,请确认是否已被删除");
2025-09-11 00:19:38 +08:00
}
return {
id: entity.id,
name: entity.name,
userId: entity.userId,
2025-09-11 23:47:05 +08:00
addonType: entity.addonType,
2025-09-28 11:29:57 +08:00
type: entity.type
2025-09-11 00:19:38 +08:00
};
}
getDefineList(addonType: string) {
2025-11-27 01:59:22 +08:00
return addonRegistry.getDefineList(addonType);
2025-09-11 00:19:38 +08:00
}
2025-09-28 11:29:57 +08:00
getDefineByType(type: string, prefix?: string) {
return addonRegistry.getDefine(type, prefix) as AddonDefine;
2025-09-11 00:19:38 +08:00
}
async getSimpleByIds(ids: number[], userId: any) {
if (ids.length === 0) {
return [];
}
if (!userId) {
return [];
}
return await this.repository.find({
where: {
id: In(ids),
2025-09-28 11:29:57 +08:00
userId
2025-09-11 00:19:38 +08:00
},
select: {
id: true,
name: true,
addonType: true,
type: true,
2025-09-28 11:29:57 +08:00
userId: true,
isSystem: true
}
2025-09-11 00:19:38 +08:00
});
}
2025-09-28 11:29:57 +08:00
async getDefault(userId: number, addonType: string): Promise<any> {
2025-09-11 00:19:38 +08:00
const res = await this.repository.findOne({
where: {
userId,
addonType
},
order: {
2025-09-28 11:29:57 +08:00
isDefault: "DESC"
}
2025-09-11 00:19:38 +08:00
});
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,
2025-09-28 11:29:57 +08:00
setting
2025-09-11 00:19:38 +08:00
};
}
2025-09-28 11:29:57 +08:00
async setDefault(id: number, userId: number, addonType: string) {
2025-09-11 00:19:38 +08:00
if (!id) {
2025-09-28 11:29:57 +08:00
throw new ValidateException("id不能为空");
2025-09-11 00:19:38 +08:00
}
if (!userId) {
2025-09-28 11:29:57 +08:00
throw new ValidateException("userId不能为空");
2025-09-11 00:19:38 +08:00
}
await this.repository.update(
{
userId,
addonType
},
{
2025-09-28 11:29:57 +08:00
isDefault: false
2025-09-11 00:19:38 +08:00
}
);
await this.repository.update(
{
id,
userId,
addonType
},
{
2025-09-28 11:29:57 +08:00
isDefault: true
2025-09-11 00:19:38 +08:00
}
);
}
2025-09-28 11:29:57 +08:00
async getOrCreateDefault(opts: { addonType: string, type: string, inputs: any, userId: any }) {
const { addonType, type, inputs, userId } = opts;
2025-09-11 00:19:38 +08:00
2025-09-28 11:29:57 +08:00
const addonDefine = this.getDefineByType(type, addonType);
2025-09-11 00:19:38 +08:00
2025-09-28 11:29:57 +08:00
const defaultConfig = await this.getDefault(userId, addonType);
2025-09-11 00:19:38 +08:00
if (defaultConfig) {
return defaultConfig;
}
const setting = {
2025-09-28 11:29:57 +08:00
...inputs
2025-09-11 00:19:38 +08:00
};
const res = await this.repository.save({
userId,
addonType,
type: type,
name: addonDefine.title,
setting: JSON.stringify(setting),
2025-09-28 11:29:57 +08:00
isDefault: true
2025-09-11 00:19:38 +08:00
});
return this.buildAddonInstanceConfig(res);
}
2025-11-26 23:25:51 +08:00
async getOneByType(req:{addonType:string,type:string,userId:number}) {
return await this.repository.findOne({
where: {
addonType: req.addonType,
type: req.type,
userId: req.userId
}
});
}
2025-09-11 00:19:38 +08:00
}