diff --git a/app/controller/v1/example.js b/app/controller/v1/example.js index 7760339..4299a59 100644 --- a/app/controller/v1/example.js +++ b/app/controller/v1/example.js @@ -1,6 +1,6 @@ 'use strict'; -const BaseController = require('../base'); +const BaseController = require('../base'); const os = require('os'); const fs = require('fs'); const path = require('path'); @@ -20,13 +20,13 @@ class ExampleController extends BaseController { break; case 'picture' : dir = os.userInfo().homedir + '/Pictures'; - break; + break; case 'doc' : dir = os.userInfo().homedir + '/Documents'; - break; + break; case 'music' : dir = os.userInfo().homedir + '/Music'; - break; + break; } await service.example.openLocalDir(dir); @@ -34,6 +34,15 @@ class ExampleController extends BaseController { self.sendSuccess(data); } + async executeJS() { + const self = this; + const { ctx, service } = this; + const body = ctx.request.body; + const str = body.str; + let data = await service.example.executeJS(str); + self.sendSuccess(data); + } + async uploadFile() { const self = this; const { ctx, service } = this; @@ -42,24 +51,24 @@ class ExampleController extends BaseController { // this.app.logger.info('file:', file); // try { - // let tmpFile = fs.readFileSync(file.filepath) + // let tmpFile = fs.readFileSync(file.filepath) // fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) // } finally { // await fs.unlink(file.filepath, function(){}); // } - // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) + // const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) // const uploadRes = await service.example.uploadFileToSMMS(fileStream); // } const file = ctx.request.files[0]; //this.app.logger.info('file:', file); try { - let tmpFile = fs.readFileSync(file.filepath) + let tmpFile = fs.readFileSync(file.filepath) fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile) } finally { await fs.unlink(file.filepath, function(){}); } - const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) + const fileStream = fs.createReadStream(path.join(tmpDir, file.filename)) const uploadRes = await service.example.uploadFileToSMMS(fileStream); self.sendData(uploadRes); diff --git a/app/router/example.js b/app/router/example.js index 0c6aab8..d2d1630 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -7,6 +7,8 @@ module.exports = app => { const { router, controller } = app; // open local dir router.post('/api/v1/example/openLocalDir', controller.v1.example.openLocalDir); + // executeJS + router.post('/api/v1/example/executeJS', controller.v1.example.executeJS); // upload file router.post('/api/v1/example/uploadFile', controller.v1.example.uploadFile); // get ws url diff --git a/app/service/example.js b/app/service/example.js index 71984b5..bd6b60f 100644 --- a/app/service/example.js +++ b/app/service/example.js @@ -11,6 +11,12 @@ class ExampleService extends BaseService { return true; } + async executeJS(str) { + const self = this; + let result = await self.ipcCall('example.executeJS', str); + return result; + } + async uploadFileToSMMS(tmpFile) { const res = { code: 1000, diff --git a/electron/apis/example.js b/electron/apis/example.js index 48824f3..c21eb48 100644 --- a/electron/apis/example.js +++ b/electron/apis/example.js @@ -1,11 +1,14 @@ 'use strict'; const path = require('path'); -const { app, shell } = require('electron'); +const { + app, + webContents, + shell +} = require('electron'); exports.getPath = function () { const dir = app.getAppPath(); - return dir; } @@ -15,14 +18,19 @@ exports.openDir = function (dir = '') { } dir = getElectronPath(dir); shell.openItem(dir); - return true; } -function getElectronPath (filepath) { +exports.executeJS = function (str) { + let jscode = `(()=>{alert('${str}');return 'fromJs:${str}';})()`; + console.log(jscode); + return webContents.fromId(1).executeJavaScript(jscode); +} + +function getElectronPath(filepath) { //filepath = path.resolve(filepath); - filepath=filepath.replace("resources",""); - filepath=filepath.replace("app.asar",""); + filepath = filepath.replace("resources", ""); + filepath = filepath.replace("app.asar", ""); filepath = path.normalize(filepath); return filepath; }; \ No newline at end of file diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index 4b79649..551b446 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -5,6 +5,7 @@ const mainApi = { outApi: '/api/v1/outApi', openDir: '/api/v1/example/openLocalDir', uploadFile: '/api/v1/example/uploadFile', + executeJS: '/api/v1/example/executeJS', autoLaunchEnable: '/api/v1/setting/autoLaunchEnable', autoLaunchDisable: '/api/v1/setting/autoLaunchDisable', autoLaunchIsEnabled: '/api/v1/setting/autoLaunchIsEnabled' @@ -52,4 +53,15 @@ export function uploadFile (parameter) { method: 'post', data: parameter }) +} + +/** + * executeJS + */ +export function executeJS (parameter) { + return request({ + url: mainApi.executeJS, + method: 'post', + data: parameter + }) } \ No newline at end of file diff --git a/frontend/src/views/example/Ipc.vue b/frontend/src/views/example/Ipc.vue index 1df2cba..908dd87 100644 --- a/frontend/src/views/example/Ipc.vue +++ b/frontend/src/views/example/Ipc.vue @@ -1,23 +1,39 @@