Files
Ydisks-Xianyu-Helper/internal/server/card_handlers.go
T
Christ d77c317452 feat: 对齐闲鱼续期流程并完善业务管理
对齐官方 WebSocket 批量握手、注册、心跳、ACK、同步恢复及运行时设备标识生命周期。

保留 Cookie 完整属性,接入静默续期和保存登录信息设置,并在凭证更新后重建账号连接。

完善订单与商品同步、卡券和通知渠道交互,更新前端品牌、时间展示及嵌入资源。

补充 Go 与前端回归测试,确保完整检查和生产构建通过。
2026-07-22 12:51:52 +08:00

186 lines
5.4 KiB
Go

package server
import (
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi/v5"
"xianyu-go/internal/auth"
"xianyu-go/internal/db"
)
// mountCardsReal 卡券 CRUD(真实实现)。发货规则已统一到 automation_rules。
func (s *Server) mountCardsReal(r chi.Router) {
r.Get("/cards", s.listCards)
r.Post("/cards", s.createCard)
r.Post("/cards/batch", s.batchCreateCards)
r.Post("/cards/{card_id}/append-data", s.appendCardData)
r.Get("/cards/{card_id}/details", s.getCard)
r.Get("/cards/{card_id}", s.getCard)
r.Put("/cards/{card_id}", s.updateCard)
r.Delete("/cards/{card_id}", s.deleteCard)
}
func (s *Server) listCards(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
cards, err := s.Store.Cards.AllForUser(r.Context(), sess.UserID)
if err != nil {
writeErr(w, http.StatusInternalServerError, "查询失败")
return
}
writeJSON(w, http.StatusOK, cards)
}
func (s *Server) getCard(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "card_id"), 10, 64)
if err != nil {
writeErr(w, http.StatusBadRequest, "无效卡券ID")
return
}
cf, ok := s.requireCardOwner(w, r, id)
if !ok {
return
}
writeJSON(w, http.StatusOK, cf)
}
func (s *Server) createCard(w http.ResponseWriter, r *http.Request) {
sess := auth.SessionFromContext(r.Context())
cf, err := decodeCard(r)
if err != nil {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
cf.UserID = sess.UserID
if cf.Type == "api" {
writeErr(w, http.StatusBadRequest, "API 卡密暂不支持自动发货,不能新建")
return
}
id, err := s.Store.Cards.Create(r.Context(), cf)
if err != nil {
writeErr(w, http.StatusInternalServerError, "创建失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true, "id": id})
}
func (s *Server) updateCard(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "card_id"), 10, 64)
if err != nil {
writeErr(w, http.StatusBadRequest, "无效卡券ID")
return
}
cf, err := decodeCard(r)
if err != nil {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
existing, ok := s.requireCardOwner(w, r, id)
if !ok {
return
}
if cf.Type == "api" && existing.Type != "api" {
writeErr(w, http.StatusBadRequest, "API 卡密暂不支持自动发货,不能转换为该类型")
return
}
cf.ID = id
cf.UserID = existing.UserID
if err := s.Store.Cards.Update(r.Context(), cf); err != nil {
writeErr(w, http.StatusInternalServerError, "更新失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func (s *Server) deleteCard(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(chi.URLParam(r, "card_id"), 10, 64)
if err != nil {
writeErr(w, http.StatusBadRequest, "无效卡券ID")
return
}
if _, ok := s.requireCardOwner(w, r, id); !ok {
return
}
if err := s.Store.Cards.Delete(r.Context(), id); err != nil {
writeErr(w, http.StatusInternalServerError, "删除失败")
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
func decodeCard(r *http.Request) (*db.CardFull, error) {
var req struct {
Name string `json:"name"`
Type string `json:"type"`
APIConfig string `json:"api_config"`
TextContent string `json:"text_content"`
DataContent string `json:"data_content"`
ImageURL string `json:"image_url"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
DelaySeconds int `json:"delay_seconds"`
IsMultiSpec bool `json:"is_multi_spec"`
SpecName string `json:"spec_name"`
SpecValue string `json:"spec_value"`
}
if err := decodeJSON(r, &req); err != nil {
return nil, err
}
if req.Name == "" || req.Type == "" {
return nil, errStr("名称和类型不能为空")
}
switch req.Type {
case "text", "data", "image", "api":
default:
return nil, errStr("类型必须为 text、data、image 或 api")
}
if req.DelaySeconds < 0 || req.DelaySeconds > 3600 {
return nil, errStr("延时发货必须在 0 到 3600 秒之间")
}
switch req.Type {
case "text":
if strings.TrimSpace(req.TextContent) == "" {
return nil, errStr("文本卡密内容不能为空")
}
case "data":
if strings.TrimSpace(req.DataContent) == "" {
return nil, errStr("数据卡密内容不能为空")
}
case "image":
if strings.TrimSpace(req.ImageURL) == "" {
return nil, errStr("图片卡密 URL 不能为空")
}
}
return &db.CardFull{
Name: req.Name, Type: req.Type, APIConfig: req.APIConfig, TextContent: req.TextContent,
DataContent: req.DataContent, ImageURL: req.ImageURL, Description: req.Description,
Enabled: req.Enabled, DelaySeconds: req.DelaySeconds, IsMultiSpec: req.IsMultiSpec,
SpecName: req.SpecName, SpecValue: req.SpecValue,
}, nil
}
// itemOwnedByUser 校验 cookieID 归属当前用户且其下存在 itemID 商品。
// 由自动化规则校验复用(原 deliveryRuleItemOwned,发货规则删除后改名)。
func (s *Server) itemOwnedByUser(r *http.Request, userID int64, cookieID, itemID string) bool {
if itemID == "" {
return true
}
all, err := s.Store.Cookies.AllForUser(r.Context(), userID)
if err != nil || cookieID == "" {
return false
}
if _, ok := all[cookieID]; !ok {
return false
}
_, err = s.Store.Items.Get(r.Context(), cookieID, itemID)
return err == nil
}
func errStr(s string) error { return &simpleError{s} }
type simpleError struct{ msg string }
func (e *simpleError) Error() string { return e.msg }