Browse Source

add MyPeers for user

Tao Chen 1 year ago
parent
commit
a4dd39043e

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

@@ -10,6 +10,7 @@ import (
10 10
 	"github.com/gin-gonic/gin"
11 11
 	"gorm.io/gorm"
12 12
 	"strconv"
13
+	"time"
13 14
 )
14 15
 
15 16
 type User struct {
@@ -299,6 +300,51 @@ func (ct *User) MyOauth(c *gin.Context) {
299 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 348
 // groupUsers
303 349
 func (ct *User) GroupUsers(c *gin.Context) {
304 350
 	q := &admin.GroupUsersQuery{}

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

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

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

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

+ 1 - 0
http/router/admin.go

@@ -53,6 +53,7 @@ func UserBind(rg *gin.RouterGroup) {
53 53
 		aR.GET("/current", cont.Current)
54 54
 		aR.POST("/changeCurPwd", cont.ChangeCurPwd)
55 55
 		aR.POST("/myOauth", cont.MyOauth)
56
+		aR.GET("/myPeer", cont.MyPeer)
56 57
 		aR.POST("/groupUsers", cont.GroupUsers)
57 58
 	}
58 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 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 92
 // Create 创建
81 93
 func (ps *PeerService) Create(u *model.Peer) error {
82 94
 	res := global.DB.Create(u).Error