he wei
2024-02-27 24fc2010253463af5266cfb0cec7c87432fbdf6f
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const { app, BrowserWindow, Menu } = require("electron");
const path = require("path");
const { readConfigFile } = require('./getConfig');
 
// 获取配置文件
const config = readConfigFile();
// console.log(config);
const {
  protocol,
  serviceIp,
  port
} = config;
const remoteUrl = `${protocol}://${serviceIp}:${port}/fg/`;
 
// console.log(remoteUrl);
 
let win = null;
let loadingWin = null;
 
// 单例锁
const gotTheLock = app.requestSingleInstanceLock();
 
if (!gotTheLock) {
  app.quit();
} else {
  app.on("second-instance", (event, commandLine, workingDirectory) => {
    // 当运行第二个实例时,将会聚焦到myWindow这个窗口
    if (win) {
      if (win.isMinimized()) win.restore();
      win.focus();
    }
  });
}
 
// Scheme must be registered before the app is ready
// protocol.registerSchemesAsPrivileged([
//   { scheme: "app", privileges: { secure: true, standard: true, stream: true } },
// ]);
 
// TODO  debug;
// win.webContents.openDevTools()
 
Menu.setApplicationMenu(null);
 
// loading 窗口
// const loadingURL = isDevelopment
//   ? path.join(__dirname, "./loading.html")
//   : `file://${__dirname}/loading.html`;
const loadingURL = `file://${__dirname}/loading.html`;
const showLoading = (cb) => {
  loadingWin = new BrowserWindow({
    // show: false,
    frame: false,
    width: 260,
    height: 260,
    resizable: false,
    transparent: true,
    // webPreferences: {
    //   preload: path.join(__dirname, "./preload.js"),
    // },
  });
  // loadingWin.webContents.openDevTools();
  loadingWin.loadURL(loadingURL);
  loadingWin.setSkipTaskbar(true);
  // loadingWin.show();
  cb(true);
  // 启动java程序
  // if (platform === 'win32') {
  //   ipcMain.once('renderer-ready', (event, data) => {
  //     let dir = path.resolve(__dirname, '..');
  //     serverProcess = require('child_process').execFile(dir + '/app_x64.exe');
  //   });
  // }
};
async function createWindow(wait) {
  // Create the browser window.
  win = new BrowserWindow({
    show: !wait,
    width: 1000,
    height: 600,
    webPreferences: {
      // Use pluginOptions.nodeIntegration, leave this alone
      // See nklayman.github.io/vue-cli-plugin-electron-builder/guide/security.html#node-integration for more info
      // nodeIntegration: process.env.ELECTRON_NODE_INTEGRATION,
      // contextIsolation: !process.env.ELECTRON_NODE_INTEGRATION
      nodeIntegration: false,
      webSecurity: true,
      allowEval: false,
      allowRunningInsecureContent: false,
      contextIsolation: true,
      enableRemoteModule: false,
      // preload: path.join(__dirname, "preload.js"),
    },
  });
 
  // remote.enable(win.webContents);
  if (process.env.WEBPACK_DEV_SERVER_URL) {
    // Load the url of the dev server if in development mode
    await win.loadURL(process.env.WEBPACK_DEV_SERVER_URL);
    if (!process.env.IS_TEST) win.webContents.openDevTools();
  } else {
    // createProtocol("app");
    // Load the index.html when not in development
    // win.loadURL("app://./index.html");
    // 加载远程地址
    // win.loadURL("http://118.89.139.230:8919/fg/");
    // win.loadURL('http://192.168.10.79:8919/fg/');
    // win.loadURL('http://192.168.10.80:8919/fg/');
    win.loadURL(remoteUrl);
  }
 
  if (wait) {
    // setTimeout(() => {
    //   loadingWin.hide();
    //   loadingWin.close();
    //   win.show();
    // }, 6000);
    win.once("ready-to-show", () => {
      loadingWin.hide();
      loadingWin.close();
      win.show();
    });
  }
}
 
// Quit when all windows are closed.
app.on("window-all-closed", () => {
  // On macOS it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== "darwin") {
    app.quit();
  }
});
 
app.on("activate", () => {
  log.info("app activate event");
  // On macOS it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
 
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", async () => {
  showLoading(createWindow);
});