diff --git a/package.json b/package.json index 16bf981..48c9342 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "一个项目用COOL就够了", "private": true, "dependencies": { - "@cool-midway/core": "^7.1.10", + "@cool-midway/core": "^7.1.11", "@cool-midway/rpc": "^7.0.0", "@cool-midway/task": "^7.0.0", "@midwayjs/bootstrap": "^3.15.0", @@ -61,8 +61,7 @@ "ci": "npm run cov", "build": "mwtsc --cleanOutDir", "pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin", - "pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin", - "pm2:debug": "pm2-runtime start ./bootstrap.js -i max --name cool-admin" + "pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin" }, "midway-bin-clean": [ ".vscode/.tsbuildinfo", diff --git a/src/modules/base/event/app.ts b/src/modules/base/event/app.ts index 05eeb96..3ee9cc6 100644 --- a/src/modules/base/event/app.ts +++ b/src/modules/base/event/app.ts @@ -21,21 +21,38 @@ export class BaseAppEvent { @Event('onServerReady') async onServerReady() { + this.checkConfig(); + } + + /** + * 检查配置 + */ + async checkConfig() { if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') { - const filePath = path.join( - this.app.getBaseDir(), - 'modules', - 'base', - 'config.ts' - ); - // 替换文件内容 - let fileData = fs.readFileSync(filePath, 'utf8'); - const secret = uuid().replace(/-/g, ''); - this.config.base.jwt.secret = secret; - fs.writeFileSync(filePath, fileData.replace('cool-admin-xxxxxx', secret)); - this.coreLogger.info( - '\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m' + this.coreLogger.warn( + '检测到模块[base] jwt.secret 配置是默认值,即将自动修改...' ); + setTimeout(() => { + const filePath = path.join( + this.app.getBaseDir(), + '..', + 'src', + 'modules', + 'base', + 'config.ts' + ); + // 替换文件内容 + let fileData = fs.readFileSync(filePath, 'utf8'); + const secret = uuid().replace(/-/g, ''); + this.config.base.jwt.secret = secret; + fs.writeFileSync( + filePath, + fileData.replace('cool-admin-xxxxxx', secret) + ); + this.coreLogger.info( + '\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m' + ); + }, 6000); } } } diff --git a/src/modules/demo/controller/open/event.ts b/src/modules/demo/controller/open/event.ts index 6ada13e..826bf6f 100644 --- a/src/modules/demo/controller/open/event.ts +++ b/src/modules/demo/controller/open/event.ts @@ -13,9 +13,15 @@ export class OpenDemoEventController extends BaseController { @Inject() coolEventManager: CoolEventManager; - @Post('/send') - async send() { - await this.coolEventManager.emit('demo', { a: 1 }, 1); + @Post('/comm', { summary: '普通事件,本进程生效' }) + async comm() { + await this.coolEventManager.emit('demo', { a: 2 }, 1); + return this.ok(); + } + + @Post('/global', { summary: '全局事件,多进程都有效' }) + async global() { + await this.coolEventManager.globalEmit('demo', false, { a: 2 }, 1); return this.ok(); } } diff --git a/src/modules/demo/event/demo.ts b/src/modules/demo/event/comm.ts similarity index 60% rename from src/modules/demo/event/demo.ts rename to src/modules/demo/event/comm.ts index 97a9a1a..8e5ef8f 100644 --- a/src/modules/demo/event/demo.ts +++ b/src/modules/demo/event/comm.ts @@ -3,19 +3,18 @@ import { CoolEvent, Event } from '@cool-midway/core'; import { Scope, ScopeEnum } from '@midwayjs/core'; /** - * 接收事件 + * 普通事件 */ -@Provide() -@Scope(ScopeEnum.Singleton) @CoolEvent() -export class DemoEvent { +export class DemoCommEvent { /** * 根据事件名接收事件 * @param msg * @param a */ @Event('demo') - async updatdemoeUser(msg, a) { - console.log('收到消息', msg, a); + async demo(msg, a) { + console.log(`comm当前进程的ID是: ${process.pid}`); + console.log('comm收到消息', msg, a); } } diff --git a/src/modules/user/event/app.ts b/src/modules/user/event/app.ts index f5fbffd..372fed2 100644 --- a/src/modules/user/event/app.ts +++ b/src/modules/user/event/app.ts @@ -21,21 +21,35 @@ export class UserAppEvent { @Event('onServerReady') async onServerReady() { + this.checkConfig(); + } + + /** + * 检查配置 + */ + async checkConfig() { if (this.config.user.jwt.secret == 'cool-app-xxxxxx') { - const filePath = path.join( - this.app.getBaseDir(), - 'modules', - 'user', - 'config.ts' - ); - // 替换文件内容 - let fileData = fs.readFileSync(filePath, 'utf8'); - const secret = uuid().replace(/-/g, ''); - this.config.user.jwt.secret = secret; - fs.writeFileSync(filePath, fileData.replace('cool-app-xxxxxx', secret)); - this.coreLogger.info( - '\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m' + this.coreLogger.warn( + '检测到模块[user] jwt.secret 配置是默认值,即将自动修改...' ); + setTimeout(() => { + const filePath = path.join( + this.app.getBaseDir(), + '..', + 'src', + 'modules', + 'user', + 'config.ts' + ); + // 替换文件内容 + let fileData = fs.readFileSync(filePath, 'utf8'); + const secret = uuid().replace(/-/g, ''); + this.config.user.jwt.secret = secret; + fs.writeFileSync(filePath, fileData.replace('cool-app-xxxxxx', secret)); + this.coreLogger.info( + '\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m' + ); + }, 8000); } } }