dev.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package pub
  2. import (
  3. "app/apis/middleware"
  4. "app/commons/constant"
  5. "app/commons/core"
  6. "app/commons/model/entity"
  7. "app/commons/utils"
  8. "app/tasks"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. "github.com/shopspring/decimal"
  12. "time"
  13. )
  14. // 开发者使用 -- 超级登录
  15. func (s *Server) DevAuth(ctx *gin.Context) {
  16. c := s.FromContext(ctx)
  17. type request struct {
  18. Uid string `json:"uid"`
  19. }
  20. req := new(request)
  21. if err := c.BindJSON(&req); err != nil {
  22. c.Fail(err.Error())
  23. return
  24. }
  25. if !utils.IsDigitsOnly(req.Uid) {
  26. c.Resp("UID只能是数字")
  27. return
  28. }
  29. if req.Uid == "" || len(req.Uid) > 32 {
  30. c.Resp("UID必填/长度大于32")
  31. return
  32. }
  33. user, err := s.GetUserByUserUid(req.Uid)
  34. if err != nil {
  35. user, err = s.CheckUserWithOpenIdAndUid(fmt.Sprintf("DEVOPENID:%s", fmt.Sprintf("DEVUID:%s", req.Uid)), req.Uid, false)
  36. if err != nil {
  37. c.Resp(err.Error())
  38. return
  39. }
  40. }
  41. type LoginResp struct {
  42. Uid string `json:"uid"`
  43. OpenId string `json:"openId"`
  44. Code string `json:"code"`
  45. Authorization string `json:"authorization"`
  46. }
  47. resp := new(LoginResp)
  48. resp.Uid = req.Uid
  49. resp.OpenId = user.OpenId
  50. resp.Code = user.Code
  51. resp.Authorization, err = s.getTokenWithUser(user)
  52. if err != nil {
  53. c.Resp(err.Error())
  54. return
  55. }
  56. // 将新token设置为用户当前有效token
  57. if err = middleware.SetUserCurrentToken(user.Id, resp.Authorization, 24*time.Hour); err != nil {
  58. core.Log.Errorf("设置用户当前token失败:%+v", err.Error())
  59. }
  60. c.Resp(resp)
  61. }
  62. func (s *Server) DevStake(ctx *gin.Context) {
  63. c := s.FromContext(ctx)
  64. type request struct {
  65. Uid string `json:"uid"`
  66. Quantity decimal.Decimal `json:"quantity"` // 消耗币种的数量
  67. }
  68. req := new(request)
  69. if err := c.ShouldBindBodyWithJSON(req); err != nil {
  70. c.Fail(constant.ErrorParams)
  71. return
  72. }
  73. // 所有 post 接口 必须做 频繁控制
  74. if !c.RepeatFilter(fmt.Sprintf("StakeByUser:%d", c.UserId()), time.Second*3) {
  75. c.Fail(constant.ErrorFrequent)
  76. return
  77. }
  78. // tips: 必须做非零限制
  79. if req.Quantity.LessThanOrEqual(decimal.Zero) {
  80. c.Fail(constant.ErrorResponseExceedAmountLimit)
  81. return
  82. }
  83. user, err := s.GetUserByUserUid(req.Uid)
  84. if err != nil {
  85. core.Log.Infof("GetUserByUserId:%s", req.Uid)
  86. c.Fail(constant.ErrorInfo)
  87. return
  88. }
  89. if !user.Enable {
  90. c.Fail(constant.ErrorAccount)
  91. return
  92. }
  93. currentProduct, err := s.FirstStakeProduct(s.DB().Where("pledge_mode", entity.StakeProductTypeCurrent))
  94. if err != nil {
  95. c.Fail(constant.ErrorNotOpen)
  96. return
  97. }
  98. if !currentProduct.Enable {
  99. c.Fail(constant.ErrorNotOpen)
  100. return
  101. }
  102. // todo: 开始质押
  103. err = s.CurrentStake(user, currentProduct, req.Quantity, 10086)
  104. if err != nil {
  105. c.Fail(err.Error())
  106. return
  107. }
  108. go s.StakeHandler() // 异步处理相关质押业务
  109. c.Resp(nil)
  110. }
  111. // todo:触发期任务
  112. func (s *Server) DevPeriodJob(ctx *gin.Context) {
  113. c := s.FromContext(ctx)
  114. tasks.PeriodTask()
  115. c.Resp(nil)
  116. }
  117. // todo:触发日任务
  118. func (s *Server) DevDailyJob(ctx *gin.Context) {
  119. c := s.FromContext(ctx)
  120. tasks.DailyTask()
  121. c.Resp(nil)
  122. }