阶段八:完成账号登录凭证repository收口
This commit is contained in:
1 parent
a2a22ad3e3
commit
98023744fa
4 files changed
+113
-18
No files matched your search
@@ -617,3 +617,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | 完成阶段 8 第五个 PR 切片“订单应用服务 repository 与事务边界” | 新增 `orderRepository`,订单应用服务的订单/商品读写、用户归属、事务、凭证锁、续期 Cookie 和远端缺失清理均通过窄接口;平台 MTOP、运行时 Cookie 更新、自动化发货和通知编排仍由 Server 负责;订单列表、详情、导入、手动发货、单笔/批量刷新语义保持不变;Server 全量测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:发布应用服务 repository 与 worker 事务边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第六个 PR 切片“发布应用服务 repository 边界” | 新增 `itemPublishRepository`,单商品发布、类目推荐、预检批次创建、批次查询/取消/删除/失败重试及凭证 Cookie 持久化不再直接访问完整 `db.Store`;批量 worker 的租约状态机和后台生命周期暂保持原有 Server 接线,下一片整体迁移;单商品/批量发布测试、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:发布 worker 租约状态机 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第七个 PR 切片“发布 worker 租约状态机 repository 边界” | 发布恢复扫描、批次租约续期、明细抢占、远端发布检查点、结果落库、取消/中断收口和过期上传清理统一通过 `itemPublishRepository`;worker 不再直接访问 `PublishBatches`、Cookies 或 Items,平台 MTOP、自动化规则和生命周期等待语义保持不变;Server 全量测试、Server race、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:账号登录应用服务 repository 边界 |
|
||||
| 2026-08-15 | 完成阶段 8 第八个 PR 切片“账号登录凭证与 Token repository 边界” | 新增 `accountLoginRepository`,账号登录服务的凭证创建/更新、Cookie Jar 元数据写回、凭证锁、Token 清理和运行状态读取统一通过窄接口;保留登录服务对资料刷新、登录审计和运行时重启的既有编排,未改变扫码幂等、Cookie 合并与凭证锁时序;Server 全量测试、Server race、Go vet、lint、注释和全量门禁通过并合并为一个可回滚提交 | 阶段 8:登录审计与资料刷新共享 repository 边界 |
|
||||
@@ -0,0 +1,92 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
"xianyu-go/internal/xianyu/cookierefresh"
|
||||
)
|
||||
|
||||
// accountLoginRepository 定义账号登录服务写入凭证、清理 Token 和读取账号状态所需的最小能力。
|
||||
type accountLoginRepository interface {
|
||||
// LockCredentials 串行化账号登录凭证变更。
|
||||
LockCredentials(accountID string) func()
|
||||
// CreateCookieOwned 创建用户拥有的账号凭证。
|
||||
CreateCookieOwned(ctx context.Context, accountID, cookies string, userID int64) error
|
||||
// LoadPlatformDetail 读取平台登录所需的最小账号视图。
|
||||
LoadPlatformDetail(ctx context.Context, accountID string) (*db.CookieDetail, error)
|
||||
// UpdateFlatCookieOwned 更新用户拥有账号的扁平 Cookie。
|
||||
UpdateFlatCookieOwned(ctx context.Context, detail *db.CookieDetail, cookies string) error
|
||||
// UpdateRenewalCookie 更新账号 Cookie 和 metadata。
|
||||
UpdateRenewalCookie(ctx context.Context, accountID, cookies, metadata string, at int64) error
|
||||
// ClearTokens 清理账号旧连接 Token。
|
||||
ClearTokens(ctx context.Context, accountID string) error
|
||||
// GetStatus 返回账号是否启用。
|
||||
GetStatus(ctx context.Context, accountID string) bool
|
||||
}
|
||||
|
||||
// storeAccountLoginRepository 将完整 Store 适配为账号登录服务窄 repository。
|
||||
type storeAccountLoginRepository struct {
|
||||
// store 保存数据库聚合入口,仅在适配器内部使用。
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
// LockCredentials 委托账号凭证锁。
|
||||
func (r storeAccountLoginRepository) LockCredentials(accountID string) func() {
|
||||
return r.store.LockAccountCredentials(accountID)
|
||||
}
|
||||
|
||||
// CreateCookieOwned 委托账号凭证创建。
|
||||
func (r storeAccountLoginRepository) CreateCookieOwned(ctx context.Context, accountID, cookies string, userID int64) error {
|
||||
return r.store.Cookies.CreateOwned(ctx, accountID, cookies, userID)
|
||||
}
|
||||
|
||||
// LoadPlatformDetail 委托平台账号视图查询并转换为 Server 内部模型。
|
||||
func (r storeAccountLoginRepository) LoadPlatformDetail(ctx context.Context, accountID string) (*db.CookieDetail, error) {
|
||||
// data 和 err 保存平台运行视图查询结果。
|
||||
data, err := r.store.Cookies.GetCookiePlatformRuntimeData(ctx, accountID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &db.CookieDetail{ID: data.ID, UserID: data.UserID, Value: data.Value, MetadataJSON: data.MetadataJSON, ShowBrowser: data.ShowBrowser}, nil
|
||||
}
|
||||
|
||||
// UpdateFlatCookieOwned 委托扁平 Cookie 更新并清除旧的完整 Cookie Jar 快照。
|
||||
func (r storeAccountLoginRepository) UpdateFlatCookieOwned(ctx context.Context, detail *db.CookieDetail, cookies string) error {
|
||||
if detail == nil {
|
||||
return db.ErrNotFound
|
||||
}
|
||||
// metadata 保存移除完整 Cookie Jar 快照后的元数据。
|
||||
metadata := cookierefresh.MetadataWithoutSnapshot(detail.MetadataJSON)
|
||||
return r.store.Cookies.UpdateRenewalCookie(ctx, detail.ID, cookies, metadata, time.Now().Unix())
|
||||
}
|
||||
|
||||
// UpdateRenewalCookie 委托 Cookie 与 metadata 更新。
|
||||
func (r storeAccountLoginRepository) UpdateRenewalCookie(ctx context.Context, accountID, cookies, metadata string, at int64) error {
|
||||
return r.store.Cookies.UpdateRenewalCookie(ctx, accountID, cookies, metadata, at)
|
||||
}
|
||||
|
||||
// ClearTokens 委托账号 Token 清理;未启用 Token repository 时保持兼容空操作。
|
||||
func (r storeAccountLoginRepository) ClearTokens(ctx context.Context, accountID string) error {
|
||||
if r.store.Tokens == nil {
|
||||
return nil
|
||||
}
|
||||
return r.store.Tokens.Clear(ctx, accountID)
|
||||
}
|
||||
|
||||
// GetStatus 委托账号启用状态查询。
|
||||
func (r storeAccountLoginRepository) GetStatus(ctx context.Context, accountID string) bool {
|
||||
return r.store.Cookies.GetStatus(ctx, accountID)
|
||||
}
|
||||
|
||||
// newStoreAccountLoginRepository 从完整 Store 构造账号登录服务窄 repository。
|
||||
func newStoreAccountLoginRepository(store *db.Store) accountLoginRepository {
|
||||
if store == nil || store.Cookies == nil {
|
||||
return nil
|
||||
}
|
||||
return storeAccountLoginRepository{store: store}
|
||||
}
|
||||
|
||||
// 确保 Store 适配器始终覆盖账号登录服务所需的全部能力。
|
||||
var _ accountLoginRepository = storeAccountLoginRepository{}
|
||||
@@ -16,6 +16,8 @@ import (
|
||||
type accountLoginService struct {
|
||||
// server 提供账号存储、运行时管理器和扫码会话持久化依赖。
|
||||
server *Server
|
||||
// repository 提供账号登录服务所需的最小凭证持久化能力。
|
||||
repository accountLoginRepository
|
||||
}
|
||||
|
||||
// accountLoginInput 是新增账号登录凭证用例的业务输入。
|
||||
@@ -62,15 +64,15 @@ func (svc *accountLoginService) CreateCookie(ctx context.Context, input accountL
|
||||
// s 是当前账号登录应用服务依赖的 Server。
|
||||
s := svc.server
|
||||
// unlock 保护账号凭证的创建与后续登录状态清理。
|
||||
unlock := s.Store.LockAccountCredentials(input.AccountID)
|
||||
unlock := svc.repository.LockCredentials(input.AccountID)
|
||||
// err 表示账号凭证创建错误。
|
||||
if err := s.Store.Cookies.CreateOwned(ctx, input.AccountID, input.Cookies, input.UserID); err != nil {
|
||||
if err := svc.repository.CreateCookieOwned(ctx, input.AccountID, input.Cookies, input.UserID); err != nil {
|
||||
unlock()
|
||||
return err
|
||||
}
|
||||
if s.Store.Tokens != nil {
|
||||
{
|
||||
// err 表示清理旧连接凭证的错误,仅记录不阻断登录。
|
||||
if err := s.Store.Tokens.Clear(ctx, input.AccountID); err != nil && s.Logger != nil {
|
||||
if err := svc.repository.ClearTokens(ctx, input.AccountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("新增账号后清理旧连接凭证失败", "cookie_id", input.AccountID, "err", err)
|
||||
}
|
||||
}
|
||||
@@ -90,7 +92,7 @@ func (svc *accountLoginService) UpdateCookie(ctx context.Context, input accountC
|
||||
// s 是当前账号登录应用服务依赖的 Server。
|
||||
s := svc.server
|
||||
// unlock 保护账号凭证更新、连接凭证清理和登录审计。
|
||||
unlock := s.Store.LockAccountCredentials(input.AccountID)
|
||||
unlock := svc.repository.LockCredentials(input.AccountID)
|
||||
// detail 和 err 保存账号凭证详情查询结果。
|
||||
detail, err := s.loadCookiePlatformDetail(ctx, input.AccountID)
|
||||
if err != nil || detail == nil || detail.UserID != input.UserID {
|
||||
@@ -101,13 +103,13 @@ func (svc *accountLoginService) UpdateCookie(ctx context.Context, input accountC
|
||||
return err
|
||||
}
|
||||
// err 表示扁平 Cookie 更新错误。
|
||||
if err := s.updateFlatCookieOwnedLocked(ctx, detail, input.Cookies); err != nil {
|
||||
if err := svc.repository.UpdateFlatCookieOwned(ctx, detail, input.Cookies); err != nil {
|
||||
unlock()
|
||||
return err
|
||||
}
|
||||
if s.Store.Tokens != nil {
|
||||
{
|
||||
// err 表示清理旧连接凭证的错误,仅记录不阻断登录。
|
||||
if err := s.Store.Tokens.Clear(ctx, input.AccountID); err != nil && s.Logger != nil {
|
||||
if err := svc.repository.ClearTokens(ctx, input.AccountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("更新账号后清理旧连接凭证失败", "cookie_id", input.AccountID, "err", err)
|
||||
}
|
||||
}
|
||||
@@ -181,7 +183,7 @@ func (svc *accountLoginService) persistQRLoginSuccessCore(ctx context.Context, u
|
||||
// isNew 标记本次扫码是否创建了新账号。
|
||||
isNew := false
|
||||
// credentialUnlock 保护账号凭证写入和登录审计。
|
||||
credentialUnlock := s.Store.LockAccountCredentials(accountID)
|
||||
credentialUnlock := svc.repository.LockCredentials(accountID)
|
||||
// saveErr 保存账号凭证、Cookie Jar 和登录审计的事务错误。
|
||||
saveErr := func() error {
|
||||
defer credentialUnlock()
|
||||
@@ -194,14 +196,14 @@ func (svc *accountLoginService) persistQRLoginSuccessCore(ctx context.Context, u
|
||||
}
|
||||
isNew = true
|
||||
// err 表示创建扫码账号的错误。
|
||||
if err := s.Store.Cookies.CreateOwned(ctx, accountID, cookies, userID); err != nil {
|
||||
if err := svc.repository.CreateCookieOwned(ctx, accountID, cookies, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
if snapshotComplete {
|
||||
// metadata 是新账号的完整 Cookie Jar 元数据。
|
||||
metadata := cookierefresh.MetadataWithSnapshot("", cookieSnapshot)
|
||||
// err 表示保存新账号 Cookie Jar 的错误。
|
||||
if err := s.Store.Cookies.UpdateRenewalCookie(ctx, accountID, cookies, metadata, time.Now().Unix()); err != nil {
|
||||
if err := svc.repository.UpdateRenewalCookie(ctx, accountID, cookies, metadata, time.Now().Unix()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -219,18 +221,18 @@ func (svc *accountLoginService) persistQRLoginSuccessCore(ctx context.Context, u
|
||||
// metadata 是已有账号合并后的 Cookie Jar 元数据。
|
||||
metadata := cookierefresh.MetadataWithSnapshot(detail.MetadataJSON, cookieSnapshot)
|
||||
// err 表示保存已有账号 Cookie Jar 的错误。
|
||||
if err := s.Store.Cookies.UpdateRenewalCookie(ctx, detail.ID, cookies, metadata, time.Now().Unix()); err != nil {
|
||||
if err := svc.repository.UpdateRenewalCookie(ctx, detail.ID, cookies, metadata, time.Now().Unix()); err != nil {
|
||||
return err
|
||||
}
|
||||
// err 表示合并扁平 Cookie 的错误。
|
||||
} else if err := s.updateFlatCookieOwnedLocked(ctx, detail, cookies); err != nil {
|
||||
} else if err := svc.repository.UpdateFlatCookieOwned(ctx, detail, cookies); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
s.markSuccessfulLogin(ctx, accountID, userID, loginMethodQRScan, "扫码登录成功")
|
||||
if s.Store.Tokens != nil {
|
||||
{
|
||||
// err 表示清理旧连接凭证的错误,仅记录不阻断扫码登录。
|
||||
if err := s.Store.Tokens.Clear(ctx, accountID); err != nil && s.Logger != nil {
|
||||
if err := svc.repository.ClearTokens(ctx, accountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("扫码登录保存后清理旧连接凭证失败", "cookie_id", accountID, "err", err)
|
||||
}
|
||||
}
|
||||
@@ -250,7 +252,7 @@ func (svc *accountLoginService) persistQRLoginSuccessCore(ctx context.Context, u
|
||||
s.refreshAccountProfile(ctx, detail)
|
||||
}
|
||||
s.wakeCredentialBlockedAutomation(ctx, accountID)
|
||||
if s.Manager != nil && s.Store.Cookies.GetStatus(ctx, accountID) {
|
||||
if s.Manager != nil && svc.repository.GetStatus(ctx, accountID) {
|
||||
// err 表示扫码登录后的账号运行时重启错误。
|
||||
if err := s.Manager.Restart(ctx, accountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("扫码登录后重启账号失败", "cookie_id", accountID, "err", err)
|
||||
@@ -274,7 +276,7 @@ func (svc *accountLoginService) refreshAndRestartAccount(ctx context.Context, us
|
||||
if err == nil {
|
||||
s.refreshAccountProfile(ctx, detail)
|
||||
}
|
||||
if s.Manager != nil && s.Store.Cookies.GetStatus(ctx, accountID) {
|
||||
if s.Manager != nil && svc.repository.GetStatus(ctx, accountID) {
|
||||
// err 表示账号运行时重启错误。
|
||||
if err := s.Manager.Restart(ctx, accountID); err != nil && s.Logger != nil {
|
||||
s.Logger.Warn("账号登录后重启账号失败", "cookie_id", accountID, "err", err)
|
||||
|
||||
@@ -19,7 +19,7 @@ func newApplicationServices(server *Server) *applicationServices {
|
||||
return &applicationServices{
|
||||
orders: &orderApplicationService{server: server, repository: newStoreOrderRepository(server.Store)},
|
||||
itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)},
|
||||
accountLogin: &accountLoginService{server: server},
|
||||
accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
analytics: &analyticsService{repository: newStoreAnalyticsRepository(server.Store)},
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user