浏览代码

auto launch

gaoshuaixing 5 年之前
父节点
当前提交
c8c0180b89

+ 1 - 1
app/controller/v1/example.js

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

+ 19 - 10
app/controller/v1/setting.js

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

+ 4 - 2
app/router/setting.js

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

+ 17 - 1
app/service/setting.js

@@ -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 - 0
electron/apis/base.js

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

+ 33 - 19
electron/lib/AutoLaunch.js

@@ -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)
+    // })
   }
 }
 

+ 1 - 1
electron/lib/Constant.js

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