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

106 lines
3.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { AppKey, PlusRequestService, verify } from '@certd/plus-core';
import { logger } from '@certd/basic';
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
@Provide()
@Scope(ScopeEnum.Singleton)
export class PlusService {
@Inject()
sysSettingsService: SysSettingsService;
@Config('plus.server.baseUrls')
plusServerBaseUrls: string[];
async getPlusRequestService() {
const subjectId = await this.getSubjectId();
return new PlusRequestService({
plusServerBaseUrls: this.plusServerBaseUrls,
subjectId,
});
}
async getSubjectId() {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
return installInfo.siteId;
}
async requestWithoutSign(config: any) {
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,
});
}
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);
}
}
async verify() {
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const plusRequestService = await this.getPlusRequestService();
return await verify({
subjectId: plusRequestService.subjectId,
license: licenseInfo.license,
plusRequestService: plusRequestService,
bindUrl: installInfo?.bindUrl,
});
}
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 res = await plusRequestService.requestWithoutSign({
url: '/activation/subject/register',
data: {
appKey: AppKey,
subjectId: installInfo.siteId,
installTime: installInfo.installTime,
},
});
if (res.license) {
await this.updateLicense(res.license);
logger.info('站点注册成功');
}
}
}
async bindUrl(subjectId: string, url: string) {
const plusRequestService = await this.getPlusRequestService();
return await plusRequestService.request({
url: '/activation/subject/urlBind',
data: {
subjectId,
appKey: AppKey,
url,
},
});
}
}