将账号、认证、管理员、分析、自动化、卡券、聊天、商品、关键词、设置和订单能力迁移到 application 与 adapter,删除 Server 内对应的旧仓储和业务服务实现,并收紧架构检查。 完善批量发布与订单刷新生命周期、凭证写回、账号任务补偿、订单 reconciliation、通知和敏感数据边界;补充正常、失败、取消、并发、越权及多方言回归测试,并修正 MySQL 延迟任务表的 InnoDB 配置。 修复空卡密响应为 null 时前端加载失败的问题,重建嵌入式前端资产;重写重构总计划,清除历史流水并明确阶段 1 至 10 各对应一次完整迭代和一个 PR。 验证:make check;make test-server-race;npm test/typecheck/comments:check/build;Docker SQLite/MySQL 8.4/PostgreSQL 17 TestMultiDB_*。
80 lines
3.0 KiB
Go
80 lines
3.0 KiB
Go
package adapter
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
accountapp "xianyu-go/internal/application/account"
|
|
"xianyu-go/internal/xianyu/cookierefresh"
|
|
"xianyu-go/internal/xianyu/mtop"
|
|
)
|
|
|
|
// CookieSession 是一次平台请求流程使用的 Cookie 会话实现别名;Server 通过本适配器访问它,避免直接依赖平台包。
|
|
type CookieSession = mtop.CookieSession
|
|
|
|
// BrowserCookie 是浏览器 Cookie 快照的适配器别名,仅用于测试和平台边界转换。
|
|
type BrowserCookie = cookierefresh.BrowserCookie
|
|
|
|
// WithCookieSnapshot 为平台请求上下文安装完整 Cookie 快照,并保留权威 Jar 的更新状态。
|
|
func WithCookieSnapshot(ctx context.Context, snapshot []BrowserCookie) (context.Context, *CookieSession) {
|
|
return mtop.WithCookieSnapshot(ctx, snapshot)
|
|
}
|
|
|
|
// WithFlatCookieSession 为历史扁平 Cookie 安装兼容会话,不伪造完整 Jar 属性。
|
|
func WithFlatCookieSession(ctx context.Context, cookies string) (context.Context, *CookieSession) {
|
|
return mtop.WithFlatCookieSession(ctx, cookies)
|
|
}
|
|
|
|
// SnapshotFromMetadata 读取完整 Cookie 快照;解析失败或 metadata 不完整时返回 false。
|
|
func SnapshotFromMetadata(metadata string) ([]BrowserCookie, bool) {
|
|
return cookierefresh.SnapshotFromMetadataOK(metadata)
|
|
}
|
|
|
|
// HasStoredCookieCredential 判断凭证视图是否包含扁平 Cookie 或完整 Cookie 快照。
|
|
func HasStoredCookieCredential(detail *accountapp.CredentialDetail) bool {
|
|
if detail == nil {
|
|
return false
|
|
}
|
|
if detail.Value != "" {
|
|
return true
|
|
}
|
|
// complete 表示 metadata 是否包含可用于平台请求的完整快照。
|
|
_, complete := cookierefresh.SnapshotFromMetadataOK(detail.MetadataJSON)
|
|
return complete
|
|
}
|
|
|
|
// PersistCookieSessionLocked 持久化平台响应后的 Cookie 会话;调用方必须已持有凭证锁。
|
|
func PersistCookieSessionLocked(
|
|
ctx context.Context,
|
|
repository accountapp.CredentialRepository,
|
|
detail *accountapp.CredentialDetail,
|
|
session *CookieSession,
|
|
) (value string, valueChanged, handled bool, err error) {
|
|
if detail == nil || session == nil {
|
|
return "", false, false, nil
|
|
}
|
|
// value、snapshot、changed 保存平台会话当前 Cookie、完整快照及变更状态。
|
|
value, snapshot, changed := session.State()
|
|
if !changed {
|
|
if snapshot != nil {
|
|
return detail.Value, false, true, nil
|
|
}
|
|
return "", false, false, nil
|
|
}
|
|
// metadata 保存去除旧快照或合并新快照后的凭证元数据。
|
|
metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON)
|
|
if snapshot != nil {
|
|
metadata = cookierefresh.MetadataWithSnapshot(detail.MetadataJSON, snapshot)
|
|
}
|
|
if repository == nil {
|
|
return value, value != detail.Value, true, errors.New("cookie 会话持久化 repository 未初始化")
|
|
}
|
|
// persistErr 保存凭证 Port 写回结果;明文 Cookie 不离开当前适配器调用边界。
|
|
persistErr := repository.UpdateRenewalCookie(ctx, detail.ID, value, metadata, time.Now().Unix())
|
|
if persistErr != nil {
|
|
return value, value != detail.Value, true, persistErr
|
|
}
|
|
return value, value != detail.Value, true, nil
|
|
}
|