Files
kra-new/internal/server/handler/recharge.go
T
2026-09-23 17:42:55 +08:00

107 lines
2.2 KiB
Go

package handler
import (
"github.com/gin-gonic/gin"
"kra/internal/service/dto"
service "kra/internal/service/recharge"
)
type Recharge struct{ s *service.Service }
func NewRecharge(s *service.Service) *Recharge { return &Recharge{s: s} }
func (h *Recharge) Providers(c *gin.Context) {
v, e := h.s.Providers(c.Request.Context())
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, gin.H{"list": v})
}
func (h *Recharge) Plans(c *gin.Context) {
v, e := h.s.Plans(c.Request.Context())
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, gin.H{"list": v})
}
func (h *Recharge) SavePlans(c *gin.Context) {
in, ok := bindJSON[dto.RechargePlansRequest](c)
if !ok {
return
}
if e := h.s.SavePlans(c.Request.Context(), in); e != nil {
businessFail(c, e)
return
}
OK(c)
}
func (h *Recharge) List(c *gin.Context) {
in, ok := bindQuery[dto.RechargeOrderListRequest](c)
if !ok {
return
}
v, total, sum, e := h.s.List(c.Request.Context(), in)
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, gin.H{"list": v, "total": total, "summary": sum})
}
func (h *Recharge) Get(c *gin.Context) {
v, e := h.s.Get(c.Request.Context(), c.Param("orderNo"))
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, v)
}
func (h *Recharge) GetMine(c *gin.Context) {
claims := Claims(c)
if claims == nil {
NoAuth(c, "请登录")
return
}
v, e := h.s.GetForUser(c.Request.Context(), claims.ID, c.Param("orderNo"))
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, v)
}
func (h *Recharge) PaymentStatus(c *gin.Context) {
claims := Claims(c)
if claims == nil {
NoAuth(c, "请登录")
return
}
v, e := h.s.PaymentStatus(c.Request.Context(), claims.ID, c.Param("orderNo"))
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, v)
}
func (h *Recharge) Create(c *gin.Context) {
in, ok := bindJSON[dto.RechargeOrderRequest](c)
if !ok {
return
}
claims := Claims(c)
if claims == nil {
NoAuth(c, "请登录")
return
}
in.ClientIP = c.ClientIP()
in.UserAgent = c.Request.UserAgent()
in.DeviceID = c.GetHeader("X-Device-Id")
v, e := h.s.Create(c.Request.Context(), claims.ID, in)
if e != nil {
businessFail(c, e)
return
}
OKWithData(c, v)
}