diff --git a/package.json b/package.json index 48c9342..d18960d 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "一个项目用COOL就够了", "private": true, "dependencies": { - "@cool-midway/core": "^7.1.11", + "@cool-midway/core": "^7.1.12", "@cool-midway/rpc": "^7.0.0", "@cool-midway/task": "^7.0.0", "@midwayjs/bootstrap": "^3.15.0", @@ -60,7 +60,7 @@ "lint:fix": "mwts fix", "ci": "npm run cov", "build": "mwtsc --cleanOutDir", - "pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin", + "pm2:start": "pm2 start ./bootstrap.js -i 2 --name cool-admin", "pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin" }, "midway-bin-clean": [ diff --git a/src/config/config.default.ts b/src/config/config.default.ts index 6ef3d8b..2aa8331 100644 --- a/src/config/config.default.ts +++ b/src/config/config.default.ts @@ -7,7 +7,7 @@ import { CoolCacheStore } from '@cool-midway/core'; export default { // use for cookie sign key, should change to your own and keep security - keys: 'cool-admin for node', + keys: 'cool-admin-keys-xxxxxx', koa: { port: 8001, }, diff --git a/src/modules/base/event/app.ts b/src/modules/base/event/app.ts index 3ee9cc6..70a3f19 100644 --- a/src/modules/base/event/app.ts +++ b/src/modules/base/event/app.ts @@ -16,12 +16,30 @@ export class BaseAppEvent { @Config('module') config; + @Config('keys') + configKeys; + + @Config('koa.port') + port; + @App() app: IMidwayKoaApplication; + @Event('onDBInit') + async onDBInit() { + this.checkConfig(); + this.checkKeys(); + } + @Event('onServerReady') async onServerReady() { - this.checkConfig(); + setTimeout(() => { + this.coreLogger.info( + ` +项目启动成功,欢迎使用 cool-admin 框架! 官网: https://cool-js.com +服务访问地址: http://127.0.0.1:${this.port}` + ); + }, 2000); } /** @@ -30,7 +48,7 @@ export class BaseAppEvent { async checkConfig() { if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') { this.coreLogger.warn( - '检测到模块[base] jwt.secret 配置是默认值,即将自动修改...' + '检测到模块[base] jwt.secret 配置是默认值,请不要关闭!即将自动修改...' ); setTimeout(() => { const filePath = path.join( @@ -52,7 +70,38 @@ export class BaseAppEvent { this.coreLogger.info( '\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m' ); - }, 6000); + }, 15000); + } + } + + /** + * 检查keys + */ + async checkKeys() { + if (this.configKeys == 'cool-admin-keys-xxxxxx') { + this.coreLogger.warn( + '检测到配置Keys是默认值,请不要关闭!即将自动修改...' + ); + setTimeout(() => { + const filePath = path.join( + this.app.getBaseDir(), + '..', + 'src', + 'config', + 'config.default.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-keys-xxxxxx', secret) + ); + this.coreLogger.info( + '\x1B[36m [cool:module:base] midwayjs cool keys auto modify \x1B[0m' + ); + }, 15000); } } } diff --git a/src/modules/user/event/app.ts b/src/modules/user/event/app.ts index 372fed2..36a3805 100644 --- a/src/modules/user/event/app.ts +++ b/src/modules/user/event/app.ts @@ -19,8 +19,8 @@ export class UserAppEvent { @App() app: IMidwayKoaApplication; - @Event('onServerReady') - async onServerReady() { + @Event('onDBInit') + async onDBInit() { this.checkConfig(); } @@ -30,7 +30,7 @@ export class UserAppEvent { async checkConfig() { if (this.config.user.jwt.secret == 'cool-app-xxxxxx') { this.coreLogger.warn( - '检测到模块[user] jwt.secret 配置是默认值,即将自动修改...' + '检测到模块[user] jwt.secret 配置是默认值,请不要关闭!即将自动修改...' ); setTimeout(() => { const filePath = path.join( @@ -49,7 +49,7 @@ export class UserAppEvent { this.coreLogger.info( '\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m' ); - }, 8000); + }, 15000); } } }