From e434b1d42ef7c70c83d8811a95fd86903effe777 Mon Sep 17 00:00:00 2001 From: haoyanbin <605649647@qq.com> Date: Sat, 2 Sep 2023 15:38:41 +0800 Subject: [PATCH] 1 --- api/mobile/survey.go | 5 +++++ config.yaml | 5 +++++ config/config.go | 1 + config/ipaddr.go | 8 +++++++ model/ipaddr.go | 10 +++++++++ model/request/ipaddr.go | 1 + service/ipaddr.go | 50 +++++++++++++++++++++++++++++++++++++++++ utils/common_api.go | 43 +++++++++++++++++++++++++++++++++++ utils/serialize.go | 40 +++++++++++++++++++++++++++++++++ 9 files changed, 163 insertions(+) create mode 100644 config/ipaddr.go create mode 100644 model/ipaddr.go create mode 100644 model/request/ipaddr.go create mode 100644 service/ipaddr.go create mode 100644 utils/common_api.go diff --git a/api/mobile/survey.go b/api/mobile/survey.go index 1975cd7..ddf3c0d 100755 --- a/api/mobile/survey.go +++ b/api/mobile/survey.go @@ -1,6 +1,7 @@ package mobile import ( + "fmt" "gin-vue-admin/global" "gin-vue-admin/model" "gin-vue-admin/model/request" @@ -45,6 +46,10 @@ func CreateSurveyUser(c *gin.Context) { func CreateSurveyLog(c *gin.Context) { var req model.SurveyLog _ = c.ShouldBindJSON(&req) + + ipData, _ := service.GetIpaddr(c.ClientIP()) + fmt.Println(ipData) + req.CreateTime = utils.NowTime() if err := service.CreateSurveyLog(req); err != nil { global.GVA_LOG.Error("鍒涘缓澶辫触!", zap.Any("err", err)) diff --git a/config.yaml b/config.yaml index fe67a50..22c54d9 100755 --- a/config.yaml +++ b/config.yaml @@ -195,3 +195,8 @@ doctor_db: account: "dbc_saas:dbc_saas888888@tcp(rm-2zepcf8kag0aol0q48o.mysql.rds.aliyuncs.com:3306)/hos_database?charset=utf8" common: "dbc_saas:dbc_saas888888@tcp(rm-2zepcf8kag0aol0q48o.mysql.rds.aliyuncs.com:3306)/saas_common?charset=utf8" +ipaddr: + url: "https://ipaddquery.market.alicloudapi.com/ip/address-query" + appkey: "203818254" + appsecret: "Po6a0U5awkWFeXCBmfYyezJdhI7p7Xib" + appcode: "6d1da9eb15b540688a505c1814e0c25f" \ No newline at end of file diff --git a/config/config.go b/config/config.go index 93c6578..11915d5 100755 --- a/config/config.go +++ b/config/config.go @@ -38,4 +38,5 @@ type Server struct { DoctorPay DoctorPay `mapstructure:"doctor_pay" json:"doctor_pay" yaml:"doctor_pay"` LisOcr LisOcr `mapstructure:"lis_ocr" json:"lis_ocr" yaml:"lis_ocr"` DoctorDb DoctorDb `mapstructure:"doctor_db" json:"doctor_db" yaml:"doctor_db"` + Ipaddr Ipaddr `mapstructure:"ipaddr" json:"ipaddr" yaml:"ipaddr"` } diff --git a/config/ipaddr.go b/config/ipaddr.go new file mode 100644 index 0000000..feb76f0 --- /dev/null +++ b/config/ipaddr.go @@ -0,0 +1,8 @@ +package config + +type Ipaddr struct { + Url string `mapstructure:"url" json:"url" yaml:"url"` // 瀛樺偍鍖哄煙 + Appkey string `mapstructure:"appkey" json:"appkey" yaml:"appkey"` // 瀛樺偍鍖哄煙 + Appsecret string `mapstructure:"appsecret" json:"appsecret" yaml:"appsecret"` // 瀛樺偍鍖哄煙 + Appcode string `mapstructure:"appcode" json:"appcode" yaml:"appcode"` // 瀛樺偍鍖哄煙 +} diff --git a/model/ipaddr.go b/model/ipaddr.go new file mode 100644 index 0000000..42685dc --- /dev/null +++ b/model/ipaddr.go @@ -0,0 +1,10 @@ +package model + +type Ipaddr struct { + Id int `gorm:"type:int(255)" json:"id"` + Ip string `gorm:"type:string(255)" json:"ip"` + Code int `gorm:"type:int(255)" json:"code"` + TaskNo string `gorm:"type:string(1000)" json:"taskNo"` + Data string `gorm:"type:string(1000)" json:"data"` + CreateTime string `gorm:"type:string(100)" json:"create_time"` +} diff --git a/model/request/ipaddr.go b/model/request/ipaddr.go new file mode 100644 index 0000000..725b8fc --- /dev/null +++ b/model/request/ipaddr.go @@ -0,0 +1 @@ +package request diff --git a/service/ipaddr.go b/service/ipaddr.go new file mode 100644 index 0000000..58ff715 --- /dev/null +++ b/service/ipaddr.go @@ -0,0 +1,50 @@ +package service + +import ( + "fmt" + "gin-vue-admin/global" + "gin-vue-admin/model" + "gin-vue-admin/utils" + "time" +) + +func GetIpaddr(ip string) (utils.IpData, string) { + + if ip == "127.0.0.1" { + return utils.IpData{}, "" + } + + var data model.Ipaddr + + nowtime := time.Now() + + d, _ := time.ParseDuration("-6h") + + beforetime := nowtime.Add(d) + + global.GVA_DB.Table("ipaddr").Where("ip = ? and code = ? and create_time > ", ip, 400, beforetime).First(&data) + if data.Id > 0 { + ipDataDb := new(utils.IpData) + utils.UnserislizeJson(data.Data, ipDataDb) + return *ipDataDb, "" + } + + ipData := utils.GetIpaddr(ip) + + IpaddrData := model.Ipaddr{ + Ip: ip, + Code: ipData.Code, + TaskNo: ipData.TaskNo, + Data: string(utils.SerializeJson(ipData.Data)), + } + + global.GVA_DB.Table("ipaddr").Create(&IpaddrData) + + if ipData.Code == 200 { + return ipData.Data, "" + } + + fmt.Println(ipData.Msg) + + return utils.IpData{}, ipData.Msg +} diff --git a/utils/common_api.go b/utils/common_api.go new file mode 100644 index 0000000..259b3d4 --- /dev/null +++ b/utils/common_api.go @@ -0,0 +1,43 @@ +package utils + +import ( + "gin-vue-admin/global" +) + +func GetIpaddr(ip string) GetIpaddrReply { + url := global.GVA_CONFIG.Ipaddr.Url + + data := new(GetIpaddrReq) + data.Ip = ip + + resp := GetIpaddrPost(url, data, global.GVA_CONFIG.Ipaddr.Appcode, 1) + + reply := new(GetIpaddrReply) + + UnserislizeJson(resp, reply) + + return *reply +} + +type GetIpaddrReq struct { + Ip string `json:"ip"` +} +type GetIpaddrReply struct { + Code int `json:"code"` + Msg string `json:"msg"` + TaskNo string `json:"taskNo"` + Data IpData `json:"data"` +} + +type IpData struct { + Country string `json:"country"` + CountryID string `json:"country_id"` + Area string `json:"area"` + Region string `json:"region"` + RegionID string `json:"region_id"` + City string `json:"city"` + CityID string `json:"city_id"` + IP string `json:"ip"` + LongIP string `json:"long_ip"` + Isp string `json:"isp"` +} diff --git a/utils/serialize.go b/utils/serialize.go index 4e25012..0521f0d 100755 --- a/utils/serialize.go +++ b/utils/serialize.go @@ -192,3 +192,43 @@ func PostWithFormData(method, url string, postData *map[string]string, token str fmt.Println("杩斿洖鏁版嵁锛�" + string(data)) return string(data) } + +func GetIpaddrPost(url string, data interface{}, token string, isPrt int) string { + // 瓒呮椂鏃堕棿锛�10绉� + client := &http.Client{Timeout: 10 * time.Second} + if isPrt == 1 { + fmt.Println("璇锋眰鍦板潃锛�" + url) + } + + jsonStr, _ := json.Marshal(data) + if isPrt == 1 { + fmt.Println("璇锋眰鏁版嵁锛�" + string(jsonStr)) + } + + reqest, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) + + if token != "" { + reqest.Header.Add("Authorization", "APPCODE "+token) + //fmt.Println("token锛�" + token) + } + reqest.Header.Add("Content-Type", "application/json") + reqest.Header.Add("Connection", "keep-alive") + if err != nil { + fmt.Println(err) + return "" + } + + resp, err := client.Do(reqest) + if err != nil { + fmt.Println(err) + return "" + } + defer resp.Body.Close() + + result, _ := ioutil.ReadAll(resp.Body) + + if isPrt == 1 { + fmt.Println("杩斿洖鏁版嵁锛�" + string(result)) + } + return string(result) +} -- 2.18.1