auto launch

This commit is contained in:
gaoshuaixing
2020-12-30 16:30:13 +08:00
parent aaa2bdb391
commit c8c0180b89
7 changed files with 96 additions and 34 deletions

View File

@@ -51,7 +51,7 @@ class ExampleController extends BaseController {
// const uploadRes = await service.example.uploadFileToSMMS(fileStream);
// }
const file = ctx.request.files[0];
this.app.logger.info('file:', file);
//this.app.logger.info('file:', file);
try {
let tmpFile = fs.readFileSync(file.filepath)

View File

@@ -5,25 +5,34 @@ const BaseController = require('../base');
class SettingController extends BaseController {
async autoLaunchEnable() {
const self = this;
const { ctx } = this;
const { service } = this;
const data = {};
const data = {
title: 'hello electron-egg'
};
await service.setting.autoLaunchEnable();
self.sendSuccess(data);
this.sendSuccess(data);
}
async autoLaunchDisable() {
const self = this;
const { ctx } = this;
const { service } = this;
const data = {};
await service.setting.autoLaunchDisable();
this.sendSuccess(data);
}
async autoLaunchIsEnabled() {
const { service } = this;
const data = {
title: 'hello'
isEnabled: null
};
self.sendSuccess(data);
const isEnabled = await service.setting.autoLaunchIsEnabled();
data.isEnabled = isEnabled;
this.sendSuccess(data);
}
}

View File

@@ -6,7 +6,9 @@
module.exports = app => {
const { router, controller } = app;
// open launch
router.get('/api/v1/setting/autoLaunchEnable', controller.v1.setting.autoLaunchEnable);
router.post('/api/v1/setting/autoLaunchEnable', controller.v1.setting.autoLaunchEnable);
// close launch
router.get('/api/v1/setting/autoLaunchDisable', controller.v1.setting.autoLaunchDisable);
router.post('/api/v1/setting/autoLaunchDisable', controller.v1.setting.autoLaunchDisable);
// is launch
router.post('/api/v1/setting/autoLaunchIsEnabled', controller.v1.setting.autoLaunchIsEnabled);
};

View File

@@ -4,7 +4,23 @@ const BaseService = require('./base');
class SettingService extends BaseService {
async autoLaunchEnable() {
const callResult = await this.ipcCall('base.autoLaunchEnable');
return callResult.data;
}
async autoLaunchDisable() {
const callResult = await this.ipcCall('base.autoLaunchDisable');
return callResult.data;
}
async autoLaunchIsEnabled() {
const callResult = await this.ipcCall('base.autoLaunchIsEnabled');
return callResult.data;
}
}
module.exports = SettingService;

21
electron/apis/base.js Normal file
View File

@@ -0,0 +1,21 @@
'use strict';
const AutoLaunchManager = require('../lib/AutoLaunch');
exports.autoLaunchEnable = function () {
const autoLaunchManager = new AutoLaunchManager();
const enable = autoLaunchManager.enable();
return enable;
}
exports.autoLaunchDisable = function () {
const autoLaunchManager = new AutoLaunchManager();
const disable = autoLaunchManager.disable();
return disable;
}
exports.autoLaunchIsEnabled = function () {
const autoLaunchManager = new AutoLaunchManager();
const isEnable = autoLaunchManager.isEnabled();
return isEnable;
}

View File

@@ -3,32 +3,46 @@ const { LOGIN_SETTING_OPTIONS } = require('./Constant').AutoLaunch;
class AutoLaunch {
enable () {
return new Promise((resolve, reject) => {
const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
if (enabled) {
resolve()
}
app.setLoginItemSettings({
...LOGIN_SETTING_OPTIONS,
openAtLogin: true
})
resolve()
const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin;
if (enabled) {
return true;
}
app.setLoginItemSettings({
...LOGIN_SETTING_OPTIONS,
openAtLogin: true
})
return true;
// return new Promise((resolve, reject) => {
// const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
// if (enabled) {
// resolve()
// }
// app.setLoginItemSettings({
// ...LOGIN_SETTING_OPTIONS,
// openAtLogin: true
// })
// resolve()
// })
}
disable () {
return new Promise((resolve, reject) => {
app.setLoginItemSettings({ openAtLogin: false })
resolve()
})
app.setLoginItemSettings({ openAtLogin: false })
return true;
// return new Promise((resolve, reject) => {
// app.setLoginItemSettings({ openAtLogin: false })
// resolve()
// })
}
isEnabled () {
return new Promise((resolve, reject) => {
const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
resolve(enabled)
})
const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin;
console.log('AutoLaunch isEnabled:', enabled);
return enabled;
// return new Promise((resolve, reject) => {
// const enabled = app.getLoginItemSettings(LOGIN_SETTING_OPTIONS).openAtLogin
// resolve(enabled)
// })
}
}

View File

@@ -1,6 +1,6 @@
module.exports = {
AutoLaunch: {
LOGIN_SETTING_OPTIONS = {
LOGIN_SETTING_OPTIONS: {
// For Windows
args: [
'--opened-at-login=1'