From 756f7adeceb35dd98bda8960864576cb0795405b Mon Sep 17 00:00:00 2001 From: gaoshuaixing Date: Thu, 9 Jan 2025 12:28:47 +0800 Subject: [PATCH] add ts types --- electron/controller/cross.ts | 12 ++--- electron/controller/effect.ts | 19 +++----- electron/controller/example.ts | 8 ++- electron/controller/framework.ts | 83 +++++++++++++++++++------------- electron/controller/os.ts | 60 ++++++++++------------- 5 files changed, 92 insertions(+), 90 deletions(-) diff --git a/electron/controller/cross.ts b/electron/controller/cross.ts index f362b23..efe4b59 100644 --- a/electron/controller/cross.ts +++ b/electron/controller/cross.ts @@ -17,7 +17,7 @@ class CrossController { /** * Get service url */ - async getUrl(args) { + async getUrl(args: { name: string }): Promise { const { name } = args; const serverUrl = crossService.getUrl(name); return serverUrl; @@ -27,7 +27,7 @@ class CrossController { * kill service * By default (modifiable), killing the process will exit the electron application. */ - async killServer(args) { + async killServer(args: { type: string; name: string }): Promise { const { type, name } = args; crossService.killServer(type, name); return; @@ -36,7 +36,7 @@ class CrossController { /** * create service */ - async createServer(args) { + async createServer(args: { program: string }): Promise { const { program } = args; if (program == 'go') { crossService.createGoServer(); @@ -52,12 +52,12 @@ class CrossController { /** * Access the api for the cross service */ - async requestApi(args) { + async requestApi(args: { name: string; urlPath: string; params: any }): Promise { const { name, urlPath, params} = args; const data = await crossService.requestApi(name, urlPath, params); return data; } } - CrossController.toString = () => '[class CrossController]'; -module.exports = CrossController; \ No newline at end of file + +export default CrossController; \ No newline at end of file diff --git a/electron/controller/effect.ts b/electron/controller/effect.ts index f732a82..e713ec2 100644 --- a/electron/controller/effect.ts +++ b/electron/controller/effect.ts @@ -1,8 +1,5 @@ -'use strict'; - -const { dialog } = require('electron'); -const _ = require('lodash'); -const { getMainWindow } = require('ee-core/electron'); +import { dialog } from 'electron'; +import { getMainWindow } from 'ee-core/electron'; /** * effect - demo @@ -13,12 +10,12 @@ class EffectController { /** * select file */ - selectFile() { + selectFile(): string | null { const filePaths = dialog.showOpenDialogSync({ properties: ['openFile'] }); - if (_.isEmpty(filePaths)) { + if (!filePaths) { return null } @@ -28,7 +25,7 @@ class EffectController { /** * login window */ - loginWindow(args) { + loginWindow(args: { width?: number; height?: number }): void { const { width, height } = args; const win = getMainWindow(); @@ -46,7 +43,7 @@ class EffectController { /** * restore window */ - restoreWindow(args) { + restoreWindow(args: { width?: number; height?: number }): void { const { width, height } = args; const win = getMainWindow(); @@ -61,6 +58,6 @@ class EffectController { win.focus(); } } - EffectController.toString = () => '[class EffectController]'; -module.exports = EffectController; \ No newline at end of file + +export default EffectController; \ No newline at end of file diff --git a/electron/controller/example.ts b/electron/controller/example.ts index 7ec8da5..a1138ef 100644 --- a/electron/controller/example.ts +++ b/electron/controller/example.ts @@ -1,5 +1,3 @@ -'use strict'; - /** * example * @class @@ -9,10 +7,10 @@ class ExampleController { /** * test */ - async test () { + async test(): Promise { return 'hello electron-egg'; } } - ExampleController.toString = () => '[class ExampleController]'; -module.exports = ExampleController; \ No newline at end of file + +export default ExampleController; \ No newline at end of file diff --git a/electron/controller/framework.ts b/electron/controller/framework.ts index 6a3293d..95edfdb 100644 --- a/electron/controller/framework.ts +++ b/electron/controller/framework.ts @@ -1,16 +1,14 @@ -'use strict'; - -const dayjs = require('dayjs'); -const path = require('path'); -const fs = require('fs'); -const { exec } = require('child_process'); -const { app: electronApp, shell } = require('electron'); -const { getExtraResourcesDir } = require('ee-core/ps'); -const { logger } = require('ee-core/log'); -const { getConfig } = require('ee-core/config'); -const { frameworkService } = require('../service/framework'); -const { sqlitedbService } = require('../service/database/sqlitedb'); -const { autoUpdaterService } = require('../service/os/auto_updater'); +import dayjs from 'dayjs'; +import path from 'path'; +import fs from 'fs'; +import { exec } from 'child_process'; +import { app as electronApp, shell } from 'electron'; +import { getExtraResourcesDir } from 'ee-core/ps'; +import { logger } from 'ee-core/log'; +import { getConfig } from 'ee-core/config'; +import { frameworkService } from '../service/framework'; +import { sqlitedbService } from '../service/database/sqlitedb'; +import { autoUpdaterService } from '../service/os/auto_updater'; /** * framework - demo @@ -27,13 +25,26 @@ class FrameworkController { /** * sqlite数据库操作 */ - async sqlitedbOperation(args) { + async sqlitedbOperation(args: { + action: string; + info?: any; + delete_name?: string; + update_name?: string; + update_age?: number; + search_age?: number; + data_dir?: string; + }): Promise<{ + action: string; + result: any; + all_list: any; + code: number; + }> { const { action, info, delete_name, update_name, update_age, search_age, data_dir } = args; const data = { action, - result: null, - all_list: [], + result: null as any, + all_list: []as any, code: 0 }; @@ -63,7 +74,7 @@ class FrameworkController { data.result = await sqlitedbService.getDataDir(); break; case 'setDataDir' : - data.result = await sqlitedbService.setCustomDataDir(data_dir); + data.result = await sqlitedbService.setCustomDataDir(data_dir || ""); break; } @@ -76,7 +87,7 @@ class FrameworkController { * 调用其它程序(exe、bash等可执行程序) * */ - openSoftware(args) { + openSoftware(args: { softName: string }): boolean { const { softName } = args; const softwarePath = path.join(getExtraResourcesDir(), softName); logger.info('[openSoftware] softwarePath:', softwarePath); @@ -98,9 +109,13 @@ class FrameworkController { /** * 检测http服务是否开启 */ - async checkHttpServer() { - const { enable, protocol, host, port } = getConfig().httpServer; - const url = protocol + host + ':' + port; + async checkHttpServer(): Promise<{ enable: boolean; server: string }> { + const httpConfig = getConfig().httpServer; + if (!httpConfig || !('enable' in httpConfig) || !('protocol' in httpConfig) || !('host' in httpConfig) || !('port' in httpConfig)) { + return { enable: false, server: '' }; + } + const { enable, protocol, host, port } = httpConfig; + const url = `${protocol}://${host}:${port}`; console.log('[checkHttpServer] url:', url); const data = { enable: enable, @@ -114,7 +129,7 @@ class FrameworkController { * args 是 前端传的参数 * ctx 是 koa 的 ctx 对象 */ - async doHttpRequest(args, ctx) { + async doHttpRequest(args: any, ctx: any): Promise { const httpInfo = { args, method: ctx.request.method, @@ -136,7 +151,7 @@ class FrameworkController { /** * 一个socket io请求访问此方法 */ - async doSocketRequest(args) { + async doSocketRequest(args: any): Promise { const { id } = args; if (!id) { return false; @@ -150,7 +165,7 @@ class FrameworkController { /** * 异步消息类型 */ - async ipcInvokeMsg(args) { + async ipcInvokeMsg(args: string): Promise { let timeNow = dayjs().format('YYYY-MM-DD HH:mm:ss'); const data = args + ' - ' + timeNow; @@ -160,7 +175,7 @@ class FrameworkController { /** * 同步消息类型 */ - async ipcSendSyncMsg(args) { + async ipcSendSyncMsg(args: string): Promise { let timeNow = dayjs().format('YYYY-MM-DD HH:mm:ss'); const data = args + ' - ' + timeNow; @@ -170,7 +185,7 @@ class FrameworkController { /** * 双向异步通信 */ - ipcSendMsg(args, event) { + ipcSendMsg(args: { type: string; content: any }, event: any): any { const { type, content } = args; const data = frameworkService.bothWayMessage(type, content, event); @@ -180,9 +195,9 @@ class FrameworkController { /** * 任务 */ - someJob(args, event) { + someJob(args: { jobId: string; action: string }, event: any): any { const { jobId, action} = args; - let result; + let result = null as any; switch (action) { case 'create': @@ -211,7 +226,7 @@ class FrameworkController { /** * 创建任务池 */ - async createPool(args, event) { + async createPool(args: { number: number }, event: any): Promise { let num = args.number; frameworkService.doCreatePool(num, event); @@ -224,9 +239,9 @@ class FrameworkController { /** * 通过进程池执行任务 */ - someJobByPool(args, event) { + someJobByPool(args: { jobId: string; action: string }, event: any): any { const { jobId, action } = args; - let result; + let result = null as any; switch (action) { case 'run': result = frameworkService.doJobByPool(jobId, action, event); @@ -261,10 +276,10 @@ class FrameworkController { /** * 测试接口 */ - hello(args) { + hello(args: any): void { logger.info('hello ', args); } } - FrameworkController.toString = () => '[class FrameworkController]'; -module.exports = FrameworkController; \ No newline at end of file + +export default FrameworkController; \ No newline at end of file diff --git a/electron/controller/os.ts b/electron/controller/os.ts index 3ebfd30..8f65770 100644 --- a/electron/controller/os.ts +++ b/electron/controller/os.ts @@ -1,12 +1,8 @@ -'use strict'; - -const _ = require('lodash'); -const fs = require('fs'); -const path = require('path'); -const { - app: electronApp, dialog, shell, Notification, -} = require('electron'); -const { windowService } = require('../service/os/window'); +import fs from 'fs'; +import path from 'path'; +import { app as electronApp, dialog, shell } from 'electron'; +import { windowService } from '../service/os/window'; +import exp from 'constants'; /** * example @@ -23,7 +19,7 @@ class OsController { /** * Message prompt dialog box */ - messageShow() { + messageShow(): string { dialog.showMessageBoxSync({ type: 'info', // "none", "info", "error", "question" 或者 "warning" title: 'Custom Title', @@ -37,7 +33,7 @@ class OsController { /** * Message prompt and confirmation dialog box */ - messageShowConfirm() { + messageShowConfirm(): string { const res = dialog.showMessageBoxSync({ type: 'info', title: 'Custom Title', @@ -60,8 +56,8 @@ class OsController { properties: ['openDirectory', 'createDirectory'] }); - if (_.isEmpty(filePaths)) { - return null + if (!filePaths) { + return "" } return filePaths[0]; @@ -70,7 +66,7 @@ class OsController { /** * open directory */ - openDirectory(args) { + openDirectory(args: { id: any }): boolean { const { id } = args; if (!id) { return false; @@ -89,7 +85,7 @@ class OsController { /** * Select Picture */ - selectPic() { + selectPic(): string | null { const filePaths = dialog.showOpenDialogSync({ title: 'select pic', properties: ['openFile'], @@ -97,7 +93,7 @@ class OsController { { name: 'Images', extensions: ['jpg', 'png', 'gif'] }, ] }); - if (_.isEmpty(filePaths)) { + if (!filePaths) { return null } @@ -114,7 +110,7 @@ class OsController { /** * Open a new window */ - createWindow(args) { + createWindow(args: any): any { const wcid = windowService.createWindow(args); return wcid; } @@ -122,7 +118,7 @@ class OsController { /** * Get Window contents id */ - getWCid(args) { + getWCid(args: any): any { const wcid = windowService.getWCid(args); return wcid; } @@ -130,40 +126,36 @@ class OsController { /** * Realize communication between two windows through the transfer of the main process */ - window1ToWindow2(args, event) { - windowService.communicate(args, event); + window1ToWindow2(args: any): void { + windowService.communicate(args); return; } /** * Realize communication between two windows through the transfer of the main process */ - window2ToWindow1(args, event) { - windowService.communicate(args, event); + window2ToWindow1(args: any): void { + windowService.communicate(args); return; } /** * Create system notifications */ - sendNotification(args, event) { + sendNotification(args: { title?: string; subtitle?: string; body?: string; silent?: boolean }, event: any): boolean { const { title, subtitle, body, silent} = args; - if (!Notification.isSupported()) { - return '当前系统不支持通知'; - } - - let options = {}; - if (!_.isEmpty(title)) { + const options: any = {}; + if (title) { options.title = title; } - if (!_.isEmpty(subtitle)) { + if (subtitle) { options.subtitle = subtitle; } - if (!_.isEmpty(body)) { + if (body) { options.body = body; } - if (!_.isEmpty(silent)) { + if (silent !== undefined) { options.silent = silent; } windowService.createNotification(options, event); @@ -171,6 +163,6 @@ class OsController { return true } } - OsController.toString = () => '[class OsController]'; -module.exports = OsController; \ No newline at end of file + +export default OsController; \ No newline at end of file