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) }