diff --git a/golang_learn/data_func/go_to_windows/main.go b/golang_learn/data_func/go_to_windows/main.go index 13676a0..7be89bd 100644 --- a/golang_learn/data_func/go_to_windows/main.go +++ b/golang_learn/data_func/go_to_windows/main.go @@ -9,6 +9,7 @@ import ( "fmt" _ "github.com/go-sql-driver/mysql" "github.com/lxn/walk" + "github.com/lxn/win" "github.com/mzky/zip" "golang.org/x/sys/windows" "golang.org/x/sys/windows/registry" @@ -30,6 +31,8 @@ var service1, service3 myService var Version = "V2021 1.0.1" // 版本号 var passwordname = "Qw^e1@10&++Hzb...?8" // 解压密码 var path string // 该部分不存在最后 横线 (格式为 D:\hzb2021),使用时需要补充横线 +var epsoft1 = `全国公务员管理信息系统2020版` // 系统名称 1 +var epsoft2 = `全国公务员管理信息系统2021版` // 系统名称 2 type myApp struct { title string @@ -155,47 +158,50 @@ func flushServiceStat(service myService) { // registerService 【0】注册服务 func registerService(service myService) { - if service.serviceName == "GWY20_Mysql" { + if service.serviceName == service1.serviceName { // 注册 MYSQL 服务 registerWindows(service.serviceName, - `SYSTEM\ControlSet001\Services\GWY20_Mysql`, + fmt.Sprintf(`SYSTEM\ControlSet001\Services\%s`, service.serviceName), `LocalSystem`, - `GWY20_Mysql`, - fmt.Sprintf(`%s\mysql\bin\mysqld GWY20_Mysql`, path)) + service.serviceName, + fmt.Sprintf(`%s\mysql\bin\mysqld %s`, path, service.serviceName)) registerWindows(service.serviceName, - `SYSTEM\CurrentControlSet\Services\GWY20_Mysql`, + fmt.Sprintf(`SYSTEM\CurrentControlSet\Services\%s`, service.serviceName), `LocalSystem`, - `GWY20_Mysql`, - fmt.Sprintf(`%s\mysql\bin\mysqld --defaults-file=%s\mysql\my.ini GWY20_Mysql`, path, path)) + service.serviceName, + fmt.Sprintf(`%s\mysql\bin\mysqld --defaults-file=%s\mysql\my.ini %s`, path, path, service.serviceName)) } else { // 注册 TOMCAT 服务 registerWindows(service.serviceName, - `SYSTEM\ControlSet001\Services\GWY20_Tomcat`, + fmt.Sprintf(`SYSTEM\ControlSet001\Services\%s`, service.serviceName), `NT Authority\LocalService`, - `GWY20_Tomcat`, - fmt.Sprintf(`%s\tomcat8\bin\Tomcat8.exe //RS//GWY20_Tomcat`, path)) + service.serviceName, + fmt.Sprintf(`%s\tomcat8\bin\Tomcat8.exe //RS//%s`, path, service.serviceName)) registerWindows(service.serviceName, - `SYSTEM\CurrentControlSet\Services\GWY20_Tomcat`, + fmt.Sprintf(`SYSTEM\CurrentControlSet\Services\%s`, service.serviceName), `NT Authority\LocalService`, - `GWY20_Tomcat`, - fmt.Sprintf(`%s\tomcat8\bin\Tomcat8.exe //RS//GWY20_Tomcat`, path)) + service.serviceName, + fmt.Sprintf(`%s\tomcat8\bin\Tomcat8.exe //RS//%s`, path, service.serviceName)) registerJava(service.serviceName, path, 128, 256, 512, 512) registerLog(service.serviceName, path) registerState(service.serviceName, - `SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\GWY20_Tomcat\Parameters\Start`, + fmt.Sprintf(`SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\%s\Parameters\Start`, service.serviceName), `start`, path) registerState(service.serviceName, - `SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\GWY20_Tomcat\Parameters\Stop`, + fmt.Sprintf(`SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\%s\Parameters\Stop`, service.serviceName), `stop`, path) } showMsg(service.serviceName + " 服务注册成功!") - walk.MsgBox(app.mw, "重启提醒", "注册成功程序,需要重启电脑!", walk.MsgBoxIconWarning) - arg := []string{"-r", "-t", "20"} - cmd := exec.Command("shutdown", arg...) - _, err := cmd.CombinedOutput() - if err != nil { - showMsg(" 重启电脑失败,手动重启电脑") + if win.IDYES == walk.MsgBox(app.mw, "重启提醒", "注册成功程序,注册信息生效,需要重启电脑!\n是否重启系统...", walk.MsgBoxYesNo) { + arg := []string{"-r", "-t", "20"} + cmd := exec.Command("shutdown", arg...) + _, err := cmd.CombinedOutput() + if err != nil { + showMsg(" 重启电脑失败,手动重启电脑") + } + } else { + showMsg(service.serviceName + " 修改注册表,若未生效,请手动重启电脑!") } } @@ -255,7 +261,7 @@ func registerWindows(serviceName, regStr, objectName, displayName, imagePath str _ = key.SetStringValue(`DisplayName`, displayName) _ = key.SetExpandStringValue(`ImagePath`, imagePath) - if serviceName == "GWY20_Tomcat" { + if serviceName == service3.serviceName { _ = key.SetStringValue(`Description`, `Apache Tomcat 8.5.55 Server - https://tomcat.apache.org/`) _ = key.SetStringsValue(`DependOnService`, []string{`Tcpip`, `Afd`}) } @@ -265,7 +271,7 @@ func registerWindows(serviceName, regStr, objectName, displayName, imagePath str // registerJava 【2】修改注册表 func registerJava(serviceName, path string, permSize, maxPermSize, xms, xmx int) { - key, exists, _ := registry.CreateKey(registry.LOCAL_MACHINE, `SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\GWY20_Tomcat\Parameters\Java`, registry.ALL_ACCESS) + key, exists, _ := registry.CreateKey(registry.LOCAL_MACHINE, fmt.Sprintf(`SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\%s\Parameters\Java`, serviceName), registry.ALL_ACCESS) defer key.Close() // 判断是否已经存在了 @@ -290,7 +296,7 @@ func registerJava(serviceName, path string, permSize, maxPermSize, xms, xmx int) // registerLog 【3】修改注册表 func registerLog(serviceName, path string) { - key, exists, _ := registry.CreateKey(registry.LOCAL_MACHINE, `SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\GWY20_Tomcat\Parameters\Log`, registry.ALL_ACCESS) + key, exists, _ := registry.CreateKey(registry.LOCAL_MACHINE, fmt.Sprintf(`SOFTWARE\WOW6432Node\Apache Software Foundation\Procrun 2.0\%s\Parameters\Log`, serviceName), registry.ALL_ACCESS) defer key.Close() // 判断是否已经存在了 @@ -545,7 +551,7 @@ func dbBak() { // 启动服务 startService(service1) showMsg("已完成数据备份!") - showMsg(fmt.Sprintf(`⭐数据备份:%s`, fileName)) + showMsg(fmt.Sprintf(`⭐数据备份:%s\%s`, fileName, fileName)) } else { showMsg("无法正常启动数据库!") } @@ -749,9 +755,9 @@ func OpenUrl(url string) error { // HasHzb 判断软件是否安装 func HasHzb() error { - key2020, err2020 := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\全国公务员管理信息系统2020版`, registry.QUERY_VALUE) + key2020, err2020 := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf(`SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\%s`, epsoft1), registry.QUERY_VALUE) defer key2020.Close() - key2021, err2021 := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\全国公务员管理信息系统2021版`, registry.QUERY_VALUE) + key2021, err2021 := registry.OpenKey(registry.LOCAL_MACHINE, fmt.Sprintf(`SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\%s`, epsoft2), registry.QUERY_VALUE) defer key2021.Close() // 判断是否已经存在了