wangzhibo
2026-07-16 bac4362a0b7726d38a23d38f0d7913f2c2bab262
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { CoolEvent, Event } from '@cool-midway/core';
import { App, ILogger, IMidwayApplication, Inject } from '@midwayjs/core';
 
/**
 * 接收事件
 */
@CoolEvent()
export class BaseAppEvent {
  @App()
  app: IMidwayApplication;
 
  @Inject()
  logger: ILogger;
 
  @Event('onServerReady')
  async onServerReady() {
    if (!process['pkg']) return;
    const port = this.app.getConfig('koa.port') || 8001;
    this.logger.info(`Server is running at http://127.0.0.1:${port}`);
    const url = `http://127.0.0.1:${port}`;
 
    // 使用 child_process 打开浏览器
    const { exec } = require('child_process');
    let command;
 
    switch (process.platform) {
      case 'darwin': // macOS
        command = `open ${url}`;
        break;
      case 'win32': // Windows
        command = `start ${url}`;
        break;
      default: // Linux
        command = `xdg-open ${url}`;
        break;
    }
 
    console.log('url=>', url);
    exec(command, (error: any) => {
      if (!error) {
        this.logger.info(`Application has opened in browser at ${url}`);
      }
    });
  }
}