mirror of
https://gitee.com/dromara/electron-egg.git
synced 2026-05-14 11:52:07 +08:00
优化代码
This commit is contained in:
@@ -456,12 +456,11 @@ class ExampleController extends Controller {
|
||||
* 检查是否有新版本
|
||||
*/
|
||||
checkForUpdater () {
|
||||
// const updateConfig = config.get('autoUpdate');
|
||||
// if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
|
||||
// || (is.linux() && updateConfig.linux)) {
|
||||
// const autoUpdater = require('../lib/autoUpdater');
|
||||
// autoUpdater.checkUpdate();
|
||||
// }
|
||||
const config = this.app.config.autoUpdate;
|
||||
if ( (is.windows() && config.windows) || (is.macOS() && config.macOS) || (is.linux() && config.linux) ) {
|
||||
const autoUpdater = require('../library/autoUpdater');
|
||||
autoUpdater.checkUpdate();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@@ -470,13 +469,11 @@ class ExampleController extends Controller {
|
||||
* 下载新版本
|
||||
*/
|
||||
downloadApp () {
|
||||
// const updateConfig = config.get('autoUpdate');
|
||||
// if ((is.windows() && updateConfig.windows) || (is.macOS() && updateConfig.macOS)
|
||||
// || (is.linux() && updateConfig.linux)) {
|
||||
// const autoUpdater = require('../lib/autoUpdater');
|
||||
// autoUpdater.download();
|
||||
// }
|
||||
|
||||
const config = this.app.config.autoUpdate;
|
||||
if ( (is.windows() && config.windows) || (is.macOS() && config.macOS) || (is.linux() && config.linux) ) {
|
||||
const autoUpdater = require('../library/autoUpdater');
|
||||
autoUpdater.download();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,10 +28,12 @@ const ipcApiRoute = {
|
||||
autoLaunch: 'controller.example.autoLaunch',
|
||||
setTheme: 'controller.example.setTheme',
|
||||
getTheme: 'controller.example.getTheme',
|
||||
checkForUpdater: 'controller.example.checkForUpdater',
|
||||
downloadApp: 'controller.example.downloadApp',
|
||||
}
|
||||
|
||||
const specialIpcRoute = {
|
||||
appUpdater: 'app.updater'
|
||||
appUpdater: 'app.updater' // 此频道在后端也有相同定义
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,12 +6,12 @@ const { ipcRenderer: ipc } = window.require && window.require('electron') || {}
|
||||
* @param param
|
||||
* @returns {Promise<unknown>}
|
||||
*/
|
||||
const callMain = (ipc, channel, param) => {
|
||||
const call = (ipc, channel, param) => {
|
||||
return new Promise((resolve) => {
|
||||
// 声明渲染进程函数, 用于主进程函数回调, 返回数据
|
||||
// 调用主进程函数
|
||||
ipc.once(channel, (event, result) => {
|
||||
console.log('[ipcRenderer] [callMain] result:', result)
|
||||
console.log('[ipcRenderer] [call] result:', result)
|
||||
resolve(result)
|
||||
})
|
||||
ipc.send(channel, param)
|
||||
@@ -21,6 +21,6 @@ const callMain = (ipc, channel, param) => {
|
||||
export default {
|
||||
install(Vue) {
|
||||
Vue.prototype.$ipc = ipc // 全局注入ipc
|
||||
Vue.prototype.$ipcCallMain = (channel, param) => callMain(ipc, channel, param) // 全局注入调用主进程函数的方法
|
||||
Vue.prototype.$ipcCall = (channel, param) => call(ipc, channel, param) // 全局注入调用主进程函数的方法
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
openDirectry (id) {
|
||||
this.$ipcCallMain(ipcApiRoute.openDirectory, {id: id}).then(r => {
|
||||
this.$ipcCall(ipcApiRoute.openDirectory, {id: id}).then(r => {
|
||||
//console.log('r:', r)
|
||||
})
|
||||
},
|
||||
@@ -145,7 +145,7 @@ export default {
|
||||
},
|
||||
selectDir() {
|
||||
const self = this;
|
||||
self.$ipcCallMain(ipcApiRoute.selectFolder, '').then(r => {
|
||||
self.$ipcCall(ipcApiRoute.selectFolder, '').then(r => {
|
||||
self.dir_path = r;
|
||||
self.$message.info(r);
|
||||
})
|
||||
@@ -163,7 +163,7 @@ export default {
|
||||
self.$message.error(err + '异常')
|
||||
})
|
||||
} else {
|
||||
self.$ipcCallMain(ipcApiRoute.messageShow, '').then(r => {
|
||||
self.$ipcCall(ipcApiRoute.messageShow, '').then(r => {
|
||||
self.$message.info(r);
|
||||
})
|
||||
}
|
||||
@@ -181,7 +181,7 @@ export default {
|
||||
self.$message.error(err + '异常')
|
||||
})
|
||||
} else {
|
||||
self.$ipcCallMain(ipcApiRoute.messageShowConfirm, '').then(r => {
|
||||
self.$ipcCall(ipcApiRoute.messageShowConfirm, '').then(r => {
|
||||
self.$message.info(r);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -60,7 +60,9 @@ export default {
|
||||
methods: {
|
||||
init () {
|
||||
const self = this;
|
||||
self.$ipc.on(ipcApiRoute.sendNotification, (event, result) => {
|
||||
// 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
|
||||
this.$ipc.removeAllListeners(ipcApiRoute.sendNotification);
|
||||
this.$ipc.on(ipcApiRoute.sendNotification, (event, result) => {
|
||||
if (Object.prototype.toString.call(result) == '[object Object]') {
|
||||
self.$message.info(result.msg);
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ export default {
|
||||
methods: {
|
||||
init () {
|
||||
const self = this;
|
||||
this.$ipc.removeAllListeners(ipcApiRoute.initPowerMonitor);
|
||||
self.$ipc.on(ipcApiRoute.initPowerMonitor, (event, result) => {
|
||||
if (Object.prototype.toString.call(result) == '[object Object]') {
|
||||
self.currentStatus = result.msg;
|
||||
|
||||
@@ -36,18 +36,14 @@ export default {
|
||||
};
|
||||
},
|
||||
mounted () {
|
||||
this.init();
|
||||
},
|
||||
methods: {
|
||||
init () {
|
||||
getScreen (index) {
|
||||
const self = this;
|
||||
self.$ipc.on(ipcApiRoute.getScreen, (event, result) => {
|
||||
this.$ipcCall(ipcApiRoute.getScreen, index).then(result => {
|
||||
self.data = result;
|
||||
})
|
||||
},
|
||||
getScreen (index) {
|
||||
this.$ipc.send(ipcApiRoute.getScreen, index);
|
||||
},
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
@@ -59,23 +59,27 @@ export default {
|
||||
methods: {
|
||||
init () {
|
||||
const self = this;
|
||||
self.$ipc.on(ipcApiRoute.socketMessageStart, (event, result) => {
|
||||
// 避免重复监听,或者将 on 功能写到一个统一的地方,只加载一次
|
||||
this.$ipc.removeAllListeners(ipcApiRoute.socketMessageStart);
|
||||
this.$ipc.removeAllListeners(ipcApiRoute.socketMessageStop);
|
||||
|
||||
this.$ipc.on(ipcApiRoute.socketMessageStart, (event, result) => {
|
||||
console.log('[ipcRenderer] [socketMsgStart] result:', result)
|
||||
self.socketMessageString = result;
|
||||
})
|
||||
self.$ipc.on(ipcApiRoute.socketMessageStop, (event, result) => {
|
||||
this.$ipc.on(ipcApiRoute.socketMessageStop, (event, result) => {
|
||||
console.log('[ipcRenderer] [socketMsgStop] result:', result)
|
||||
self.socketMessageString = result;
|
||||
})
|
||||
},
|
||||
helloHandle(value) {
|
||||
const self = this;
|
||||
this.$ipcCallMain(ipcApiRoute.hello, value).then(r => {
|
||||
this.$ipcCall(ipcApiRoute.hello, value).then(r => {
|
||||
self.$message.info(r);
|
||||
})
|
||||
},
|
||||
executeJSHandle(value) {
|
||||
this.$ipcCallMain(ipcApiRoute.executeJS, value).then(r => {
|
||||
this.$ipcCall(ipcApiRoute.executeJS, value).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
|
||||
@@ -39,13 +39,12 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
openSoft (id) {
|
||||
const self = this;
|
||||
this.$ipc.on(ipcApiRoute.openSoftware, (event, result) => {
|
||||
const self = this;
|
||||
this.$ipcCall(ipcApiRoute.openSoftware, id).then(result => {
|
||||
if (!result) {
|
||||
self.$message.error('程序不存在');
|
||||
}
|
||||
})
|
||||
this.$ipc.send(ipcApiRoute.openSoftware, id);
|
||||
})
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@@ -41,7 +41,7 @@ export default {
|
||||
// this.autoLaunchChecked = result.status;
|
||||
// })
|
||||
// this.$ipc.send(ipcApiRoute.autoLaunch, 'check');
|
||||
self.$ipcCallMain(ipcApiRoute.autoLaunch, 'check').then(result => {
|
||||
self.$ipcCall(ipcApiRoute.autoLaunch, 'check').then(result => {
|
||||
console.log('[ipcRenderer] [autoLaunch] result:', result)
|
||||
this.autoLaunchChecked = result.status;
|
||||
console.log('[ipcRenderer] [autoLaunch] result2:', self.autoLaunchChecked)
|
||||
@@ -54,7 +54,7 @@ export default {
|
||||
// } else {
|
||||
// this.$ipc.send(ipcApiRoute.autoLaunch, 'open');
|
||||
// }
|
||||
// self.$ipcCallMain(ipcApiRoute.selectFolder, '').then(r => {
|
||||
// self.$ipcCall(ipcApiRoute.selectFolder, '').then(r => {
|
||||
// self.dir_path = r;
|
||||
// self.$message.info(r);
|
||||
// })
|
||||
|
||||
@@ -46,28 +46,24 @@ export default {
|
||||
};
|
||||
},
|
||||
mounted () {
|
||||
this.init();
|
||||
},
|
||||
methods: {
|
||||
init () {
|
||||
const self = this;
|
||||
this.$ipc.on(ipcApiRoute.setTheme, (event, result) => {
|
||||
console.log('result:', result)
|
||||
self.currentThemeMode = result;
|
||||
})
|
||||
|
||||
this.$ipc.on(ipcApiRoute.getTheme, (event, result) => {
|
||||
console.log('result:', result)
|
||||
self.currentThemeMode = result;
|
||||
})
|
||||
},
|
||||
setTheme (e) {
|
||||
const self = this;
|
||||
this.currentThemeMode = e.target.value;
|
||||
console.log('setTheme currentThemeMode:', this.currentThemeMode)
|
||||
this.$ipc.send(ipcApiRoute.setTheme, this.currentThemeMode);
|
||||
|
||||
this.$ipcCall(ipcApiRoute.setTheme, this.currentThemeMode).then(result => {
|
||||
console.log('result:', result)
|
||||
self.currentThemeMode = result;
|
||||
})
|
||||
},
|
||||
getTheme () {
|
||||
this.$ipc.send(ipcApiRoute.getTheme, '');
|
||||
const self = this;
|
||||
this.$ipcCall(ipcApiRoute.getTheme).then(result => {
|
||||
console.log('result:', result)
|
||||
self.currentThemeMode = result;
|
||||
})
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@@ -41,7 +41,8 @@ export default {
|
||||
methods: {
|
||||
init () {
|
||||
const self = this;
|
||||
self.$ipc.on(specialIpcRoute.appUpdater, (event, result) => {
|
||||
this.$ipc.removeAllListeners(specialIpcRoute.appUpdater);
|
||||
this.$ipc.on(specialIpcRoute.appUpdater, (event, result) => {
|
||||
result = JSON.parse(result);
|
||||
self.status = result.status;
|
||||
if (result.status == 3) {
|
||||
@@ -53,20 +54,18 @@ export default {
|
||||
})
|
||||
},
|
||||
checkForUpdater () {
|
||||
// const self = this;
|
||||
// self.$ipcCallMain('example.checkForUpdater').then(r => {
|
||||
// console.log(r);
|
||||
// })
|
||||
this.$ipcCall(ipcApiRoute.checkForUpdater).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
download () {
|
||||
// if (this.status !== 1) {
|
||||
// this.$message.info('没有可用版本');
|
||||
// return
|
||||
// }
|
||||
// const self = this;
|
||||
// self.$ipcCallMain('example.downloadApp').then(r => {
|
||||
// console.log(r);
|
||||
// })
|
||||
if (this.status !== 1) {
|
||||
this.$message.info('没有可用版本');
|
||||
return
|
||||
}
|
||||
this.$ipcCall(ipcApiRoute.downloadApp).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
@@ -42,7 +42,7 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
createWindow (index) {
|
||||
this.$ipcCallMain(ipcApiRoute.createWindow, this.views[index]).then(r => {
|
||||
this.$ipcCall(ipcApiRoute.createWindow, this.views[index]).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
|
||||
@@ -45,13 +45,13 @@ export default {
|
||||
methods: {
|
||||
loadViewContent (index) {
|
||||
const self = this;
|
||||
self.$ipcCallMain(ipcApiRoute.loadViewContent, this.views[index]).then(r => {
|
||||
self.$ipcCall(ipcApiRoute.loadViewContent, this.views[index]).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
removeViewContent (index) {
|
||||
const self = this;
|
||||
self.$ipcCallMain(ipcApiRoute.removeViewContent, self.views[index]).then(r => {
|
||||
self.$ipcCall(ipcApiRoute.removeViewContent, self.views[index]).then(r => {
|
||||
console.log(r);
|
||||
})
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user