func_list.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/configs"
  5. "github.com/xinliangnote/go-gin-api/internal/code"
  6. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  7. "github.com/xinliangnote/go-gin-api/internal/pkg/password"
  8. "github.com/xinliangnote/go-gin-api/internal/services/admin"
  9. "github.com/xinliangnote/go-gin-api/pkg/timeutil"
  10. "github.com/spf13/cast"
  11. )
  12. type listRequest struct {
  13. Page int `form:"page"` // 第几页
  14. PageSize int `form:"page_size"` // 每页显示条数
  15. Username string `form:"username"` // 用户名
  16. Nickname string `form:"nickname"` // 昵称
  17. Mobile string `form:"mobile"` // 手机号
  18. }
  19. type listData struct {
  20. Id int `json:"id"` // ID
  21. HashID string `json:"hashid"` // hashid
  22. Username string `json:"username"` // 用户名
  23. Nickname string `json:"nickname"` // 昵称
  24. Mobile string `json:"mobile"` // 手机号
  25. IsUsed int `json:"is_used"` // 是否启用 1:是 -1:否
  26. IsOnline int `json:"is_online"` // 是否在线 1:是 -1:否
  27. CreatedAt string `json:"created_at"` // 创建时间
  28. CreatedUser string `json:"created_user"` // 创建人
  29. UpdatedAt string `json:"updated_at"` // 更新时间
  30. UpdatedUser string `json:"updated_user"` // 更新人
  31. }
  32. type listResponse struct {
  33. List []listData `json:"list"`
  34. Pagination struct {
  35. Total int `json:"total"`
  36. CurrentPage int `json:"current_page"`
  37. PerPageCount int `json:"per_page_count"`
  38. } `json:"pagination"`
  39. }
  40. // List 管理员列表
  41. // @Summary 管理员列表
  42. // @Description 管理员列表
  43. // @Tags API.admin
  44. // @Accept application/x-www-form-urlencoded
  45. // @Produce json
  46. // @Param page query int true "第几页" default(1)
  47. // @Param page_size query int true "每页显示条数" default(10)
  48. // @Param username query string false "用户名"
  49. // @Param nickname query string false "昵称"
  50. // @Param mobile query string false "手机号"
  51. // @Success 200 {object} listResponse
  52. // @Failure 400 {object} code.Failure
  53. // @Router /api/admin [get]
  54. // @Security LoginToken
  55. func (h *handler) List() core.HandlerFunc {
  56. return func(c core.Context) {
  57. req := new(listRequest)
  58. res := new(listResponse)
  59. if err := c.ShouldBindForm(req); err != nil {
  60. c.AbortWithError(core.Error(
  61. http.StatusBadRequest,
  62. code.ParamBindError,
  63. code.Text(code.ParamBindError)).WithError(err),
  64. )
  65. return
  66. }
  67. page := req.Page
  68. if page == 0 {
  69. page = 1
  70. }
  71. pageSize := req.PageSize
  72. if pageSize == 0 {
  73. pageSize = 10
  74. }
  75. searchData := new(admin.SearchData)
  76. searchData.Page = page
  77. searchData.PageSize = pageSize
  78. searchData.Username = req.Username
  79. searchData.Nickname = req.Nickname
  80. searchData.Mobile = req.Mobile
  81. resListData, err := h.adminService.PageList(c, searchData)
  82. if err != nil {
  83. c.AbortWithError(core.Error(
  84. http.StatusBadRequest,
  85. code.AdminListError,
  86. code.Text(code.AdminListError)).WithError(err),
  87. )
  88. return
  89. }
  90. resCountData, err := h.adminService.PageListCount(c, searchData)
  91. if err != nil {
  92. c.AbortWithError(core.Error(
  93. http.StatusBadRequest,
  94. code.AdminListError,
  95. code.Text(code.AdminListError)).WithError(err),
  96. )
  97. return
  98. }
  99. res.Pagination.Total = cast.ToInt(resCountData)
  100. res.Pagination.PerPageCount = pageSize
  101. res.Pagination.CurrentPage = page
  102. res.List = make([]listData, len(resListData))
  103. for k, v := range resListData {
  104. hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(v.Id)})
  105. if err != nil {
  106. c.AbortWithError(core.Error(
  107. http.StatusBadRequest,
  108. code.HashIdsEncodeError,
  109. code.Text(code.HashIdsEncodeError)).WithError(err),
  110. )
  111. return
  112. }
  113. isOnline := -1
  114. if h.cache.Exists(configs.RedisKeyPrefixLoginUser + password.GenerateLoginToken(v.Id)) {
  115. isOnline = 1
  116. }
  117. data := listData{
  118. Id: cast.ToInt(v.Id),
  119. HashID: hashId,
  120. Username: v.Username,
  121. Nickname: v.Nickname,
  122. Mobile: v.Mobile,
  123. IsUsed: cast.ToInt(v.IsUsed),
  124. IsOnline: isOnline,
  125. CreatedAt: v.CreatedAt.Format(timeutil.CSTLayout),
  126. CreatedUser: v.CreatedUser,
  127. UpdatedAt: v.UpdatedAt.Format(timeutil.CSTLayout),
  128. UpdatedUser: v.UpdatedUser,
  129. }
  130. res.List[k] = data
  131. }
  132. c.Payload(res)
  133. }
  134. }