From be6b954dbad29709e7a9156e5b8feb8ec705dbd7 Mon Sep 17 00:00:00 2001 From: dongchangxi <458593490@qq.com> Date: Fri, 13 Dec 2024 10:03:35 +0800 Subject: [PATCH] 1 --- main.go | 57 ++++++--------------------------------------------------- 1 file changed, 6 insertions(+), 51 deletions(-) diff --git a/main.go b/main.go index 1e1f594..276891c 100644 --- a/main.go +++ b/main.go @@ -5,9 +5,6 @@ import ( "changeme/initFunc" "context" "embed" - "fmt" - "os/exec" - "runtime" "strings" "github.com/gogf/gf/v2/net/ghttp" @@ -17,6 +14,7 @@ import ( "github.com/gogf/gf/v2/util/gconv" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/menu" + "github.com/wailsapp/wails/v2/pkg/menu/keys" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" ) @@ -56,10 +54,10 @@ func main() { //增加菜单 AppMenu := menu.NewMenu() - // SubMenu := AppMenu.AddSubmenu("工具") - // SubMenu.AddText("刷新", keys.CmdOrCtrl("F5"), func(_ *menu.CallbackData) { - // runtime.Reload(app.ctx) - // }) + SubMenu := AppMenu.AddSubmenu("工具") + SubMenu.AddText("刷新", keys.CmdOrCtrl("F5"), func(_ *menu.CallbackData) { + // runtime.Reload(app.ctx) + }) // Create application with options @@ -110,52 +108,9 @@ func (a *App) ShopUrl() string { return domain+"?oem_id="+gconv.String(wailsConfigOem["id"])+"&t="+gconv.String(gtime.TimestampMilli()) } -func releasePort(port int) error { - var cmd *exec.Cmd - - // 根据操作系统选择不同的命令 - if runtime.GOOS == "windows" { - // Windows: 使用 netstat 查找占用端口的 PID - // netstat -ano | findstr : 获取对应的 PID,然后使用 taskkill 杀死进程 - cmd = exec.Command("cmd", "/C", fmt.Sprintf("netstat -ano | findstr :%d", port)) - output, err := cmd.CombinedOutput() - if err != nil { - return fmt.Errorf("error running netstat: %v", err) - } - - // 在 Windows 上,输出格式是:TCP [::]: [::]:0 LISTENING - // 需要提取出 PID - lines := string(output) - for _, line := range strings.Split(lines, "\n") { - if strings.Contains(line, fmt.Sprintf(":%d", port)) { - // 提取 PID(PID 是最后一个字段) - fields := strings.Fields(line) - if len(fields) >= 5 { - pid := fields[len(fields)-1] - // 使用 taskkill 结束进程 - cmdKill := exec.Command("taskkill", "/PID", pid, "/F") - err := cmdKill.Run() - if err != nil { - return fmt.Errorf("failed to kill process with PID %s: %v", pid, err) - } - fmt.Printf("Killed process with PID %s on port %d\n", pid, port) - return nil - } - } - } - return fmt.Errorf("no process found listening on port %d", port) - - } else { - // Linux/MacOS: 使用 lsof 和 kill 杀死占用端口的进程 - exec.Command("bash", "-c", "lsof -i :${port} | grep LISTEN | awk '{print 8383}' | xargs kill -9") - } - return nil -} - - func HttpServer(){ - releasePort(8383) + // releasePort(8383) s := ghttp.GetServer() // 处理 /take_photo_page 路由 s.BindHandler("/take_photo_page",api.Page)