Files
Ydisks-Xianyu-Helper/internal/engine/account.go
T
ChristandClaude Opus 4.8 3f7954af84 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 <noreply@anthropic.com>
2026-07-06 12:27:37 +08:00

642 lines
20 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package engine 实现单账号运行时:WebSocket 连接生命周期、token 刷新、
// 消息分发主循环(信号量限并发 + 防抖 + 去重)、重连策略。
//
// 业务逻辑(自动发货、回复)在 Phase 3 通过 Handler 接口注入,
// Phase 2 先搭好骨架并跑通"收消息→解密→去重→防抖→回调"。
package engine
import (
"context"
"fmt"
"log/slog"
"strings"
"sync"
"time"
"xianyu-go/internal/automation"
"xianyu-go/internal/db"
"xianyu-go/internal/xianyu/mtop"
"xianyu-go/internal/xianyu/protocol"
"xianyu-go/internal/xianyu/ws"
)
// 账号运行时参数。
const (
MaxConnectionFailures = 5 // 连续失败上限,触发密码登录刷新
MessageSemaphoreSize = 100 // 并发消息处理上限
MessageDebounceDelay = 1 * time.Second // 防抖延迟:用户停止发送 1s 后回复
MessageExpireTime = time.Hour // 去重有效期
ProcessedIDsMaxSize = 10000 // 去重表上限,超限清理
HeartbeatInterval = 15 * time.Second
// token 心跳间隔。主动刷新 token 让服务端持续续期 cookie(_m_h5_tk /
// sgcookie 等),避免 cookie 自然滑向过期。15 分钟一次,平衡保活与风控。
// 不宜过短,否则易触发阿里系滑块风控。
TokenRefreshInterval = 15 * time.Minute
TokenRefreshMinGap = time.Minute
MessageCooldown = 5 * time.Minute // 收到消息后 5 分钟内不刷 cookie
PasswordLoginMinGap = 30 * time.Minute
)
// 告警级别(OnAccountAlert 的 level 参数)。
const (
AlertLevelInfo = "info"
AlertLevelWarn = "warn"
AlertLevelCritical = "critical"
)
// Handler 是业务逻辑注入点(Phase 3 实现)。
// 收到一条防抖后的聊天消息时回调;返回错误仅记录日志、不影响主循环。
// 注:生产 handlerAdapter.HandleChatMessage 当前为 no-op,留作未来注入聊天旁路处理
// (如外部消息持久化)。回复链由 ReplyService.Handle 完成,不依赖本回调。
type Handler interface {
HandleChatMessage(ctx context.Context, m ChatMessage) error
// HandleSystemEvent 处理平台系统事件。系统卡片永远不进入 AI 回复链,
// 这里只把事件交给自动化中心,由自动化规则决定是否执行。
HandleSystemEvent(ctx context.Context, task automation.Task) error
// OnPasswordLoginRefresh 连续失败时触发浏览器密码登录刷新;返回是否成功。
OnPasswordLoginRefresh(ctx context.Context, cookieID string) bool
// OnAccountAlert 账号告警通知(token 失效/自动恢复失败/风控验证等)。
// level 取 AlertLevel* 常量。实现方应把告警推送到该账号绑定的通知渠道。
OnAccountAlert(ctx context.Context, cookieID, level, title, body string)
}
// ChatMessage 防抖后投递给业务层的一条聊天消息。
type ChatMessage struct {
AccountID string // cookie_id
CookieStr string
ChatID string
SenderUserID string
SenderName string
Text string
ItemID string
Raw map[string]any // 解密后的完整消息
}
// RuntimeStatus 是账号引擎的实时连接状态,不写入数据库。
type RuntimeStatus struct {
State string `json:"state"`
Message string `json:"message,omitempty"`
Connected bool `json:"connected"`
Failures int `json:"failures"`
UpdatedAt time.Time `json:"updated_at"`
}
const (
RuntimeStarting = "starting"
RuntimeConnecting = "connecting"
RuntimeOnline = "online"
RuntimeReconnecting = "reconnecting"
RuntimeAuthExpired = "auth_expired"
RuntimeVerificationRequired = "verification_required"
RuntimeError = "error"
RuntimeStopped = "stopped"
)
// Account 单账号运行时。
type Account struct {
CookieID string
CookieStr string
UserID string // unb(myid)
store *db.Store
mtop *mtop.Client
wsDialer wsDialer
handler Handler
logger *slog.Logger
// 运行时状态(受 mu 保护)
mu sync.Mutex
currentToken string
deviceID string
connFailures int
lastMsgReceived time.Time
lastTokenRefresh time.Time
lastPasswordLogin time.Time
runtimeState string
runtimeMessage string
runtimeUpdatedAt time.Time
stopFn context.CancelFunc
stopped bool
conn *ws.Conn
// 去重
dedupMu sync.Mutex
processed map[string]time.Time
// 防抖:chat_id → 防抖句柄
debounceMu sync.Mutex
debounceTimers map[string]*debounceEntry
// 消息处理信号量
sem chan struct{}
reply *ReplyService
}
type debounceEntry struct {
timer *time.Timer
lastMsg ChatMessage
deadline time.Time
}
// wsDialer 抽象 ws.Dial,便于测试替身。
type wsDialer interface {
Dial(ctx context.Context, cfg ws.Config, logger *slog.Logger) (*ws.Conn, error)
}
type defaultDialer struct{}
func (defaultDialer) Dial(ctx context.Context, cfg ws.Config, logger *slog.Logger) (*ws.Conn, error) {
return ws.Dial(ctx, cfg, logger)
}
// Config 构造 Account 所需依赖。
type Config struct {
CookieID string
CookieStr string
Store *db.Store
Handler Handler
Logger *slog.Logger
}
// New 构造单账号运行时(未启动)。
func New(cfg Config) *Account {
logger := cfg.Logger
if logger == nil {
logger = slog.Default()
}
cookies := protocol.TransCookies(cfg.CookieStr)
myid := cookies["unb"]
a := &Account{
CookieID: cfg.CookieID,
CookieStr: cfg.CookieStr,
UserID: myid,
store: cfg.Store,
mtop: &mtop.Client{},
wsDialer: defaultDialer{},
handler: cfg.Handler,
logger: logger.With("account", cfg.CookieID),
deviceID: protocol.GenerateDeviceID(myid),
processed: make(map[string]time.Time),
debounceTimers: make(map[string]*debounceEntry),
sem: make(chan struct{}, MessageSemaphoreSize),
runtimeState: RuntimeStarting,
runtimeMessage: "正在启动账号服务",
runtimeUpdatedAt: time.Now(),
}
if cfg.Store != nil {
a.reply = NewReplyService(cfg.CookieID, cfg.Store, a, nil, NewAIReplier(cfg.CookieID, cfg.Store, logger), logger)
}
return a
}
// Run 阻塞运行账号主循环,直到 ctx 取消或不可恢复错误。
// 调用方应在独立 goroutine 中运行;Stop 可优雅停止。
func (a *Account) Run(parent context.Context) error {
ctx, cancel := context.WithCancel(parent)
a.mu.Lock()
a.stopFn = cancel
a.mu.Unlock()
for {
if ctx.Err() != nil {
return ctx.Err()
}
// 账号是否被禁用。
if !a.store.Cookies.GetStatus(ctx, a.CookieID) {
a.logger.Info("账号已禁用,停止主循环")
return nil
}
// 1) 刷新 token。若服务端返回 session 过期,直接跳到密码登录刷新,
// 不再空跑 5 次冷却重试。
token, cookieStr, err := a.refreshToken(ctx)
if err != nil {
a.logger.Error("刷新 token 失败", "err", err)
a.mu.Lock()
a.connFailures++
failures := a.connFailures
a.mu.Unlock()
a.setRuntimeError(ctx, err)
if mtop.IsSessionExpiredErr(err) {
a.logger.Warn("session 已失效,直接触发密码登录刷新", "err", err)
a.alert(ctx, AlertLevelWarn, "账号 session 已失效",
"登录凭证已被闲鱼服务端判定为失效,系统正在尝试通过浏览器密码登录自动恢复。若长时间未恢复,请人工处理。")
if err := a.handleMaxFailures(ctx); err != nil {
return err
}
continue
}
if failures >= MaxConnectionFailures {
if err := a.handleMaxFailures(ctx); err != nil {
return err
}
continue
}
if sleepCtx(ctx, a.retryDelay("token_refresh")) != nil {
return ctx.Err()
}
continue
}
a.mu.Lock()
a.currentToken = token
a.CookieStr = cookieStr
a.connFailures = 0
a.mu.Unlock()
a.logger.Info("token 刷新成功")
a.setRuntimeState(RuntimeConnecting, "登录凭证有效,正在连接消息服务")
// 2) 连接 WS + 注册。
var recorder func(direction, rawText, parsedJSON, parseStatus, errMsg string)
if a.store != nil && a.store.WSMessages != nil {
recorder = func(direction, rawText, parsedJSON, parseStatus, errMsg string) {
_ = a.store.WSMessages.Add(context.Background(), db.WSMessage{
CookieID: a.CookieID,
Direction: direction,
RawText: rawText,
ParsedJSON: parsedJSON,
MessageKind: "",
ParseStatus: parseStatus,
Error: errMsg,
})
}
}
a.mu.Lock()
deviceID := a.deviceID
a.mu.Unlock()
conn, err := a.wsDialer.Dial(ctx, ws.Config{
CookieStr: cookieStr,
DeviceID: deviceID,
AccessToken: token,
Recorder: recorder,
}, a.logger)
if err != nil {
a.logger.Error("WS 连接失败", "err", err)
a.mu.Lock()
a.connFailures++
failures := a.connFailures
a.mu.Unlock()
a.setRuntimeState(RuntimeReconnecting, "消息服务连接失败,稍后自动重试")
if failures >= MaxConnectionFailures {
if err := a.handleMaxFailures(ctx); err != nil {
return err
}
continue
}
if sleepCtx(ctx, a.retryDelay("ws_dial")) != nil {
return ctx.Err()
}
continue
}
a.mu.Lock()
a.conn = conn
a.mu.Unlock()
a.setRuntimeState(RuntimeOnline, "消息服务连接正常")
// 3) 心跳 + 消息接收,并并行运行 token 定时刷新。
hbCtx, hbCancel := context.WithCancel(ctx)
var hbErr error
go func() {
hbErr = conn.HeartbeatLoop(hbCtx, HeartbeatInterval)
hbCancel()
}()
refreshDone := make(chan struct{})
go func() {
a.tokenRefreshLoop(hbCtx, conn)
close(refreshDone)
}()
recvErr := conn.ReceiveLoop(ctx, a.dispatch)
hbCancel()
<-refreshDone
_ = conn.Close()
// 连接结束:清 token,统计失败,决定重连或密码刷新。
a.mu.Lock()
a.currentToken = ""
a.conn = nil
a.connFailures++
failures := a.connFailures
a.mu.Unlock()
a.setRuntimeState(RuntimeReconnecting, "消息连接已断开,稍后自动重连")
a.logger.Warn("WS 连接结束", "err", recvErr, "failures", failures, "heartbeat_err", hbErr)
if failures >= MaxConnectionFailures {
if err := a.handleMaxFailures(ctx); err != nil {
return err
}
continue
}
if sleepCtx(ctx, a.retryDelay(errString(recvErr))) != nil {
return ctx.Err()
}
}
}
// Stop 优雅停止。
func (a *Account) Stop() {
a.mu.Lock()
defer a.mu.Unlock()
if a.stopped {
return
}
a.stopped = true
a.runtimeState = RuntimeStopped
a.runtimeMessage = "账号服务已停止"
a.runtimeUpdatedAt = time.Now()
if a.stopFn != nil {
a.stopFn()
}
// 取消所有防抖定时器。
a.debounceMu.Lock()
for _, e := range a.debounceTimers {
if e.timer != nil {
e.timer.Stop()
}
}
a.debounceTimers = make(map[string]*debounceEntry)
a.debounceMu.Unlock()
}
// handleMaxFailures 连续失败达上限:尝试密码登录刷新;失败则返回 err 触发上层重启实例。
func (a *Account) handleMaxFailures(ctx context.Context) error {
a.logger.Warn("连续失败达上限,触发密码登录刷新", "failures", MaxConnectionFailures)
a.mu.Lock()
lastMsg := a.lastMsgReceived
lastLogin := a.lastPasswordLogin
a.mu.Unlock()
if !lastMsg.IsZero() && time.Since(lastMsg) < MessageCooldown {
a.logger.Warn("最近仍收到消息,跳过密码登录刷新", "cooldown", MessageCooldown)
a.resetFailures()
return sleepCtx(ctx, a.retryDelay("recent_message_skip_password_login"))
}
if !lastLogin.IsZero() && time.Since(lastLogin) < PasswordLoginMinGap {
remain := PasswordLoginMinGap - time.Since(lastLogin)
a.logger.Warn("密码登录刷新冷却中,跳过本次刷新", "remain", remain.Round(time.Second))
a.resetFailures()
return sleepCtx(ctx, minDuration(remain, time.Minute))
}
a.mu.Lock()
a.lastPasswordLogin = time.Now()
a.mu.Unlock()
if a.handler != nil && a.handler.OnPasswordLoginRefresh(ctx, a.CookieID) {
if d, err := a.store.Cookies.GetDetails(ctx, a.CookieID); err == nil && d != nil && d.Value != "" {
a.replaceCookieStr(d.Value)
}
a.logger.Info("密码登录刷新成功,重置失败计数")
a.setRuntimeState(RuntimeConnecting, "登录凭证已刷新,正在重新连接")
a.resetFailures()
return sleepCtx(ctx, 2*time.Second)
}
a.resetFailures()
a.setRuntimeState(RuntimeAuthExpired, "登录凭证已失效,请重新扫码登录")
a.alert(ctx, AlertLevelCritical, "账号自动恢复失败,需人工处理",
fmt.Sprintf("账号 %s 连续失败 %d 次,浏览器密码登录刷新未能恢复登录凭证,账号已停止运行。请前往后台重新扫码授权或检查账号用户名/密码配置。", a.CookieID, MaxConnectionFailures))
return fmt.Errorf("账号 %s 连续失败 %d 次且密码登录刷新失败,需重启实例", a.CookieID, MaxConnectionFailures)
}
// alert 触发账号告警通知。handler 未注入或为 nil 时静默跳过。
func (a *Account) alert(ctx context.Context, level, title, body string) {
if a.handler == nil {
return
}
a.handler.OnAccountAlert(ctx, a.CookieID, level, title, body)
}
func (a *Account) resetFailures() {
a.mu.Lock()
defer a.mu.Unlock()
a.connFailures = 0
}
// retryDelay 移植自 _calculate_retry_delay。
func (a *Account) retryDelay(errMsg string) time.Duration {
a.mu.Lock()
f := a.connFailures
a.mu.Unlock()
if f < 1 {
f = 1
}
secs := 0
switch {
case contains(errMsg, "no close frame received or sent"):
secs = min(3*f, 15)
case contains(errMsg, "connection refused") || contains(errMsg, "timeout"):
secs = min(10*f, 60)
default:
secs = min(5*f, 30)
}
return time.Duration(secs) * time.Second
}
// refreshToken 调 mtop token API,返回 (accessToken, 更新后的 cookie)。
func (a *Account) refreshToken(ctx context.Context) (string, string, error) {
a.mu.Lock()
cookieStr := a.CookieStr
last := a.lastTokenRefresh
if !last.IsZero() && time.Since(last) < TokenRefreshMinGap {
remain := TokenRefreshMinGap - time.Since(last)
a.mu.Unlock()
a.logger.Info("等待 token 刷新冷却", "remain", remain.Round(time.Second))
if err := sleepCtx(ctx, remain); err != nil {
return "", "", err
}
a.mu.Lock()
cookieStr = a.CookieStr
}
a.lastTokenRefresh = time.Now()
a.mu.Unlock()
deviceID := strings.TrimSpace(a.deviceID)
if deviceID == "" {
if unb := protocol.TransCookies(cookieStr)["unb"]; unb != "" {
deviceID = protocol.GenerateDeviceID(unb)
a.mu.Lock()
a.deviceID = deviceID
a.mu.Unlock()
}
}
res, err := a.mtop.RefreshTokenWithDeviceIDContext(ctx, cookieStr, deviceID)
if res != nil && res.UpdatedCookies != "" && res.UpdatedCookies != cookieStr {
a.replaceCookieStr(res.UpdatedCookies)
cookieStr = res.UpdatedCookies
if a.store != nil {
if d, e := a.store.Cookies.GetDetails(ctx, a.CookieID); e == nil {
_ = a.store.Cookies.Save(ctx, a.CookieID, res.UpdatedCookies, d.UserID)
}
}
}
if err != nil {
return "", "", err
}
if res == nil {
return "", "", fmt.Errorf("token API 未返回结果")
}
// 若 cookie 被刷新,持久化回数据库。
if res.UpdatedCookies != cookieStr {
a.mu.Lock()
a.CookieStr = res.UpdatedCookies
cookieStr = res.UpdatedCookies
a.mu.Unlock()
if a.store != nil {
// 保留原 user_id:用 GetDetails 取后回写。
if d, e := a.store.Cookies.GetDetails(ctx, a.CookieID); e == nil {
_ = a.store.Cookies.Save(ctx, a.CookieID, res.UpdatedCookies, d.UserID)
}
}
}
return res.AccessToken, cookieStr, nil
}
// tokenRefreshLoop 在 WS 在线期间按 TokenRefreshInterval 定时刷新 token。
// 刷新失败不重连(让心跳/接收循环去判定),仅记录日志;session 过期会触发
// 接收循环断开,主循环再走 handleMaxFailures 兜底。
func (a *Account) tokenRefreshLoop(ctx context.Context, conn *ws.Conn) {
ticker := time.NewTicker(TokenRefreshInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
}
if ctx.Err() != nil {
return
}
token, _, err := a.refreshToken(ctx)
if err != nil {
a.logger.Warn("定时刷新 token 失败", "err", err)
if mtop.IsSessionExpiredErr(err) {
// session 失效:让接收循环尽快断开走兜底,主动关闭连接。
_ = conn.Close()
return
}
continue
}
a.mu.Lock()
a.currentToken = token
a.mu.Unlock()
conn.SetAccessToken(token)
a.logger.Info("定时刷新 token 成功")
}
}
// RuntimeStatus 返回账号当前连接状态的线程安全快照。
func (a *Account) RuntimeStatus() RuntimeStatus {
a.mu.Lock()
defer a.mu.Unlock()
return RuntimeStatus{
State: a.runtimeState,
Message: a.runtimeMessage,
Connected: a.conn != nil && a.runtimeState == RuntimeOnline,
Failures: a.connFailures,
UpdatedAt: a.runtimeUpdatedAt,
}
}
func (a *Account) setRuntimeState(state, message string) {
a.mu.Lock()
defer a.mu.Unlock()
a.runtimeState = state
a.runtimeMessage = message
a.runtimeUpdatedAt = time.Now()
}
func (a *Account) setRuntimeError(ctx context.Context, err error) {
msg := strings.ToLower(errString(err))
prev := a.runtimeState
switch {
case strings.Contains(msg, "验证"), strings.Contains(msg, "captcha"), strings.Contains(msg, "risk"), strings.Contains(msg, "rgv587"), strings.Contains(msg, "fail_sys_user_validate"):
a.setRuntimeState(RuntimeVerificationRequired, "闲鱼要求安全验证,请重新扫码并完成验证")
// 仅在从非验证状态转入时告警一次,避免重复刷屏。
if prev != RuntimeVerificationRequired {
a.alert(ctx, AlertLevelWarn, "闲鱼要求安全验证",
"账号触发闲鱼风控验证(滑块/短信/人脸等)。系统可能无法自动恢复,请前往后台扫码完成验证。")
}
case strings.Contains(msg, "登录凭证已失效"), strings.Contains(msg, "fail_sys_token_exoired"), strings.Contains(msg, "fail_sys_token_expired"), strings.Contains(msg, "cookie 缺少 unb"):
a.setRuntimeState(RuntimeAuthExpired, "登录凭证已失效,请重新扫码登录")
default:
a.setRuntimeState(RuntimeReconnecting, "连接异常,系统将在限速后自动重试")
}
}
// SendText 通过当前 WebSocket 给买家发送文本消息。
func (a *Account) SendText(ctx context.Context, chatID, toUserID, text string) error {
text = strings.TrimSpace(text)
if text == "" {
return nil
}
conn, myID, err := a.currentSenderState()
if err != nil {
return err
}
sendCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return conn.SendText(sendCtx, myID, chatID, toUserID, text)
}
// SendImage 通过当前 WebSocket 给买家发送图片消息。当前仅支持可直接访问的 CDN/公网 URL。
func (a *Account) SendImage(ctx context.Context, chatID, toUserID, imageURL string, cardID int64) error {
imageURL = strings.TrimSpace(imageURL)
if imageURL == "" {
return nil
}
if strings.HasPrefix(imageURL, "/static/") || strings.HasPrefix(imageURL, "static/") {
return fmt.Errorf("当前运行时暂不支持本地图片自动上传到闲鱼 CDN: %s", imageURL)
}
conn, myID, err := a.currentSenderState()
if err != nil {
return err
}
sendCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
defer cancel()
return conn.SendImage(sendCtx, myID, chatID, toUserID, imageURL, 800, 600)
}
func (a *Account) currentSenderState() (*ws.Conn, string, error) {
a.mu.Lock()
defer a.mu.Unlock()
if a.conn == nil {
return nil, "", fmt.Errorf("账号 %s 当前没有可用 WebSocket 连接", a.CookieID)
}
myID := strings.TrimSpace(a.UserID)
if myID == "" {
myID = protocol.TransCookies(a.CookieStr)["unb"]
}
if myID == "" {
return nil, "", fmt.Errorf("账号 %s 缺少 unb,无法发送消息", a.CookieID)
}
return a.conn, myID, nil
}
func (a *Account) replaceCookieStr(cookieStr string) {
a.mu.Lock()
defer a.mu.Unlock()
a.CookieStr = cookieStr
if unb := protocol.TransCookies(cookieStr)["unb"]; unb != "" {
if a.UserID != "" && a.UserID != unb {
a.deviceID = protocol.GenerateDeviceID(unb)
}
a.UserID = unb
if a.deviceID == "" {
a.deviceID = protocol.GenerateDeviceID(unb)
}
}
}
// UpdateCookie 用外部刷新得到的新 cookie 更新运行时状态。
func (a *Account) UpdateCookie(cookieStr string) {
if strings.TrimSpace(cookieStr) == "" {
return
}
a.replaceCookieStr(cookieStr)
}
func minDuration(a, b time.Duration) time.Duration {
if a < b {
return a
}
return b
}