This commit is contained in:
xiaojunnuo
2024-11-07 02:05:20 +08:00
parent 17638f3d3a
commit cd83a6f209
2 changed files with 37 additions and 66 deletions

View File

@@ -1,5 +1,5 @@
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { AppKey, PlusRequestService, verify } from '@certd/plus-core';
import { PlusRequestService } from '@certd/plus-core';
import { logger } from '@certd/basic';
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
@@ -9,9 +9,26 @@ export class PlusService {
@Inject()
sysSettingsService: SysSettingsService;
plusRequestService: PlusRequestService;
async getPlusRequestService() {
const subjectId = await this.getSubjectId();
return new PlusRequestService({ subjectId });
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 });
}
async getSubjectId() {
@@ -19,47 +36,19 @@ export class PlusService {
return installInfo.siteId;
}
async requestWithoutSign(config: any) {
async active(code: string) {
const plusRequestService = await this.getPlusRequestService();
return await plusRequestService.requestWithoutSign(config);
}
async request(config: any) {
const plusRequestService = await this.getPlusRequestService();
return await plusRequestService.request(config);
}
async active(formData: { code: any; appKey: string; subjectId: string }) {
const plusRequestService = await this.getPlusRequestService();
return await plusRequestService.requestWithoutSign({
url: '/activation/active',
method: 'post',
data: formData,
});
return await plusRequestService.active(code);
}
async updateLicense(license: string) {
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
if (!licenseInfo) {
licenseInfo = new SysLicenseInfo();
}
licenseInfo.license = license;
await this.sysSettingsService.saveSetting(licenseInfo);
const verifyRes = await this.verify();
if (!verifyRes.isPlus) {
const message = verifyRes.message || '授权码校验失败';
logger.error(message);
throw new Error(message);
}
const plusRequestService = await this.getPlusRequestService();
await plusRequestService.updateLicense({ license });
}
async verify() {
const plusRequestService = await this.getPlusRequestService();
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
return await verify({
subjectId: installInfo.siteId,
license: licenseInfo.license,
bindUrl: installInfo?.bindUrl,
});
await plusRequestService.verify({ license: licenseInfo.license });
}
async bindUrl(url: string) {
@@ -70,16 +59,15 @@ export class PlusService {
async register() {
const plusRequestService = await this.getPlusRequestService();
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
if (!licenseInfo?.license) {
//还没有license注册一个
const license = await plusRequestService.register({
installTime: installInfo.installTime,
});
if (license) {
await this.updateLicense(license);
logger.info('站点注册成功');
}
if (!licenseInfo.license) {
await plusRequestService.register();
logger.info('站点注册成功');
}
}
async getAccessToken() {
const plusRequestService = await this.getPlusRequestService();
await this.register();
return await plusRequestService.getAccessToken();
}
}