929bbf334b
P5.1 收尾:此前生成码只有 API。计费页现在从上到下 = 计费规则(积分→token 汇率) → 充值渠道(钱→积分:兑换码 + 微信定价用的积分包) → 用量观测, 两层汇率在同一页可见、各管各的。 - 兑换码:面额/张数/备注生成;**明文码只在生成响应显示一次**(等同现金, 台账 GET /admin/redeem-codes 服务端脱敏只露首尾,丢码重生成、不提供找回 ——顺手把接口这个第二明文出口堵了);台账含核销状态。 - 积分包:新增/上下架(微信 P5.2 上线前把定价面备好);admin api.ts 补 packs/redeem-codes 四个函数。 - launch.json 加 admin-console-alt(:5176)——5174 被用户自己的 sundynix-site 占着,不动别人端口。 live:5176 登录→生成 5 张(绿色一次性面板+复制全部)→配「入门包 1000分/¥9.9」 在售可下架→台账脱敏 curl 复核;tsc+41 vitest 全绿。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
136 lines
4.6 KiB
Go
136 lines
4.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/sundynix/sundynix-gateway/internal/store"
|
|
)
|
|
|
|
// 充值(P5.1:兑换码渠道;设计见 PAYMENT_DESIGN.md)。
|
|
// 入账目标一律是「计费租户」(ResolveBillingTenantID)——和消耗记账同一本账,
|
|
// 谁的池子扣钱就往谁的池子充,别让用户充进一个花不到的池。
|
|
|
|
// BillingPacks: GET /api/v1/billing/packs —— 在售积分包(微信渠道 P5.2 上线前仅展示)。
|
|
func (h *Handler) BillingPacks(c *gin.Context) {
|
|
packs, err := h.db.ActivePacks(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"packs": packs, "channels": []string{store.ChannelRedeem}})
|
|
}
|
|
|
|
// BillingRedeem: POST /api/v1/billing/redeem {code} —— 核销兑换码,积分入计费租户。
|
|
func (h *Handler) BillingRedeem(c *gin.Context) {
|
|
var b struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if err := c.ShouldBindJSON(&b); err != nil || strings.TrimSpace(b.Code) == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "code 必填"})
|
|
return
|
|
}
|
|
ctx := c.Request.Context()
|
|
uid := userID(c)
|
|
billing := h.db.ResolveBillingTenantID(ctx, uid, tenantID(c))
|
|
if billing == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无计费租户上下文"})
|
|
return
|
|
}
|
|
order, err := h.db.Redeem(ctx, b.Code, billing, uid)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"order": order,
|
|
"balance_micro": h.db.TenantBalance(ctx, billing),
|
|
})
|
|
}
|
|
|
|
// BillingOrders: GET /api/v1/billing/orders —— 计费租户最近充值记录(账单页展示)。
|
|
func (h *Handler) BillingOrders(c *gin.Context) {
|
|
ctx := c.Request.Context()
|
|
billing := h.db.ResolveBillingTenantID(ctx, userID(c), tenantID(c))
|
|
rows, err := h.db.TenantOrders(ctx, billing, 20)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"orders": rows})
|
|
}
|
|
|
|
// AdminGenRedeemCodes: POST /api/v1/admin/redeem-codes {credits, count, memo} —— 批量生成兑换码。
|
|
// credits 单位:积分(面向人,非 micro)。
|
|
func (h *Handler) AdminGenRedeemCodes(c *gin.Context) {
|
|
var b struct {
|
|
Credits float64 `json:"credits"`
|
|
Count int `json:"count"`
|
|
Memo string `json:"memo"`
|
|
}
|
|
if err := c.ShouldBindJSON(&b); err != nil || b.Credits <= 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "credits 必填且为正"})
|
|
return
|
|
}
|
|
if b.Count <= 0 {
|
|
b.Count = 1
|
|
}
|
|
codes, err := h.db.GenerateRedeemCodes(c.Request.Context(), b.Count, int64(b.Credits*1e6), b.Memo)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"codes": codes})
|
|
}
|
|
|
|
// AdminRedeemCodes: GET /api/v1/admin/redeem-codes —— 兑换码台账(含核销状态)。
|
|
// 码在台账里脱敏只露首尾:完整明文只在生成响应里给一次。兑换码等同现金,
|
|
// 常驻可查的列表接口不该是第二个明文出口(丢了码就重新生成一张,不提供找回)。
|
|
func (h *Handler) AdminRedeemCodes(c *gin.Context) {
|
|
rows, err := h.db.ListRedeemCodes(c.Request.Context(), 200)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
for i := range rows {
|
|
if n := len(rows[i].Code); n > 12 {
|
|
rows[i].Code = rows[i].Code[:8] + "…" + rows[i].Code[n-4:]
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"codes": rows})
|
|
}
|
|
|
|
// AdminSavePack: PUT /api/v1/admin/packs {id?, name, credits, price_fen, active, sort} —— 配积分包。
|
|
func (h *Handler) AdminSavePack(c *gin.Context) {
|
|
var b struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Credits float64 `json:"credits"` // 积分(面向人)
|
|
PriceFen int64 `json:"price_fen"`
|
|
Active bool `json:"active"`
|
|
Sort int `json:"sort"`
|
|
}
|
|
if err := c.ShouldBindJSON(&b); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
|
return
|
|
}
|
|
pk := &store.CreditPack{Name: strings.TrimSpace(b.Name), CreditsMicro: int64(b.Credits * 1e6), PriceFen: b.PriceFen, Active: b.Active, Sort: b.Sort}
|
|
pk.ID = b.ID
|
|
if err := h.db.SavePack(c.Request.Context(), pk); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"pack": pk})
|
|
}
|
|
|
|
// AdminPacks: GET /api/v1/admin/packs —— 全部积分包(含下架)。
|
|
func (h *Handler) AdminPacks(c *gin.Context) {
|
|
rows, err := h.db.ListPacks(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"packs": rows})
|
|
}
|