mirror of
https://github.com/snowykami/neo-blog.git
synced 2025-09-26 11:06:23 +00:00
feat: 添加用户位置、操作系统和浏览器信息到评论功能
This commit is contained in:
@ -1 +1,60 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type IPData struct {
|
||||
IP string `json:"ip"`
|
||||
Dec string `json:"dec"`
|
||||
Country string `json:"country"`
|
||||
CountryCode string `json:"countryCode"`
|
||||
Province string `json:"province"`
|
||||
City string `json:"city"`
|
||||
Districts string `json:"districts"`
|
||||
IDC string `json:"idc"`
|
||||
ISP string `json:"isp"`
|
||||
Net string `json:"net"`
|
||||
Zipcode string `json:"zipcode"`
|
||||
Areacode string `json:"areacode"`
|
||||
Protocol string `json:"protocol"`
|
||||
Location string `json:"location"`
|
||||
MyIP string `json:"myip"`
|
||||
Time string `json:"time"`
|
||||
}
|
||||
|
||||
type IPInfoResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data *IPData `json:"data"`
|
||||
}
|
||||
|
||||
func GetIPInfo(ip string) (*IPData, error) {
|
||||
// https://api.mir6.com/api/ip?ip={ip}&type=json
|
||||
ipInfoResponse := &IPInfoResponse{}
|
||||
logrus.Info(fmt.Sprintf("https://api.mir6.com/api/ip?ip=%s&type=json", ip))
|
||||
resp, err := client.R().
|
||||
SetResult(ipInfoResponse).
|
||||
Get(fmt.Sprintf("https://api.mir6.com/api/ip?ip=%s&type=json", ip))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode() != 200 {
|
||||
return nil, fmt.Errorf("状态码: %d,响应: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
return ipInfoResponse.Data, nil
|
||||
}
|
||||
|
||||
func GetLocationString(ip string) string {
|
||||
ipInfo, err := GetIPInfo(ip)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return ""
|
||||
}
|
||||
if ipInfo == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("%s %s %s %s", ipInfo.Country, ipInfo.Province, ipInfo.City, ipInfo.ISP)
|
||||
}
|
||||
|
Reference in New Issue
Block a user