DengBiao 1 rok temu
rodzic
commit
4fbeb29f73
2 zmienionych plików z 20 dodań i 5 usunięć
  1. +17
    -5
      consume/zhios_user_visit_ip_address_consume.go
  2. +3
    -0
      go.mod

+ 17
- 5
consume/zhios_user_visit_ip_address_consume.go Wyświetl plik

@@ -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 == "闽" {


+ 3
- 0
go.mod Wyświetl plik

@@ -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


Ładowanie…
Anuluj
Zapisz