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

120 lines
3.2KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/base"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/services/mailer"
  11. "fmt"
  12. "strconv"
  13. )
  14. const (
  15. tplFunds base.TplName = "repo/fund/list"
  16. )
  17. // Funds render repository branch page
  18. func Funds(ctx *context.Context) {
  19. ctx.Data["Title"] = "Funds"
  20. ctx.Data["PageIsFund"] = true
  21. page := ctx.QueryInt("page")
  22. funds, err := models.GetFunds(ctx.Repo.Repository.ID, page)
  23. if err != nil {
  24. ctx.ServerError("GetFunds", err)
  25. return
  26. }
  27. ctx.Data["Funds"] = funds
  28. ctx.Data["FundingToCollaboration"] = ctx.Repo.Repository.FundingToCollaboration
  29. ctx.HTML(200, tplFunds)
  30. }
  31. func Funding(ctx *context.Context) {
  32. //减少发送者点数
  33. //增加接收者点数
  34. //创建transfer记录
  35. var err error
  36. Qty, err := strconv.Atoi(ctx.Query("qty"))
  37. if err != nil {
  38. ctx.Flash.Error("请输入数字")
  39. }
  40. if Qty < 1 {
  41. ctx.Flash.Error("请至少发1个红包!")
  42. ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
  43. return
  44. }
  45. var repoid int
  46. repoid, err = strconv.Atoi(ctx.Query("repoid"))
  47. repoPoint, err := models.GetRepoPointByUserID(ctx.User.ID)
  48. if err != nil {
  49. ctx.ServerError("GetUserByName", err)
  50. return
  51. }
  52. if ctx.User.Point-repoPoint < Qty {
  53. ctx.Flash.Error(fmt.Sprintf("余额不足,转出红包数目应该少于(个人账户余额减去个人名下仓库需要支出的红包数目!个人余额%d;名下仓库需要支出%d", ctx.User.Point, repoPoint))
  54. ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
  55. return
  56. }
  57. why := ctx.Query("why")
  58. toUserName := ctx.Query("toid")
  59. err = models.TransferPoint(ctx.User.Name,
  60. why,
  61. toUserName,
  62. Qty)
  63. if err != nil {
  64. ctx.ServerError("Transfer", err)
  65. return
  66. }
  67. err = models.NewFund(ctx.User.Name,
  68. int64(repoid),
  69. int64(Qty))
  70. if err != nil {
  71. ctx.ServerError("Transfer", err)
  72. return
  73. }
  74. toUser, err := models.GetUserByName(toUserName)
  75. if err != nil {
  76. if models.IsErrUserNotExist(err) {
  77. ctx.NotFound("GetUserByName", nil)
  78. } else {
  79. ctx.ServerError("GetUserByName", err)
  80. }
  81. return
  82. }
  83. got, err := ctx.Repo.Repository.IsCollaborator(ctx.User.ID)
  84. if err != nil {
  85. ctx.ServerError("GetUserByName", err)
  86. return
  87. }
  88. if !ctx.Repo.IsOwner() && !got {
  89. if int64(Qty) >= ctx.Repo.Repository.FundingToCollaboration && ctx.Repo.Repository.FundingToCollaboration != 0 {
  90. userNeedAdd := &models.User{
  91. ID: ctx.User.ID,
  92. }
  93. err = ctx.Repo.Repository.AddCollaborator(userNeedAdd)
  94. if err != nil {
  95. ctx.ServerError("AddCollaborator", err)
  96. return
  97. }
  98. if err := ctx.Repo.Repository.ChangeCollaborationAccessMode(userNeedAdd.ID, models.AccessModeRead); err != nil {
  99. ctx.ServerError("AddCollaborator/ChangeCollaborationAccessMode", err)
  100. return
  101. }
  102. }
  103. }
  104. //发通知邮件给接受积分的人
  105. mailer.SendRecievePointNotifyMail(
  106. ctx.User, //积分发送者
  107. toUser, //积分接受者
  108. Qty, //积分点数
  109. why) //原因
  110. ctx.RedirectToFirst(ctx.Query("redirect_to"), ctx.Repo.RepoLink+"/funds")
  111. }
上海开阖软件有限公司 沪ICP备12045867号-1