feat: add charge page

This commit is contained in:
Rogee
2024-12-12 19:46:59 +08:00
parent 38a45958bd
commit 3ce3c9ee84
5 changed files with 68 additions and 0 deletions

BIN
backend/__debug_bin3277441022 Executable file

Binary file not shown.

View File

@@ -2,6 +2,7 @@ package users
import (
"backend/pkg/consts"
"backend/pkg/errorx"
"backend/providers/jwt"
"github.com/gofiber/fiber/v3"
@@ -56,3 +57,38 @@ func (c *Controller) Info(ctx fiber.Ctx) error {
return ctx.JSON(info)
}
func (c *Controller) GetChargeCodes(ctx fiber.Ctx) error {
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
log.Debug(claim)
tenant, err := c.svc.GetTenantByID(ctx.Context(), claim.TenantID)
if err != nil {
return errors.Wrapf(err, "get tenant: %d", claim.TenantID)
}
if tenant.BindUserID != claim.UserID {
return errorx.RequestParseError
}
type generateCode struct {
Amount int64 `json:"amount"`
Code string `json:"code"`
}
amount := []int64{1, 5, 10, 20, 50, 100}
codes := []generateCode{}
for _, a := range amount {
code, err := c.svc.GenerateChargeCode(ctx.Context(), claim.TenantID, a*100)
if err != nil {
log.WithError(err).Errorf("generate charge code")
return errorx.InternalError
}
codes = append(codes, generateCode{
Amount: a * 100,
Code: code,
})
}
return ctx.JSON(codes)
}

View File

@@ -28,4 +28,5 @@ func (r *Router) Register(router fiber.Router) {
group := router.Group(r.Name())
group.Get("info", r.controller.Info)
group.Patch("charge/:code", r.controller.Charge)
group.Get("codes", r.controller.GetChargeCodes)
}