From f351a756498f93e6763c2e1dd3f86cd37b314d45 Mon Sep 17 00:00:00 2001 From: VIVIMAN Date: Fri, 6 May 2022 13:18:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=84=9A=E6=9C=AC=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../data_func/go_to_tray/demo03/build.bat | 2 + .../data_func/go_to_tray/demo03/main.go | 80 +++++++++++++++---- .../data_func/go_to_tray/demo03/run.bat | 7 ++ .../go_to_tray/demo03/shell/closeCX.bat | 1 + .../go_to_tray/demo03/shell/closeCX.sh | 1 + .../go_to_tray/demo03/shell/closeDB.bat | 1 + .../go_to_tray/demo03/shell/closeDB.sh | 1 + .../go_to_tray/demo03/shell/startCX.bat | 1 + .../go_to_tray/demo03/shell/startCX.sh | 1 + .../go_to_tray/demo03/shell/startDB.bat | 1 + .../go_to_tray/demo03/shell/startDB.sh | 1 + 11 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 golang_learn/data_func/go_to_tray/demo03/run.bat create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/closeCX.bat create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/closeCX.sh create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/closeDB.bat create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/closeDB.sh create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/startCX.bat create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/startCX.sh create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/startDB.bat create mode 100644 golang_learn/data_func/go_to_tray/demo03/shell/startDB.sh diff --git a/golang_learn/data_func/go_to_tray/demo03/build.bat b/golang_learn/data_func/go_to_tray/demo03/build.bat index 3a9eed4..b4079df 100644 --- a/golang_learn/data_func/go_to_tray/demo03/build.bat +++ b/golang_learn/data_func/go_to_tray/demo03/build.bat @@ -1,5 +1,7 @@ set GO111MODULE=on +SET CGO_ENABLED=0 set GOARCH=386 +set GOOS=windows rsrc -manifest main.manifest -ico main.ico -o main.syso go generate go build -ldflags="-s -w -H=windowsgui" -o ˵-м.exe diff --git a/golang_learn/data_func/go_to_tray/demo03/main.go b/golang_learn/data_func/go_to_tray/demo03/main.go index a7c7561..18327dd 100644 --- a/golang_learn/data_func/go_to_tray/demo03/main.go +++ b/golang_learn/data_func/go_to_tray/demo03/main.go @@ -44,7 +44,6 @@ func (mw *MyWindow) AddNotifyIcon() { // 其他快捷键 mw.addOther() - } func init() { @@ -63,7 +62,63 @@ func checkError(err error) { } } -// open opens the specified URL in the default browser of the user. +// OpenServe 打开服务 +func OpenServe(isDb bool) error { + var cmd string + var args []string + + switch runtime.GOOS { + case "windows": + if isDb { + cmd = "cmd" + args = []string{"/c", "./shell/startDB.bat"} + } else { + cmd = "/bin/sh" + args = []string{"-c", "./shell/startDB.sh"} + } + default: // "linux", "freebsd", "openbsd", "netbsd", "mac" + if isDb { + cmd = "cmd" + args = []string{"/c", "./shell/startCX.bat"} + } else { + cmd = "/bin/sh" + args = []string{"-c", "./shell/startCX.sh"} + } + } + c := exec.Command(cmd, args...) + c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return c.Start() +} + +// CloseServe 停止服务 +func CloseServe(isDb bool) error { + var cmd string + var args []string + + switch runtime.GOOS { + case "windows": + if isDb { + cmd = "cmd" + args = []string{"/c", "./shell/stopDB.bat"} + } else { + cmd = "/bin/sh" + args = []string{"-c", "./shell/stopDB.sh"} + } + default: // "linux", "freebsd", "openbsd", "netbsd", "mac" + if isDb { + cmd = "cmd" + args = []string{"/c", "./shell/stopCX.bat"} + } else { + cmd = "/bin/sh" + args = []string{"-c", "./shell/stopCX.sh"} + } + } + c := exec.Command(cmd, args...) + c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true} + return c.Start() +} + +// OpenUrl 按照地址打开浏览器,进行请求。 func OpenUrl(url string) error { var cmd string var args []string @@ -72,9 +127,7 @@ func OpenUrl(url string) error { case "windows": cmd = "cmd" args = []string{"/c", "start"} - case "darwin": - cmd = "open" - default: // "linux", "freebsd", "openbsd", "netbsd" + default: // "linux", "freebsd", "openbsd", "netbsd", "mac" cmd = "xdg-open" } args = append(args, url) @@ -172,18 +225,13 @@ func (mw *MyWindow) addCxMenu() { // AddListen 增加服务监听,控制按钮状态 func AddListen(startAction *walk.Action, stopAction *walk.Action, address string, timeout time.Duration) { conn, err := net.DialTimeout("tcp", address, timeout) - if err != nil { - // fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") - setEnableAndCheck(startAction, stopAction, false, false) + if err == nil && conn != nil { + // fmt.Println("["+now+"]", ipPort, "端口已开启(success)!") + setEnableAndCheck(startAction, stopAction, false, true) + _ = conn.Close() } else { - if conn != nil { - // fmt.Println("["+now+"]", ipPort, "端口已开启(success)!") - setEnableAndCheck(startAction, stopAction, false, true) - _ = conn.Close() - } else { - // fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") - setEnableAndCheck(startAction, stopAction, true, false) - } + // fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!") + setEnableAndCheck(startAction, stopAction, true, false) } } diff --git a/golang_learn/data_func/go_to_tray/demo03/run.bat b/golang_learn/data_func/go_to_tray/demo03/run.bat new file mode 100644 index 0000000..3ae25dc --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/run.bat @@ -0,0 +1,7 @@ +set GO111MODULE=on +SET CGO_ENABLED=0 +set GOARCH=arm +set GOOS=linux +rsrc -manifest main.manifest -ico main.ico -o main.syso +go generate +go build main.go diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.bat b/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.bat new file mode 100644 index 0000000..07603f8 --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.bat @@ -0,0 +1 @@ +net stop GWY20_Tomcat \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.sh b/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.sh new file mode 100644 index 0000000..f88041a --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/closeCX.sh @@ -0,0 +1 @@ +net start GWY20_Mysql \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.bat b/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.bat new file mode 100644 index 0000000..c8e402e --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.bat @@ -0,0 +1 @@ +net stop GWY20_Mysql \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.sh b/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.sh new file mode 100644 index 0000000..f88041a --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/closeDB.sh @@ -0,0 +1 @@ +net start GWY20_Mysql \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/startCX.bat b/golang_learn/data_func/go_to_tray/demo03/shell/startCX.bat new file mode 100644 index 0000000..9348e8d --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/startCX.bat @@ -0,0 +1 @@ +net start GWY20_Tomcat \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/startCX.sh b/golang_learn/data_func/go_to_tray/demo03/shell/startCX.sh new file mode 100644 index 0000000..f88041a --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/startCX.sh @@ -0,0 +1 @@ +net start GWY20_Mysql \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/startDB.bat b/golang_learn/data_func/go_to_tray/demo03/shell/startDB.bat new file mode 100644 index 0000000..f88041a --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/startDB.bat @@ -0,0 +1 @@ +net start GWY20_Mysql \ No newline at end of file diff --git a/golang_learn/data_func/go_to_tray/demo03/shell/startDB.sh b/golang_learn/data_func/go_to_tray/demo03/shell/startDB.sh new file mode 100644 index 0000000..f88041a --- /dev/null +++ b/golang_learn/data_func/go_to_tray/demo03/shell/startDB.sh @@ -0,0 +1 @@ +net start GWY20_Mysql \ No newline at end of file