fix: 修复地理位置GBK编码问题
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user