From 62a6242dd6219c1695614d8632143ca9dad5a4d1 Mon Sep 17 00:00:00 2001 From: wangbin Date: Tue, 8 Jul 2025 15:32:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E5=A4=87=E6=9C=BA=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Common/commonfunc.go | 24 ++++++++++++++++++++++++ main.go | 10 +++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Common/commonfunc.go b/Common/commonfunc.go index 5dabeaf..35186e4 100644 --- a/Common/commonfunc.go +++ b/Common/commonfunc.go @@ -1024,3 +1024,27 @@ func FormatTimes(now time.Time, format string) string { return now.Format(format) } + +func CheckIPReachable(ip string, port int, timeout time.Duration) bool { + conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), timeout) + if err != nil { + return false + } + conn.Close() + return true +} + +func GetAvailableEndpoint() string { + primaryIP := LoadConfig().SeverData.ServerIP + backupIP := LoadConfig().SeverData.BackUpServerIP // 替换为实际备选IP + port := 9800 + timeout := 3 * time.Second + + if CheckIPReachable(primaryIP, port, timeout) { + return primaryIP + } + if CheckIPReachable(backupIP, port, timeout) { + return backupIP + } + return "" +} diff --git a/main.go b/main.go index 2fe1b88..3d43824 100644 --- a/main.go +++ b/main.go @@ -1817,7 +1817,15 @@ func GetAgenda(AgID string) { //通过接口获取会议,人员信息 var agendaPersons []Model.T_AgendaPerson = make([]Model.T_AgendaPerson, 0) - url := "http://" + Common.LoadConfig().SeverData.ServerIP + ":9800/vhwcapi/v1/web/api/service/congresscontrol/getpersonforcheckdoor" + + targetIP := Common.GetAvailableEndpoint() + if targetIP == "" { + log.Error("所有IP均不可达") + log.Println("所有IP均不可达") + return + } + + url := "http://" + targetIP + ":9800/vhwcapi/v1/web/api/service/congresscontrol/getpersonforcheckdoor" jsonData := `{}` body, statusCode, err := Common.SendPostRequest(url, jsonData, "application/json") if err != nil {