chore: 优化性能

pull/189/head
xiaojunnuo 2024-09-24 11:11:08 +08:00
parent 9a3ff8ad1a
commit 8de56feeb7
5 changed files with 37 additions and 69 deletions

View File

@ -121,7 +121,9 @@ export function createAxiosService({ logger }: { logger: Logger }) {
`请求出错status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}`
);
logger.error("返回数据:", JSON.stringify(error.response?.data));
if (error.response?.data) {
error.message = error.response.data.message || error.response.data.msg || error.response.data.error || error.response.data;
}
if (error instanceof AggregateError) {
logger.error("AggregateError", error);
}

View File

@ -57,8 +57,7 @@ export class BasicController extends BaseController {
@Post('/updateLicense', { summary: 'sys:settings:edit' })
public async updateLicense(@Body(ALL) body: { license: string }) {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
await this.plusService.updateLicense(installInfo.siteId, body.license);
await this.plusService.updateLicense(body.license);
return this.ok(true);
}
}

View File

@ -3,9 +3,10 @@ import { logger } from '../../utils/logger.js';
import { UserService } from '../authority/service/user-service.js';
import { SysSettingsService } from '../system/service/sys-settings-service.js';
import { nanoid } from 'nanoid';
import { SysInstallInfo, SysLicenseInfo, SysPrivateSettings } from '../system/service/models.js';
import { verify } from '@certd/pipeline';
import { SysInstallInfo, SysPrivateSettings } from '../system/service/models.js';
import crypto from 'crypto';
import { PlusService } from '../basic/service/plus-service.js';
export type InstallInfo = {
installTime: number;
instanceId?: string;
@ -22,6 +23,8 @@ export class AutoInitSite {
@Inject()
sysSettingsService: SysSettingsService;
@Inject()
plusService: PlusService;
@Init()
async init() {
@ -52,12 +55,7 @@ export class AutoInitSite {
}
// 授权许可
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const req = {
subjectId: installInfo.siteId,
license: licenseInfo.license,
};
await verify(req);
await this.plusService.verify();
logger.info('初始化站点完成');
}

View File

@ -1,9 +1,7 @@
import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { Config, Init, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core';
import { SysSettingsService } from '../../system/service/sys-settings-service.js';
import { SysInstallInfo, SysLicenseInfo } from '../../system/service/models.js';
import { AppKey, getPlusInfo, isPlus, verify } from '@certd/pipeline';
import * as crypto from 'crypto';
import { request } from '../../../utils/http.js';
import { AppKey, http, PlusRequestService, verify } from '@certd/pipeline';
import { logger } from '../../../utils/logger.js';
@Provide()
@ -14,81 +12,52 @@ export class PlusService {
@Config('plus.server.baseUrl')
plusServerBaseUrl;
async requestWithoutSign(config: any): Promise<any> {
config.baseURL = this.plusServerBaseUrl;
config.method = config.method || 'POST';
return await request(config);
}
plusRequestService: PlusRequestService;
async request(config: any) {
if (!isPlus()) {
throw new Error('您还不是专业版,请先激活专业版');
}
const { url, data } = config;
const timestamps = Date.now();
@Init()
async init() {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const sign = await this.sign(data, timestamps);
const requestHeader = {
this.plusRequestService = new PlusRequestService({
plusServerBaseUrl: this.plusServerBaseUrl,
http: http,
logger,
subjectId: installInfo.siteId,
appKey: AppKey,
sign: sign,
timestamps: timestamps,
};
let requestHeaderStr = JSON.stringify(requestHeader);
requestHeaderStr = Buffer.from(requestHeaderStr).toString('base64');
const headers = {
'Content-Type': 'application/json',
'X-Plus-Subject': requestHeaderStr,
};
return await request({
url: url,
baseURL: this.plusServerBaseUrl,
method: 'POST',
data: data,
headers: headers,
});
}
async sign(body: any, timestamps: number) {
//content := fmt.Sprintf("%s.%d.%s", in.Params, in.Timestamps, secret)
const params = JSON.stringify(body);
const plusInfo = getPlusInfo();
const secret = plusInfo.secret;
if (!secret) {
const randomTime = Math.floor(Math.random() * 3 * 60 * 1000 + 30 * 1000);
setTimeout(() => {
process.exit();
}, randomTime);
return 'xxxxx';
}
const content = `${params}.${timestamps}.${secret}`;
// sha256
const sign = crypto.createHash('sha256').update(content).digest('base64');
logger.info('content:', content, 'sign:', sign);
return sign;
async requestWithoutSign(config: any) {
return await this.plusRequestService.requestWithoutSign(config);
}
async request(config: any) {
return await this.plusRequestService.request(config);
}
async active(formData: { code: any; appKey: string; subjectId: string }) {
return await this.requestWithoutSign({
return await this.plusRequestService.requestWithoutSign({
url: '/activation/active',
method: 'post',
data: formData,
});
}
async updateLicense(siteId: string, license: string) {
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);
await this.verify();
}
async verify() {
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const verifyRes = await verify({
subjectId: siteId,
license,
subjectId: installInfo.siteId,
license: licenseInfo.license,
plusRequestService: this.plusRequestService,
bindUrl: installInfo?.bindUrl,
});
if (!verifyRes.isPlus) {
@ -99,7 +68,7 @@ export class PlusService {
}
async bindUrl(subjectId: string, url: string) {
return await this.request({
return await this.plusRequestService.request({
url: '/activation/subject/urlBind',
data: {
subjectId,

View File

@ -36,7 +36,7 @@ export class SysPlusController extends BaseController {
}
const license = res.data.license;
await this.plusService.updateLicense(siteId, license);
await this.plusService.updateLicense(license);
return this.ok(true);
}