1
This commit is contained in:
1 parent
083857d130
commit
be6b954dba
1 file changed
+6
-51
@@ -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 :<port> 获取对应的 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 [::]:<port> [::]:0 LISTENING <PID>
|
||||
// 需要提取出 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)
|
||||
|
||||
Reference in new issue
Block a user