You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package GeoIPUtil
import (
"dsDataex/Utils/CacheUtil"
"dsDataex/Utils/RedisUtil"
"github.com/yinheli/qqwry"
"time"
)
var DB *qqwry.QQwry
var Province2260 map[string]string
var City2260 map[string][]string
func init() {
//打开 纯真 IP离线库
DB = qqwry.NewQQwry("GeoLite2/qqwry.dat")
//行政区划缓存
var sql = "SELECT area_code from t_dataex_gbt2260 where area_type=2 "
var list, _, _ = CacheUtil.Page(sql, 5000, 0)
Province2260 = make(map[string]string)
for no := 0; no < len(list); no++ {
Province2260[list[no]["area_code"].(string)] = list[no]["area_name"].(string)
}
City2260 = make(map[string][]string)
for no := 0; no < len(list); no++ {
City2260[list[no]["area_name"].(string)] = []string{list[no]["area_code"].(string), list[no]["area_name"].(string), "", ""}
}
sql = "SELECT area_code from t_dataex_gbt2260 where area_type=3 "
list, _, _ = CacheUtil.Page(sql, 5000, 0)
for no := 0; no < len(list); no++ {
var provinceCode = list[no]["area_code"].(string)[0:2] + "0000"
var provinceName = Province2260[provinceCode]
City2260[provinceName+list[no]["area_name"].(string)] = []string{provinceCode, provinceName, list[no]["area_code"].(string), list[no]["area_name"].(string)}
}
}
func GetGeo4IP(temp string) []string {
//add by zhangjun 2020-08-04 使用 Redis 缓存,缓解 IP文件并发读问题
var city,err =RedisUtil.RedisClient.Get("Dataex_IP:"+temp).Result()
if err==nil && city != "" {
return City2260[ city ]
} else {
DB.Find(temp)
RedisUtil.RedisClient.Set("Dataex_IP:"+temp, DB.Country, 10*time.Hour)
//fmt.Println("qqwry :",DB.Country,DB.City)
return City2260[DB.Country]
}
}