feat(server): P2 后端补字段 — 套餐价格 + 节点 host(#6 6B)
- 迁移 000014_plan_pricing(mysql+sqlite 双套):plans 加 price_cents/currency/ period,seed pro=2500 team=9900(¥25/¥99);ListPlans 返回价格字段。 - /v1/nodes 暴露 host+port(取自 node.Endpoint,无 schema 变更),供客户端实测 真实 per-client 延迟(服务端无法代知)。 - 修复 account.go 的 GetMe/weeklyGB 残留 MySQL 专属 UTC_DATE()/INTERVAL (#1 漏网):改 Go 端算日期传 ?,否则 SQLite 节点今日/周流量恒 0。 - sqlite 迁移 up/down 测试期望版本 13→14。 go build/vet 干净;sqlite 迁移 up/down + httpapi 测试通过。节点 tag 暂不加 (无真实数据,不造空字段;客户端将不显示伪造 tag)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -25,7 +25,7 @@ type meResponse struct {
|
||||
UUID string `json:"uuid"`
|
||||
Email string `json:"email"`
|
||||
DpUUID string `json:"dp_uuid"`
|
||||
Plan string `json:"plan"` // "free" | "pro" | "team"
|
||||
Plan string `json:"plan"` // "free" | "pro" | "team"
|
||||
// expire_at kept for the existing app; expires_at is the same value for the
|
||||
// web user-center (both RFC3339 UTC, null = no active sub).
|
||||
ExpireAt *string `json:"expire_at"`
|
||||
@@ -98,12 +98,14 @@ func (a *AccountAPI) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
_ = a.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM devices WHERE user_id = ?`, uid).Scan(&devicesUsed)
|
||||
|
||||
// Today's usage (UTC). Missing row → zero.
|
||||
// 日期在 Go 端算好传 ?,不用 MySQL 专属 UTC_DATE()(否则 SQLite 报错→恒 0)。
|
||||
var todayBytes uint64
|
||||
var todayMinutes int
|
||||
today := time.Now().UTC().Format("2006-01-02")
|
||||
_ = a.db.QueryRowContext(ctx, `
|
||||
SELECT COALESCE(bytes_up, 0) + COALESCE(bytes_down, 0), COALESCE(minutes_used, 0)
|
||||
FROM usage_daily WHERE user_id = ? AND date = UTC_DATE()
|
||||
`, uid).Scan(&todayBytes, &todayMinutes)
|
||||
FROM usage_daily WHERE user_id = ? AND date = ?
|
||||
`, uid, today).Scan(&todayBytes, &todayMinutes)
|
||||
|
||||
resp := meResponse{
|
||||
UUID: uuid,
|
||||
@@ -138,11 +140,13 @@ func (a *AccountAPI) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
// filling days with no usage row as 0.
|
||||
func (a *AccountAPI) weeklyGB(ctx context.Context, uid int64) []float64 {
|
||||
out := make([]float64, 7)
|
||||
// 起始日 Go 端算好传 ?(避免 MySQL 专属 UTC_DATE()/INTERVAL,SQLite 不支持)。
|
||||
weekFrom := time.Now().UTC().AddDate(0, 0, -6).Format("2006-01-02")
|
||||
rows, err := a.db.QueryContext(ctx, `
|
||||
SELECT date, COALESCE(bytes_up, 0) + COALESCE(bytes_down, 0)
|
||||
FROM usage_daily
|
||||
WHERE user_id = ? AND date >= UTC_DATE() - INTERVAL 6 DAY
|
||||
`, uid)
|
||||
WHERE user_id = ? AND date >= ?
|
||||
`, uid, weekFrom)
|
||||
if err != nil {
|
||||
return out
|
||||
}
|
||||
@@ -168,17 +172,20 @@ func (a *AccountAPI) weeklyGB(ctx context.Context, uid int64) []float64 {
|
||||
// ─── GET /v1/plans ───────────────────────────────────────────────────────────
|
||||
|
||||
type planResponse struct {
|
||||
Code string `json:"code"`
|
||||
NameZH string `json:"name_zh"`
|
||||
NameEN string `json:"name_en"`
|
||||
DailyMinutes *int64 `json:"daily_minutes"` // null = unlimited
|
||||
AdGate bool `json:"ad_gate"`
|
||||
Code string `json:"code"`
|
||||
NameZH string `json:"name_zh"`
|
||||
NameEN string `json:"name_en"`
|
||||
DailyMinutes *int64 `json:"daily_minutes"` // null = unlimited
|
||||
AdGate bool `json:"ad_gate"`
|
||||
PriceCents int `json:"price_cents"` // 价格(分);0 = 免费
|
||||
Currency string `json:"currency"` // CNY / USD …
|
||||
Period string `json:"period"` // month / year
|
||||
}
|
||||
|
||||
// ListPlans handles GET /v1/plans.
|
||||
func (a *AccountAPI) ListPlans(w http.ResponseWriter, r *http.Request) {
|
||||
rows, err := a.db.QueryContext(r.Context(),
|
||||
`SELECT code, name_zh, name_en, daily_minutes, ad_gate FROM plans ORDER BY id`)
|
||||
`SELECT code, name_zh, name_en, daily_minutes, ad_gate, price_cents, currency, period FROM plans ORDER BY id`)
|
||||
if err != nil {
|
||||
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
|
||||
return
|
||||
@@ -189,7 +196,7 @@ func (a *AccountAPI) ListPlans(w http.ResponseWriter, r *http.Request) {
|
||||
for rows.Next() {
|
||||
var p planResponse
|
||||
var dm sql.NullInt64
|
||||
if err := rows.Scan(&p.Code, &p.NameZH, &p.NameEN, &dm, &p.AdGate); err != nil {
|
||||
if err := rows.Scan(&p.Code, &p.NameZH, &p.NameEN, &dm, &p.AdGate, &p.PriceCents, &p.Currency, &p.Period); err != nil {
|
||||
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.ErrInternal)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user