Files
Blizzard b1fea23a0c feat(site): 官网定价页 —— 套餐由后台配置驱动,公开可见
官网加「定价」菜单与页面。数据来自 GET /api/v1/pricing(**不挂鉴权**):
运营改价、上下架、调发放节奏都在管理端完成,官网跟着变,不用发版;
未登录就能看到价格,这是转化的前提,也是官网存在的意义。

接口只吐在售项与展示字段,成本、权重、租户信息一律不出去。

结账仍在 Web 面完成(那里有登录态、租户上下文与支付轮询),官网只负责
「看价 → 去买」。同一套支付流程不在两处各实现一遍——这也是本次订阅开发
一直遵循的那条线:积分包与订阅共用一个支付弹窗,桌面端不重复购买入口。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-20 16:59:58 +08:00

96 lines
3.4 KiB
Go

package handler
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
"github.com/sundynix/sundynix-gateway/internal/store"
)
// 订阅 API。用户面只有「看套餐 / 看我的订阅」,下单复用现有 /billing/orders
// (多传 plan_id 即可),因为支付链路、幂等、掉单补偿都已经在那条路上验过了,
// 没必要为订阅再造一条支付路径。
// ---- 用户面 ----
// BillingSubPlans: GET /api/v1/billing/sub-plans —— 在售订阅套餐。
func (h *Handler) BillingSubPlans(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"plans": h.db.ListSubPlans(c.Request.Context(), true)})
}
// MySubscription: GET /api/v1/billing/subscription —— 我的当前订阅(无则 null)。
func (h *Handler) MySubscription(c *gin.Context) {
ctx := c.Request.Context()
billing := h.db.ResolveBillingTenantID(ctx, userID(c), tenantID(c))
if billing == "" {
c.JSON(http.StatusOK, gin.H{"subscription": nil})
return
}
sub := h.db.ActiveSubscription(ctx, billing)
if sub == nil {
c.JSON(http.StatusOK, gin.H{"subscription": nil})
return
}
c.JSON(http.StatusOK, gin.H{"subscription": sub, "plan": h.db.GetSubPlan(ctx, sub.PlanID)})
}
// PublicPricing: GET /api/v1/pricing —— **公开**(无需登录):官网定价页用。
// 官网要在用户登录前就能看到价格,所以这条不挂鉴权;只暴露在售项,且不含任何
// 内部字段(成本、权重、租户信息一概不出去)。
func (h *Handler) PublicPricing(c *gin.Context) {
ctx := c.Request.Context()
packs, _ := h.db.ActivePacks(ctx)
c.JSON(http.StatusOK, gin.H{
"plans": h.db.ListSubPlans(ctx, true),
"packs": packs,
})
}
// ---- 管理端 ----
// AdminSubPlans: GET /api/v1/admin/sub-plans —— 全部套餐(含下架)。
func (h *Handler) AdminSubPlans(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"plans": h.db.ListSubPlans(c.Request.Context(), false)})
}
// AdminSaveSubPlan: PUT /api/v1/admin/sub-plans —— 新增/改套餐(id 空=新增)。
// 积分以「积分」为单位收(面向人),服务端转 micro。
func (h *Handler) AdminSaveSubPlan(c *gin.Context) {
var b struct {
ID string `json:"id"`
Name string `json:"name"`
PriceFen int64 `json:"price_fen"`
DurationDays int `json:"duration_days"`
RefillCredits float64 `json:"refill_credits"`
RefillInterval int `json:"refill_interval_days"`
Active bool `json:"active"`
Sort int `json:"sort"`
}
if err := c.ShouldBindJSON(&b); err != nil || strings.TrimSpace(b.Name) == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "name 必填"})
return
}
pl := &store.SubscriptionPlan{
BaseModel: store.BaseModel{ID: b.ID},
Name: strings.TrimSpace(b.Name),
PriceFen: b.PriceFen,
DurationDays: b.DurationDays,
RefillCreditsMicro: int64(b.RefillCredits * 1e6),
RefillIntervalDays: b.RefillInterval,
Active: b.Active,
Sort: b.Sort,
}
if err := h.db.SaveSubPlan(c.Request.Context(), pl); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"id": pl.ID})
}
// AdminSubscriptions: GET /api/v1/admin/subscriptions —— 全平台订阅观测。
func (h *Handler) AdminSubscriptions(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"subscriptions": h.db.AllSubscriptions(c.Request.Context(), 200)})
}