From 174c8a83c70585a2d3b07290d67a3b70b3db21dd Mon Sep 17 00:00:00 2001 From: xiangheng <11675084@qq.com> Date: Mon, 22 Apr 2024 02:25:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../monitor_client/monitor_client_schema.go | 44 ++++++------ .../monitor_client/monitor_client_service.go | 67 +------------------ .../admin/monitor_web/monitor_web_schema.go | 30 ++++----- server/core/time.go | 26 +++---- server/model/monitor_client.go | 49 +++++++------- 5 files changed, 73 insertions(+), 143 deletions(-) diff --git a/server/admin/monitor_client/monitor_client_schema.go b/server/admin/monitor_client/monitor_client_schema.go index 864cc82..7426bd1 100644 --- a/server/admin/monitor_client/monitor_client_schema.go +++ b/server/admin/monitor_client/monitor_client_schema.go @@ -23,31 +23,31 @@ type MonitorClientDetailReq struct { //MonitorClientAddReq 客户端信息新增参数 type MonitorClientAddReq struct { - ProjectKey string `form:"projectKey"` // 项目key - ClientId string `form:"clientId"` // sdk生成的客户端id - UserId string `form:"userId"` // 用户id - Os string `form:"os"` // 系统 - Browser string `form:"browser"` // 浏览器 - City string `form:"city"` // 城市 - Width int `form:"width"` // 屏幕 - Height int `form:"height"` // 屏幕高度 - Ua string `form:"ua"` // ua记录 - ClientTime core.TsTime `form:"clientTime"` // 客户端时间 + ProjectKey string `form:"projectKey"` // 项目key + ClientId string `form:"clientId"` // sdk生成的客户端id + UserId string `form:"userId"` // 用户id + Os string `form:"os"` // 系统 + Browser string `form:"browser"` // 浏览器 + City string `form:"city"` // 城市 + Width int `form:"width"` // 屏幕 + Height int `form:"height"` // 屏幕高度 + Ua string `form:"ua"` // ua记录 + ClientTime string `form:"clientTime"` // 客户端时间 } //MonitorClientEditReq 客户端信息编辑参数 type MonitorClientEditReq struct { - Id int `form:"id"` // uuid - ProjectKey string `form:"projectKey"` // 项目key - ClientId string `form:"clientId"` // sdk生成的客户端id - UserId string `form:"userId"` // 用户id - Os string `form:"os"` // 系统 - Browser string `form:"browser"` // 浏览器 - City string `form:"city"` // 城市 - Width int `form:"width"` // 屏幕 - Height int `form:"height"` // 屏幕高度 - Ua string `form:"ua"` // ua记录 - ClientTime core.TsTime `form:"clientTime"` // 客户端时间 + Id int `form:"id"` // uuid + ProjectKey string `form:"projectKey"` // 项目key + ClientId string `form:"clientId"` // sdk生成的客户端id + UserId string `form:"userId"` // 用户id + Os string `form:"os"` // 系统 + Browser string `form:"browser"` // 浏览器 + City string `form:"city"` // 城市 + Width int `form:"width"` // 屏幕 + Height int `form:"height"` // 屏幕高度 + Ua string `form:"ua"` // ua记录 + ClientTime string `form:"clientTime"` // 客户端时间 } //MonitorClientDelReq 客户端信息新增参数 @@ -67,6 +67,6 @@ type MonitorClientResp struct { Width int `json:"width" structs:"width" excel:"name:屏幕;"` // 屏幕 Height int `json:"height" structs:"height" excel:"name:屏幕高度;"` // 屏幕高度 Ua string `json:"ua" structs:"ua" excel:"name:ua记录;"` // ua记录 - ClientTime int `json:"clientTime" structs:"clientTime" excel:"name:客户端时间;"` // 客户端时间 + ClientTime core.TsTime `json:"clientTime" structs:"clientTime" excel:"name:客户端时间;"` // 客户端时间 CreateTime core.TsTime `json:"createTime" structs:"createTime" excel:"name:创建时间;"` // 创建时间 } diff --git a/server/admin/monitor_client/monitor_client_service.go b/server/admin/monitor_client/monitor_client_service.go index e90ce23..d103b32 100644 --- a/server/admin/monitor_client/monitor_client_service.go +++ b/server/admin/monitor_client/monitor_client_service.go @@ -76,38 +76,7 @@ func (service monitorClientService) List(page request.PageReq, listReq MonitorCl offset := page.PageSize * (page.PageNo - 1) dbModel := service.GetModel(listReq) - // 查询 - // dbModel := service.db.Model(&model.MonitorClient{}) - // if listReq.ProjectKey != "" { - // dbModel = dbModel.Where("project_key = ?", listReq.ProjectKey) - // } - // if listReq.ClientId != "" { - // dbModel = dbModel.Where("client_id = ?", listReq.ClientId) - // } - // if listReq.UserId != "" { - // dbModel = dbModel.Where("user_id = ?", listReq.UserId) - // } - // if listReq.Os != "" { - // dbModel = dbModel.Where("os = ?", listReq.Os) - // } - // if listReq.Browser != "" { - // dbModel = dbModel.Where("browser = ?", listReq.Browser) - // } - // if listReq.City != "" { - // dbModel = dbModel.Where("city = ?", listReq.City) - // } - // if listReq.Width > 0 { - // dbModel = dbModel.Where("width = ?", listReq.Width) - // } - // if listReq.Height > 0 { - // dbModel = dbModel.Where("height = ?", listReq.Height) - // } - // if listReq.Ua != "" { - // dbModel = dbModel.Where("ua = ?", listReq.Ua) - // } - // if listReq.ClientTime > 0 { - // dbModel = dbModel.Where("client_time = ?", listReq.ClientTime) - // } + // 总数 var count int64 err := dbModel.Count(&count).Error @@ -160,6 +129,8 @@ func (service monitorClientService) Detail(id int) (res MonitorClientResp, e err func (service monitorClientService) Add(addReq MonitorClientAddReq) (e error) { var obj model.MonitorClient response.Copy(&obj, addReq) + obj.ClientTime = core.ToUnix(addReq.ClientTime) + err := service.db.Create(&obj).Error e = response.CheckMysqlErr(err) if e != nil { @@ -209,38 +180,6 @@ func (service monitorClientService) ExportFile(listReq MonitorClientListReq) (re // 查询 dbModel := service.GetModel(listReq) - // dbModel := service.db.Model(&model.MonitorClient{}) - // if listReq.ProjectKey != "" { - // dbModel = dbModel.Where("project_key = ?", listReq.ProjectKey) - // } - // if listReq.ClientId != "" { - // dbModel = dbModel.Where("client_id = ?", listReq.ClientId) - // } - // if listReq.UserId != "" { - // dbModel = dbModel.Where("user_id = ?", listReq.UserId) - // } - // if listReq.Os != "" { - // dbModel = dbModel.Where("os = ?", listReq.Os) - // } - // if listReq.Browser != "" { - // dbModel = dbModel.Where("browser = ?", listReq.Browser) - // } - // if listReq.City != "" { - // dbModel = dbModel.Where("city = ?", listReq.City) - // } - // if listReq.Width > 0 { - // dbModel = dbModel.Where("width = ?", listReq.Width) - // } - // if listReq.Height > 0 { - // dbModel = dbModel.Where("height = ?", listReq.Height) - // } - // if listReq.Ua != "" { - // dbModel = dbModel.Where("ua = ?", listReq.Ua) - // } - // if listReq.ClientTime > 0 { - // dbModel = dbModel.Where("client_time = ?", listReq.ClientTime) - // } - // 数据 var objs []model.MonitorClient err := dbModel.Order("id asc").Find(&objs).Error diff --git a/server/admin/monitor_web/monitor_web_schema.go b/server/admin/monitor_web/monitor_web_schema.go index 257af73..5130a7a 100644 --- a/server/admin/monitor_web/monitor_web_schema.go +++ b/server/admin/monitor_web/monitor_web_schema.go @@ -23,25 +23,25 @@ type MonitorWebDetailReq struct { //MonitorWebAddReq 错误收集error新增参数 type MonitorWebAddReq struct { - ProjectKey string `form:"projectKey"` // 项目key - ClientId string `form:"clientId"` // sdk生成的客户端id - EventType string `form:"eventType"` // 事件类型 - Page string `form:"page"` // URL地址 - Message string `form:"message"` // 错误消息 - Stack string `form:"stack"` // 错误堆栈 - ClientTime core.TsTime `form:"clientTime"` // 客户端时间 + ProjectKey string `form:"projectKey"` // 项目key + ClientId string `form:"clientId"` // sdk生成的客户端id + EventType string `form:"eventType"` // 事件类型 + Page string `form:"page"` // URL地址 + Message string `form:"message"` // 错误消息 + Stack string `form:"stack"` // 错误堆栈 + ClientTime string `form:"clientTime"` // 客户端时间 } //MonitorWebEditReq 错误收集error编辑参数 type MonitorWebEditReq struct { - Id int `form:"id"` // uuid - ProjectKey string `form:"projectKey"` // 项目key - ClientId string `form:"clientId"` // sdk生成的客户端id - EventType string `form:"eventType"` // 事件类型 - Page string `form:"page"` // URL地址 - Message string `form:"message"` // 错误消息 - Stack string `form:"stack"` // 错误堆栈 - ClientTime core.TsTime `form:"clientTime"` // 客户端时间 + Id int `form:"id"` // uuid + ProjectKey string `form:"projectKey"` // 项目key + ClientId string `form:"clientId"` // sdk生成的客户端id + EventType string `form:"eventType"` // 事件类型 + Page string `form:"page"` // URL地址 + Message string `form:"message"` // 错误消息 + Stack string `form:"stack"` // 错误堆栈 + ClientTime string `form:"clientTime"` // 客户端时间 } //MonitorWebDelReq 错误收集error新增参数 diff --git a/server/core/time.go b/server/core/time.go index becc13b..233f355 100644 --- a/server/core/time.go +++ b/server/core/time.go @@ -2,10 +2,7 @@ package core import ( "encoding/json" - "strconv" "time" - - "github.com/gin-gonic/gin" ) const DateFormat = "2006-01-02" @@ -34,20 +31,6 @@ type OnlyRespTsTime time.Time // return json.Marshal(tt) // } // -// 实现自定义的解析逻辑 -func (t *TsTime) Bind(ctx *gin.Context) error { - // 尝试从表单中获取时间字符串 - str := ctx.Query("clientTime") // 对于GET请求使用Query,对于POST请求使用PostForm - // 对于POST请求中的JSON体,你应该使用 ShouldBindJSON 或其他相关的ShouldBind方法 - - // 假设传入的是UNIX时间戳的字符串形式 - i, err := strconv.ParseInt(str, 10, 64) - if err != nil { - return err - } - *t = TsTime(i) - return nil -} func (tst *TsTime) UnmarshalJSON(bs []byte) error { var date string @@ -67,6 +50,15 @@ func (tst TsTime) MarshalJSON() ([]byte, error) { return json.Marshal(tt) } +// 通过时间字符串生成时间戳 +func ToUnix(date string) int64 { + if date == "" { + return 0 + } + tt, _ := time.ParseInLocation(TimeFormat, date, time.Local) + return tt.Unix() +} + func (otst OnlyRespTsTime) MarshalJSON() ([]byte, error) { tt := time.Time(otst).Format(TimeFormat) return json.Marshal(tt) diff --git a/server/model/monitor_client.go b/server/model/monitor_client.go index 680b7af..ae3960b 100644 --- a/server/model/monitor_client.go +++ b/server/model/monitor_client.go @@ -2,29 +2,28 @@ package model //MonitorClient 客户端信息实体 type MonitorClient struct { - - Id int `gorm:"primarykey;comment:'uuid'" excel:"name:uuid;"` // uuid - - ProjectKey string `gorm:"comment:'项目key'" excel:"name:项目key;"` // 项目key - - ClientId string `gorm:"comment:'sdk生成的客户端id'" excel:"name:sdk生成的客户端id;"` // sdk生成的客户端id - - UserId string `gorm:"comment:'用户id'" excel:"name:用户id;"` // 用户id - - Os string `gorm:"comment:'系统'" excel:"name:系统;"` // 系统 - - Browser string `gorm:"comment:'浏览器'" excel:"name:浏览器;"` // 浏览器 - - City string `gorm:"comment:'城市'" excel:"name:城市;"` // 城市 - - Width int `gorm:"comment:'屏幕'" excel:"name:屏幕;"` // 屏幕 - - Height int `gorm:"comment:'屏幕高度'" excel:"name:屏幕高度;"` // 屏幕高度 - - Ua string `gorm:"comment:'ua记录'" excel:"name:ua记录;"` // ua记录 - - ClientTime int `gorm:"comment:'客户端时间'" excel:"name:客户端时间;"` // 客户端时间 - - CreateTime int64 `gorm:"autoCreateTime;comment:'创建时间'" excel:"name:创建时间;"` // 创建时间 - + Id int `gorm:"primarykey;comment:'uuid'" excel:"name:uuid;"` // uuid + + ProjectKey string `gorm:"comment:'项目key'" excel:"name:项目key;"` // 项目key + + ClientId string `gorm:"comment:'sdk生成的客户端id'" excel:"name:sdk生成的客户端id;"` // sdk生成的客户端id + + UserId string `gorm:"comment:'用户id'" excel:"name:用户id;"` // 用户id + + Os string `gorm:"comment:'系统'" excel:"name:系统;"` // 系统 + + Browser string `gorm:"comment:'浏览器'" excel:"name:浏览器;"` // 浏览器 + + City string `gorm:"comment:'城市'" excel:"name:城市;"` // 城市 + + Width int `gorm:"comment:'屏幕'" excel:"name:屏幕;"` // 屏幕 + + Height int `gorm:"comment:'屏幕高度'" excel:"name:屏幕高度;"` // 屏幕高度 + + Ua string `gorm:"comment:'ua记录'" excel:"name:ua记录;"` // ua记录 + + ClientTime int64 `gorm:"comment:'客户端时间'" excel:"name:客户端时间;"` // 客户端时间 + + CreateTime int64 `gorm:"autoCreateTime;comment:'创建时间'" excel:"name:创建时间;"` // 创建时间 + }