后端
- token 刷新:session 过期立即触发密码登录兜底(不再空跑5次冷却);
FAIL_SYS_SESSION_EXPIRED 识别为凭证失效;token 刷新成功日志;
在线定时刷新 token 间隔从 1h 调整为 15min,持续续期 cookie
- QR 登录:修复自动验证成功状态被 EXPIRED 覆盖的竞态(前端拿不到 cookie)
- 通知系统:Notifier 加 NotifyAccountAlert/SendToChannel;engine Handler
加 OnAccountAlert(warn/critical/风控三触发点);handlerAdapter 接 notifier;
通知渠道测试发送端点 POST /notification-channels/{id}/test;
NotificationChannelRow 加 json tag 修复前端读不到字段
- 卡密批量:POST /cards/batch 批量创建卡密组;POST /cards/{id}/append-data
往 data 组充卡密;DB AppendBatchData
- 通知渠道启用切换修复(全量更新避免 type CHECK 约束报错)
前端
- history 路由(/app/ 前缀避开 API 冲突,刷新不丢位置,popstate 同步)
- 通知设置页:渠道管理 + 账号绑定 + 配置指南 + SMTP(从系统设置移入)
- 卡密批量导入弹窗(批量建组 + 单组充卡密)
- 商品卡片缩小约 2/3;Rules/Notifications 头部样式对齐标准页
- 初始化提示按 OS 区分命令(Windows/macOS)
- updateChannel 全量更新修复启用切换失败
文档
- docs/批量铺货使用文档.md(给铺货 agent 的资料包准备规格)
Co-Authored-By: Claude Opus 4.8 <[email protected]>
148 lines
4.4 KiB
Go
148 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"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, err := s.Store.Cards.Get(r.Context(), id)
|
|
if err != nil {
|
|
writeErr(w, http.StatusNotFound, "卡券不存在")
|
|
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
|
|
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
|
|
}
|
|
cf.ID = id
|
|
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 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("名称和类型不能为空")
|
|
}
|
|
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 }
|