ExtensionBillingModuleExtensionBillingServiceuser.power。packages/@buildingai/extension-sdk/src/modules/billing/
├── extension-billing.module.ts
└── extension-billing.service.tsBaseBillingServiceUserAccountLogaccount_loguser.power,会丢失:ExtensionBillingModuleimport { ExtensionBillingModule } from "@buildingai/extension-sdk";
@Module({
imports: [ExtensionBillingModule],
providers: [ArticleService],
})
export class ArticleModule {}@Global(),但仍建议由插件某个根模块显式导入一次,避免运行时 provider 不可见。@Injectable()
export class ArticleService {
constructor(
private readonly extensionBillingService: ExtensionBillingService,
) {}
}ExtensionBillingService 提供的方法hasSufficientPower(userId, requiredAmount)const ok = await this.extensionBillingService.hasSufficientPower(userId, 5);
if (!ok) {
throw HttpErrorFactory.badRequest("积分不足");
}deductUserPower(options, entityManager?)addUserPower(options, entityManager?)@Injectable()
export class ArticleService {
constructor(
private readonly extensionBillingService: ExtensionBillingService,
) {}
async polish(userId: string, taskNo: string) {
await this.extensionBillingService.deductUserPower({
userId,
amount: 5,
remark: "文章润色消耗",
associationNo: taskNo,
});
}
}sourceaccountTypeExtensionBillingService 自动补。await this.extensionBillingService.deductUserPower({
userId,
amount: 5,
remark: "文章生成消耗",
associationNo: taskNo,
});accountType: ACCOUNT_LOG_TYPE.PLUGIN_DECsource.type: ACCOUNT_LOG_SOURCE.PLUGINsource.source: 当前插件名称await this.extensionBillingService.addUserPower({
userId,
amount: 10,
remark: "任务失败返还积分",
associationNo: taskNo,
});deductUserPower() 和 addUserPower()ACCOUNT_LOG_TYPE.PLUGIN_DECAccountLog.action:ACTION.DECACTION.INCaccountType 判断增减,要结合:accountTypeactionExtensionBillingService 会通过调用栈识别当前插件标识符,再去扩展配置里取插件名称。Extension not foundconst ok = await this.extensionBillingService.hasSufficientPower(userId, 5);
if (!ok) {
throw HttpErrorFactory.badRequest("积分不足,请充值后再试");
}await this.extensionBillingService.deductUserPower({
userId,
amount: 5,
remark: "文章润色消耗",
associationNo: taskNo,
});await this.extensionBillingService.addUserPower({
userId,
amount: 5,
remark: "文章润色失败返还",
associationNo: taskNo,
});await this.extensionBillingService.deductUserPower({
userId,
amount: 2,
remark: "文档解析消耗",
associationNo: taskNo,
});
await this.extensionBillingService.deductUserPower({
userId,
amount: 3,
remark: "摘要生成消耗",
associationNo: taskNo,
});entityManager 传进去。await this.articleRepository.manager.transaction(async (entityManager) => {
await this.extensionBillingService.deductUserPower(
{
userId,
amount: 5,
remark: "文章生成消耗",
associationNo: taskNo,
},
entityManager,
);
await entityManager.insert(ArticleTask, {
userId,
taskNo,
status: "done",
});
});associationNo 建议用任务号、订单号、工作流号remark 要能让运营一眼看懂ExtensionBillingService 自己改余额