DengBiao 1 vuosi sitten
vanhempi
commit
64ec33219a
3 muutettua tiedostoa jossa 162 lisäystä ja 2 poistoa
  1. +118
    -0
      app/db/db_user_login_ip_list.go
  2. +10
    -0
      app/db/model/user_login_ip_list.go
  3. +34
    -2
      consume/canal_guide_order_consume.go

+ 118
- 0
app/db/db_user_login_ip_list.go Näytä tiedosto

@@ -0,0 +1,118 @@
package db

import (
"applet/app/db/model"
"applet/app/utils"
"applet/app/utils/logx"
"errors"
"fmt"
"reflect"
"xorm.io/xorm"
)

// BatchSelectUserLoginIpLists 批量查询数据 TODO::和下面的方法重复了,建议采用下面的 `UserLoginIpListFindByParams` 方法
func BatchSelectUserLoginIpLists(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserLoginIpList, error) {
var UserLoginIpListData []model.UserLoginIpList
if err := Db.In(utils.AnyToString(params["key"]), params["value"]).
Find(&UserLoginIpListData); err != nil {
return nil, logx.Warn(err)
}
return &UserLoginIpListData, nil
}

// UserLoginIpListInsert 插入单条数据
func UserLoginIpListInsert(Db *xorm.Engine, UserLoginIpList *model.UserLoginIpList) (int64, error) {
_, err := Db.InsertOne(UserLoginIpList)
if err != nil {
return 0, err
}
return UserLoginIpList.Id, nil
}

// BatchAddUserLoginIpLists 批量新增数据
func BatchAddUserLoginIpLists(Db *xorm.Engine, UserLoginIpListData []*model.UserLoginIpList) (int64, error) {
affected, err := Db.Insert(UserLoginIpListData)
if err != nil {
return 0, err
}
return affected, nil
}

func GetUserLoginIpListCount(Db *xorm.Engine) int {
var UserLoginIpList model.UserLoginIpList
session := Db.Where("")
count, err := session.Count(&UserLoginIpList)
if err != nil {
return 0
}
return int(count)
}

// UserLoginIpListDelete 删除记录
func UserLoginIpListDelete(Db *xorm.Engine, id interface{}) (int64, error) {
if reflect.TypeOf(id).Kind() == reflect.Slice {
return Db.In("id", id).Delete(model.UserLoginIpList{})
} else {
return Db.Where("id = ?", id).Delete(model.UserLoginIpList{})
}
}

// UserLoginIpListUpdate 更新记录
func UserLoginIpListUpdate(Db *xorm.Engine, id interface{}, UserLoginIpList *model.UserLoginIpList, forceColums ...string) (int64, error) {
var (
affected int64
err error
)
if forceColums != nil {
affected, err = Db.Where("id=?", id).Cols(forceColums...).Update(UserLoginIpList)
} else {
affected, err = Db.Where("id=?", id).Update(UserLoginIpList)
}
if err != nil {
return 0, err
}
return affected, nil
}

// UserLoginIpListGetOneByParams 通过传入的参数查询数据(单条)
func UserLoginIpListGetOneByParams(Db *xorm.Engine, params map[string]interface{}) (*model.UserLoginIpList, error) {
var m model.UserLoginIpList
var query = fmt.Sprintf("%s =?", params["key"])
has, err := Db.Where(query, params["value"]).Get(&m)
if err != nil || has == false {
return nil, logx.Error(err)
}
return &m, nil
}

// UserLoginIpListFindByParams 通过传入的参数查询数据(多条)
func UserLoginIpListFindByParams(Db *xorm.Engine, params map[string]interface{}) (*[]model.UserLoginIpList, error) {
var m []model.UserLoginIpList
if params["value"] == nil {
return nil, errors.New("参数有误")
}
if params["key"] == nil {
//查询全部数据
err := Db.Find(&m)
if err != nil {
return nil, logx.Error(err)
}
return &m, nil
} else {
if reflect.TypeOf(params["value"]).Kind() == reflect.Slice {
//指定In查询
if err := Db.In(utils.AnyToString(params["key"]), params["value"]).Find(&m); err != nil {
return nil, logx.Warn(err)
}
return &m, nil
} else {
var query = fmt.Sprintf("%s =?", params["key"])
err := Db.Where(query, params["value"]).Find(&m)
if err != nil {
return nil, logx.Error(err)
}
return &m, nil
}

}
}

+ 10
- 0
app/db/model/user_login_ip_list.go Näytä tiedosto

@@ -0,0 +1,10 @@
package model

type UserLoginIpList struct {
Id int64 `json:"id"`
Uid int `json:"uid"`
NowIp string `json:"now_ip"`
NowCity string `json:"now_city"`
LastIp string `json:"last_ip"`
LastCity string `json:"last_city"`
}

+ 34
- 2
consume/canal_guide_order_consume.go Näytä tiedosto

@@ -11,7 +11,10 @@ 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"
@@ -55,6 +58,10 @@ func handleGuideOrdTable(msg []byte) error {
if err != nil {
return err
}

geoIp2db, _ := geoip2db.NewGeoipDbByStatik()
defer geoIp2db.Close()

//2、判断操作(目前只针对insert进行修改)
if canalMsg.Type == md.CanalMsgInsertSqlType {
//3、TODO::判断es索引是否创建(因为这里我已经手动创建了,省略此步骤)
@@ -71,7 +78,7 @@ func handleGuideOrdTable(msg []byte) error {
if err != nil {
return err
}
provinceId, cityId, countyId, provinceName, cityName, countyName := getUserAddress(db.DBs[masterId], data.Uid)
provinceId, cityId, countyId, provinceName, cityName, countyName := getUserAddress(db.DBs[masterId], data.Uid, geoIp2db)
now := time.Now()
esData := esMd.ZhiosOrdersEs{
OrdKind: "guide",
@@ -123,7 +130,7 @@ func handleGuideOrdTable(msg []byte) error {
return nil
}

func getUserAddress(Db *xorm.Engine, uid string) (provinceId, cityId, countyId, provinceName, cityName, countyName string) {
func getUserAddress(Db *xorm.Engine, uid string, geoIp2db *geoip2.DBReader) (provinceId, cityId, countyId, provinceName, cityName, countyName string) {
regionalAgentUserBelong, err := db.RegionalAgentUserBelongGetOneByParams(Db, map[string]interface{}{
"key": "uid",
"value": uid,
@@ -132,6 +139,31 @@ func getUserAddress(Db *xorm.Engine, uid string) (provinceId, cityId, countyId,
return
}
if regionalAgentUserBelong == nil {
//TODO::没开区域代理的,使用用户ip归属地
userLoginIpList, err := db.UserLoginIpListGetOneByParams(Db, map[string]interface{}{
"key": "uid",
"value": uid,
})
if err != nil {
return
}
record, _ := geoIp2db.City(net.ParseIP(userLoginIpList.NowIp))
if record.Country.Names != nil && record.Subdivisions != nil && record.City.Names != nil {
province, err := db.ProvinceGetOneByName(record.Subdivisions[0].Names["zh-CN"])
if err != nil {
return
}
provinceId = province.Id
provinceName = province.Name

city, err := db.CityGetOneByName(record.City.Names["zh-CN"])
if err != nil {
return
}
cityId = city.Id
cityName = city.Name
return
}
//TODO::没开区域代理的,默认归属到广东
provinceName = "广东省"
provinceId = "440000000000"


Ladataan…
Peruuta
Tallenna