mirror of
https://github.com/certd/certd.git
synced 2025-11-25 09:10:11 +08:00
chore:
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user