From 17638f3d3a6be6d60c30ead51ed035967c3b6b88 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Thu, 7 Nov 2024 00:17:35 +0800 Subject: [PATCH 1/3] chore: --- index.ts | 0 package.json | 5 ++- .../src/system/basic/service/plus-service.ts | 38 +++++-------------- packages/ui/certd-server/.env.localplus.yaml | 29 ++++++++++++++ packages/ui/certd-server/package.json | 1 + 5 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 index.ts create mode 100644 packages/ui/certd-server/.env.localplus.yaml diff --git a/index.ts b/index.ts new file mode 100644 index 00000000..e69de29b diff --git a/package.json b/package.json index 5a65b030..58c4ebf5 100644 --- a/package.json +++ b/package.json @@ -34,9 +34,10 @@ "license": "AGPL-3.0", "dependencies": { "axios": "^1.7.7", - "lodash-es": "^4.17.21" + "lodash-es": "^4.17.21", + "typescript": "^5.4.2" }, "workspaces": [ "packages/**" ] -} \ No newline at end of file +} diff --git a/packages/libs/lib-server/src/system/basic/service/plus-service.ts b/packages/libs/lib-server/src/system/basic/service/plus-service.ts index 26a5cfd8..7c78696d 100644 --- a/packages/libs/lib-server/src/system/basic/service/plus-service.ts +++ b/packages/libs/lib-server/src/system/basic/service/plus-service.ts @@ -1,4 +1,4 @@ -import { Config, Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; +import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; import { AppKey, PlusRequestService, verify } from '@certd/plus-core'; import { logger } from '@certd/basic'; import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js'; @@ -8,15 +8,10 @@ import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settin export class PlusService { @Inject() sysSettingsService: SysSettingsService; - @Config('plus.server.baseUrls') - plusServerBaseUrls: string[]; async getPlusRequestService() { const subjectId = await this.getSubjectId(); - return new PlusRequestService({ - plusServerBaseUrls: this.plusServerBaseUrls, - subjectId, - }); + return new PlusRequestService({ subjectId }); } async getSubjectId() { @@ -60,26 +55,16 @@ export class PlusService { const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo); const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); - const plusRequestService = await this.getPlusRequestService(); - return await verify({ - subjectId: plusRequestService.subjectId, + subjectId: installInfo.siteId, license: licenseInfo.license, - plusRequestService: plusRequestService, bindUrl: installInfo?.bindUrl, }); } - async bindUrl(subjectId: string, url: string) { + async bindUrl(url: string) { const plusRequestService = await this.getPlusRequestService(); - return await plusRequestService.request({ - url: '/activation/subject/urlBind', - data: { - subjectId, - appKey: AppKey, - url, - }, - }); + return await plusRequestService.bindUrl(url); } async register() { @@ -88,16 +73,11 @@ export class PlusService { const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); if (!licenseInfo?.license) { //还没有license,注册一个 - const res = await plusRequestService.requestWithoutSign({ - url: '/activation/subject/register', - data: { - appKey: AppKey, - subjectId: installInfo.siteId, - installTime: installInfo.installTime, - }, + const license = await plusRequestService.register({ + installTime: installInfo.installTime, }); - if (res.license) { - await this.updateLicense(res.license); + if (license) { + await this.updateLicense(license); logger.info('站点注册成功'); } } diff --git a/packages/ui/certd-server/.env.localplus.yaml b/packages/ui/certd-server/.env.localplus.yaml new file mode 100644 index 00000000..84423ee1 --- /dev/null +++ b/packages/ui/certd-server/.env.localplus.yaml @@ -0,0 +1,29 @@ +# key: ./data/ssl/cert.key +# cert: ./data/ssl/cert.crt +#plus: +# server: +# baseUrl: 'http://127.0.0.1:11007' + +#flyway: +# scriptDir: './db/migration-pg' + +#typeorm: +# dataSource: +# default: +# type: postgres +# host: localhost +# port: 5433 +# username: postgres +# password: root +# database: postgres + +typeorm: + dataSource: + default: + database: './data/db-plus-dev.sqlite' + +# plus server: 'http://127.0.0.1:11007' + +account: + server: + baseUrl: 'http://127.0.0.1:1017/subject' diff --git a/packages/ui/certd-server/package.json b/packages/ui/certd-server/package.json index 0ec75646..4e766a67 100644 --- a/packages/ui/certd-server/package.json +++ b/packages/ui/certd-server/package.json @@ -10,6 +10,7 @@ "commdev": "cross-env NODE_ENV=commdev mwtsc --watch --run @midwayjs/mock/app", "commpro": "cross-env NODE_ENV=commpro mwtsc --watch --run @midwayjs/mock/app", "pgdev": "cross-env NODE_ENV=pgdev mwtsc --watch --run @midwayjs/mock/app", + "local-plus": "cross-env NODE_ENV=localplus mwtsc --watch --run @midwayjs/mock/app", "pgpl": "cross-env NODE_ENV=pgpl mwtsc --watch --run @midwayjs/mock/app", "dev-new": "cross-env NODE_ENV=devnew mwtsc --watch --run @midwayjs/mock/app", "rm-db-new": "rimraf ./data/db-new.sqlite", From cd83a6f2096bf6ba523003b5748bff624e984342 Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Thu, 7 Nov 2024 02:05:20 +0800 Subject: [PATCH 2/3] chore: --- .../src/system/basic/service/plus-service.ts | 82 ++++++++----------- .../controller/sys/plus/plus-controller.ts | 21 +---- 2 files changed, 37 insertions(+), 66 deletions(-) diff --git a/packages/libs/lib-server/src/system/basic/service/plus-service.ts b/packages/libs/lib-server/src/system/basic/service/plus-service.ts index 7c78696d..e33a7173 100644 --- a/packages/libs/lib-server/src/system/basic/service/plus-service.ts +++ b/packages/libs/lib-server/src/system/basic/service/plus-service.ts @@ -1,5 +1,5 @@ import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; -import { AppKey, PlusRequestService, verify } from '@certd/plus-core'; +import { PlusRequestService } from '@certd/plus-core'; import { logger } from '@certd/basic'; import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js'; @@ -9,9 +9,26 @@ export class PlusService { @Inject() sysSettingsService: SysSettingsService; + plusRequestService: PlusRequestService; + async getPlusRequestService() { - const subjectId = await this.getSubjectId(); - return new PlusRequestService({ subjectId }); + 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); + }; + return new PlusRequestService({ subjectId, bindUrl, installTime, saveLicense }); } async getSubjectId() { @@ -19,47 +36,19 @@ export class PlusService { return installInfo.siteId; } - async requestWithoutSign(config: any) { + async active(code: string) { const plusRequestService = await this.getPlusRequestService(); - return await plusRequestService.requestWithoutSign(config); - } - async request(config: any) { - const plusRequestService = await this.getPlusRequestService(); - return await plusRequestService.request(config); - } - - async active(formData: { code: any; appKey: string; subjectId: string }) { - const plusRequestService = await this.getPlusRequestService(); - return await plusRequestService.requestWithoutSign({ - url: '/activation/active', - method: 'post', - data: formData, - }); + return await plusRequestService.active(code); } 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); - const verifyRes = await this.verify(); - if (!verifyRes.isPlus) { - const message = verifyRes.message || '授权码校验失败'; - logger.error(message); - throw new Error(message); - } + const plusRequestService = await this.getPlusRequestService(); + await plusRequestService.updateLicense({ license }); } async verify() { + const plusRequestService = await this.getPlusRequestService(); const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo); - const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); - - return await verify({ - subjectId: installInfo.siteId, - license: licenseInfo.license, - bindUrl: installInfo?.bindUrl, - }); + await plusRequestService.verify({ license: licenseInfo.license }); } async bindUrl(url: string) { @@ -70,16 +59,15 @@ export class PlusService { async register() { const plusRequestService = await this.getPlusRequestService(); const licenseInfo: SysLicenseInfo = await this.sysSettingsService.getSetting(SysLicenseInfo); - const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); - if (!licenseInfo?.license) { - //还没有license,注册一个 - const license = await plusRequestService.register({ - installTime: installInfo.installTime, - }); - if (license) { - await this.updateLicense(license); - logger.info('站点注册成功'); - } + if (!licenseInfo.license) { + await plusRequestService.register(); + logger.info('站点注册成功'); } } + + async getAccessToken() { + const plusRequestService = await this.getPlusRequestService(); + await this.register(); + return await plusRequestService.getAccessToken(); + } } diff --git a/packages/ui/certd-server/src/controller/sys/plus/plus-controller.ts b/packages/ui/certd-server/src/controller/sys/plus/plus-controller.ts index c62fbbf4..ce8d1923 100644 --- a/packages/ui/certd-server/src/controller/sys/plus/plus-controller.ts +++ b/packages/ui/certd-server/src/controller/sys/plus/plus-controller.ts @@ -1,7 +1,5 @@ import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core'; import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server'; -import { AppKey } from '@certd/plus-core'; -import { logger } from '@certd/basic'; /** */ @@ -17,23 +15,8 @@ export class SysPlusController extends BaseController { @Post('/active', { summary: 'sys:settings:edit' }) async active(@Body(ALL) body) { const { code } = body; - const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); - const siteId = installInfo.siteId; - const formData = { - appKey: AppKey, - code, - subjectId: siteId, - }; - const res: any = await this.plusService.active(formData); - - if (res.code > 0) { - logger.error('激活失败', res.message); - return this.fail(res.message, 1); - } - const license = res.data.license; - - await this.plusService.updateLicense(license); + await this.plusService.active(code); return this.ok(true); } @@ -42,7 +25,7 @@ export class SysPlusController extends BaseController { const { url } = body; const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); - await this.plusService.bindUrl(installInfo.siteId, url); + await this.plusService.bindUrl(url); installInfo.bindUrl = url; await this.sysSettingsService.saveSetting(installInfo); From 1c33fb4e14ad66a77d41f200628c2dbac95c295b Mon Sep 17 00:00:00 2001 From: xiaojunnuo Date: Thu, 7 Nov 2024 02:22:14 +0800 Subject: [PATCH 3/3] chore: --- .../src/system/basic/service/plus-service.ts | 42 ++++++++++++++++++- .../sys/account/account-controller.ts | 17 +------- .../modules/basic/service/email-service.ts | 9 +--- .../modules/cname/service/common-provider.ts | 4 +- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/packages/libs/lib-server/src/system/basic/service/plus-service.ts b/packages/libs/lib-server/src/system/basic/service/plus-service.ts index e33a7173..875a9ed0 100644 --- a/packages/libs/lib-server/src/system/basic/service/plus-service.ts +++ b/packages/libs/lib-server/src/system/basic/service/plus-service.ts @@ -1,7 +1,8 @@ import { Inject, Provide, Scope, ScopeEnum } from '@midwayjs/core'; -import { PlusRequestService } from '@certd/plus-core'; -import { logger } from '@certd/basic'; +import { AppKey, PlusRequestService } from '@certd/plus-core'; +import { http, HttpRequestConfig, logger } from '@certd/basic'; import { SysInstallInfo, SysLicenseInfo, SysSettingsService } from '../../settings/index.js'; +import { merge } from 'lodash-es'; @Provide() @Scope(ScopeEnum.Singleton) @@ -65,9 +66,46 @@ export class PlusService { } } + 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, + }, + }); + } + async getAccessToken() { const plusRequestService = await this.getPlusRequestService(); await this.register(); return await plusRequestService.getAccessToken(); } + + 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); + } } diff --git a/packages/ui/certd-server/src/controller/sys/account/account-controller.ts b/packages/ui/certd-server/src/controller/sys/account/account-controller.ts index bd5a0b23..cd55986a 100644 --- a/packages/ui/certd-server/src/controller/sys/account/account-controller.ts +++ b/packages/ui/certd-server/src/controller/sys/account/account-controller.ts @@ -1,8 +1,5 @@ import { ALL, Body, Controller, Inject, Post, Provide } from '@midwayjs/core'; -import { BaseController, PlusService } from '@certd/lib-server'; -import { AppKey } from '@certd/plus-core'; -import { SysSettingsService } from '@certd/lib-server'; -import { SysInstallInfo } from '@certd/lib-server'; +import { BaseController, PlusService, SysInstallInfo, SysSettingsService } from '@certd/lib-server'; export type PreBindUserReq = { userId: number; @@ -23,18 +20,8 @@ export class BasicController extends BaseController { @Post('/preBindUser', { summary: 'sys:settings:edit' }) public async preBindUser(@Body(ALL) body: PreBindUserReq) { - const installInfo: SysInstallInfo = await this.sysSettingsService.getSetting(SysInstallInfo); // 设置缓存内容 - await this.plusService.requestWithoutSign({ - url: '/activation/subject/preBind', - method: 'POST', - data: { - userId: body.userId, - appKey: AppKey, - subjectId: installInfo.siteId, - }, - }); - + await this.plusService.userPreBind(body.userId); return this.ok({}); } diff --git a/packages/ui/certd-server/src/modules/basic/service/email-service.ts b/packages/ui/certd-server/src/modules/basic/service/email-service.ts index 03440a1a..ff1a2c46 100644 --- a/packages/ui/certd-server/src/modules/basic/service/email-service.ts +++ b/packages/ui/certd-server/src/modules/basic/service/email-service.ts @@ -49,14 +49,7 @@ export class EmailService implements IEmailService { * receivers: string[]; */ - await this.plusService.request({ - url: '/activation/emailSend', - data: { - subject: email.subject, - text: email.content, - to: email.receivers, - }, - }); + await this.plusService.sendEmail(email); } /** diff --git a/packages/ui/certd-server/src/modules/cname/service/common-provider.ts b/packages/ui/certd-server/src/modules/cname/service/common-provider.ts index e4e198f9..bbbdc30f 100644 --- a/packages/ui/certd-server/src/modules/cname/service/common-provider.ts +++ b/packages/ui/certd-server/src/modules/cname/service/common-provider.ts @@ -32,7 +32,7 @@ export class CommonDnsProvider implements IDnsProvider { await this.plusService.register(); - const res = await this.plusService.requestWithoutSign({ + const res = await this.plusService.requestWithToken({ url: '/activation/certd/cname/recordCreate', data: { subjectId: this.plusService.getSubjectId(), @@ -45,7 +45,7 @@ export class CommonDnsProvider implements IDnsProvider { return res; } async removeRecord(options: RemoveRecordOptions) { - const res = await this.plusService.requestWithoutSign({ + const res = await this.plusService.requestWithToken({ url: '/activation/certd/cname/recordRemove', data: { subjectId: this.plusService.getSubjectId(),