package magic import ( "app/commons/constant" "app/commons/model/entity" "fmt" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" "time" ) func (s *Server) StakeByUser(ctx *gin.Context) { c := s.FromContext(ctx) type request struct { Quantity decimal.Decimal `json:"quantity"` // 消耗币种的数量 } req := new(request) if err := c.ShouldBindBodyWithJSON(req); err != nil { c.Fail(constant.ErrorParams) return } // 所有 post 接口 必须做 频繁控制 if !c.RepeatFilter(fmt.Sprintf("StakeByUser:%d", c.UserId()), time.Second*3) { c.Fail(constant.ErrorFrequent) return } // tips: 必须做非零限制 if req.Quantity.LessThanOrEqual(decimal.Zero) { c.Fail(constant.ErrorResponseExceedAmountLimit) return } user, err := s.GetUserByUserUid(c.Uid()) if err != nil { c.Fail(constant.ErrorInfo) return } if !user.Enable { c.Fail(constant.ErrorAccount) return } paySymbol := constant.CoinSystemSymbol // todo: 检查用户是否合法 userAsset, err := s.GetAssetBySymbol(c.UserId(), paySymbol) if err != nil { c.Fail(constant.ErrorAccount) return } // todo: 检查用户资产 if userAsset.Balance.LessThan(req.Quantity) { c.Fail(constant.ErrorResponseBalanceNotEnough) return } currentProduct, err := s.FirstStakeProduct(s.DB().Where("pledge_mode", entity.StakeProductTypeCurrent)) if err != nil { c.Fail(constant.ErrorNotOpen) return } if !currentProduct.Enable { c.Fail(constant.ErrorNotOpen) return } // todo: 开始质押 err = s.CurrentStake(user, currentProduct, req.Quantity, 0) if err != nil { c.Fail(err.Error()) return } go s.StakeHandler() // 异步处理相关质押业务 c.Resp(nil) }