func_list.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package authorized
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/services/authorized"
  7. "github.com/xinliangnote/go-gin-api/pkg/timeutil"
  8. "github.com/spf13/cast"
  9. )
  10. type listRequest struct {
  11. Page int `form:"page"` // 第几页
  12. PageSize int `form:"page_size"` // 每页显示条数
  13. BusinessKey string `form:"business_key"` // 调用方key
  14. BusinessSecret string `form:"business_secret"` // 调用方secret
  15. BusinessDeveloper string `form:"business_developer"` // 调用方对接人
  16. Remark string `form:"remark"` // 备注
  17. }
  18. type listData struct {
  19. Id int `json:"id"` // ID
  20. HashID string `json:"hashid"` // hashid
  21. BusinessKey string `json:"business_key"` // 调用方key
  22. BusinessSecret string `json:"business_secret"` // 调用方secret
  23. BusinessDeveloper string `json:"business_developer"` // 调用方对接人
  24. Remark string `json:"remark"` // 备注
  25. IsUsed int `json:"is_used"` // 是否启用 1:是 -1:否
  26. CreatedAt string `json:"created_at"` // 创建时间
  27. CreatedUser string `json:"created_user"` // 创建人
  28. UpdatedAt string `json:"updated_at"` // 更新时间
  29. UpdatedUser string `json:"updated_user"` // 更新人
  30. }
  31. type listResponse struct {
  32. List []listData `json:"list"`
  33. Pagination struct {
  34. Total int `json:"total"`
  35. CurrentPage int `json:"current_page"`
  36. PerPageCount int `json:"per_page_count"`
  37. } `json:"pagination"`
  38. }
  39. // List 调用方列表
  40. // @Summary 调用方列表
  41. // @Description 调用方列表
  42. // @Tags API.authorized
  43. // @Accept application/x-www-form-urlencoded
  44. // @Produce json
  45. // @Param page query int true "第几页" default(1)
  46. // @Param page_size query int true "每页显示条数" default(10)
  47. // @Param business_key query string false "调用方key"
  48. // @Param business_secret query string false "调用方secret"
  49. // @Param business_developer query string false "调用方对接人"
  50. // @Param remark path string false "备注"
  51. // @Success 200 {object} listResponse
  52. // @Failure 400 {object} code.Failure
  53. // @Router /api/authorized [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(authorized.SearchData)
  76. searchData.Page = page
  77. searchData.PageSize = pageSize
  78. searchData.BusinessKey = req.BusinessKey
  79. searchData.BusinessSecret = req.BusinessSecret
  80. searchData.Remark = req.Remark
  81. resListData, err := h.authorizedService.PageList(c, searchData)
  82. if err != nil {
  83. c.AbortWithError(core.Error(
  84. http.StatusBadRequest,
  85. code.AuthorizedListError,
  86. code.Text(code.AuthorizedListError)).WithError(err),
  87. )
  88. return
  89. }
  90. resCountData, err := h.authorizedService.PageListCount(c, searchData)
  91. if err != nil {
  92. c.AbortWithError(core.Error(
  93. http.StatusBadRequest,
  94. code.AuthorizedListError,
  95. code.Text(code.AuthorizedListError)).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. data := listData{
  114. Id: cast.ToInt(v.Id),
  115. HashID: hashId,
  116. BusinessKey: v.BusinessKey,
  117. BusinessSecret: v.BusinessSecret,
  118. BusinessDeveloper: v.BusinessDeveloper,
  119. Remark: v.Remark,
  120. IsUsed: cast.ToInt(v.IsUsed),
  121. CreatedAt: v.CreatedAt.Format(timeutil.CSTLayout),
  122. CreatedUser: v.CreatedUser,
  123. UpdatedAt: v.UpdatedAt.Format(timeutil.CSTLayout),
  124. UpdatedUser: v.UpdatedUser,
  125. }
  126. res.List[k] = data
  127. }
  128. c.Payload(res)
  129. }
  130. }