From 4fbeb29f7327e18e557c8ed0c9c3996b2a9c2d03 Mon Sep 17 00:00:00 2001 From: DengBiao <2319963317@qq.com> Date: Wed, 8 Mar 2023 16:54:25 +0800 Subject: [PATCH] update --- .../zhios_user_visit_ip_address_consume.go | 22 ++++++++++++++----- go.mod | 3 +++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/consume/zhios_user_visit_ip_address_consume.go b/consume/zhios_user_visit_ip_address_consume.go index 8dcf77a..11aa85a 100644 --- a/consume/zhios_user_visit_ip_address_consume.go +++ b/consume/zhios_user_visit_ip_address_consume.go @@ -11,10 +11,12 @@ import ( "encoding/json" "errors" "fmt" + "github.com/cc14514/go-geoip2" + geoip2db "github.com/cc14514/go-geoip2-db" "github.com/streadway/amqp" + "net" "strings" "time" - "xorm.io/xorm" ) var data []*model.MasterAreaVisitsFlows @@ -26,6 +28,9 @@ func ZhiOsUserVisitIpAddressConsume(queue md.MqQueue) { logx.Error(err) return } + geoIp2db, _ := geoip2db.NewGeoipDbByStatik() + defer geoIp2db.Close() + defer ch.Release() //1、将自己绑定到交换机上 ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey) @@ -40,7 +45,7 @@ func ZhiOsUserVisitIpAddressConsume(queue md.MqQueue) { if ok == true { //fmt.Println(string(res.Body)) fmt.Println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>") - err = handleUserVisitIpAddress(res.Body) + err = handleUserVisitIpAddress(res.Body, geoIp2db) //_ = res.Reject(false) _ = res.Ack(true) if len(data) >= 100 { @@ -54,7 +59,7 @@ func ZhiOsUserVisitIpAddressConsume(queue md.MqQueue) { fmt.Println("get msg done") } -func handleUserVisitIpAddress(msg []byte) error { +func handleUserVisitIpAddress(msg []byte, geoIp2db *geoip2.DBReader) error { now := time.Now() today := now.Format("2006-01-02") var tmpString string @@ -89,7 +94,7 @@ func handleUserVisitIpAddress(msg []byte) error { //if get == "" { if true { //2、分析ip归属地 - countryName, provinceId, cityId, provinceName, cityName := getIpAddress(db.DBs[msgStruct.MasterId], msgStruct) + countryName, provinceId, cityId, provinceName, cityName := getIpAddress(geoIp2db, msgStruct) if provinceId != "" || cityId != "" || countryName != "" || provinceName != "" || cityName != "" { //3、插入`master_area_visits_flows` 表 if provinceName == "" { @@ -121,7 +126,14 @@ func handleUserVisitIpAddress(msg []byte) error { return nil } -func getIpAddress(Db *xorm.Engine, message md.ZhiOsUserVisitIpAddressMessage) (countryName, provinceId, cityId, provinceName, cityName string) { +func getIpAddress(geoIp2db *geoip2.DBReader, message md.ZhiOsUserVisitIpAddressMessage) (countryName, provinceId, cityId, provinceName, cityName string) { + record, _ := geoIp2db.City(net.ParseIP(message.Ip)) + if record.Country.Names != nil && record.Subdivisions != nil && record.City.Names != nil { + message.Country = record.Country.Names["zh-CN"] + message.Province = record.Subdivisions[0].Names["zh-CN"] + message.City = record.City.Names["zh-CN"] + } + countryName = message.Country if countryName == "中国" && message.Province != "" { if message.Province == "闽" { diff --git a/go.mod b/go.mod index 40c70a3..1c9f73d 100644 --- a/go.mod +++ b/go.mod @@ -36,6 +36,8 @@ require ( github.com/KyleBanks/depth v1.2.1 // indirect github.com/PuerkitoBio/purell v1.1.1 // indirect github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect + github.com/cc14514/go-geoip2 v0.0.0-20190105051856-0a1854480a11 // indirect + github.com/cc14514/go-geoip2-db v0.0.0-20190106063142-7b6408a9812a // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.5 // indirect @@ -57,6 +59,7 @@ require ( github.com/onsi/gomega v1.10.5 // indirect github.com/pelletier/go-toml/v2 v2.0.1 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/rakyll/statik v0.1.7 // indirect github.com/syndtr/goleveldb v1.0.0 // indirect github.com/tidwall/match v1.0.3 // indirect github.com/tidwall/pretty v1.1.0 // indirect