diff --git a/models/user.go b/models/user.go index b56df9b..e78d2d1 100644 --- a/models/user.go +++ b/models/user.go @@ -32,8 +32,8 @@ type User struct { Status int GroupID uint Storage uint64 - OpenID string `json:"-"` - TwoFactor string `json:"-"` + OpenID string + TwoFactor string Avatar string Options string `json:"-",gorm:"type:text"` Authn string `gorm:"type:text"` diff --git a/routers/controllers/admin.go b/routers/controllers/admin.go index 68db9d2..a5010a1 100644 --- a/routers/controllers/admin.go +++ b/routers/controllers/admin.go @@ -271,3 +271,14 @@ func AdminGetGroup(c *gin.Context) { c.JSON(200, ErrorResponse(err)) } } + +// AdminListUser 列出用户 +func AdminListUser(c *gin.Context) { + var service admin.AdminListService + if err := c.ShouldBindJSON(&service); err == nil { + res := service.Users() + c.JSON(200, res) + } else { + c.JSON(200, ErrorResponse(err)) + } +} diff --git a/routers/router.go b/routers/router.go index 5e49c70..2d5669a 100644 --- a/routers/router.go +++ b/routers/router.go @@ -365,6 +365,12 @@ func InitMasterRouter() *gin.Engine { group.DELETE(":id", controllers.AdminDeleteGroup) } + user := admin.Group("user") + { + // 列出用户 + user.POST("list", controllers.AdminListUser) + } + } // 用户 diff --git a/service/admin/user.go b/service/admin/user.go new file mode 100644 index 0000000..768355f --- /dev/null +++ b/service/admin/user.go @@ -0,0 +1,34 @@ +package admin + +import ( + model "github.com/HFO4/cloudreve/models" + "github.com/HFO4/cloudreve/pkg/serializer" +) + +// Users 列出用户 +func (service *AdminListService) Users() serializer.Response { + var res []model.User + total := 0 + + tx := model.DB.Model(&model.User{}) + if service.OrderBy != "" { + tx = tx.Order(service.OrderBy) + } + + for k, v := range service.Conditions { + tx = tx.Where(k+" = ?", v) + } + + // 计算总数用于分页 + tx.Count(&total) + + // 查询记录 + tx.Set("gorm:auto_preload", true).Limit(service.PageSize).Offset((service.Page - 1) * service.PageSize).Find(&res) + + // 补齐缺失用户组 + + return serializer.Response{Data: map[string]interface{}{ + "total": total, + "items": res, + }} +}