Files
certd/packages/libs/lib-server/src/system/basic/service/plus-service.ts
T

74 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-11-07 00:17:35 +08:00
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
2024-11-07 02:05:20 +08:00
import { PlusRequestService } from '@certd/plus-core';
2024-10-09 02:34:28 +08:00
import { logger } from '@certd/basic';
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
2024-11-06 02:20:52 +08:00
2024-08-23 11:35:34 +08:00
@Provide()
@Scope(ScopeEnum.Singleton)
export class PlusService {
@Inject()
sysSettingsService: SysSettingsService;
2024-11-07 02:05:20 +08:00
plusRequestService: PlusRequestService;
async getPlusRequestService() {
2024-11-07 02:05:20 +08:00
if (this.plusRequestService) {
return this.plusRequestService;
}
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const subjectId = installInfo.siteId;
const bindUrl = installInfo.bindUrl;
const installTime = installInfo.installTime;
const saveLicense = async (license: string) => {
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
if (!licenseInfo) {
licenseInfo = new SysLicenseInfo();
}
licenseInfo.license = license;
await this.sysSettingsService.saveSetting(licenseInfo);
};
return new PlusRequestService({ subjectId, bindUrl, installTime, saveLicense });
2024-08-23 11:35:34 +08:00
}
async getSubjectId() {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
return installInfo.siteId;
}
2024-11-07 02:05:20 +08:00
async active(code: string) {
const plusRequestService = await this.getPlusRequestService();
2024-11-07 02:05:20 +08:00
return await plusRequestService.active(code);
2024-08-24 01:05:06 +08:00
}
2024-09-23 14:04:33 +08:00
2024-09-24 11:11:08 +08:00
async updateLicense(license: string) {
2024-11-07 02:05:20 +08:00
const plusRequestService = await this.getPlusRequestService();
await plusRequestService.updateLicense({ license });
2024-09-24 11:11:08 +08:00
}
async verify() {
2024-11-07 02:05:20 +08:00
const plusRequestService = await this.getPlusRequestService();
2024-09-24 11:11:08 +08:00
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
2024-11-07 02:05:20 +08:00
await plusRequestService.verify({ license: licenseInfo.license });
2024-09-23 14:04:33 +08:00
}
2024-09-24 02:42:08 +08:00
2024-11-07 00:17:35 +08:00
async bindUrl(url: string) {
2024-11-06 02:20:52 +08:00
const plusRequestService = await this.getPlusRequestService();
2024-11-07 00:17:35 +08:00
return await plusRequestService.bindUrl(url);
2024-11-06 02:20:52 +08:00
}
async register() {
const plusRequestService = await this.getPlusRequestService();
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
2024-11-07 02:05:20 +08:00
if (!licenseInfo.license) {
await plusRequestService.register();
logger.info('站点注册成功');
}
}
2024-11-07 02:05:20 +08:00
async getAccessToken() {
const plusRequestService = await this.getPlusRequestService();
await this.register();
return await plusRequestService.getAccessToken();
}
2024-08-23 11:35:34 +08:00
}