Tao Chen 1 год назад
Родитель
Сommit
a4dd39043e
5 измененных файлов с 61 добавлено и 2 удалено
  1. 46 0
      http/controller/admin/user.go
  2. 1 1
      http/controller/api/login.go
  3. 1 1
      http/response/admin/user.go
  4. 1 0
      http/router/admin.go
  5. 12 0
      service/peer.go

+ 46 - 0
http/controller/admin/user.go

@@ -10,6 +10,7 @@ import (
10
 	"github.com/gin-gonic/gin"
10
 	"github.com/gin-gonic/gin"
11
 	"gorm.io/gorm"
11
 	"gorm.io/gorm"
12
 	"strconv"
12
 	"strconv"
13
+	"time"
13
 )
14
 )
14
 
15
 
15
 type User struct {
16
 type User struct {
@@ -299,6 +300,51 @@ func (ct *User) MyOauth(c *gin.Context) {
299
 	response.Success(c, res)
300
 	response.Success(c, res)
300
 }
301
 }
301
 
302
 
303
+// List 列表
304
+// @Tags 设备
305
+// @Summary 设备列表
306
+// @Description 设备列表
307
+// @Accept  json
308
+// @Produce  json
309
+// @Param page query int false "页码"
310
+// @Param page_size query int false "页大小"
311
+// @Param time_ago query int false "时间"
312
+// @Param id query string false "ID"
313
+// @Param hostname query string false "主机名"
314
+// @Param uuids query string false "uuids 用逗号分隔"
315
+// @Success 200 {object} response.Response{data=model.PeerList}
316
+// @Failure 500 {object} response.Response
317
+// @Router /admin/user/myPeer [get]
318
+// @Security token
319
+func (ct *User) MyPeer(c *gin.Context) {
320
+	query := &admin.PeerQuery{}
321
+	if err := c.ShouldBindQuery(query); err != nil {
322
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
323
+		return
324
+	}
325
+	u := service.AllService.UserService.CurUser(c)
326
+	res := service.AllService.PeerService.ListFilterByUserId(query.Page, query.PageSize, func(tx *gorm.DB) {
327
+		if query.TimeAgo > 0 {
328
+			lt := time.Now().Unix() - int64(query.TimeAgo)
329
+			tx.Where("last_online_time < ?", lt)
330
+		}
331
+		if query.TimeAgo < 0 {
332
+			lt := time.Now().Unix() + int64(query.TimeAgo)
333
+			tx.Where("last_online_time > ?", lt)
334
+		}
335
+		if query.Id != "" {
336
+			tx.Where("id like ?", "%"+query.Id+"%")
337
+		}
338
+		if query.Hostname != "" {
339
+			tx.Where("hostname like ?", "%"+query.Hostname+"%")
340
+		}
341
+		if query.Uuids != "" {
342
+			tx.Where("uuid in (?)", query.Uuids)
343
+		}
344
+	}, u.Id)
345
+	response.Success(c, res)
346
+}
347
+
302
 // groupUsers
348
 // groupUsers
303
 func (ct *User) GroupUsers(c *gin.Context) {
349
 func (ct *User) GroupUsers(c *gin.Context) {
304
 	q := &admin.GroupUsersQuery{}
350
 	q := &admin.GroupUsersQuery{}

+ 1 - 1
http/controller/api/login.go

@@ -60,7 +60,7 @@ func (l *Login) Login(c *gin.Context) {
60
 	ut := service.AllService.UserService.Login(u, &model.LoginLog{
60
 	ut := service.AllService.UserService.Login(u, &model.LoginLog{
61
 		UserId:   u.Id,
61
 		UserId:   u.Id,
62
 		Client:   f.DeviceInfo.Type,
62
 		Client:   f.DeviceInfo.Type,
63
-		DeviceId:       f.Id,
63
+		DeviceId: f.Id,
64
 		Uuid:     f.Uuid,
64
 		Uuid:     f.Uuid,
65
 		Ip:       c.ClientIP(),
65
 		Ip:       c.ClientIP(),
66
 		Type:     model.LoginLogTypeAccount,
66
 		Type:     model.LoginLogTypeAccount,

+ 1 - 1
http/response/admin/user.go

@@ -12,7 +12,7 @@ type LoginPayload struct {
12
 }
12
 }
13
 
13
 
14
 var UserRouteNames = []string{
14
 var UserRouteNames = []string{
15
-	"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection",
15
+	"MyTagList", "MyAddressBookList", "MyInfo", "MyAddressBookCollection", "MyPeer",
16
 }
16
 }
17
 var AdminRouteNames = []string{"*"}
17
 var AdminRouteNames = []string{"*"}
18
 
18
 

+ 1 - 0
http/router/admin.go

@@ -53,6 +53,7 @@ func UserBind(rg *gin.RouterGroup) {
53
 		aR.GET("/current", cont.Current)
53
 		aR.GET("/current", cont.Current)
54
 		aR.POST("/changeCurPwd", cont.ChangeCurPwd)
54
 		aR.POST("/changeCurPwd", cont.ChangeCurPwd)
55
 		aR.POST("/myOauth", cont.MyOauth)
55
 		aR.POST("/myOauth", cont.MyOauth)
56
+		aR.GET("/myPeer", cont.MyPeer)
56
 		aR.POST("/groupUsers", cont.GroupUsers)
57
 		aR.POST("/groupUsers", cont.GroupUsers)
57
 	}
58
 	}
58
 	aRP := rg.Group("/user").Use(middleware.AdminPrivilege())
59
 	aRP := rg.Group("/user").Use(middleware.AdminPrivilege())

+ 12 - 0
service/peer.go

@@ -77,6 +77,18 @@ func (ps *PeerService) List(page, pageSize uint, where func(tx *gorm.DB)) (res *
77
 	return
77
 	return
78
 }
78
 }
79
 
79
 
80
+// ListFilterByUserId 根据用户id过滤Peer列表
81
+func (ps *PeerService) ListFilterByUserId(page, pageSize uint, where func(tx *gorm.DB), userId uint) (res *model.PeerList) {
82
+	userWhere := func(tx *gorm.DB) {
83
+		tx.Where("user_id = ?", userId)
84
+		// 如果还有额外的筛选条件,执行它
85
+		if where != nil {
86
+			where(tx)
87
+		}
88
+	}
89
+	return ps.List(page, pageSize, userWhere)
90
+}
91
+
80
 // Create 创建
92
 // Create 创建
81
 func (ps *PeerService) Create(u *model.Peer) error {
93
 func (ps *PeerService) Create(u *model.Peer) error {
82
 	res := global.DB.Create(u).Error
94
 	res := global.DB.Create(u).Error