mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-24 08:58:09 +08:00
feat: demo-js v4
This commit is contained in:
65
electron/controller/cross.js
Normal file
65
electron/controller/cross.js
Normal file
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
const { crossService } = require('../service/cross');
|
||||
|
||||
/**
|
||||
* Cross
|
||||
* @class
|
||||
*/
|
||||
class CrossController {
|
||||
|
||||
/**
|
||||
* View process service information
|
||||
*/
|
||||
info() {
|
||||
crossService.info();
|
||||
return 'hello electron-egg';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get service url
|
||||
*/
|
||||
async getUrl(args) {
|
||||
const { name } = args;
|
||||
const serverUrl = crossService.getUrl(name);
|
||||
return serverUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
* kill service
|
||||
* By default (modifiable), killing the process will exit the electron application.
|
||||
*/
|
||||
async killServer(args) {
|
||||
const { type, name } = args;
|
||||
crossService.killServer(type, name);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* create service
|
||||
*/
|
||||
async createServer(args) {
|
||||
const { program } = args;
|
||||
if (program == 'go') {
|
||||
crossService.createGoServer();
|
||||
} else if (program == 'java') {
|
||||
crossService.createJavaServer();
|
||||
} else if (program == 'python') {
|
||||
crossService.createPythonServer();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Access the api for the cross service
|
||||
*/
|
||||
async requestApi(args) {
|
||||
const { name, urlPath, params} = args;
|
||||
const data = await crossService.requestApi(name, urlPath, params);
|
||||
return data;
|
||||
}
|
||||
}
|
||||
CrossController.toString = () => '[class CrossController]';
|
||||
|
||||
module.exports = CrossController;
|
||||
65
electron/controller/effect.js
Normal file
65
electron/controller/effect.js
Normal file
@@ -0,0 +1,65 @@
|
||||
'use strict';
|
||||
|
||||
const { dialog } = require('electron');
|
||||
const { getMainWindow } = require('ee-core/electron');
|
||||
|
||||
/**
|
||||
* effect - demo
|
||||
* @class
|
||||
*/
|
||||
class EffectController {
|
||||
|
||||
/**
|
||||
* select file
|
||||
*/
|
||||
selectFile() {
|
||||
const filePaths = dialog.showOpenDialogSync({
|
||||
properties: ['openFile']
|
||||
});
|
||||
|
||||
if (!filePaths) {
|
||||
return null
|
||||
}
|
||||
|
||||
return filePaths[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* login window
|
||||
*/
|
||||
loginWindow(args) {
|
||||
const { width, height } = args;
|
||||
const win = getMainWindow();
|
||||
|
||||
const size = {
|
||||
width: width || 400,
|
||||
height: height || 300
|
||||
}
|
||||
win.setSize(size.width, size.height);
|
||||
win.setResizable(true);
|
||||
win.center();
|
||||
win.show();
|
||||
win.focus();
|
||||
}
|
||||
|
||||
/**
|
||||
* restore window
|
||||
*/
|
||||
restoreWindow(args) {
|
||||
const { width, height } = args;
|
||||
const win = getMainWindow();
|
||||
|
||||
const size = {
|
||||
width: width || 980,
|
||||
height: height || 650
|
||||
}
|
||||
win.setSize(size.width, size.height);
|
||||
win.setResizable(true);
|
||||
win.center();
|
||||
win.show();
|
||||
win.focus();
|
||||
}
|
||||
}
|
||||
EffectController.toString = () => '[class EffectController]';
|
||||
|
||||
module.exports = EffectController;
|
||||
18
electron/controller/example.js
Normal file
18
electron/controller/example.js
Normal file
@@ -0,0 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
/**
|
||||
* example
|
||||
* @class
|
||||
*/
|
||||
class ExampleController {
|
||||
|
||||
/**
|
||||
* test
|
||||
*/
|
||||
async test () {
|
||||
return 'hello electron-egg';
|
||||
}
|
||||
}
|
||||
ExampleController.toString = () => '[class ExampleController]';
|
||||
|
||||
module.exports = ExampleController;
|
||||
270
electron/controller/framework.js
Normal file
270
electron/controller/framework.js
Normal file
@@ -0,0 +1,270 @@
|
||||
'use strict';
|
||||
|
||||
const dayjs = require('dayjs');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const { exec } = require('child_process');
|
||||
const { app: electronApp, shell } = require('electron');
|
||||
const { getExtraResourcesDir } = require('ee-core/ps');
|
||||
const { logger } = require('ee-core/log');
|
||||
const { getConfig } = require('ee-core/config');
|
||||
const { frameworkService } = require('../service/framework');
|
||||
const { sqlitedbService } = require('../service/database/sqlitedb');
|
||||
const { autoUpdaterService } = require('../service/os/auto_updater');
|
||||
|
||||
/**
|
||||
* framework - demo
|
||||
* @class
|
||||
*/
|
||||
class FrameworkController {
|
||||
|
||||
/**
|
||||
* 所有方法接收两个参数
|
||||
* @param args 前端传的参数
|
||||
* @param event - ipc通信时才有值。详情见:控制器文档
|
||||
*/
|
||||
|
||||
/**
|
||||
* sqlite数据库操作
|
||||
*/
|
||||
async sqlitedbOperation(args) {
|
||||
const { action, info, delete_name, update_name, update_age, search_age, data_dir } = args;
|
||||
|
||||
const data = {
|
||||
action,
|
||||
result: null,
|
||||
all_list: [],
|
||||
code: 0
|
||||
};
|
||||
|
||||
try {
|
||||
// test
|
||||
sqlitedbService.getDataDir();
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
data.code = -1;
|
||||
return data;
|
||||
}
|
||||
|
||||
switch (action) {
|
||||
case 'add' :
|
||||
data.result = await sqlitedbService.addTestDataSqlite(info);;
|
||||
break;
|
||||
case 'del' :
|
||||
data.result = await sqlitedbService.delTestDataSqlite(delete_name);;
|
||||
break;
|
||||
case 'update' :
|
||||
data.result = await sqlitedbService.updateTestDataSqlite(update_name, update_age);
|
||||
break;
|
||||
case 'get' :
|
||||
data.result = await sqlitedbService.getTestDataSqlite(search_age);
|
||||
break;
|
||||
case 'getDataDir' :
|
||||
data.result = await sqlitedbService.getDataDir();
|
||||
break;
|
||||
case 'setDataDir' :
|
||||
data.result = await sqlitedbService.setCustomDataDir(data_dir);
|
||||
break;
|
||||
}
|
||||
|
||||
data.all_list = await sqlitedbService.getAllTestDataSqlite();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用其它程序(exe、bash等可执行程序)
|
||||
*
|
||||
*/
|
||||
openSoftware(args) {
|
||||
const { softName } = args;
|
||||
const softwarePath = path.join(getExtraResourcesDir(), softName);
|
||||
logger.info('[openSoftware] softwarePath:', softwarePath);
|
||||
|
||||
// 检查程序是否存在
|
||||
if (!fs.existsSync(softwarePath)) {
|
||||
return false;
|
||||
}
|
||||
// 命令行字符串 并 执行, start 命令后面的路径要加双引号
|
||||
const cmdStr = `start "${softwarePath}"`;
|
||||
exec(cmdStr);
|
||||
|
||||
// 方法二
|
||||
// 使用cross模块
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检测http服务是否开启
|
||||
*/
|
||||
async checkHttpServer() {
|
||||
const { enable, protocol, host, port } = getConfig().httpServer;
|
||||
const url = protocol + host + ':' + port;
|
||||
console.log('[checkHttpServer] url:', url);
|
||||
const data = {
|
||||
enable: enable,
|
||||
server: url
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个 http 请求
|
||||
* args 是 前端传的参数
|
||||
* ctx 是 koa 的 ctx 对象
|
||||
*/
|
||||
async doHttpRequest(args, ctx) {
|
||||
const httpInfo = {
|
||||
args,
|
||||
method: ctx.request.method,
|
||||
query: ctx.request.query,
|
||||
body: ctx.request.body
|
||||
}
|
||||
logger.info('httpInfo:', httpInfo);
|
||||
|
||||
const { id } = args;
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
const dir = electronApp.getPath(id);
|
||||
shell.openPath(dir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 一个socket io请求访问此方法
|
||||
*/
|
||||
async doSocketRequest(args) {
|
||||
const { id } = args;
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
const dir = electronApp.getPath(id);
|
||||
shell.openPath(dir);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步消息类型
|
||||
*/
|
||||
async ipcInvokeMsg(args) {
|
||||
let timeNow = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
||||
const data = args + ' - ' + timeNow;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 同步消息类型
|
||||
*/
|
||||
async ipcSendSyncMsg(args) {
|
||||
let timeNow = dayjs().format('YYYY-MM-DD HH:mm:ss');
|
||||
const data = args + ' - ' + timeNow;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 双向异步通信
|
||||
*/
|
||||
ipcSendMsg(args, event) {
|
||||
const { type, content } = args;
|
||||
const data = frameworkService.bothWayMessage(type, content, event);
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 任务
|
||||
*/
|
||||
someJob(args, event) {
|
||||
const { jobId, action} = args;
|
||||
let result;
|
||||
|
||||
switch (action) {
|
||||
case 'create':
|
||||
result = frameworkService.doJob(jobId, action, event);
|
||||
break;
|
||||
case 'close':
|
||||
frameworkService.doJob(jobId, action, event);
|
||||
break;
|
||||
case 'pause':
|
||||
frameworkService.doJob(jobId, action, event);
|
||||
break;
|
||||
case 'resume':
|
||||
frameworkService.doJob(jobId, action, event);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
let data = {
|
||||
jobId,
|
||||
action,
|
||||
result
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建任务池
|
||||
*/
|
||||
async createPool(args, event) {
|
||||
let num = args.number;
|
||||
frameworkService.doCreatePool(num, event);
|
||||
|
||||
// test monitor
|
||||
frameworkService.monitorJob();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过进程池执行任务
|
||||
*/
|
||||
someJobByPool(args, event) {
|
||||
const { jobId, action } = args;
|
||||
let result;
|
||||
switch (action) {
|
||||
case 'run':
|
||||
result = frameworkService.doJobByPool(jobId, action, event);
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
let data = {
|
||||
jobId,
|
||||
action,
|
||||
result
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否有新版本
|
||||
*/
|
||||
checkForUpdater() {
|
||||
autoUpdaterService.checkUpdate();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载新版本
|
||||
*/
|
||||
downloadApp() {
|
||||
autoUpdaterService.download();
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试接口
|
||||
*/
|
||||
hello(args) {
|
||||
logger.info('hello ', args);
|
||||
}
|
||||
}
|
||||
FrameworkController.toString = () => '[class FrameworkController]';
|
||||
|
||||
module.exports = FrameworkController;
|
||||
175
electron/controller/os.js
Normal file
175
electron/controller/os.js
Normal file
@@ -0,0 +1,175 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const {
|
||||
app: electronApp, dialog, shell, Notification,
|
||||
} = require('electron');
|
||||
const { windowService } = require('../service/os/window');
|
||||
|
||||
/**
|
||||
* example
|
||||
* @class
|
||||
*/
|
||||
class OsController {
|
||||
|
||||
/**
|
||||
* All methods receive two parameters
|
||||
* @param args Parameters transmitted by the frontend
|
||||
* @param event - Event are only available during IPC communication. For details, please refer to the controller documentation
|
||||
*/
|
||||
|
||||
/**
|
||||
* Message prompt dialog box
|
||||
*/
|
||||
messageShow() {
|
||||
dialog.showMessageBoxSync({
|
||||
type: 'info', // "none", "info", "error", "question" 或者 "warning"
|
||||
title: 'Custom Title',
|
||||
message: 'Customize message content',
|
||||
detail: 'Other additional information'
|
||||
})
|
||||
|
||||
return 'Opened the message box';
|
||||
}
|
||||
|
||||
/**
|
||||
* Message prompt and confirmation dialog box
|
||||
*/
|
||||
messageShowConfirm() {
|
||||
const res = dialog.showMessageBoxSync({
|
||||
type: 'info',
|
||||
title: 'Custom Title',
|
||||
message: 'Customize message content',
|
||||
detail: 'Other additional information',
|
||||
cancelId: 1, // Index of buttons used to cancel dialog boxes
|
||||
defaultId: 0, // Set default selected button
|
||||
buttons: ['confirm', 'cancel'],
|
||||
})
|
||||
let data = (res === 0) ? 'click the confirm button' : 'click the cancel button';
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select Directory
|
||||
*/
|
||||
selectFolder() {
|
||||
const filePaths = dialog.showOpenDialogSync({
|
||||
properties: ['openDirectory', 'createDirectory']
|
||||
});
|
||||
|
||||
if (!filePaths) {
|
||||
return null
|
||||
}
|
||||
|
||||
return filePaths[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* open directory
|
||||
*/
|
||||
openDirectory(args) {
|
||||
const { id } = args;
|
||||
if (!id) {
|
||||
return false;
|
||||
}
|
||||
let dir = '';
|
||||
if (path.isAbsolute(id)) {
|
||||
dir = id;
|
||||
} else {
|
||||
dir = electronApp.getPath(id);
|
||||
}
|
||||
|
||||
shell.openPath(dir);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select Picture
|
||||
*/
|
||||
selectPic() {
|
||||
const filePaths = dialog.showOpenDialogSync({
|
||||
title: 'select pic',
|
||||
properties: ['openFile'],
|
||||
filters: [
|
||||
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
|
||||
]
|
||||
});
|
||||
if (!filePaths) {
|
||||
return null
|
||||
}
|
||||
|
||||
try {
|
||||
const data = fs.readFileSync(filePaths[0]);
|
||||
const pic = 'data:image/jpeg;base64,' + data.toString('base64');
|
||||
return pic;
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a new window
|
||||
*/
|
||||
createWindow(args) {
|
||||
const wcid = windowService.createWindow(args);
|
||||
return wcid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Window contents id
|
||||
*/
|
||||
getWCid(args) {
|
||||
const wcid = windowService.getWCid(args);
|
||||
return wcid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Realize communication between two windows through the transfer of the main process
|
||||
*/
|
||||
window1ToWindow2(args, event) {
|
||||
windowService.communicate(args, event);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Realize communication between two windows through the transfer of the main process
|
||||
*/
|
||||
window2ToWindow1(args, event) {
|
||||
windowService.communicate(args, event);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create system notifications
|
||||
*/
|
||||
sendNotification(args, event) {
|
||||
const { title, subtitle, body, silent} = args;
|
||||
|
||||
if (!Notification.isSupported()) {
|
||||
return '当前系统不支持通知';
|
||||
}
|
||||
|
||||
let options = {};
|
||||
if (!title) {
|
||||
options.title = title;
|
||||
}
|
||||
if (!subtitle) {
|
||||
options.subtitle = subtitle;
|
||||
}
|
||||
if (!body) {
|
||||
options.body = body;
|
||||
}
|
||||
if (!silent) {
|
||||
options.silent = silent;
|
||||
}
|
||||
windowService.createNotification(options, event);
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
OsController.toString = () => '[class OsController]';
|
||||
|
||||
module.exports = OsController;
|
||||
Reference in New Issue
Block a user