feat: 对齐闲鱼续期流程并完善业务管理
对齐官方 WebSocket 批量握手、注册、心跳、ACK、同步恢复及运行时设备标识生命周期。 保留 Cookie 完整属性,接入静默续期和保存登录信息设置,并在凭证更新后重建账号连接。 完善订单与商品同步、卡券和通知渠道交互,更新前端品牌、时间展示及嵌入资源。 补充 Go 与前端回归测试,确保完整检查和生产构建通过。
This commit is contained in:
1 parent
3cc1d4094d
commit
d77c317452
69 files changed
+3169
-819
No files matched your search
@@ -7,11 +7,9 @@ import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// AccountToken 持久化的账号登录凭证缓存(device_id + accessToken)。
|
||||
//
|
||||
// 设计动机(参考 xianyu-auto-reply 的 xy_token_cache):
|
||||
// - device_id 跨进程重启复用,避免每次重启换新设备 ID 触发阿里端设备绑定/风控;
|
||||
// - accessToken 缓存后,短重启可复用、瞬时 mtop 失败可回退到上次有效 token,不掉线。
|
||||
// AccountToken 持久化最近一次页面运行实例的 device_id 和 token 请求元数据。
|
||||
// accessToken 不用于后续 WebSocket 注册;官方消息页在每次 loginV2/reConnect
|
||||
// 前都会重新获取 token,页面重载后也会生成新的 device_id。
|
||||
type AccountToken struct {
|
||||
CookieID string
|
||||
DeviceID string
|
||||
@@ -56,17 +54,9 @@ func (t *AccountTokens) Save(ctx context.Context, cookieID, deviceID, accessToke
|
||||
return t.SaveBound(ctx, cookieID, deviceID, accessToken, expireAt, "")
|
||||
}
|
||||
|
||||
// SaveBound persists an access token together with the canonical Cookie state
|
||||
// from which it was issued. The binding lets a later reconnect reject a stale
|
||||
// token after an independent Cookie renewal without rotating device_id.
|
||||
// SaveBound persists an access token together with the page-runtime device ID
|
||||
// and canonical Cookie state from which it was issued.
|
||||
func (t *AccountTokens) SaveBound(ctx context.Context, cookieID, deviceID, accessToken string, expireAt int64, cookieFingerprint string) error {
|
||||
// device_id is an account identity, not part of the expiring token cache.
|
||||
// Once stored, token refreshes must never replace it.
|
||||
if existing, err := t.Get(ctx, cookieID); err == nil && existing.DeviceID != "" {
|
||||
deviceID = existing.DeviceID
|
||||
} else if err != nil && !errors.Is(err, ErrNotFound) {
|
||||
return err
|
||||
}
|
||||
encryptedDeviceID, err := t.codec.encrypt("device-id", cookieID, deviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -39,21 +39,21 @@ func TestAccountTokens_CRUD(t *testing.T) {
|
||||
t.Fatalf("Get 返回不匹配: %+v", tk)
|
||||
}
|
||||
|
||||
// 再次 Save 更新 token,但永久 device ID 不允许覆盖。
|
||||
// 页面运行实例变化后,再次 Save 同步更新 device ID 和 token。
|
||||
if err := store.Tokens.SaveBound(ctx, "cid", "dev-2", "tok-2", expire+1, "cookie-hash-2"); err != nil {
|
||||
t.Fatalf("Save upsert: %v", err)
|
||||
}
|
||||
tk, _ = store.Tokens.Get(ctx, "cid")
|
||||
if tk.DeviceID != "dev-1" || tk.AccessToken != "tok-2" || tk.ExpireAt != expire+1 || tk.CookieFingerprint != "cookie-hash-2" {
|
||||
if tk.DeviceID != "dev-2" || tk.AccessToken != "tok-2" || tk.ExpireAt != expire+1 || tk.CookieFingerprint != "cookie-hash-2" {
|
||||
t.Fatalf("upsert 后字段应更新: %+v", tk)
|
||||
}
|
||||
|
||||
// Clear 只清 token,永久 device ID 必须保留。
|
||||
// Clear 只清 token,保留最近一次页面运行实例的 device ID。
|
||||
if err := store.Tokens.Clear(ctx, "cid"); err != nil {
|
||||
t.Fatalf("Clear: %v", err)
|
||||
}
|
||||
tk, err = store.Tokens.Get(ctx, "cid")
|
||||
if err != nil || tk.DeviceID != "dev-1" || tk.AccessToken != "" || tk.ExpireAt != 0 || tk.CookieFingerprint != "" {
|
||||
if err != nil || tk.DeviceID != "dev-2" || tk.AccessToken != "" || tk.ExpireAt != 0 || tk.CookieFingerprint != "" {
|
||||
t.Fatalf("Clear 后 device ID 应保留且 token 清空: tk=%+v err=%v", tk, err)
|
||||
}
|
||||
|
||||
@@ -63,7 +63,7 @@ func TestAccountTokens_CRUD(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccountTokens_DeviceIDSurvivesTokenLifecycle(t *testing.T) {
|
||||
func TestAccountTokens_SaveTracksLatestRuntimeDeviceID(t *testing.T) {
|
||||
store, cleanup := newTestDB(t)
|
||||
defer cleanup()
|
||||
ctx := context.Background()
|
||||
@@ -83,8 +83,8 @@ func TestAccountTokens_DeviceIDSurvivesTokenLifecycle(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
afterRestart, err := store.Tokens.GetOrCreateDeviceID(ctx, "cid", "device-after-restart")
|
||||
if err != nil || first != "device-first" || afterRestart != first {
|
||||
t.Fatalf("device ID changed across token lifecycle: first=%q after=%q err=%v", first, afterRestart, err)
|
||||
if err != nil || first != "device-first" || afterRestart != "device-replacement" {
|
||||
t.Fatalf("未保留最近一次运行时 device ID: first=%q after=%q err=%v", first, afterRestart, err)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -201,6 +201,9 @@ func orderUpsertAssignments(opts OrderUpsertOpts) ([]string, []any) {
|
||||
if opts.SystemShipped != nil {
|
||||
add("system_shipped", boolToInt(*opts.SystemShipped), true)
|
||||
}
|
||||
if opts.IsBargain != nil {
|
||||
add("is_bargain", boolToInt(*opts.IsBargain), true)
|
||||
}
|
||||
add("chat_id", opts.ChatID, opts.ChatID != "")
|
||||
add("item_id", opts.ItemID, opts.ItemID != "")
|
||||
add("buyer_id", opts.BuyerID, opts.BuyerID != "")
|
||||
@@ -277,6 +280,7 @@ type OrderUpsertOpts struct {
|
||||
ReceiverAddr string
|
||||
ReceiverCity string
|
||||
ChatID string
|
||||
IsBargain *bool
|
||||
SystemShipped *bool
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user