This commit is contained in:
gaoshuaixing
2022-02-21 15:32:00 +08:00
parent 6a21ef01e7
commit 4f6fb41a7a
7 changed files with 31 additions and 115 deletions

View File

@@ -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);
}
/**
* 显示消息对话框
*/

View File

@@ -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

View File

@@ -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');

View File

@@ -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;

View File

@@ -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',
}
/**

View File

@@ -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;

View File

@@ -10,7 +10,7 @@
</div>
</template>
<script>
import { localApi } from '@/api/main'
import { requestEggApi } from '@/api/main'
export default {
data() {
@@ -19,7 +19,7 @@ export default {
methods: {
test () {
const params = {}
localApi('openDir', params).then(res => {
requestEggApi('openDir', params).then(res => {
if (res.code !== 0) {
return false
}