可靠性与数据一致性:为 SQLite、MySQL、PostgreSQL 增加 00017 至 00021 迁移,引入账号暂停时间、默认回复和批量铺货租约、自动化运行恢复、延迟任务、动作检查点与通知 outbox。补强订单、卡密发货、Cookie、批量铺货和自动化任务的原子认领、幂等更新、并发控制、失败分类、重试与进程重启恢复,并扩展跨方言覆盖。 登录与风控恢复:完善扫码、密码登录、Cookie 续期、MTOP token、UA 与 WebSocket 生命周期;加入账号级持久化浏览器目录、Playwright 主引擎和直接 Chromium/CDP 备用引擎、严格的新 x5sec 成功判定、失败控件恢复及真实浏览器集成测试。增加可配置远程滑块服务,在明确失败、链接过期和服务不可用场景中保持既定的本地回退语义。 安全与配置:通过 XIANYU_DATA_KEY 为 Cookie、登录密码、设备与访问令牌、AI/SMTP 密钥及通知配置提供 AES-256-GCM 落库加密和历史明文升级。新增出站网络目标分级、防 DNS rebinding、登录限流、HTTP 客户端超时与 SMTP 目标校验;管理员配置的 AI 服务允许可信内网地址,非可信 Webhook 和远程资源继续限制为公网目标。 自动化与通知:为发货、评价赠送、邀评和回复动作增加断点、租约、延迟执行及人工继续/重试/取消入口;通知改为 outbox 驱动的可恢复投递。同步完善账号运行态、暂停状态、续期冷却、告警审计和异常查询接口。 前端体验:新增风控验证面板、方形二维码、账号运行态合并、暂停状态保护、自动化异常处置、批量铺货状态选择、订单导入结果和最新请求门禁。完善订单搜索与导入、规则编辑、批次恢复、通知与设置页面,并为请求取消、超时、陈旧响应和主要状态转换补充 Vitest 覆盖。 测试与部署:扩展三库功能编排、SQLite 一致性备份、数据卷持久化、浏览器集成镜像、前端类型检查和构建资产分包;功能测试覆盖健康检查、订单、铺货、自动化异常、账号暂停、关键词和系统设置。更新 README 中数据密钥、可信 AI 地址和测试端口说明。 验证结果:go vet ./...、go build ./cmd/server、gofmt 检查、npm run typecheck、npm test(17 个文件、95 个用例)及 npm run build 通过。go test ./... 未完全通过:internal/adapter 仍有 2 个密码续期用例失败,internal/renewal 仍有 4 个浏览器续期调度用例失败;其余 Go 包通过,失败现状随本次既有工作区一并提交。
291 lines
8.6 KiB
Go
291 lines
8.6 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"strings"
|
|
)
|
|
|
|
// ItemInfo 对应 item_info 表。
|
|
type ItemInfo struct {
|
|
ID int64
|
|
CookieID string
|
|
ItemID string
|
|
ItemTitle string
|
|
ItemDescription string
|
|
ItemCategory string
|
|
ItemPrice string
|
|
ItemDetail string
|
|
IsMultiSpec bool
|
|
MultiQuantityDelivery bool
|
|
}
|
|
|
|
// Card 对应 cards 表(发货用字段)。
|
|
type Card struct {
|
|
ID int64
|
|
Name string
|
|
Type string // api/text/data/image
|
|
APIConfig string // JSON
|
|
TextContent string
|
|
DataContent string
|
|
ImageURL string
|
|
Description string
|
|
Enabled bool
|
|
DelaySeconds int
|
|
IsMultiSpec bool
|
|
SpecName string
|
|
SpecValue string
|
|
UserID int64
|
|
}
|
|
|
|
// Items 商品信息操作。
|
|
type Items struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
}
|
|
|
|
// Get 取某账号下某商品信息。不存在返回 ErrNotFound。
|
|
func (i *Items) Get(ctx context.Context, cookieID, itemID string) (*ItemInfo, error) {
|
|
var it ItemInfo
|
|
var isMulti, multiQty int
|
|
var title, desc, cat, price, detail sql.NullString
|
|
err := i.DB.QueryRowContext(ctx,
|
|
`SELECT id, cookie_id, item_id, item_title, item_description, item_category, item_price, item_detail,
|
|
is_multi_spec, multi_quantity_delivery
|
|
FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(
|
|
&it.ID, &it.CookieID, &it.ItemID, &title, &desc, &cat,
|
|
&price, &detail, &isMulti, &multiQty)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
it.ItemTitle = title.String
|
|
it.ItemDescription = desc.String
|
|
it.ItemCategory = cat.String
|
|
it.ItemPrice = price.String
|
|
it.ItemDetail = detail.String
|
|
it.IsMultiSpec = isMulti != 0
|
|
it.MultiQuantityDelivery = multiQty != 0
|
|
return &it, nil
|
|
}
|
|
|
|
// IsMultiSpec 是否多规格商品。
|
|
func (i *Items) IsMultiSpec(ctx context.Context, cookieID, itemID string) bool {
|
|
var v int
|
|
err := i.DB.QueryRowContext(ctx, `SELECT is_multi_spec FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(&v)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return v != 0
|
|
}
|
|
|
|
// MultiQuantityDelivery 是否开启多数量发货。
|
|
func (i *Items) MultiQuantityDelivery(ctx context.Context, cookieID, itemID string) bool {
|
|
var v int
|
|
err := i.DB.QueryRowContext(ctx, `SELECT multi_quantity_delivery FROM item_info WHERE cookie_id=? AND item_id=?`, cookieID, itemID).Scan(&v)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return v != 0
|
|
}
|
|
|
|
// Cards 卡券操作。
|
|
type Cards struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
}
|
|
|
|
// ConsumeBatchData 原子预留一条批量数据卡券(data 类型),返回内容。
|
|
// 通过快照条件更新处理并发追加/消费;调用方发送失败时应调用 RestoreBatchData。
|
|
func (c *Cards) ConsumeBatchData(ctx context.Context, cardID int64) (string, error) {
|
|
for attempts := 0; attempts < 20; attempts++ {
|
|
var dataContent sql.NullString
|
|
err := c.DB.QueryRowContext(ctx, `SELECT data_content FROM cards WHERE id=?`, cardID).Scan(&dataContent)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", ErrNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
if !dataContent.Valid || dataContent.String == "" {
|
|
return "", errors.New("卡券批量数据为空")
|
|
}
|
|
lines := splitLines(dataContent.String)
|
|
if len(lines) == 0 {
|
|
return "", errors.New("卡券批量数据无有效行")
|
|
}
|
|
remaining := ""
|
|
if len(lines) > 1 {
|
|
remaining = joinLines(lines[1:])
|
|
}
|
|
res, err := c.DB.ExecContext(ctx,
|
|
`UPDATE cards SET data_content=?,updated_at=CURRENT_TIMESTAMP WHERE id=? AND data_content=?`,
|
|
remaining, cardID, dataContent.String)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if affected, err := res.RowsAffected(); err == nil && affected == 1 {
|
|
return lines[0], nil
|
|
}
|
|
}
|
|
return "", errors.New("卡券库存并发修改过于频繁,请稍后重试")
|
|
}
|
|
|
|
// RestoreBatchData 把发送失败的预留卡密放回库存头部。恢复失败时宁可进入人工处理,
|
|
// 也不能把一个已成功发送但响应不确定的卡密自动重复发给下一位买家。
|
|
func (c *Cards) RestoreBatchData(ctx context.Context, cardID int64, content string) error {
|
|
content = strings.TrimSpace(content)
|
|
if content == "" {
|
|
return errors.New("恢复卡密内容为空")
|
|
}
|
|
for attempts := 0; attempts < 20; attempts++ {
|
|
var current sql.NullString
|
|
if err := c.DB.QueryRowContext(ctx, `SELECT data_content FROM cards WHERE id=?`, cardID).Scan(¤t); err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
merged := content
|
|
if current.Valid && strings.TrimSpace(current.String) != "" {
|
|
merged += "\n" + current.String
|
|
}
|
|
res, err := c.DB.ExecContext(ctx,
|
|
`UPDATE cards SET data_content=?,updated_at=CURRENT_TIMESTAMP WHERE id=? AND COALESCE(data_content,'')=?`,
|
|
merged, cardID, current.String)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if affected, err := res.RowsAffected(); err == nil && affected == 1 {
|
|
return nil
|
|
}
|
|
}
|
|
return errors.New("恢复卡密时库存并发修改过于频繁")
|
|
}
|
|
|
|
// FirstBatchData 返回 data 类型卡券当前第一条有效内容和原始快照。
|
|
// 调用方发送成功后应调用 CommitFirstBatchData 删除同一快照中的第一条,避免发送失败丢卡。
|
|
func (c *Cards) FirstBatchData(ctx context.Context, cardID int64) (content, snapshot string, err error) {
|
|
var dataContent sql.NullString
|
|
err = c.DB.QueryRowContext(ctx, `SELECT data_content FROM cards WHERE id=?`, cardID).Scan(&dataContent)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return "", "", ErrNotFound
|
|
}
|
|
return "", "", err
|
|
}
|
|
if !dataContent.Valid || dataContent.String == "" {
|
|
return "", "", errors.New("卡券批量数据为空")
|
|
}
|
|
lines := splitLines(dataContent.String)
|
|
if len(lines) == 0 {
|
|
return "", "", errors.New("卡券批量数据无有效行")
|
|
}
|
|
return lines[0], dataContent.String, nil
|
|
}
|
|
|
|
// CommitFirstBatchData 在 data_content 仍等于 snapshot 时删除第一条有效内容。
|
|
// 条件更新失败表示库存被并发修改,调用方应停止本次发货,避免错删卡密。
|
|
func (c *Cards) CommitFirstBatchData(ctx context.Context, cardID int64, snapshot string) error {
|
|
lines := splitLines(snapshot)
|
|
if len(lines) == 0 {
|
|
return errors.New("卡券批量数据无有效行")
|
|
}
|
|
remaining := ""
|
|
if len(lines) > 1 {
|
|
remaining = joinLines(lines[1:])
|
|
}
|
|
res, err := c.DB.ExecContext(ctx, `UPDATE cards SET data_content=? WHERE id=? AND data_content=?`, remaining, cardID, snapshot)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
n, err := res.RowsAffected()
|
|
if err == nil && n == 0 {
|
|
return errors.New("卡券库存已被并发修改,请重试")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AppendBatchData 往 data 类型卡密组追加卡密号(按行)。返回新增的有效(非空)行数。
|
|
// 已有 data_content 非空时在末尾换行追加,否则直接写入。
|
|
func (c *Cards) AppendBatchData(ctx context.Context, cardID int64, content string) (int, error) {
|
|
lines := splitLines(content)
|
|
valid := 0
|
|
for _, l := range lines {
|
|
if strings.TrimSpace(l) != "" {
|
|
valid++
|
|
}
|
|
}
|
|
if valid == 0 {
|
|
return 0, errors.New("无有效卡密行")
|
|
}
|
|
for attempts := 0; attempts < 20; attempts++ {
|
|
var existing sql.NullString
|
|
err := c.DB.QueryRowContext(ctx, `SELECT data_content FROM cards WHERE id=?`, cardID).Scan(&existing)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return 0, ErrNotFound
|
|
}
|
|
return 0, err
|
|
}
|
|
merged := content
|
|
if existing.Valid && existing.String != "" {
|
|
merged = existing.String + "\n" + content
|
|
}
|
|
res, err := c.DB.ExecContext(ctx,
|
|
`UPDATE cards SET data_content=?,updated_at=CURRENT_TIMESTAMP WHERE id=? AND COALESCE(data_content,'')=?`,
|
|
merged, cardID, existing.String)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
if affected, err := res.RowsAffected(); err == nil && affected == 1 {
|
|
return valid, nil
|
|
}
|
|
}
|
|
return 0, errors.New("追加卡密时库存并发修改过于频繁")
|
|
}
|
|
|
|
// splitLines / joinLines 统一处理多行库存内容。
|
|
func splitLines(s string) []string {
|
|
var out []string
|
|
cur := []rune{}
|
|
for _, r := range s {
|
|
switch r {
|
|
case '\n':
|
|
out = append(out, string(cur))
|
|
cur = cur[:0]
|
|
case '\r':
|
|
out = append(out, string(cur))
|
|
cur = cur[:0]
|
|
// 跳过 \r,处理 \r\n
|
|
default:
|
|
cur = append(cur, r)
|
|
}
|
|
}
|
|
if len(cur) > 0 {
|
|
out = append(out, string(cur))
|
|
}
|
|
// 过滤空行。
|
|
res := out[:0]
|
|
for _, l := range out {
|
|
if strings.TrimSpace(l) != "" {
|
|
res = append(res, l)
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
func joinLines(lines []string) string {
|
|
if len(lines) == 0 {
|
|
return ""
|
|
}
|
|
out := lines[0]
|
|
for _, l := range lines[1:] {
|
|
out += "\n" + l
|
|
}
|
|
return out
|
|
}
|