feat(user):基本完成用户分页查询列表功能,待测。
This commit is contained in:
parent
cba0968e18
commit
dd38fd6d6f
|
@ -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},
|
||||
)
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"`
|
||||
Page int `json:"current"`
|
||||
Size int `json:"pageSize"`
|
||||
Total int `json:"total"`
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user