You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

point.go 4.2 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package point
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/base"
  5. "code.gitea.io/gitea/modules/context"
  6. "code.gitea.io/gitea/modules/log"
  7. "code.gitea.io/gitea/routers/response"
  8. "code.gitea.io/gitea/services/reward"
  9. "code.gitea.io/gitea/services/reward/point/account"
  10. "errors"
  11. "net/http"
  12. )
  13. const tplPoint base.TplName = "reward/point"
  14. const tplPointRule base.TplName = "reward/point/rule"
  15. type AccountResponse struct {
  16. Balance int64
  17. TotalEarned int64
  18. TotalConsumed int64
  19. }
  20. func GetPointAccount(ctx *context.Context) {
  21. userId := ctx.User.ID
  22. a, err := account.GetAccount(userId)
  23. if err != nil {
  24. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  25. return
  26. }
  27. res := &AccountResponse{
  28. Balance: a.Balance,
  29. TotalEarned: a.TotalEarned,
  30. TotalConsumed: a.TotalConsumed,
  31. }
  32. ctx.JSON(http.StatusOK, response.SuccessWithData(res))
  33. }
  34. func GetPointRecordList(ctx *context.Context) {
  35. operateType := ctx.Query("Operate")
  36. page := ctx.QueryInt("Page")
  37. var orderBy models.RewardOperateOrderBy
  38. switch ctx.Query("sort") {
  39. default:
  40. orderBy = models.RewardOrderByIDDesc
  41. }
  42. t := models.GetRewardOperateTypeInstance(operateType)
  43. if t == "" {
  44. ctx.JSON(http.StatusOK, response.ServerError("param error"))
  45. return
  46. }
  47. r, err := reward.GetRewardRecordList(&models.RewardRecordListOpts{
  48. ListOptions: models.ListOptions{PageSize: 10, Page: page},
  49. UserId: ctx.User.ID,
  50. OperateType: t,
  51. RewardType: models.RewardTypePoint,
  52. OrderBy: orderBy,
  53. IsAdmin: false,
  54. UserName: ctx.User.Name,
  55. })
  56. if err != nil {
  57. log.Error("GetPointRecordList error.%v", err)
  58. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  59. return
  60. }
  61. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  62. return
  63. }
  64. func OperatePointAccountBalance(ctx *context.Context, req models.AdminRewardOperateReq) {
  65. req.RewardType = models.RewardTypePoint
  66. if req.OperateType.Name() == "" || req.Remark == "" {
  67. ctx.JSON(http.StatusOK, "param error")
  68. return
  69. }
  70. err := reward.AdminBalanceOperate(req, ctx.User)
  71. if err != nil {
  72. log.Error("OperatePointAccountBalance error.%v", err)
  73. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  74. return
  75. }
  76. ctx.JSON(http.StatusOK, response.Success())
  77. }
  78. func GetPointPage(ctx *context.Context) {
  79. ctx.HTML(200, tplPoint)
  80. }
  81. func GetRulePage(ctx *context.Context) {
  82. ctx.HTML(200, tplPointRule)
  83. }
  84. func GetAdminRewardList(ctx *context.Context) {
  85. opts, err := buildAdminRewardRecordListOpts(ctx)
  86. if err != nil {
  87. log.Error("buildAdminRewardRecordListOpts error.%v", err)
  88. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  89. return
  90. }
  91. username := ctx.Query("userName")
  92. if username != "" {
  93. user, err := models.GetUserByName(username)
  94. if err != nil {
  95. log.Error("GetUserByName error.%v", err)
  96. if models.IsErrUserNotExist(err) {
  97. ctx.JSON(http.StatusOK, response.ServerError("user not exist"))
  98. } else {
  99. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  100. }
  101. return
  102. }
  103. opts.UserId = user.ID
  104. opts.UserName = user.Name
  105. }
  106. r, err := reward.GetRewardRecordList(opts)
  107. if err != nil {
  108. log.Error("GetRewardRecordList error.%v", err)
  109. ctx.JSON(http.StatusOK, response.ServerError(err.Error()))
  110. return
  111. }
  112. ctx.JSON(http.StatusOK, response.SuccessWithData(r))
  113. }
  114. func buildAdminRewardRecordListOpts(ctx *context.Context) (*models.RewardRecordListOpts, error) {
  115. operateType := ctx.Query("operate")
  116. sourceType := ctx.Query("source")
  117. actionType := ctx.QueryInt("action")
  118. serialNo := ctx.Query("serialNo")
  119. status := ctx.Query("status")
  120. page := ctx.QueryInt("page")
  121. var orderBy models.RewardOperateOrderBy
  122. switch ctx.Query("sort") {
  123. default:
  124. orderBy = models.RewardOrderByIDDesc
  125. }
  126. t := models.GetRewardOperateTypeInstance(operateType)
  127. if t == "" {
  128. return nil, errors.New("param error")
  129. }
  130. opts := &models.RewardRecordListOpts{
  131. ListOptions: models.ListOptions{PageSize: 10, Page: page},
  132. OperateType: t,
  133. RewardType: models.RewardTypePoint,
  134. OrderBy: orderBy,
  135. SourceType: sourceType,
  136. ActionType: actionType,
  137. SerialNo: serialNo,
  138. IsAdmin: true,
  139. Status: status,
  140. }
  141. return opts, nil
  142. }