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}` `请求出错status:${error.response?.status},statusText:${error.response?.statusText},url:${error.config?.url},method:${error.config?.method}`
); );
logger.error("返回数据:", JSON.stringify(error.response?.data)); 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) { if (error instanceof AggregateError) {
logger.error("AggregateError", error); logger.error("AggregateError", error);
} }

View File

@ -57,8 +57,7 @@ export class BasicController extends BaseController {
@Post('/updateLicense', { summary: 'sys:settings:edit' }) @Post('/updateLicense', { summary: 'sys:settings:edit' })
public async updateLicense(@Body(ALL) body: { license: string }) { public async updateLicense(@Body(ALL) body: { license: string }) {
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); await this.plusService.updateLicense(body.license);
await this.plusService.updateLicense(installInfo.siteId, body.license);
return this.ok(true); 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 { UserService } from '../authority/service/user-service.js';
import { SysSettingsService } from '../system/service/sys-settings-service.js'; import { SysSettingsService } from '../system/service/sys-settings-service.js';
import { nanoid } from 'nanoid'; import { nanoid } from 'nanoid';
import { SysInstallInfo, SysLicenseInfo, SysPrivateSettings } from '../system/service/models.js'; import { SysInstallInfo, SysPrivateSettings } from '../system/service/models.js';
import { verify } from '@certd/pipeline';
import crypto from 'crypto'; import crypto from 'crypto';
import { PlusService } from '../basic/service/plus-service.js';
export type InstallInfo = { export type InstallInfo = {
installTime: number; installTime: number;
instanceId?: string; instanceId?: string;
@ -22,6 +23,8 @@ export class AutoInitSite {
@Inject() @Inject()
sysSettingsService: SysSettingsService; sysSettingsService: SysSettingsService;
@Inject()
plusService: PlusService;
@Init() @Init()
async init() { async init() {
@ -52,12 +55,7 @@ export class AutoInitSite {
} }
// 授权许可 // 授权许可
const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo); await this.plusService.verify();
const req = {
subjectId: installInfo.siteId,
license: licenseInfo.license,
};
await verify(req);
logger.info('初始化站点完成'); 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 { SysSettingsService } from '../../system/service/sys-settings-service.js';
import { SysInstallInfo, SysLicenseInfo } from '../../system/service/models.js'; import { SysInstallInfo, SysLicenseInfo } from '../../system/service/models.js';
import { AppKey, getPlusInfo, isPlus, verify } from '@certd/pipeline'; import { AppKey, http, PlusRequestService, verify } from '@certd/pipeline';
import * as crypto from 'crypto';
import { request } from '../../../utils/http.js';
import { logger } from '../../../utils/logger.js'; import { logger } from '../../../utils/logger.js';
@Provide() @Provide()
@ -14,81 +12,52 @@ export class PlusService {
@Config('plus.server.baseUrl') @Config('plus.server.baseUrl')
plusServerBaseUrl; plusServerBaseUrl;
async requestWithoutSign(config: any): Promise<any> { plusRequestService: PlusRequestService;
config.baseURL = this.plusServerBaseUrl;
config.method = config.method || 'POST';
return await request(config);
}
async request(config: any) { @Init()
if (!isPlus()) { async init() {
throw new Error('您还不是专业版,请先激活专业版');
}
const { url, data } = config;
const timestamps = Date.now();
const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo);
const sign = await this.sign(data, timestamps); this.plusRequestService = new PlusRequestService({
plusServerBaseUrl: this.plusServerBaseUrl,
const requestHeader = { http: http,
logger,
subjectId: installInfo.siteId, 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) { async requestWithoutSign(config: any) {
//content := fmt.Sprintf("%s.%d.%s", in.Params, in.Timestamps, secret) return await this.plusRequestService.requestWithoutSign(config);
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}`; async request(config: any) {
return await this.plusRequestService.request(config);
// sha256
const sign = crypto.createHash('sha256').update(content).digest('base64');
logger.info('content:', content, 'sign:', sign);
return sign;
} }
async active(formData: { code: any; appKey: string; subjectId: string }) { async active(formData: { code: any; appKey: string; subjectId: string }) {
return await this.requestWithoutSign({ return await this.plusRequestService.requestWithoutSign({
url: '/activation/active', url: '/activation/active',
method: 'post', method: 'post',
data: formData, data: formData,
}); });
} }
async updateLicense(siteId: string, license: string) { async updateLicense(license: string) {
let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo); let licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo);
if (!licenseInfo) { if (!licenseInfo) {
licenseInfo = new SysLicenseInfo(); licenseInfo = new SysLicenseInfo();
} }
licenseInfo.license = license; licenseInfo.license = license;
await this.sysSettingsService.saveSetting(licenseInfo); 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({ const verifyRes = await verify({
subjectId: siteId, subjectId: installInfo.siteId,
license, license: licenseInfo.license,
plusRequestService: this.plusRequestService,
bindUrl: installInfo?.bindUrl,
}); });
if (!verifyRes.isPlus) { if (!verifyRes.isPlus) {
@ -99,7 +68,7 @@ export class PlusService {
} }
async bindUrl(subjectId: string, url: string) { async bindUrl(subjectId: string, url: string) {
return await this.request({ return await this.plusRequestService.request({
url: '/activation/subject/urlBind', url: '/activation/subject/urlBind',
data: { data: {
subjectId, subjectId,

View File

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