2024-11-07 00:17:35 +08:00
|
|
|
import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
2024-11-07 02:22:14 +08:00
|
|
|
import { AppKey, PlusRequestService } from '@certd/plus-core';
|
|
|
|
|
import { http, HttpRequestConfig, logger } from '@certd/basic';
|
2024-10-03 23:11:50 +08:00
|
|
|
import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js';
|
2024-11-07 02:22:14 +08:00
|
|
|
import { merge } from 'lodash-es';
|
2024-11-06 02:20:52 +08:00
|
|
|
|
2024-08-23 11:35:34 +08:00
|
|
|
@Provide()
|
|
|
|
|
@Scope(ScopeEnum.Singleton)
|
|
|
|
|
export class PlusService {
|
|
|
|
|
@Inject()
|
|
|
|
|
sysSettingsService: SysSettingsService;
|
|
|
|
|
|
2024-11-07 02:05:20 +08:00
|
|
|
plusRequestService: PlusRequestService;
|
|
|
|
|
|
2024-10-11 03:13:34 +08:00
|
|
|
async getPlusRequestService() {
|
2024-11-07 02:05:20 +08:00
|
|
|
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);
|
|
|
|
|
};
|
2024-11-07 10:05:03 +08:00
|
|
|
|
2024-11-07 02:05:20 +08:00
|
|
|
return new PlusRequestService({ subjectId, bindUrl, installTime, saveLicense });
|
2024-08-23 11:35:34 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-01 18:09:32 +08:00
|
|
|
async getSubjectId() {
|
|
|
|
|
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
|
|
|
|
|
return installInfo.siteId;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 02:05:20 +08:00
|
|
|
async active(code: string) {
|
2024-10-11 03:13:34 +08:00
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
2024-11-07 02:05:20 +08:00
|
|
|
return await plusRequestService.active(code);
|
2024-08-24 01:05:06 +08:00
|
|
|
}
|
2024-09-23 14:04:33 +08:00
|
|
|
|
2024-09-24 11:11:08 +08:00
|
|
|
async updateLicense(license: string) {
|
2024-11-07 02:05:20 +08:00
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
await plusRequestService.updateLicense({ license });
|
2024-09-24 11:11:08 +08:00
|
|
|
}
|
|
|
|
|
async verify() {
|
2024-11-07 02:05:20 +08:00
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
2024-09-24 11:11:08 +08:00
|
|
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
2024-11-07 02:05:20 +08:00
|
|
|
await plusRequestService.verify({ license: licenseInfo.license });
|
2024-09-23 14:04:33 +08:00
|
|
|
}
|
2024-09-24 02:42:08 +08:00
|
|
|
|
2024-11-07 00:17:35 +08:00
|
|
|
async bindUrl(url: string) {
|
2024-11-06 02:20:52 +08:00
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
2024-11-07 00:17:35 +08:00
|
|
|
return await plusRequestService.bindUrl(url);
|
2024-11-06 02:20:52 +08:00
|
|
|
}
|
|
|
|
|
|
2024-11-05 11:38:27 +08:00
|
|
|
async register() {
|
|
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
|
2024-11-07 02:05:20 +08:00
|
|
|
if (!licenseInfo.license) {
|
|
|
|
|
await plusRequestService.register();
|
|
|
|
|
logger.info('站点注册成功');
|
2024-11-05 11:38:27 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-11-07 02:05:20 +08:00
|
|
|
|
2024-11-07 02:22:14 +08:00
|
|
|
async userPreBind(userId: number) {
|
|
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
await plusRequestService.requestWithoutSign({
|
|
|
|
|
url: '/activation/subject/preBind',
|
|
|
|
|
method: 'POST',
|
|
|
|
|
data: {
|
|
|
|
|
userId,
|
|
|
|
|
appKey: AppKey,
|
|
|
|
|
subjectId: this.getSubjectId(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async sendEmail(email: any) {
|
|
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
await plusRequestService.request({
|
|
|
|
|
url: '/activation/emailSend',
|
|
|
|
|
data: {
|
|
|
|
|
subject: email.subject,
|
|
|
|
|
text: email.content,
|
|
|
|
|
to: email.receivers,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 02:05:20 +08:00
|
|
|
async getAccessToken() {
|
|
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
await this.register();
|
|
|
|
|
return await plusRequestService.getAccessToken();
|
|
|
|
|
}
|
2024-11-07 02:22:14 +08:00
|
|
|
|
|
|
|
|
async requestWithToken(config: HttpRequestConfig) {
|
|
|
|
|
const plusRequestService = await this.getPlusRequestService();
|
|
|
|
|
const token = await this.getAccessToken();
|
|
|
|
|
merge(config, {
|
|
|
|
|
baseURL: plusRequestService.getBaseURL(),
|
|
|
|
|
headers: {
|
|
|
|
|
Authorization: token,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
return await http.request(config);
|
|
|
|
|
}
|
2024-08-23 11:35:34 +08:00
|
|
|
}
|