From dd38fd6d6f0def2bd681aa6bdf57d257f3d608ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 12 Aug 2022 15:22:34 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=88=86=E9=A1=B5=E6=9F=A5=E8=AF=A2=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=BE=85=E6=B5=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/user.go | 37 +++++++++++++++++++++++++++++++++++++ model/user_detail.go | 11 +++++++++++ repository/user.go | 32 ++++++++++++++++++++++++++++++++ response/base_response.go | 19 ++++++++++++++++--- 4 files changed, 96 insertions(+), 3 deletions(-) diff --git a/controller/user.go b/controller/user.go index e56a97c..1d38c77 100644 --- a/controller/user.go +++ b/controller/user.go @@ -4,11 +4,13 @@ import ( "electricity_bill_calc/cache" "electricity_bill_calc/exceptions" "electricity_bill_calc/model" + "electricity_bill_calc/repository" "electricity_bill_calc/response" "electricity_bill_calc/security" "electricity_bill_calc/service" "errors" "net/http" + "strconv" "github.com/gin-gonic/gin" ) @@ -27,6 +29,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.DELETE("/logout", security.MustAuthenticated, UserController.Logout) UserController.Router.DELETE("/password/:uid", security.OPSAuthorize, UserController.InvalidUserPassword) UserController.Router.PUT("/password", UserController.ResetUserPassword) + UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser) } type LoginFormData struct { @@ -133,3 +136,37 @@ func (_UserController) ResetUserPassword(c *gin.Context) { } result.NotAccept("用户凭据未能成功更新。") } + +func (_UserController) ListPagedUser(c *gin.Context) { + result := response.NewResult(c) + requestPage, err := strconv.Atoi(c.DefaultQuery("page", "1")) + if err != nil { + result.NotAccept("查询参数[page]格式不正确。") + return + } + requestKeyword := c.DefaultQuery("keyword", "") + requestUserType, err := strconv.Atoi(c.DefaultQuery("type", "-1")) + if err != nil { + result.NotAccept("查询参数[type]格式不正确。") + return + } + var requestUserStat *bool + state, err := strconv.ParseBool(c.Query("state")) + if err != nil { + requestUserStat = nil + } else { + requestUserStat = &state + } + users, total, err := repository.UserRepo.ListUserDetail(requestKeyword, requestUserType, requestUserStat, requestPage) + if err != nil { + result.Error(http.StatusNotFound, err.Error()) + return + } + result.Json( + http.StatusOK, + http.StatusOK, + "已取得符合条件的用户集合。", + response.NewPagedResponse(requestPage, total).ToMap(), + gin.H{"accounts": users}, + ) +} diff --git a/model/user_detail.go b/model/user_detail.go index d680656..5086b54 100644 --- a/model/user_detail.go +++ b/model/user_detail.go @@ -23,3 +23,14 @@ type UserDetail struct { func (UserDetail) TableName() string { return "user_detail" } + +type JoinedUserDetail struct { + UserDetail `xorm:"extends"` + Username string `json:"username"` + Type int8 `json:"type"` + Enabled bool `json:"enabled"` +} + +func (JoinedUserDetail) TableName() string { + return "user" +} diff --git a/repository/user.go b/repository/user.go index 692f18c..bf73949 100644 --- a/repository/user.go +++ b/repository/user.go @@ -2,6 +2,7 @@ package repository import ( "electricity_bill_calc/cache" + "electricity_bill_calc/config" "electricity_bill_calc/global" "electricity_bill_calc/model" @@ -50,3 +51,34 @@ func (_UserRepository) FindUserByID(uid string) (*model.User, error) { } return _postProcessSingle(user, has, err) } + +func (_UserRepository) ListUserDetail(keyword string, userType int, userState *bool, page int) ([]*model.JoinedUserDetail, int64, error) { + var cond = builder.NewCond() + if len(keyword) != 0 { + keywordCond := builder.NewCond(). + Or(builder.Like{"user.username", keyword}). + Or(builder.Like{"user_detail.name", keyword}) + cond = cond.And(keywordCond) + } + if userType != -1 { + cond = cond.And(builder.Eq{"user.type": userType}) + } + if userState != nil { + cond = cond.And(builder.Eq{"user.enabled": *userState}) + } + startItem := (page - 1) * config.ServiceSettings.ItemsPageSize + total, err := global.DBConn. + Join("INNER", "user_detail", "user_detail.id=user.id"). + Where(cond). + Count() + if err != nil { + return nil, -1, err + } + users := make([]*model.JoinedUserDetail, 0) + err = global.DBConn. + Join("INNER", "user_detail", "user_detail.id=user.id"). + Where(cond). + Limit(config.ServiceSettings.ItemsPageSize, startItem). + Find(users) + return users, total, err +} diff --git a/response/base_response.go b/response/base_response.go index df479c7..cf34f31 100644 --- a/response/base_response.go +++ b/response/base_response.go @@ -1,6 +1,7 @@ package response import ( + "electricity_bill_calc/config" "net/http" "github.com/gin-gonic/gin" @@ -16,9 +17,9 @@ type BaseResponse struct { } type PagedResponse struct { - Page int `json:"page"` - Size int `json:"pageSize"` - Total int `json:"total"` + Page int `json:"current"` + Size int `json:"pageSize"` + Total int64 `json:"total"` } func NewResult(ctx *gin.Context) *Result { @@ -96,3 +97,15 @@ func (r *Result) Json(status, code int, msg string, payloads ...map[string]inter r.Ctx.JSON(status, finalPayload) } + +func NewPagedResponse(page int, total int64) *PagedResponse { + return &PagedResponse{page, config.ServiceSettings.ItemsPageSize, total} +} + +func (r PagedResponse) ToMap() map[string]interface{} { + return gin.H{ + "current": r.Page, + "pageSize": r.Size, + "total": r.Total, + } +}