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

148 lines
4.5 KiB
TypeScript
Raw Normal View History

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';
2024-11-08 01:31:20 +08:00
import { cache, http, HttpRequestConfig, logger } from '@certd/basic';
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;
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
}
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) {
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-19 18:07:34 +08:00
const res = await plusRequestService.bindUrl(url);
this.plusRequestService = null;
return res;
2024-11-06 02:20:52 +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-19 18:07:34 +08:00
this.plusRequestService = null;
}
}
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,
2024-11-08 01:31:20 +08:00
subjectId: plusRequestService.getSubjectId(),
2024-11-07 02:22:14 +08:00
},
});
}
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() {
2024-11-08 01:31:20 +08:00
const cacheKey = 'certd:subject:access_token';
const token = cache.get(cacheKey);
if (token) {
return token;
}
2024-11-07 02:05:20 +08:00
const plusRequestService = await this.getPlusRequestService();
await this.register();
2024-11-08 01:31:20 +08:00
const res = await plusRequestService.getAccessToken();
const ttl = res.expiresIn * 1000 - Date.now().valueOf();
cache.set(cacheKey, res.accessToken, {
ttl,
});
return res.accessToken;
2024-11-07 02:05:20 +08:00
}
2024-11-07 02:22:14 +08:00
2024-11-17 01:06:27 +08:00
async getVipTrial() {
await this.register();
2024-11-19 17:43:06 +08:00
const plusRequestService = await this.getPlusRequestService();
2024-11-17 01:06:27 +08:00
const res = await plusRequestService.request({
url: '/activation/subject/vip/trialGet',
method: 'POST',
});
if (res.license) {
await this.updateLicense(res.license);
return {
duration: res.duration,
};
} else {
throw new Error('您已经领取过VIP试用了');
}
}
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(),
2024-11-08 01:31:20 +08:00
method: 'post',
2024-11-07 02:22:14 +08:00
headers: {
2024-11-08 01:31:20 +08:00
Authorization: `Berear ${token}`,
2024-11-07 02:22:14 +08:00
},
});
2024-11-08 01:31:20 +08:00
const res = await http.request(config);
if (res.code !== 0) {
throw new Error(res.message);
}
return res.data;
2024-11-07 02:22:14 +08:00
}
2024-08-23 11:35:34 +08:00
}