From 90cf3990d35712dfe877beff97ea18d3b363e108 Mon Sep 17 00:00:00 2001 From: hujie Date: Wed, 4 Mar 2026 18:30:31 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=9C=B0=E7=90=86?= =?UTF-8?q?=E4=BD=8D=E7=BD=AEGBK=E7=BC=96=E7=A0=81=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/location.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/utils/location.go b/utils/location.go index 6575055..68c9d51 100644 --- a/utils/location.go +++ b/utils/location.go @@ -8,6 +8,9 @@ import ( "math/rand" "net/http" "time" + + "golang.org/x/text/encoding/simplifiedchinese" + "golang.org/x/text/transform" ) // LocationResponse represents the response from IP location API @@ -74,7 +77,9 @@ func GetLocation(ctx context.Context, ip string) (province string, city string, return "", "", fmt.Errorf("request failed with status: %d", resp.StatusCode) } - body, err := io.ReadAll(resp.Body) + // API返回的是GBK编码,需要转换为UTF-8 + utf8Reader := transform.NewReader(resp.Body, simplifiedchinese.GBK.NewDecoder()) + body, err := io.ReadAll(utf8Reader) if err != nil { return "", "", fmt.Errorf("read response failed: %w", err) }