main.js
9.36 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//开机自动启动
const AutoLaunch = require('auto-launch');
const appLauncher = new AutoLaunch({
name: 'XNTALK App'
});
const net = require('net');
const http = require("http");
const url = require("url");
const fs = require("fs");
const electron = require('electron'); // 控制应用生命周期的模块。
// 控制应用生命周期的模块。
const BrowserWindow = electron.BrowserWindow; // 创建原生浏览器窗口的模块
var app = electron.app;
var Menu = require('electron').Menu;
var Tray = require('electron').Tray;
//这页列出了Chrome浏览器和Electron支持的命令行开关. 你也可以在app模块的ready事件发出之前使用app.commandLine.appendSwitch 来添加它们到你应用的main脚本里面:
app.commandLine.appendArgument('enable-file-cookies');
app.commandLine.appendArgument('allow-file-access-from-files');
app.commandLine.appendArgument('allow-displaying-insecure-content');
//主进程 与渲染进程 交互
const ipcMain = electron.ipcMain;
//设置常量
global.isDev=false;
global.isDebug=true;
global.__dirname=__dirname;
global.port = 7543;
/**
* win32 //
* linux
* darwin //mac
*
*/
global.platform = process.platform;
/*获取版本信息*/
// var config = require("./package.json");
// console.log(config);
fs.readFile(__dirname+ "/package.json",{encoding:'utf8',flag:'r'},function readData(err, data) {
if(err){
console.log(err);
}else {
global.appInfo=JSON.parse(data);
}
});
if(global.isDev!= true){
global.serveUrl="https://xntalk-api.xiniunet.com/router?";
global.app_key="0617CA8376F9901F28FF46B69BF9CF47";
global.secret="28570C9D069ED51226DD9F028BD5E6DC";
}else{
global.serveUrl="https://xntalk-api-dev.xiniunet.com/router?";
global.app_key="0617CA8376F9901F28FF46B69BF9CF44";
global.secret="28570C9D069ED51226DD9F028BD5E6DD";
}
if(global.isDev==true){
global.appKey = '232b9bc7c221f25576e3b80458fb812f';
}else {
global.appKey = '708e73e559eb1c86b6573f72c47afbdc';
}
// 保持一个对于 window 对象的全局引用,不然,当 JavaScript 被 GC,
// window 会被自动地关闭
var mainWindow = null;
var tray = null;
const nativeImageTray = electron.nativeImage.createFromPath(__dirname + '/images/20x20-2.png');
const nativeImageTrayMsg = electron.nativeImage.createFromPath(__dirname +"/images/20x20.png");
try{
/*
* 启动服务
* */
global.port = 7543;
var createHttp = function(port)
{
var server = http.createServer(function(req,res){
var pathname = url.parse(req.url).pathname;
fs.readFile(__dirname+ "/"+ pathname, function readData(err, data) {
res.writeHead(200);
res.end(data);
});
}).listen(port);
server.on('listening', function () { // 执行这块代码说明端口未被占用
global.port = port;
console.log("################port##########" + port);
});
server.on('error', function (err) {
createHttp(++port);
});
};
createHttp(global.port);
// 当 Electron 完成了初始化并且准备创建浏览器窗口的时候
// 这个方法就被调用
app.on('ready', function() {
if(process.platform == 'darwin'){
// Create the Application's main menu
var template = [{
label: "Application",
submenu: [
{ label: "About Application", selector: "orderFrontStandardAboutPanel:" },
{ type: "separator" },
{ label: "Quit", accelerator: "Command+Q", click: function() { app.quit(); }}
]}, {
label: "Edit",
submenu: [
{ label: "Undo", accelerator: "CmdOrCtrl+Z", selector: "undo:" },
{ label: "Redo", accelerator: "Shift+CmdOrCtrl+Z", selector: "redo:" },
{ type: "separator" },
{ label: "Cut", accelerator: "CmdOrCtrl+X", selector: "cut:" },
{ label: "Copy", accelerator: "CmdOrCtrl+C", selector: "copy:" },
{ label: "Paste", accelerator: "CmdOrCtrl+V", selector: "paste:" },
{ label: "Select All", accelerator: "CmdOrCtrl+A", selector: "selectAll:" }
]}
];
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
// 用一个 Tray 来表示一个图标,这个图标处于正在运行的系统的通知区
tray = new Tray(nativeImageTray);
var contextTemplate=[
{label: '开机启动', type: 'checkbox', click:function(){
if(contextMenu.items[0].checked == true){
appLauncher.enable();
}else {
appLauncher.disable();
}
}},
{label: '打开', type: 'normal',click:function(){
var wins = BrowserWindow.getAllWindows();
for(var i in wins){
console.log(wins[i].webContents);
if(wins[i].webContents.getURL().indexOf('login.html') >= 0){
wins[i].show();
return;
}
if(wins[i].webContents.getURL().indexOf('main.html') >= 0){
wins[i].show();
return;
}
}
}},
{label: '退出', type: 'normal',click:function(){
app.quit();
}}
];
const contextMenu = Menu.buildFromTemplate(contextTemplate);
appLauncher.isEnabled().then(function(enabled){
contextMenu.items[0].checked = enabled;
});
tray.on('double-click', function(){
var wins = BrowserWindow.getAllWindows();
tray.setImage(__dirname + "/images/win_icon.ico");
for(var i in wins) {
console.log("#####" +wins.length + "#####" + wins[i].webContents.getURL());
if(wins[i].webContents.getURL().indexOf('login.html') >= 0){
wins[i].show();
return;
} if(wins[i].webContents.getURL().indexOf('main.html') >= 0) {
wins[i].show();
return;
}
}
});
tray.setToolTip('犀牛XNTALK');
tray.setContextMenu(contextMenu);
// 加载应用的 index.html
console.log(__dirname);
// 打开开发工具
if(global.isDebug == true){
/*frame Boolean - 指定 false 来创建一个 Frameless Window. 默认为 true。 是否有边框
* useContentSize 使用web网页size, 这意味着实际窗口的size应该包括窗口框架的size,
* center 居中
* resizable 改变窗口大小
* transparent : 背景透明
*
* 启动服务为了存储Cookie
* */
mainWindow = new BrowserWindow({frame: false, useContentSize:true,center:true, resizable:false, transparent:true,width:960, height:600});//
mainWindow.loadURL(("http://127.0.0.1:#port#/xnLogin/page/login.html").replace("#port#",global.port));
// mainWindow.loadURL('file://' + __dirname + '/index.html');
mainWindow.openDevTools();
} else {
mainWindow = new BrowserWindow({ frame: false, useContentSize:true,center:true, resizable:false, transparent:true,width:960, height:600});//
mainWindow.loadURL(("http://127.0.0.1:#port#/xnLogin/page/login.html").replace("#port#",global.port));
// mainWindow.maximize();
}
//设置菜单栏是否可见.如果菜单栏自动隐藏,用户仍然可以按下 Alt 键来显示.
mainWindow.setMenuBarVisibility(false);
//窗口居中.
mainWindow.center();
// 当 window 被关闭,这个事件会被发出
mainWindow.on('closed', function() {
// 取消引用 window 对象,如果你的应用支持多窗口的话,
// 通常会把多个 window 对象存放在一个数组里面,
// 但这次不是。
mainWindow = null;
});
// In main process.
//退出
ipcMain.on('xntalkMessage', function(event, arg1, arg2) {
if (process.platform != 'darwin'){
if(arg2 != undefined && arg2 != null && arg2 == "watch")
{
tray.setImage(nativeImageTray);
}
else
{
if(BrowserWindow.getFocusedWindow() == null || BrowserWindow.getFocusedWindow().webContents.getURL().indexOf('main.html') < 0){
tray.setImage(nativeImageTrayMsg);
}
}
}
});
});
// 当所有窗口被关闭了,退出。
app.on('window-all-closed', function() {
// 在 OS X 上,通常用户在明确地按下 Cmd + Q 之前
// 应用会保持活动状态
if (process.platform != 'darwin') {
app.quit();
}
});
app.on('browser-window-created', function(arg1, arg2){
if(arg2.webContents.getURL().indexOf('main.html')){
arg2.on('focus', function(){
tray.setImage(nativeImageTray);
});
}
});
} catch (e){
app.quit();
}