支持按账号商品监控库存抢购
This commit is contained in:
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -13,16 +14,23 @@ import (
|
||||
|
||||
// LoadProducts 刷新商品列表。refresh=true 时从斗鱼实时拉取。
|
||||
func (a *App) LoadProducts(refresh bool) types.ProductPayload {
|
||||
return a.loadProducts(refresh, nil)
|
||||
}
|
||||
|
||||
// LoadProductsForAccounts 使用指定账号随机刷新和平小店商品。
|
||||
// 库存监控仅使用参与监控的账号,避免无关账号承担角色鉴权请求。
|
||||
func (a *App) LoadProductsForAccounts(accountNames []string) types.ProductPayload {
|
||||
return a.loadProducts(true, accountNames)
|
||||
}
|
||||
|
||||
func (a *App) loadProducts(refresh bool, accountNames []string) types.ProductPayload {
|
||||
if refresh {
|
||||
cfg := a.getCfg()
|
||||
options := types.XiaodianProductListOptions{PageSize: cfg.XiaodianPageSize}
|
||||
var products []types.Product
|
||||
err := fmt.Errorf("没有可用账号 Cookie")
|
||||
accounts := a.scheduler.AccountSnapshot()
|
||||
accounts := productRefreshAccounts(a.scheduler.AccountSnapshot(), accountNames)
|
||||
for _, account := range accounts {
|
||||
if strings.TrimSpace(account.Cookie) == "" {
|
||||
continue
|
||||
}
|
||||
products, err = a.getClient().Products(account.Cookie, options)
|
||||
if err == nil {
|
||||
break
|
||||
@@ -41,6 +49,30 @@ func (a *App) LoadProducts(refresh bool) types.ProductPayload {
|
||||
return a.productPayload("cache")
|
||||
}
|
||||
|
||||
func productRefreshAccounts(accounts []types.Account, preferredNames []string) []types.Account {
|
||||
preferred := make(map[string]struct{}, len(preferredNames))
|
||||
for _, name := range preferredNames {
|
||||
preferred[strings.TrimSpace(name)] = struct{}{}
|
||||
}
|
||||
|
||||
filtered := make([]types.Account, 0, len(accounts))
|
||||
for _, account := range accounts {
|
||||
if strings.TrimSpace(account.Cookie) == "" {
|
||||
continue
|
||||
}
|
||||
if len(preferred) > 0 {
|
||||
if _, ok := preferred[account.Name]; !ok {
|
||||
continue
|
||||
}
|
||||
}
|
||||
filtered = append(filtered, account)
|
||||
}
|
||||
rand.Shuffle(len(filtered), func(i, j int) {
|
||||
filtered[i], filtered[j] = filtered[j], filtered[i]
|
||||
})
|
||||
return filtered
|
||||
}
|
||||
|
||||
// GetAccounts 返回账号列表。
|
||||
func (a *App) GetAccounts() []types.AccountView {
|
||||
accounts := a.scheduler.AccountSnapshot()
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"douyu_go/internal/types"
|
||||
)
|
||||
|
||||
func TestProductRefreshAccountsUsesOnlyPreferredAccounts(t *testing.T) {
|
||||
accounts := []types.Account{
|
||||
{Name: "a", Cookie: "cookie-a"},
|
||||
{Name: "b", Cookie: ""},
|
||||
{Name: "c", Cookie: "cookie-c"},
|
||||
}
|
||||
result := productRefreshAccounts(accounts, []string{"c", "b"})
|
||||
if len(result) != 1 || result[0].Name != "c" {
|
||||
t.Fatalf("productRefreshAccounts() = %#v", result)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user