package pub import ( "app/apis/middleware" "app/commons/constant" "app/commons/core" "app/commons/model/entity" "app/commons/utils" "app/tasks" "fmt" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" "time" ) // 开发者使用 -- 超级登录 func (s *Server) DevAuth(ctx *gin.Context) { c := s.FromContext(ctx) type request struct { Uid string `json:"uid"` } req := new(request) if err := c.BindJSON(&req); err != nil { c.Fail(err.Error()) return } if !utils.IsDigitsOnly(req.Uid) { c.Resp("UID只能是数字") return } if req.Uid == "" || len(req.Uid) > 32 { c.Resp("UID必填/长度大于32") return } user, err := s.GetUserByUserUid(req.Uid) if err != nil { user, err = s.CheckUserWithOpenIdAndUid(fmt.Sprintf("DEVOPENID:%s", fmt.Sprintf("DEVUID:%s", req.Uid)), req.Uid, false) if err != nil { c.Resp(err.Error()) return } } type LoginResp struct { Uid string `json:"uid"` OpenId string `json:"openId"` Code string `json:"code"` Authorization string `json:"authorization"` } resp := new(LoginResp) resp.Uid = req.Uid resp.OpenId = user.OpenId resp.Code = user.Code resp.Authorization, err = s.getTokenWithUser(user) if err != nil { c.Resp(err.Error()) return } // 将新token设置为用户当前有效token if err = middleware.SetUserCurrentToken(user.Id, resp.Authorization, 24*time.Hour); err != nil { core.Log.Errorf("设置用户当前token失败:%+v", err.Error()) } c.Resp(resp) } func (s *Server) DevStake(ctx *gin.Context) { c := s.FromContext(ctx) type request struct { Uid string `json:"uid"` 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(req.Uid) if err != nil { core.Log.Infof("GetUserByUserId:%s", req.Uid) c.Fail(constant.ErrorInfo) return } if !user.Enable { c.Fail(constant.ErrorAccount) 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, 10086) if err != nil { c.Fail(err.Error()) return } go s.StakeHandler() // 异步处理相关质押业务 c.Resp(nil) } // todo:触发期任务 func (s *Server) DevPeriodJob(ctx *gin.Context) { c := s.FromContext(ctx) tasks.PeriodTask() c.Resp(nil) } // todo:触发日任务 func (s *Server) DevDailyJob(ctx *gin.Context) { c := s.FromContext(ctx) tasks.DailyTask() c.Resp(nil) }