mirror of https://github.com/certd/certd
chore:
parent
17638f3d3a
commit
cd83a6f209
|
@ -1,5 +1,5 @@
|
||||||
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
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 { logger } from '@certd/basic';
|
||||||
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
|
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
|
||||||
|
|
||||||
|
@ -9,9 +9,26 @@ export class PlusService {
|
||||||
@Inject()
|
@Inject()
|
||||||
sysSettingsService: SysSettingsService;
|
sysSettingsService: SysSettingsService;
|
||||||
|
|
||||||
|
plusRequestService: PlusRequestService;
|
||||||
|
|
||||||
async getPlusRequestService() {
|
async getPlusRequestService() {
|
||||||
const subjectId = await this.getSubjectId();
|
if (this.plusRequestService) {
|
||||||
return new PlusRequestService({ subjectId });
|
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() {
|
async getSubjectId() {
|
||||||
|
@ -19,47 +36,19 @@ export class PlusService {
|
||||||
return installInfo.siteId;
|
return installInfo.siteId;
|
||||||
}
|
}
|
||||||
|
|
||||||
async requestWithoutSign(config: any) {
|
async active(code: string) {
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
return await plusRequestService.requestWithoutSign(config);
|
return await plusRequestService.active(code);
|
||||||
}
|
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateLicense(license: string) {
|
async updateLicense(license: string) {
|
||||||
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
if (!licenseInfo) {
|
await plusRequestService.updateLicense({ license });
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
async verify() {
|
async verify() {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
await plusRequestService.verify({ license: licenseInfo.license });
|
||||||
|
|
||||||
return await verify({
|
|
||||||
subjectId: installInfo.siteId,
|
|
||||||
license: licenseInfo.license,
|
|
||||||
bindUrl: installInfo?.bindUrl,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async bindUrl(url: string) {
|
async bindUrl(url: string) {
|
||||||
|
@ -70,16 +59,15 @@ export class PlusService {
|
||||||
async register() {
|
async register() {
|
||||||
const plusRequestService = await this.getPlusRequestService();
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
if (!licenseInfo.license) {
|
||||||
if (!licenseInfo?.license) {
|
await plusRequestService.register();
|
||||||
//还没有license,注册一个
|
logger.info('站点注册成功');
|
||||||
const license = await plusRequestService.register({
|
|
||||||
installTime: installInfo.installTime,
|
|
||||||
});
|
|
||||||
if (license) {
|
|
||||||
await this.updateLicense(license);
|
|
||||||
logger.info('站点注册成功');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getAccessToken() {
|
||||||
|
const plusRequestService = await this.getPlusRequestService();
|
||||||
|
await this.register();
|
||||||
|
return await plusRequestService.getAccessToken();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core';
|
||||||
import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server';
|
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' })
|
@Post('/active', { summary: 'sys:settings:edit' })
|
||||||
async active(@Body(ALL) body) {
|
async active(@Body(ALL) body) {
|
||||||
const { code } = 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);
|
await this.plusService.active(code);
|
||||||
|
|
||||||
if (res.code > 0) {
|
|
||||||
logger.error('激活失败', res.message);
|
|
||||||
return this.fail(res.message, 1);
|
|
||||||
}
|
|
||||||
const license = res.data.license;
|
|
||||||
|
|
||||||
await this.plusService.updateLicense(license);
|
|
||||||
|
|
||||||
return this.ok(true);
|
return this.ok(true);
|
||||||
}
|
}
|
||||||
|
@ -42,7 +25,7 @@ export class SysPlusController extends BaseController {
|
||||||
const { url } = body;
|
const { url } = body;
|
||||||
|
|
||||||
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
||||||
await this.plusService.bindUrl(installInfo.siteId, url);
|
await this.plusService.bindUrl(url);
|
||||||
|
|
||||||
installInfo.bindUrl = url;
|
installInfo.bindUrl = url;
|
||||||
await this.sysSettingsService.saveSetting(installInfo);
|
await this.sysSettingsService.saveSetting(installInfo);
|
||||||
|
|
Loading…
Reference in New Issue