From 4f6fb41a7ad758e97218f9d5e1f2a1e7f20b1f49 Mon Sep 17 00:00:00 2001 From: gaoshuaixing Date: Mon, 21 Feb 2022 15:32:00 +0800 Subject: [PATCH] 1 --- app/controller/example.js | 59 -------------------------- app/router/example.js | 4 -- app/service/example.js | 21 --------- electron/controller/example.js | 20 +++++++-- frontend/src/api/main.js | 5 +-- frontend/src/views/base/file/Index.vue | 33 +++++--------- frontend/src/views/other/Index.vue | 4 +- 7 files changed, 31 insertions(+), 115 deletions(-) diff --git a/app/controller/example.js b/app/controller/example.js index 9981ea0..2991e13 100644 --- a/app/controller/example.js +++ b/app/controller/example.js @@ -1,7 +1,6 @@ 'use strict'; const BaseController = require('./base'); -const os = require('os'); const fs = require('fs'); const path = require('path'); const Utils = require('ee-core').Utils; @@ -36,33 +35,6 @@ class ExampleController extends BaseController { this.sendSuccess(data); } - async openLocalDir() { - const self = this; - const { ctx, service } = this; - const body = ctx.request.body; - const id = body.id; - const data = {}; - let dir = ''; - switch (id) { - case 'download' : - dir = os.userInfo().homedir + '/Downloads'; - break; - case 'picture' : - dir = os.userInfo().homedir + '/Pictures'; - break; - case 'doc' : - dir = os.userInfo().homedir + '/Documents'; - break; - case 'music' : - dir = os.userInfo().homedir + '/Music'; - break; - } - - await service.example.openLocalDir(dir); - - self.sendSuccess(data); - } - async executeJS() { const self = this; const { ctx, service } = this; @@ -115,22 +87,6 @@ class ExampleController extends BaseController { self.sendData(data); } - async setShortcut() { - const self = this; - const { ctx, service } = this; - const shortcutObj = ctx.request.body; - const data = {}; - - if (!shortcutObj['id'] || !shortcutObj['name'] || !shortcutObj['cmd']) { - self.sendFail({}, 'param error', 100); - return; - } - - await service.example.setShortcut(shortcutObj); - - self.sendSuccess(data); - } - async dbOperation() { const self = this; const { ctx, service } = this; @@ -257,21 +213,6 @@ class ExampleController extends BaseController { this.sendSuccess(data); } - /** - * 选择文件夹目录 - */ - async selectFileDir() { - const { service } = this; - - const data = { - dir: '' - }; - const dir = await service.example.selectDir(); - data.dir = dir; - - this.sendSuccess(data); - } - /** * 显示消息对话框 */ diff --git a/app/router/example.js b/app/router/example.js index 1a2aebe..964c633 100644 --- a/app/router/example.js +++ b/app/router/example.js @@ -19,8 +19,6 @@ module.exports = app => { router.post('/api/example/updateTestData', controller.example.updateTestData); // get test data router.post('/api/example/getTestData', controller.example.getTestData); - // set shortcut - router.post('/api/example/setShortcut', controller.example.setShortcut); // open launch router.post('/api/example/autoLaunchEnable', controller.example.autoLaunchEnable); // close launch @@ -29,8 +27,6 @@ module.exports = app => { router.post('/api/example/autoLaunchIsEnabled', controller.example.autoLaunchIsEnabled); // open software router.post('/api/example/openSoftware', controller.example.openSoftware); - // select file dir - router.post('/api/example/selectFileDir', controller.example.selectFileDir); // test some electron api router.post('/api/example/testElectronApi', controller.example.testElectronApi); // test2 diff --git a/app/service/example.js b/app/service/example.js index 841f7cf..eaf3828 100644 --- a/app/service/example.js +++ b/app/service/example.js @@ -12,23 +12,11 @@ class ExampleService extends Service { return null; } - async openLocalDir(dir) { - - await socketClient.call('controller.example.openDir', dir); - - return true; - } - async executeJS(str) { let result = await socketClient.call('controller.example.executeJS', str); return result; } - async setShortcut(shortcutStr) { - let result = await socketClient.call('controller.example.setShortcut', shortcutStr); - return result; - } - async uploadFileToSMMS(tmpFile) { const res = { code: 1000, @@ -90,15 +78,6 @@ class ExampleService extends Service { return callResult.data; } - async selectDir() { - const result = await socketClient.call('controller.example.selectDir'); - if (!result) { - return ''; - } - - return result; - } - async messageShow() { await socketClient.call('controller.example.messageShow'); diff --git a/electron/controller/example.js b/electron/controller/example.js index 1105154..b1869ff 100644 --- a/electron/controller/example.js +++ b/electron/controller/example.js @@ -2,7 +2,7 @@ const _ = require('lodash'); const Controller = require('ee-core').Controller; -const {app, dialog, BrowserWindow, BrowserView, Notification, powerMonitor, screen, nativeTheme} = require('electron'); +const {app, dialog, webContents, shell, BrowserWindow, BrowserView, Notification, powerMonitor, screen, nativeTheme} = require('electron'); /** * 示例控制器 @@ -80,17 +80,29 @@ class ExampleController extends Controller { /** * 选择目录 */ - selectDir () { + selectFolder () { const filePaths = dialog.showOpenDialogSync({ properties: ['openDirectory', 'createDirectory'] }); - console.log('[example] [selectDir] filePaths:', filePaths); + if (_.isEmpty(filePaths)) { return null } return filePaths[0]; - } + } + + /** + * 打开目录 + */ + openDirectory (args) { + if (!args.id) { + return false; + } + const dir = app.getPath(args.id); + shell.openPath(dir); + return true; + } } module.exports = ExampleController; diff --git a/frontend/src/api/main.js b/frontend/src/api/main.js index 0432093..8a37f87 100644 --- a/frontend/src/api/main.js +++ b/frontend/src/api/main.js @@ -1,15 +1,12 @@ import request from '@/utils/request' const eggApiroute = { - openDir: '/api/example/openLocalDir', uploadFile: '/api/example/uploadFile', executeJS: '/api/example/executeJS', - setShortcut: '/api/example/setShortcut', autoLaunchEnable: '/api/example/autoLaunchEnable', autoLaunchDisable: '/api/example/autoLaunchDisable', autoLaunchIsEnabled: '/api/example/autoLaunchIsEnabled', openSoftware: '/api/example/openSoftware', - selectFileDir: '/api/example/selectFileDir', messageShow: '/api/example/messageShow', messageShowConfirm: '/api/example/messageShowConfirm', dbOperation: '/api/example/dbOperation', @@ -19,6 +16,8 @@ const eggApiroute = { const ipcApiRoute = { messageShow: 'controller.example.messageShow', messageShowConfirm: 'controller.example.messageShowConfirm', + selectFolder: 'controller.example.selectFolder', + openDirectory: 'controller.example.openDirectory', } /** diff --git a/frontend/src/views/base/file/Index.vue b/frontend/src/views/base/file/Index.vue index 8b49991..aa264d1 100644 --- a/frontend/src/views/base/file/Index.vue +++ b/frontend/src/views/base/file/Index.vue @@ -87,15 +87,15 @@ import { requestEggApi, ipcApiRoute } from '@/api/main' const fileList = [ { content: '【下载】目录', - id: 'download' + id: 'downloads' }, { content: '【图片】目录', - id: 'picture' + id: 'pictures' }, { content: '【文档】目录', - id: 'doc' + id: 'documents' }, { content: '【音乐】目录', @@ -115,16 +115,9 @@ export default { }, methods: { openDirectry (id) { - const params = { - 'id': id - } - requestEggApi('openDir', params).then(res => { - if (res.code !== 0) { - return false - } - }).catch(err => { - console.log('err:', err) - }) + this.$ipcCallMain(ipcApiRoute.openDirectory, {id: id}).then(r => { + //console.log('r:', r) + }) }, uploadStatus(info) { const status = info.file.status; @@ -151,15 +144,11 @@ export default { } }, selectDir() { - requestEggApi('selectFileDir', {}).then(res => { - if (res.code !== 0) { - return false - } - console.log('res.data.dir:', res.data.dir) - this.dir_path = res.data.dir; - }).catch(err => { - this.$message.error('异常') - }) + const self = this; + self.$ipcCallMain(ipcApiRoute.selectFolder, '').then(r => { + self.dir_path = r; + self.$message.info(r); + }) }, messageShow(type) { const self = this; diff --git a/frontend/src/views/other/Index.vue b/frontend/src/views/other/Index.vue index 4c84d19..8c2fd95 100644 --- a/frontend/src/views/other/Index.vue +++ b/frontend/src/views/other/Index.vue @@ -10,7 +10,7 @@