pull/243/head
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();
}
}

View File

@ -1,7 +1,5 @@
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
import { AppKey } from '@certd/plus-core';
import { logger } from '@certd/basic';
/**
*/
@ -17,23 +15,8 @@ export class SysPlusController extends BaseController {
@Post('/active', { summary: 'sys:settings:edit' })
async active(@Body(ALL) body) {
const { code } = body;
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const siteId = installInfo.siteId;
const formData = {
appKey: AppKey,
code,
subjectId: siteId,
};
const res: any = await this.plusService.active(formData);
if (res.code > 0) {
logger.error('激活失败', res.message);
return this.fail(res.message, 1);
}
const license = res.data.license;
await this.plusService.updateLicense(license);
await this.plusService.active(code);
return this.ok(true);
}
@ -42,7 +25,7 @@ export class SysPlusController extends BaseController {
const { url } = body;
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
await this.plusService.bindUrl(installInfo.siteId, url);
await this.plusService.bindUrl(url);
installInfo.bindUrl = url;
await this.sysSettingsService.saveSetting(installInfo);