mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-06-13 11:14:37 +08:00
ipc call & example
This commit is contained in:
@@ -6,7 +6,7 @@ class ExampleController extends BaseController {
|
||||
|
||||
async openLocalDir() {
|
||||
const self = this;
|
||||
const { ctx } = this;
|
||||
const { service } = this;
|
||||
|
||||
const data = {
|
||||
title: 'example test'
|
||||
|
||||
@@ -11,18 +11,25 @@ class BaseService extends Service {
|
||||
if (!method) {
|
||||
return 'Method does not exist';
|
||||
}
|
||||
const res = {};
|
||||
let result = {
|
||||
code: 0,
|
||||
data: null
|
||||
};
|
||||
const port = this.service.storage.getElectronIPCPort();
|
||||
const url = 'localhost:' + port + '/send';
|
||||
|
||||
try {
|
||||
res = await request.post(url)
|
||||
.send({ cmd: method, data: data });
|
||||
console.log(res);
|
||||
const response = await request.post(url)
|
||||
.send({ cmd: method, data: data })
|
||||
.set('accept', 'json');
|
||||
|
||||
result = JSON.parse(response.text);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
ELog.error('[base] [ipcCall] request error:', err);
|
||||
result.code = -10;
|
||||
}
|
||||
|
||||
return res;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class ExampleService extends BaseService {
|
||||
async openLocalDir() {
|
||||
const self = this;
|
||||
|
||||
await self.ipcCall('example.getPath');
|
||||
await self.ipcCall('example.openDir');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class StorageService extends BaseService {
|
||||
* getElectronIPCPort
|
||||
*/
|
||||
getElectronIPCPort() {
|
||||
const key = storageKey.ELECTRON_IPC + 'port';
|
||||
const key = storageKey.ELECTRON_IPC + '.port';
|
||||
const port = this.instance()
|
||||
.get(key)
|
||||
.value();
|
||||
|
||||
@@ -8,17 +8,16 @@ const storage = require('./storage');
|
||||
const apis = {};
|
||||
|
||||
exports.setup = async function () {
|
||||
ELog.info('[api] [setup]');
|
||||
setApi();
|
||||
|
||||
// use api server
|
||||
let port = await storage.setIpcDynamicPort();
|
||||
console.log('api port:', port);
|
||||
ELog.info('[api] [setup] dynamic port:', port);
|
||||
const listen = 'localhost';
|
||||
port = port ? port : 7069;
|
||||
|
||||
const server = http.createServer(function(req, res) {
|
||||
ELog.info('[ api ] command received', { method: req.method, url: req.url });
|
||||
ELog.info('[ api ] [setup] command received', { method: req.method, url: req.url });
|
||||
if ((req.method === 'POST' && req.url === '/send')) {
|
||||
let body = '';
|
||||
req.setEncoding('utf8');
|
||||
@@ -35,7 +34,7 @@ exports.setup = async function () {
|
||||
return res.end('request body parse failure.');
|
||||
}
|
||||
if (!apis[message.cmd]) {
|
||||
ELog.info('[ api ] invalid command called:', message.cmd);
|
||||
ELog.info('[ api ] [setup] invalid command called:', message.cmd);
|
||||
res.statusCode = 404;
|
||||
return res.end('NG');
|
||||
}
|
||||
@@ -43,11 +42,11 @@ exports.setup = async function () {
|
||||
const start = Date.now();
|
||||
const data = apis[message.cmd]();
|
||||
const elapsed = Date.now() - start;
|
||||
ELog.info(`api [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
ELog.info(`[api] [setup] [${message.cmd}] success. elapsed: ${elapsed}ms`, data);
|
||||
res.statusCode = 200;
|
||||
const result = {
|
||||
code: 0,
|
||||
data,
|
||||
data: data,
|
||||
};
|
||||
res.end(JSON.stringify(result));
|
||||
});
|
||||
@@ -58,7 +57,7 @@ exports.setup = async function () {
|
||||
});
|
||||
|
||||
server.listen(port, listen, function() {
|
||||
ELog.info('[ api ] server is listening on', `${listen}:${port}`);
|
||||
ELog.info('[ api ] [setup] server is listening on', `${listen}:${port}`);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const {app} = require('electron');
|
||||
const path = require('path');
|
||||
const { app, shell } = require('electron');
|
||||
|
||||
exports.getPath = function () {
|
||||
const dir = app.getAppPath();
|
||||
@@ -9,11 +10,20 @@ exports.getPath = function () {
|
||||
return dir;
|
||||
}
|
||||
|
||||
exports.openDir = function () {
|
||||
const dir = app.getAppPath();
|
||||
ELog.info('dir:', dir);
|
||||
exports.openDir = function (dir = 'D:/www/xing/electron-egg') {
|
||||
if (!dir) {
|
||||
return false;
|
||||
}
|
||||
dir = getElectronPath(dir);
|
||||
shell.openItem(dir);
|
||||
|
||||
return dir;
|
||||
return true;
|
||||
}
|
||||
|
||||
exports = module.exports;
|
||||
function getElectronPath (filepath) {
|
||||
filepath = path.resolve(filepath);
|
||||
filepath=filepath.replace("resources","");
|
||||
filepath=filepath.replace("app.asar","");
|
||||
filepath = path.normalize(filepath);
|
||||
return filepath;
|
||||
};
|
||||
Reference in New Issue
Block a user