可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。 登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。 安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。 自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。 前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。 测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。 验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
186 lines
5.4 KiB
Go
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 > 86400 {
|
|
return nil, errStr("延时发货必须在 0 到 86400 秒之间")
|
|
}
|
|
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 }
|