init项目

This commit is contained in:
2024-08-07 21:48:41 +08:00
parent 2c3a02d33b
commit 1581a5aaf5
18 changed files with 938 additions and 0 deletions

11
electron/README.md Normal file
View File

@@ -0,0 +1,11 @@
#### 基础配置
主进程的配置文件在./config文件夹下
```shell
# 说明
bin.js // 开发配置
config.default.js // 默认配置文件,开发环境和生产环境都会加载
config.local.js // 开发环境配置文件追加和覆盖default配置文件
config.prod.js // 生产环境配置文件追加和覆盖default配置文件
nodemon.json // 开发环境,代码(监控)热加载
builder.json // 打包配置
```

View File

@@ -0,0 +1,170 @@
const { app: electronApp } = require('electron');
const { autoUpdater } = require("electron-updater");
const is = require('ee-core/utils/is');
const Log = require('ee-core/log');
const Conf = require('ee-core/config');
const CoreWindow = require('ee-core/electron/window');
const Electron = require('ee-core/electron');
/**
* 自动升级插件
* @class
*/
class AutoUpdaterAddon {
constructor() {
}
/**
* 创建
*/
create () {
Log.info('[addon:autoUpdater] load');
const cfg = Conf.getValue('addons.autoUpdater');
if ((is.windows() && cfg.windows)
|| (is.macOS() && cfg.macOS)
|| (is.linux() && cfg.linux))
{
// continue
} else {
return
}
// 是否检查更新
if (cfg.force) {
this.checkUpdate();
}
const status = {
error: -1,
available: 1,
noAvailable: 2,
downloading: 3,
downloaded: 4,
}
const version = electronApp.getVersion();
Log.info('[addon:autoUpdater] current version: ', version);
// 设置下载服务器地址
let server = cfg.options.url;
let lastChar = server.substring(server.length - 1);
server = lastChar === '/' ? server : server + "/";
//Log.info('[addon:autoUpdater] server: ', server);
cfg.options.url = server;
// 是否后台自动下载
autoUpdater.autoDownload = cfg.force ? true : false;
try {
autoUpdater.setFeedURL(cfg.options);
} catch (error) {
Log.error('[addon:autoUpdater] setFeedURL error : ', error);
}
autoUpdater.on('checking-for-update', () => {
//sendStatusToWindow('正在检查更新...');
})
autoUpdater.on('update-available', (info) => {
info.status = status.available;
info.desc = '有可用更新';
this.sendStatusToWindow(info);
})
autoUpdater.on('update-not-available', (info) => {
info.status = status.noAvailable;
info.desc = '没有可用更新';
this.sendStatusToWindow(info);
})
autoUpdater.on('error', (err) => {
let info = {
status: status.error,
desc: err
}
this.sendStatusToWindow(info);
})
autoUpdater.on('download-progress', (progressObj) => {
let percentNumber = parseInt(progressObj.percent);
let totalSize = this.bytesChange(progressObj.total);
let transferredSize = this.bytesChange(progressObj.transferred);
let text = '已下载 ' + percentNumber + '%';
text = text + ' (' + transferredSize + "/" + totalSize + ')';
let info = {
status: status.downloading,
desc: text,
percentNumber: percentNumber,
totalSize: totalSize,
transferredSize: transferredSize
}
Log.info('[addon:autoUpdater] progress: ', text);
this.sendStatusToWindow(info);
})
autoUpdater.on('update-downloaded', (info) => {
info.status = status.downloaded;
info.desc = '下载完成';
this.sendStatusToWindow(info);
// 托盘插件默认会阻止窗口关闭,这里设置允许关闭窗口
Electron.extra.closeWindow = true;
autoUpdater.quitAndInstall();
// const mainWindow = CoreWindow.getMainWindow();
// if (mainWindow) {
// mainWindow.destroy()
// }
// electronApp.appQuit()
});
}
/**
* 检查更新
*/
checkUpdate () {
autoUpdater.checkForUpdates();
}
/**
* 下载更新
*/
download () {
autoUpdater.downloadUpdate();
}
/**
* 向前端发消息
*/
sendStatusToWindow(content = {}) {
const textJson = JSON.stringify(content);
const channel = 'app.updater';
const win = CoreWindow.getMainWindow();
win.webContents.send(channel, textJson);
}
/**
* 单位转换
*/
bytesChange (limit) {
let size = "";
if(limit < 0.1 * 1024){
size = limit.toFixed(2) + "B";
}else if(limit < 0.1 * 1024 * 1024){
size = (limit/1024).toFixed(2) + "KB";
}else if(limit < 0.1 * 1024 * 1024 * 1024){
size = (limit/(1024 * 1024)).toFixed(2) + "MB";
}else{
size = (limit/(1024 * 1024 * 1024)).toFixed(2) + "GB";
}
let sizeStr = size + "";
let index = sizeStr.indexOf(".");
let dou = sizeStr.substring(index + 1 , index + 3);
if(dou == "00"){
return sizeStr.substring(0, index) + sizeStr.substring(index + 3, index + 5);
}
return size;
}
}
AutoUpdaterAddon.toString = () => '[class AutoUpdaterAddon]';
module.exports = AutoUpdaterAddon;

View File

@@ -0,0 +1,67 @@
const { app: electronApp } = require('electron');
const Log = require('ee-core/log');
const Conf = require('ee-core/config');
/**
* 唤醒插件
* @class
*/
class AwakenAddon {
constructor() {
this.protocol = '';
}
/**
* 创建
*/
create () {
Log.info('[addon:awaken] load');
const cfg = Conf.getValue('addons.awaken');
this.protocol = cfg.protocol;
electronApp.setAsDefaultProtocolClient(this.protocol);
this.handleArgv(process.argv);
electronApp.on('second-instance', (event, argv) => {
if (process.platform === 'win32') {
this.handleArgv(argv)
}
})
// 仅用于macOS
electronApp.on('open-url', (event, urlStr) => {
this.handleUrl(urlStr)
})
}
/**
* 参数处理
*/
handleArgv(argv) {
const offset = electronApp.isPackaged ? 1 : 2;
const url = argv.find((arg, i) => i >= offset && arg.startsWith(this.protocol));
this.handleUrl(url)
}
/**
* url解析
*/
handleUrl(awakeUrlStr) {
if (!awakeUrlStr || awakeUrlStr.length === 0) {
return
}
const {hostname, pathname, search} = new URL(awakeUrlStr);
let awakeUrlInfo = {
urlStr: awakeUrlStr,
urlHost: hostname,
urlPath: pathname,
urlParams: search && search.slice(1)
}
Log.info('[addon:awaken] awakeUrlInfo:', awakeUrlInfo);
}
}
AwakenAddon.toString = () => '[class AwakenAddon]';
module.exports = AwakenAddon;

View File

@@ -0,0 +1,94 @@
const { app, session } = require('electron');
const _ = require('lodash');
const fs = require('fs');
const path = require('path');
const Log = require('ee-core/log');
/**
* 扩展插件 electron自身对该功能并不完全支持官方也不建议使用
* @class
*/
class ChromeExtensionAddon {
constructor() {
}
/**
* 创建
*/
async create () {
Log.info('[addon:chromeExtension] load');
const extensionIds = this.getAllIds();
for (let i = 0; i < extensionIds.length; i++) {
await this.load(extensionIds[i]);
}
}
/**
* 获取扩展id列表crx解压后的目录名即是该扩展的id
*/
getAllIds () {
const extendsionDir = this.getDirectory();
const ids = this.getDirs(extendsionDir);
return ids;
}
/**
* 扩展所在目录
*/
getDirectory () {
let extensionDirPath = '';
let variablePath = 'build'; // 打包前路径
if (app.isPackaged) {
variablePath = '..'; // 打包后路径
}
extensionDirPath = path.join(app.getAppPath(), variablePath, "extraResources", "chromeExtension");
return extensionDirPath;
}
/**
* 加载扩展
*/
async load (extensionId = '') {
if (_.isEmpty(extensionId)) {
return false
}
try {
const extensionPath = path.join(this.getDirectory(), extensionId);
Log.info('[addon:chromeExtension] extensionPath:', extensionPath);
await session.defaultSession.loadExtension(extensionPath, { allowFileAccess: true });
} catch (e) {
Log.info('[addon:chromeExtension] load extension error extensionId:%s, errorInfo:%s', extensionId, e.toString());
return false
}
return true
}
/**
* 获取目录下所有文件夹
*/
getDirs(dir) {
if (!dir) {
return [];
}
const components = [];
const files = fs.readdirSync(dir);
files.forEach(function(item, index) {
const stat = fs.lstatSync(dir + '/' + item);
if (stat.isDirectory() === true) {
components.push(item);
}
});
return components;
};
}
ChromeExtensionAddon.toString = () => '[class ChromeExtensionAddon]';
module.exports = ChromeExtensionAddon;

View File

@@ -0,0 +1,33 @@
const Log = require('ee-core/log');
const EE = require('ee-core/ee');
/**
* 安全插件
* @class
*/
class SecurityAddon {
constructor() {
}
/**
* 创建
*/
create () {
Log.info('[addon:security] load');
const { CoreApp } = EE;
const runWithDebug = process.argv.find(function(e){
let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port");
return isHasDebug;
})
// 不允许远程调试
if (runWithDebug) {
Log.error('[error] Remote debugging is not allowed, runWithDebug:', runWithDebug);
CoreApp.appQuit();
}
}
}
SecurityAddon.toString = () => '[class SecurityAddon]';
module.exports = SecurityAddon;

View File

@@ -0,0 +1,72 @@
const { Tray, Menu } = require('electron');
const path = require('path');
const Ps = require('ee-core/ps');
const Log = require('ee-core/log');
const Electron = require('ee-core/electron');
const CoreWindow = require('ee-core/electron/window');
const Conf = require('ee-core/config');
const EE = require('ee-core/ee');
/**
* 托盘插件
* @class
*/
class TrayAddon {
constructor() {
this.tray = null;
}
/**
* 创建托盘
*/
create () {
// 开发环境,代码热更新开启时,会导致托盘中有残影
if (Ps.isDev() && Ps.isHotReload()) return;
Log.info('[addon:tray] load');
const { CoreApp } = EE;
const cfg = Conf.getValue('addons.tray');
const mainWindow = CoreWindow.getMainWindow();
// 托盘图标
let iconPath = path.join(Ps.getHomeDir(), cfg.icon);
// 托盘菜单功能列表
let trayMenuTemplate = [
{
label: '显示',
click: function () {
mainWindow.show();
}
},
{
label: '退出',
click: function () {
CoreApp.appQuit();
}
}
]
// 点击关闭,最小化到托盘
mainWindow.on('close', (event) => {
if (Electron.extra.closeWindow == true) {
return;
}
mainWindow.hide();
event.preventDefault();
});
// 实例化托盘
this.tray = new Tray(iconPath);
this.tray.setToolTip(cfg.title);
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
this.tray.setContextMenu(contextMenu);
this.tray.on('double-click', () => {
mainWindow.show()
})
}
}
TrayAddon.toString = () => '[class TrayAddon]';
module.exports = TrayAddon;

View File

@@ -0,0 +1,60 @@
{
"productName": "pqs9100",
"appId": "com.njcn.pqs9100",
"copyright": "hongawen.com",
"directories": {
"output": "out"
},
"asar": true,
"files": [
"**/*",
"!frontend/",
"!run/",
"!logs/",
"!data/"
],
"extraResources": {
"from": "build/extraResources/",
"to": "extraResources"
},
"nsis": {
"oneClick": false,
"allowElevation": true,
"allowToChangeInstallationDirectory": true,
"installerIcon": "build/icons/icon.ico",
"uninstallerIcon": "build/icons/icon.ico",
"installerHeaderIcon": "build/icons/icon.ico",
"createDesktopShortcut": true,
"createStartMenuShortcut": true,
"shortcutName": "自动检测平台"
},
"publish": [
{
"provider": "generic",
"url": "http://www.shining-electric.com/"
}
],
"mac": {
"icon": "build/icons/icon.icns",
"artifactName": "${productName}-${os}-${version}-${arch}.${ext}",
"darkModeSupport": true,
"hardenedRuntime": false
},
"win": {
"icon": "build/icons/icon.ico",
"artifactName": "${productName}-${os}-${version}-${arch}.${ext}",
"target": [
{
"target": "nsis"
}
]
},
"linux": {
"icon": "build/icons/icon.icns",
"artifactName": "${productName}-${os}-${version}-${arch}.${ext}",
"target": [
"deb"
],
"category": "Utility"
}
}

View File

@@ -0,0 +1,185 @@
'use strict';
const path = require('path');
/**
* 默认配置
*/
module.exports = (appInfo) => {
const config = {};
/**
* 开发者工具
*/
config.openDevTools = false;
/**
* 应用程序顶部菜单
*/
config.openAppMenu = true;
/**
* 1.507
* 主窗口
*/
config.windowsOption = {
title: '自动检测平台',
width: 1280,
height: 850,
minWidth: 1280,
minHeight: 850,
webPreferences: {
//webSecurity: false,
contextIsolation: false, // false -> 可在渲染进程中使用electron的apitrue->需要bridge.js(contextBridge)
nodeIntegration: true,
//preload: path.join(appInfo.baseDir, 'preload', 'bridge.js'),
},
frame: true,
show: false,
icon: path.join(appInfo.home, 'public', 'images', 'logo-32.png'),
};
/**
* ee框架日志
*/
config.logger = {
encoding: 'utf8',
level: 'INFO',
outputJSON: false,
buffer: true,
enablePerformanceTimer: false,
rotator: 'day',
appLogName: 'pqs9100.log',
coreLogName: 'pqs9100-core.log',
errorLogName: 'pqs9100-error.log'
}
/**
* 远程模式-web地址
*/
config.remoteUrl = {
enable: false,
url: 'http://electron-egg.kaka996.com/'
};
/**
* 内置socket服务
*/
config.socketServer = {
enable: false, // 是否开启
port: 7070,// 默认端口
path: "/socket.io/", // 默认路径名称
connectTimeout: 45000, // 客户端连接超时时间
pingTimeout: 30000, // 心跳检测超时时间
pingInterval: 25000, // 心跳检测间隔时间
maxHttpBufferSize: 1e8, // 每条消息的数据最大值
transports: ["polling", "websocket"], // http轮询和websocket
cors: {
origin: true, // http协议时需要设置允许跨域
},
channel: 'c1' // 默认频道c1可以自定义
};
/**
* 内置http服务
*/
config.httpServer = {
enable: false,
https: {
enable: false,
key: '/public/ssl/localhost+1.key',
cert: '/public/ssl/localhost+1.pem'
},
host: '127.0.0.1',
port: 7071,
cors: {
origin: "*" // 默认允许跨域
},
body: {
multipart: true,
formidable: {
keepExtensions: true
}
},
filterRequest: {
uris: [
'favicon.ico' // 默认过滤的uri favicon.ico
],
returnData: ''
}
};
/**
* 主进程
*/
config.mainServer = {
protocol: 'file://',
indexPath: '/public/dist/index.html',
};
/**
* 硬件加速
*/
config.hardGpu = {
enable: true
};
/**
* 异常捕获
*/
config.exception = {
mainExit: false, // 主进程退出时是否捕获异常
childExit: true,
rendererExit: true,
};
/**
* jobs
*/
config.jobs = {
messageLog: true // 是否打印进程间通信的消息log
};
/**
* 插件功能
* @param window 官方内置插件
* @param tray 托盘插件
* @param security 安全插件
* @param awaken 唤醒插件
* @param autoUpdater 自动升级插件
*/
config.addons = {
window: {
enable: true,
},
tray: {
enable: true,
title: '自动检测平台',
icon: '/public/images/tray.png'
},
security: {
enable: true,
},
awaken: {
enable: true,
protocol: 'ee',
args: []
},
autoUpdater: {
enable: true,
windows: false,
macOS: false,
linux: false,
options: {
provider: 'generic',
url: 'http://kodo.qiniu.com/'
},
force: false,
}
};
return {
...config
};
}

View File

@@ -0,0 +1,31 @@
'use strict';
/**
* 开发环境配置,覆盖 config.default.js
*/
module.exports = (appInfo) => {
const config = {};
/**
* 开发者工具
*/
config.openDevTools = {
mode: 'undocked'
};
/**
* 应用程序顶部菜单
*/
config.openAppMenu = true;
/**
* jobs
*/
config.jobs = {
messageLog: true
};
return {
...config
};
};

View File

@@ -0,0 +1,29 @@
'use strict';
/**
* 生产环境配置,覆盖 config.default.js
*/
module.exports = (appInfo) => {
const config = {};
/**
* 开发者工具
*/
config.openDevTools = false;
/**
* 应用程序顶部菜单
*/
config.openAppMenu = false;
/**
* jobs
*/
config.jobs = {
messageLog: false
};
return {
...config
};
};

View File

@@ -0,0 +1,13 @@
{
"watch": [
"electron/",
"main.js"
],
"ignore": [],
"ext": "js,json",
"verbose": true,
"exec": "electron . --env=local --hot-reload=1",
"restartable": "hr",
"colours": true,
"events": {}
}

View File

@@ -0,0 +1,36 @@
'use strict';
const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
/**
* example
* @class
*/
class ExampleController extends Controller {
constructor(ctx) {
super(ctx);
}
/**
* 所有方法接收两个参数
* @param args 前端传的参数
* @param event - ipc通信时才有值。详情见控制器文档
*/
/**
* test
*/
async test () {
const result = await Services.get('example').test('electron');
Log.info('service result:', result);
return 'hello electron-egg';
}
}
ExampleController.toString = () => '[class ExampleController]';
module.exports = ExampleController;

50
electron/index.js Normal file
View File

@@ -0,0 +1,50 @@
const { Application } = require('ee-core');
class Index extends Application {
constructor() {
super();
// this === eeApp;
}
/**
* core app have been loaded
*/
async ready () {
// do some things
}
/**
* electron app ready
*/
async electronAppReady () {
// do some things
}
/**
* main window have been loaded
*/
async windowReady () {
// do some things
// 延迟加载,无白屏
const winOpt = this.config.windowsOption;
if (winOpt.show == false) {
const win = this.electron.mainWindow;
win.once('ready-to-show', () => {
win.show();
win.focus();
})
}
}
/**
* before app close
*/
async beforeClose () {
// do some things
}
}
Index.toString = () => '[class Index]';
module.exports = Index;

View File

@@ -0,0 +1,5 @@
const Log = require('ee-core/log');
exports.welcome = function () {
Log.info('[child-process] [jobs/example/hello] welcome ! ');
}

View File

@@ -0,0 +1,29 @@
const Job = require('ee-core/jobs/baseJobClass');
const Log = require('ee-core/log');
const Ps = require('ee-core/ps');
/**
* example - TimerJob
* @class
*/
class TimerJob extends Job {
constructor(params) {
super();
this.params = params;
}
/**
* handle()方法是必要的,且会被自动调用
*/
async handle () {
Log.info("[child-process] TimerJob params: ", this.params);
if (Ps.isChildJob()) {
Ps.exit();
}
}
}
TimerJob.toString = () => '[class TimerJob]';
module.exports = TimerJob;

View File

@@ -0,0 +1,10 @@
/*
* 如果启用了上下文隔离渲染进程无法使用electron的api
* 可通过contextBridge 导出api给渲染进程使用
*/
const { contextBridge, ipcRenderer } = require('electron')
contextBridge.exposeInMainWorld('electron', {
ipcRenderer: ipcRenderer,
})

14
electron/preload/index.js Normal file
View File

@@ -0,0 +1,14 @@
/*************************************************
** preload为预加载模块该文件将会在程序启动时加载 **
*************************************************/
const Addon = require('ee-core/addon');
/**
* 预加载模块入口
*/
module.exports = async () => {
// 示例功能模块,可选择性使用和修改
Addon.get('tray').create();
Addon.get('security').create();
}

View File

@@ -0,0 +1,29 @@
'use strict';
const { Service } = require('ee-core');
/**
* 示例服务service层为单例
* @class
*/
class ExampleService extends Service {
constructor(ctx) {
super(ctx);
}
/**
* test
*/
async test(args) {
let obj = {
status:'ok',
params: args
}
return obj;
}
}
ExampleService.toString = () => '[class ExampleService]';
module.exports = ExampleService;