diff --git a/http/controller/admin/user.go b/http/controller/admin/user.go index 2b1f17c..ea8c8cd 100644 --- a/http/controller/admin/user.go +++ b/http/controller/admin/user.go @@ -10,6 +10,7 @@ import ( "github.com/gin-gonic/gin" "gorm.io/gorm" "strconv" + "time" ) type User struct { @@ -299,6 +300,51 @@ func (ct *User) MyOauth(c *gin.Context) { response.Success(c, res) } +// List 列表 +// @Tags 设备 +// @Summary 设备列表 +// @Description 设备列表 +// @Accept json +// @Produce json +// @Param page query int false "页码" +// @Param page_size query int false "页大小" +// @Param time_ago query int false "时间" +// @Param id query string false "ID" +// @Param hostname query string false "主机名" +// @Param uuids query string false "uuids 用逗号分隔" +// @Success 200 {object} response.Response{data=model.PeerList} +// @Failure 500 {object} response.Response +// @Router /admin/user/myPeer [get] +// @Security token +func (ct *User) MyPeer(c *gin.Context) { + query := &admin.PeerQuery{} + if err := c.ShouldBindQuery(query); err != nil { + response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) + return + } + u := service.AllService.UserService.CurUser(c) + res := service.AllService.PeerService.ListFilterByUserId(query.Page, query.PageSize, func(tx *gorm.DB) { + if query.TimeAgo > 0 { + lt := time.Now().Unix() - int64(query.TimeAgo) + tx.Where("last_online_time < ?", lt) + } + if query.TimeAgo < 0 { + lt := time.Now().Unix() + int64(query.TimeAgo) + tx.Where("last_online_time > ?", lt) + } + if query.Id != "" { + tx.Where("id like ?", "%"+query.Id+"%") + } + if query.Hostname != "" { + tx.Where("hostname like ?", "%"+query.Hostname+"%") + } + if query.Uuids != "" { + tx.Where("uuid in (?)", query.Uuids) + } + }, u.Id) + response.Success(c, res) +} + // groupUsers func (ct *User) GroupUsers(c *gin.Context) { q := &admin.GroupUsersQuery{} diff --git a/http/controller/api/login.go b/http/controller/api/login.go index f8be2e3..1d093b5 100644 --- a/http/controller/api/login.go +++ b/http/controller/api/login.go @@ -60,7 +60,7 @@ func (l *Login) Login(c *gin.Context) { ut := service.AllService.UserService.Login(u, &model.LoginLog{ UserId: u.Id, Client: f.DeviceInfo.Type, - DeviceId: f.Id, + DeviceId: f.Id, Uuid: f.Uuid, Ip: c.ClientIP(), Type: model.LoginLogTypeAccount, diff --git a/http/response/admin/user.go b/http/response/admin/user.go index 857fe69..df2a5ae 100644 --- a/http/response/admin/user.go +++ b/http/response/admin/user.go @@ -12,7 +12,7 @@ type LoginPayload struct { } var UserRouteNames = []string{ - "MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", + "MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer", } var AdminRouteNames = []string{"*"} diff --git a/http/router/admin.go b/http/router/admin.go index 368a081..bd1aac9 100644 --- a/http/router/admin.go +++ b/http/router/admin.go @@ -53,6 +53,7 @@ func UserBind(rg *gin.RouterGroup) { aR.GET("/current", cont.Current) aR.POST("/changeCurPwd", cont.ChangeCurPwd) aR.POST("/myOauth", cont.MyOauth) + aR.GET("/myPeer", cont.MyPeer) aR.POST("/groupUsers", cont.GroupUsers) } aRP := rg.Group("/user").Use(middleware.AdminPrivilege()) diff --git a/service/peer.go b/service/peer.go index d64eb99..d534d93 100644 --- a/service/peer.go +++ b/service/peer.go @@ -77,6 +77,18 @@ func (ps *PeerService) List(page, pageSize uint, where func(tx *gorm.DB)) (res * return } +// ListFilterByUserId 根据用户id过滤Peer列表 +func (ps *PeerService) ListFilterByUserId(page, pageSize uint, where func(tx *gorm.DB), userId uint) (res *model.PeerList) { + userWhere := func(tx *gorm.DB) { + tx.Where("user_id = ?", userId) + // 如果还有额外的筛选条件,执行它 + if where != nil { + where(tx) + } + } + return ps.List(page, pageSize, userWhere) +} + // Create 创建 func (ps *PeerService) Create(u *model.Peer) error { res := global.DB.Create(u).Error