2024-09-24 11:11:08 +08:00
|
|
|
import { Config, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
2024-10-03 22:03:49 +08:00
|
|
|
import { SysSettingsService } from '@certd/lib-server';
|
|
|
|
|
import { SysInstallInfo, SysLicenseInfo } from '@certd/lib-server';
|
2024-09-24 11:11:08 +08:00
|
|
|
import { AppKey, http, PlusRequestService, verify } from '@certd/pipeline';
|
2024-10-03 22:03:49 +08:00
|
|
|
import { logger } from '@certd/pipeline';
|
2024-08-23 11:35:34 +08:00
|
|
|
|
|
|
|
|
@Provide()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class PlusService {
|
|
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
2024-09-30 00:22:50 +08:00
|
|
|
@Config('plus.server.baseUrls')
|
|
|
|
|
plusServerBaseUrls: string[];
|
2024-08-23 11:35:34 +08:00
|
|
|
|
2024-09-24 11:11:08 +08:00
|
|
|
plusRequestService: PlusRequestService;
|
2024-08-24 01:05:06 +08:00
|
|
|
|
2024-09-24 11:11:08 +08:00
|
|
|
@Init()
|
|
|
|
|
async init() {
|
2024-08-23 11:35:34 +08:00
|
|
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
2024-09-24 11:11:08 +08:00
|
|
|
this.plusRequestService = new PlusRequestService({
|
2024-09-30 00:22:50 +08:00
|
|
|
plusServerBaseUrls: this.plusServerBaseUrls,
|
2024-09-24 11:11:08 +08:00
|
|
|
http: http,
|
|
|
|
|
logger,
|
2024-08-23 11:35:34 +08:00
|
|
|
subjectId: installInfo.siteId,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-24 11:11:08 +08:00
|
|
|
async requestWithoutSign(config: any) {
|
|
|
|
|
return await this.plusRequestService.requestWithoutSign(config);
|
|
|
|
|
}
|
|
|
|
|
async request(config: any) {
|
|
|
|
|
return await this.plusRequestService.request(config);
|
2024-08-23 11:35:34 +08:00
|
|
|
}
|
2024-08-24 01:05:06 +08:00
|
|
|
|
|
|
|
|
async active(formData: { code: any; appKey: string; subjectId: string }) {
|
2024-09-24 11:11:08 +08:00
|
|
|
return await this.plusRequestService.requestWithoutSign({
|
2024-08-24 01:05:06 +08:00
|
|
|
url: '/activation/active',
|
|
|
|
|
method: 'post',
|
|
|
|
|
data: formData,
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-09-23 14:04:33 +08:00
|
|
|
|
2024-09-24 11:11:08 +08:00
|
|
|
async updateLicense(license: string) {
|
2024-09-23 14:04:33 +08:00
|
|
|
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
|
|
|
|
if (!licenseInfo) {
|
|
|
|
|
licenseInfo = new SysLicenseInfo();
|
|
|
|
|
}
|
|
|
|
|
licenseInfo.license = license;
|
|
|
|
|
await this.sysSettingsService.saveSetting(licenseInfo);
|
2024-09-25 09:46:13 +08:00
|
|
|
const verifyRes = await this.verify();
|
|
|
|
|
if (!verifyRes.isPlus) {
|
|
|
|
|
const message = verifyRes.message || '授权码校验失败';
|
|
|
|
|
logger.error(message);
|
|
|
|
|
throw new Error(message);
|
|
|
|
|
}
|
2024-09-24 11:11:08 +08:00
|
|
|
}
|
|
|
|
|
async verify() {
|
|
|
|
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
|
|
|
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
2024-09-23 14:04:33 +08:00
|
|
|
|
2024-09-25 09:46:13 +08:00
|
|
|
return await verify({
|
2024-09-24 11:11:08 +08:00
|
|
|
subjectId: installInfo.siteId,
|
|
|
|
|
license: licenseInfo.license,
|
|
|
|
|
plusRequestService: this.plusRequestService,
|
|
|
|
|
bindUrl: installInfo?.bindUrl,
|
2024-09-23 14:04:33 +08:00
|
|
|
});
|
|
|
|
|
}
|
2024-09-24 02:42:08 +08:00
|
|
|
|
|
|
|
|
async bindUrl(subjectId: string, url: string) {
|
2024-09-24 11:11:08 +08:00
|
|
|
return await this.plusRequestService.request({
|
2024-09-24 02:42:08 +08:00
|
|
|
url: '/activation/subject/urlBind',
|
|
|
|
|
data: {
|
|
|
|
|
subjectId,
|
|
|
|
|
appKey: AppKey,
|
|
|
|
|
url,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-08-23 11:35:34 +08:00
|
|
|
}
|