| 123456789101112131415161718192021222324252627282930313233343536373839404142 |
- package utils
- import (
- "github.com/gogf/gf/v2/encoding/gcharset"
- "github.com/gogf/gf/v2/encoding/gjson"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/mssola/user_agent"
- )
- // GetClientBrowserInfo 获取浏览器信息
- func GetClientBrowserInfo(clientIP, userAgent string) (string, string, string) {
- ua := user_agent.New(userAgent)
- browser, _ := ua.Browser()
- return ua.OS(), browser, GetClientCityByIp(clientIP)
- }
- // GetClientCityByIp 获取ip所属城市
- func GetClientCityByIp(ip string) string {
- if ip == "" {
- return ""
- }
- if ip == "[::1]" || ip == "127.0.0.1" {
- return "内网IP"
- }
- url := "http://whois.pconline.com.cn/ipJson.jsp?json=true&ip=" + ip
- bytes := g.Client().GetBytes(gctx.New(), url)
- src := string(bytes)
- srcCharset := "GBK"
- tmp, _ := gcharset.ToUTF8(srcCharset, src)
- resData, err := gjson.DecodeToJson(tmp)
- if err != nil {
- return ""
- }
- if resData.Get("code").Int() == 0 {
- pro := resData.Get("pro").String()
- city := resData.Get("city").String()
- return pro + city
- } else {
- return ""
- }
- }
|