Files
mp-qvyun/backend/modules/users/controller.go
2024-12-12 19:29:02 +08:00

59 lines
1.2 KiB
Go

package users
import (
"backend/pkg/consts"
"backend/providers/jwt"
"github.com/gofiber/fiber/v3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
hashids "github.com/speps/go-hashids/v2"
)
// @provider
type Controller struct {
svc *Service
hashIds *hashids.HashID
}
// List
func (c *Controller) List(ctx fiber.Ctx) error {
return ctx.JSON(nil)
}
// Charge
func (c *Controller) Charge(ctx fiber.Ctx) error {
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
// [tenantId, chargeAmount, timestamp]
code := ctx.Params("code")
if err := c.svc.Charge(ctx.Context(), claim, code); err != nil {
return err
}
return ctx.JSON(nil)
}
// Info
func (c *Controller) Info(ctx fiber.Ctx) error {
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
info := &UserInfo{}
balance, err := c.svc.GetTenantUserBalance(ctx.Context(), claim.TenantID, claim.UserID)
if err != nil {
return err
}
info.Balance = balance
tenant, err := c.svc.GetTenantByID(ctx.Context(), claim.TenantID)
if err != nil {
return errors.Wrapf(err, "get tenant: %d", claim.TenantID)
}
info.IsAdmin = tenant.BindUserID == claim.UserID
return ctx.JSON(info)
}