| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 'use strict';
- const BaseController = require('./base');
- const fs = require('fs');
- const path = require('path');
- const Utils = require('ee-core').Utils;
- 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 uploadFile() {
- const self = this;
- const { ctx, service } = this;
- let tmpDir = Utils.getLogDir();
- 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);
- }
- /**
- * json数据库操作
- */
- 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 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 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;
|