func_list.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package cron
  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/pkg/validation"
  7. "github.com/xinliangnote/go-gin-api/internal/repository/mysql/cron_task"
  8. "github.com/xinliangnote/go-gin-api/internal/services/cron"
  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. Name string `form:"name"` // 任务名称
  16. Protocol int `form:"protocol"` // 执行方式 1:shell 2:http
  17. IsUsed int `form:"is_used"` // 是否启用 1:是 -1:否
  18. }
  19. type listData struct {
  20. Id int `json:"id"` // ID
  21. HashID string `json:"hashid"` // hashid
  22. Name string `json:"name"` // 任务名称
  23. Protocol int `json:"protocol"` // 执行方式 1:shell 2:http
  24. ProtocolText string `json:"protocol_text"` // 执行方式
  25. Spec string `json:"spec"` // crontab 表达式
  26. Command string `json:"command"` // 执行命令
  27. HttpMethod int `json:"http_method"` // http 请求方式 1:get 2:post
  28. HttpMethodText string `json:"http_method_text"` // http 请求方式
  29. Timeout int `json:"timeout"` // 超时时间(单位:秒)
  30. RetryTimes int `json:"retry_times"` // 重试次数
  31. RetryInterval int `json:"retry_interval"` // 重试间隔(单位:秒)
  32. NotifyStatus int `json:"notify_status"` // 执行结束是否通知 1:不通知 2:失败通知 3:结束通知 4:结果关键字匹配通知
  33. NotifyStatusText string `json:"notify_status_text"` // 执行结束是否通知
  34. IsUsed int `json:"is_used"` // 是否启用 1=启用 2=禁用
  35. IsUsedText string `json:"is_used_text"` // 是否启用
  36. CreatedAt string `json:"created_at"` // 创建时间
  37. CreatedUser string `json:"created_user"` // 创建人
  38. UpdatedAt string `json:"updated_at"` // 更新时间
  39. UpdatedUser string `json:"updated_user"` // 更新人
  40. }
  41. type listResponse struct {
  42. List []listData `json:"list"`
  43. Pagination struct {
  44. Total int `json:"total"`
  45. CurrentPage int `json:"current_page"`
  46. PerPageCount int `json:"per_page_count"`
  47. } `json:"pagination"`
  48. }
  49. // List 任务列表
  50. // @Summary 任务列表
  51. // @Description 任务列表
  52. // @Tags API.cron
  53. // @Accept application/x-www-form-urlencoded
  54. // @Produce json
  55. // @Param page query int true "第几页" default(1)
  56. // @Param page_size query int true "每页显示条数" default(10)
  57. // @Param name query string false "任务名称"
  58. // @Param protocol query int false "执行方式 1:shell 2:http"
  59. // @Param is_used query int false "是否启用 1:是 -1:否"
  60. // @Success 200 {object} listResponse
  61. // @Failure 400 {object} code.Failure
  62. // @Router /api/cron [get]
  63. // @Security LoginToken
  64. func (h *handler) List() core.HandlerFunc {
  65. return func(ctx core.Context) {
  66. req := new(listRequest)
  67. res := new(listResponse)
  68. if err := ctx.ShouldBindForm(req); err != nil {
  69. ctx.AbortWithError(core.Error(
  70. http.StatusBadRequest,
  71. code.ParamBindError,
  72. validation.Error(err)).WithError(err),
  73. )
  74. return
  75. }
  76. page := req.Page
  77. if page == 0 {
  78. page = 1
  79. }
  80. pageSize := req.PageSize
  81. if pageSize == 0 {
  82. pageSize = 10
  83. }
  84. searchData := new(cron.SearchData)
  85. searchData.Page = req.Page
  86. searchData.PageSize = req.PageSize
  87. searchData.Name = req.Name
  88. searchData.Protocol = cast.ToInt32(req.Protocol)
  89. searchData.IsUsed = cast.ToInt32(req.IsUsed)
  90. resListData, err := h.cronService.PageList(ctx, searchData)
  91. if err != nil {
  92. ctx.AbortWithError(core.Error(
  93. http.StatusBadRequest,
  94. code.CronListError,
  95. code.Text(code.CronListError)).WithError(err),
  96. )
  97. return
  98. }
  99. resCountData, err := h.cronService.PageListCount(ctx, searchData)
  100. if err != nil {
  101. ctx.AbortWithError(core.Error(
  102. http.StatusBadRequest,
  103. code.CronListError,
  104. code.Text(code.CronListError)).WithError(err),
  105. )
  106. return
  107. }
  108. res.Pagination.Total = cast.ToInt(resCountData)
  109. res.Pagination.PerPageCount = pageSize
  110. res.Pagination.CurrentPage = page
  111. res.List = make([]listData, len(resListData))
  112. for k, v := range resListData {
  113. hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(v.Id)})
  114. if err != nil {
  115. ctx.AbortWithError(core.Error(
  116. http.StatusBadRequest,
  117. code.HashIdsEncodeError,
  118. code.Text(code.HashIdsEncodeError)).WithError(err),
  119. )
  120. return
  121. }
  122. data := listData{
  123. Id: cast.ToInt(v.Id),
  124. HashID: hashId,
  125. Name: v.Name,
  126. Protocol: cast.ToInt(v.Protocol),
  127. ProtocolText: cron_task.ProtocolText[v.Protocol],
  128. Spec: v.Spec,
  129. Command: v.Command,
  130. HttpMethod: cast.ToInt(v.HttpMethod),
  131. HttpMethodText: cron_task.HttpMethodText[v.HttpMethod],
  132. Timeout: cast.ToInt(v.Timeout),
  133. RetryTimes: cast.ToInt(v.RetryTimes),
  134. RetryInterval: cast.ToInt(v.RetryInterval),
  135. NotifyStatus: cast.ToInt(v.NotifyStatus),
  136. NotifyStatusText: cron_task.NotifyStatusText[v.NotifyStatus],
  137. IsUsed: cast.ToInt(v.IsUsed),
  138. IsUsedText: cron_task.IsUsedText[v.IsUsed],
  139. CreatedAt: v.CreatedAt.Format(timeutil.CSTLayout),
  140. CreatedUser: v.CreatedUser,
  141. UpdatedAt: v.UpdatedAt.Format(timeutil.CSTLayout),
  142. UpdatedUser: v.UpdatedUser,
  143. }
  144. res.List[k] = data
  145. }
  146. ctx.Payload(res)
  147. }
  148. }