| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- 'use strict';
- const BaseController = require('../base');
- const os = require('os');
- const fs = require('fs');
- const path = require('path');
- class ExampleController extends BaseController {
- /**
- * test electron api
- */
- async testElectronApi() {
- const { ctx, service } = this;
- const body = ctx.request.body;
- const id = body.id;
- const data = {};
- await service.example.testElectronApi(id);
- this.sendSuccess(data);
- }
- /**
- * test2
- */
- test2() {
- const { ctx, service } = this;
- const body = ctx.request.body;
- console.log('test2 params:', body);
- const data = {
- age: 32
- };
- 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;
- 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;
- let tmpDir = service.storage.getStorageDir();
- const file = ctx.request.files[0];
- try {
- 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 uploadRes = await service.example.uploadFileToSMMS(fileStream);
- self.sendData(uploadRes);
- }
- async uploadExtension() {
- const self = this;
- const { ctx, service } = this;
- const data = {};
- let tmpDir = service.storage.getStorageDir();
- const file = ctx.request.files[0];
- this.app.logger.info('file:', file);
- try {
- let tmpFile = fs.readFileSync(file.filepath)
- fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
- } finally {
- await fs.unlink(file.filepath, function(){});
- }
-
- const filePath = path.join(tmpDir, file.filename);
- await service.example.loadExtension(filePath);
- 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 getWsUrl() {
- const self = this;
- const { service } = this;
- const data = {};
- const addr = await service.socket.getWsUrl();
- data.url = addr;
- self.sendSuccess(data);
- }
- async dbOperation() {
- const self = this;
- const { ctx, service } = this;
- const paramsObj = ctx.request.body;
- const data = {
- action: paramsObj.action,
- result: null,
- all_list: []
- };
-
- switch (paramsObj.action) {
- case 'add' :
- data.result = await service.storage.addTestData(paramsObj.info);;
- break;
- case 'del' :
- data.result = await service.storage.delTestData(paramsObj.delete_name);;
- break;
- case 'update' :
- data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
- break;
- case 'get' :
- data.result = await service.storage.getTestData(paramsObj.search_age);
- break;
- }
- data.all_list = await service.storage.getAllTestData();
- self.sendSuccess(data);
- }
- async addTestData() {
- const self = this;
- const { service } = this;
- const data = {};
- const userInfo = {
- name: 'jame',
- age: 18,
- gender: 'man'
- }
- await service.storage.addTestData(userInfo);
- self.sendSuccess(data);
- }
- async delTestData() {
- const self = this;
- const { service } = this;
- const data = {};
- const name = 'jame';
- await service.storage.delTestData(name);
- self.sendSuccess(data);
- }
- async updateTestData() {
- const self = this;
- const { service } = this;
- const data = {};
- const name = 'jame';
- const age = 20;
- await service.storage.updateTestData(name, age);
- self.sendSuccess(data);
- }
- async getTestData() {
- const self = this;
- const { service } = this;
- const data = {};
- const name = 'jame';
- const user = await service.storage.getTestData(name);
- data.user = user;
- self.sendSuccess(data);
- }
- async autoLaunchEnable() {
- const { service } = this;
- await service.example.autoLaunchEnable();
- const data = {
- isEnabled: true
- };
- this.sendSuccess(data);
- }
-
- async autoLaunchDisable() {
- const { service } = this;
-
- await service.example.autoLaunchDisable();
- const data = {
- isEnabled: false
- };
- this.sendSuccess(data);
- }
- async autoLaunchIsEnabled() {
- const { service } = this;
- const data = {
- isEnabled: null
- };
- const isEnabled = await service.example.autoLaunchIsEnabled();
- data.isEnabled = isEnabled;
- this.sendSuccess(data);
- }
- /**
- * 调用其它程序
- */
- async openSoftware() {
- const { service } = this;
- const data = {};
- const openResult = await service.example.openSoftware('powershell.exe');
- if (!openResult) {
- this.sendFail({}, '程序不存在', 100);
- return;
- }
- this.sendSuccess(data);
- }
- /**
- * 选择文件夹目录
- */
- async selectFileDir() {
- const { service } = this;
- const data = {
- dir: ''
- };
- const dir = await service.example.selectDir();
- data.dir = dir;
- this.sendSuccess(data);
- }
- /**
- * 显示消息对话框
- */
- async messageShow() {
- const { service } = this;
- const data = {};
- await service.example.messageShow();
- this.sendSuccess(data);
- }
- /**
- * 显示消息对话框和确认
- */
- async messageShowConfirm() {
- const { service } = this;
- const data = {};
- await service.example.messageShowConfirm();
- this.sendSuccess(data);
- }
- }
- module.exports = ExampleController;
|