feat: token 保活 + 通知系统 + 批量铺货 + 卡密批量 + 前端路由与样式对齐
后端
- 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]>
This commit is contained in:
1 parent
cf109ae6ce
commit
3f7954af84
30 files changed
+1826
-259
No files matched your search
@@ -0,0 +1,172 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"xianyu-go/internal/auth"
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
const maxCardBatchRows = 200
|
||||
|
||||
type cardBatchResultRow struct {
|
||||
RowNo int `json:"row_no"`
|
||||
Success bool `json:"success"`
|
||||
ID int64 `json:"id,omitempty"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// batchCreateCards 上传表格批量创建卡密组。每行一个组定义。
|
||||
func (s *Server) batchCreateCards(w http.ResponseWriter, r *http.Request) {
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
// 表格最大 5 MiB(卡密组定义都很小)。
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 6<<20)
|
||||
if err := r.ParseMultipartForm(6 << 20); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "解析上传文件失败")
|
||||
return
|
||||
}
|
||||
source, sourceHeader, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "缺少卡密表格文件")
|
||||
return
|
||||
}
|
||||
defer source.Close()
|
||||
sourceBytes, err := io.ReadAll(io.LimitReader(source, 5<<20))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "读取卡密表格失败")
|
||||
return
|
||||
}
|
||||
sourceName := safeBaseName(sourceHeader.Filename)
|
||||
if sourceName == "" {
|
||||
sourceName = "cards.csv"
|
||||
}
|
||||
maps, err := parsePublishSheetBytes(sourceBytes, sourceName)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
if len(maps) > maxCardBatchRows {
|
||||
writeErr(w, http.StatusBadRequest, fmt.Sprintf("单次最多创建 %d 个卡密组", maxCardBatchRows))
|
||||
return
|
||||
}
|
||||
|
||||
results := make([]cardBatchResultRow, 0, len(maps))
|
||||
created, failed := 0, 0
|
||||
for i, m := range maps {
|
||||
rowNo := i + 2
|
||||
name := strings.TrimSpace(firstImportString(m, "name", "名称", "卡密组名称", "卡密名称"))
|
||||
cardType := strings.ToLower(strings.TrimSpace(firstImportString(m, "type", "类型", "卡密类型")))
|
||||
content := firstImportString(m, "content", "内容", "卡密内容")
|
||||
|
||||
// 校验
|
||||
if name == "" {
|
||||
results = append(results, cardBatchResultRow{RowNo: rowNo, Success: false, Name: name, Type: cardType, Error: "缺少名称"})
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
switch cardType {
|
||||
case "text", "data", "image", "api":
|
||||
default:
|
||||
results = append(results, cardBatchResultRow{RowNo: rowNo, Success: false, Name: name, Type: cardType, Error: "类型必须为 text/data/image/api"})
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
// content 按类型映射;api 类型允许 content 为空(用 description 等另行说明时)
|
||||
if cardType != "api" && strings.TrimSpace(content) == "" {
|
||||
results = append(results, cardBatchResultRow{RowNo: rowNo, Success: false, Name: name, Type: cardType, Error: "缺少内容"})
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
|
||||
cf := &db.CardFull{
|
||||
Name: name,
|
||||
Type: cardType,
|
||||
Description: firstImportString(m, "description", "描述"),
|
||||
Enabled: true,
|
||||
DelaySeconds: atoiPublishDefault(firstImportString(m, "delay_seconds", "延迟秒"), 0),
|
||||
IsMultiSpec: parseLooseBool(firstImportString(m, "is_multi_spec", "多规格")),
|
||||
SpecName: firstImportString(m, "spec_name", "规格名"),
|
||||
SpecValue: firstImportString(m, "spec_value", "规格值"),
|
||||
UserID: sess.UserID,
|
||||
}
|
||||
if v := firstImportString(m, "enabled", "启用"); v != "" {
|
||||
cf.Enabled = parseLooseBool(v)
|
||||
}
|
||||
switch cardType {
|
||||
case "text":
|
||||
cf.TextContent = content
|
||||
case "data":
|
||||
cf.DataContent = content
|
||||
case "image":
|
||||
cf.ImageURL = content
|
||||
case "api":
|
||||
cf.APIConfig = content
|
||||
}
|
||||
|
||||
id, err := s.Store.Cards.Create(r.Context(), cf)
|
||||
if err != nil {
|
||||
results = append(results, cardBatchResultRow{RowNo: rowNo, Success: false, Name: name, Type: cardType, Error: "创建失败: " + err.Error()})
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
results = append(results, cardBatchResultRow{RowNo: rowNo, Success: true, ID: id, Name: name, Type: cardType})
|
||||
created++
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"total": len(maps),
|
||||
"created": created,
|
||||
"failed": failed,
|
||||
"rows": results,
|
||||
})
|
||||
}
|
||||
|
||||
// appendCardData 往 data 类型卡密组追加卡密号(按行)。
|
||||
func (s *Server) appendCardData(w http.ResponseWriter, r *http.Request) {
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "card_id"), 10, 64)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "无效卡券ID")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Content string `json:"content"`
|
||||
}
|
||||
if err := decodeJSON(r, &req); err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
content := strings.TrimSpace(req.Content)
|
||||
if content == "" {
|
||||
writeErr(w, http.StatusBadRequest, "内容为空")
|
||||
return
|
||||
}
|
||||
cf, err := s.Store.Cards.Get(r.Context(), id)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusNotFound, "卡券不存在")
|
||||
return
|
||||
}
|
||||
if cf.UserID != sess.UserID {
|
||||
writeErr(w, http.StatusForbidden, "无权操作该卡密组")
|
||||
return
|
||||
}
|
||||
if cf.Type != "data" {
|
||||
writeErr(w, http.StatusBadRequest, "只有 data(批量卡密)类型支持追加卡密")
|
||||
return
|
||||
}
|
||||
added, err := s.Store.Cards.AppendBatchData(r.Context(), id, content)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "追加失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true, "added": added})
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import (
|
||||
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)
|
||||
|
||||
@@ -3,6 +3,7 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
@@ -15,6 +16,7 @@ func (s *Server) mountNotificationsReal(r chi.Router) {
|
||||
r.Post("/notification-channels", s.createChannel)
|
||||
r.Put("/notification-channels/{channel_id}", s.updateChannel)
|
||||
r.Delete("/notification-channels/{channel_id}", s.deleteChannel)
|
||||
r.Post("/notification-channels/{channel_id}/test", s.testChannel)
|
||||
r.Get("/message-notifications", s.listMessageNotifications)
|
||||
r.Delete("/message-notifications/account/{cid}", s.deleteAccountNotifications)
|
||||
r.Delete("/message-notifications/{notification_id}", s.deleteMessageNotification)
|
||||
@@ -92,6 +94,26 @@ func (s *Server) deleteChannel(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
}
|
||||
|
||||
// testChannel 向指定渠道发送一条测试通知,便于用户验证配置是否正确。
|
||||
func (s *Server) testChannel(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "channel_id"), 10, 64)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusBadRequest, "无效ID")
|
||||
return
|
||||
}
|
||||
if s.Notifier == nil {
|
||||
writeErr(w, http.StatusServiceUnavailable, "通知器未启用")
|
||||
return
|
||||
}
|
||||
body := "🧪 通知渠道测试\n\n这是一条来自闲鱼助手的测试通知,收到说明渠道配置正常。\n时间: " +
|
||||
time.Now().Format("2006-01-02 15:04:05")
|
||||
if err := s.Notifier.SendToChannel(id, body); err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "发送失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
}
|
||||
|
||||
func (s *Server) getAccountBindings(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
ids, err := s.Store.Notifications.AccountBindings(r.Context(), cid)
|
||||
|
||||
@@ -56,6 +56,7 @@ type noopHandler struct{}
|
||||
func (noopHandler) HandleChatMessage(context.Context, engine.ChatMessage) error { return nil }
|
||||
func (noopHandler) HandleSystemEvent(context.Context, automation.Task) error { return nil }
|
||||
func (noopHandler) OnPasswordLoginRefresh(context.Context, string) bool { return false }
|
||||
func (noopHandler) OnAccountAlert(context.Context, string, string, string, string) {}
|
||||
|
||||
// TestLoginVerifyLogout 登录→verify→登出 全链路。
|
||||
func TestLoginVerifyLogout(t *testing.T) {
|
||||
|
||||
Reference in new issue
Block a user