fix(发布与账号): 修复虚拟铺货和删除任务体验

- 虚拟商品发布跳过地址查询与地址字段,避免被默认地址校验拦截。
- 取消批量铺货后保留上传图片,支持失败项重试并按过期策略清理。
- 优化账号删除的确认弹窗、进度反馈与后台停止逻辑,避免误删和页面卡顿。
This commit is contained in:
Christ committed 2026-07-25 18:37:30 +08:00
1 parent 7fd11106af
commit fd30a0d17a
14 files changed
+278 -57

No files matched your search

+8 -3
View File
@@ -651,10 +651,15 @@ func (s *Server) deleteCookie(w http.ResponseWriter, r *http.Request) {
return
}
credentialUnlock()
// Stop 会等待 Account.Run 退出,而 Run 可能正在等待同一凭证锁,因此必须
// 在删除事务完成并释放锁后调用。
s.Logger.Info("账号已删除",
"cookie_id", cid,
"nickname", cachedAccountNickname(latest),
"user_id", latest.UserID,
)
// Stop 可能需要等待运行中任务收尾,不应阻塞删除 HTTP 请求。
// 数据库事务已完成,先向前端确认删除,再在后台精确停止该 cid 的实例。
if s.Manager != nil {
s.Manager.Stop(cid)
go s.Manager.Stop(cid)
}
writeJSON(w, http.StatusOK, map[string]any{"success": true})
}
+8
View File
@@ -3,6 +3,7 @@ package server
import (
"context"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strconv"
@@ -738,6 +739,7 @@ func TestDeleteCookie(t *testing.T) {
defer cleanup()
ctx := context.Background()
store.Cookies.Save(ctx, "acc-del", "unb=1; _m_h5_tk=t_1;", 1)
store.Cookies.Save(ctx, "acc-keep", "unb=2; _m_h5_tk=t_2;", 1)
h := srv.Router()
cookie := loginHelper(t, h)
@@ -748,6 +750,12 @@ func TestDeleteCookie(t *testing.T) {
if rec.Code != 200 {
t.Fatalf("delete status=%d", rec.Code)
}
if _, err := store.Cookies.GetDetails(ctx, "acc-del"); !errors.Is(err, db.ErrNotFound) {
t.Fatalf("目标账号应被删除,err=%v", err)
}
if kept, err := store.Cookies.GetDetails(ctx, "acc-keep"); err != nil || kept.ID != "acc-keep" {
t.Fatalf("非目标账号不应被删除,kept=%+v err=%v", kept, err)
}
}
// TestAddCookieBad 缺 id 或 value 400。
+1
View File
@@ -108,6 +108,7 @@ func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
Quantity: quantity,
PostageMode: postageMode,
PostageCents: postageCents,
Virtual: true,
Images: images,
})
runtimeCookie := ""
@@ -364,10 +364,6 @@ func (s *Server) cancelItemPublishBatch(w http.ResponseWriter, r *http.Request)
}
if running {
s.cancelPublishBatch(batchID, workerToken)
} else {
if batch, getErr := s.Store.PublishBatches.Get(r.Context(), sess.UserID, batchID); getErr == nil {
s.removePublishUploadDir(r.Context(), batch)
}
}
writeJSON(w, http.StatusOK, map[string]any{"success": true, "status": map[bool]string{true: "canceling", false: "canceled"}[running]})
}
@@ -477,7 +473,6 @@ func (s *Server) recoverPublishBatchesOnce(ctx context.Context) {
if finalizeErr != nil || !finalized {
continue
}
s.removePublishUploadDir(ctx, &batch)
continue
}
workerToken := randomHex(16)
@@ -710,7 +705,7 @@ func (s *Server) finishInterruptedPublishBatch(ctx context.Context, userID int64
return
}
}
s.removePublishUploadDir(wctx, batch)
// 取消产生的 interrupted 失败行允许用户稍后重试,图片由统一的过期清理保留 7 天。
return
}
_, _, finalizeErr := s.Store.PublishBatches.FinalizeInterrupted(wctx, batchID, workerToken, "任务超时或已中断")
@@ -731,7 +726,7 @@ func (s *Server) finishPublishBatch(ctx context.Context, userID int64, batchID,
if finalizeErr != nil || !finalized {
return
}
s.removePublishUploadDir(wctx, batch)
// 取消任务仍可“重试失败项”,不能在此删除重试所需的本地图片。
return
}
finalStatus, finished, finishErr := s.Store.PublishBatches.FinalizeBatch(wctx, batchID, workerToken)
@@ -814,6 +809,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop.
Quantity: row.Quantity,
PostageMode: row.PostageMode,
PostageCents: postageCents,
Virtual: true,
Images: images,
})
value, valueChanged, handled, persistErr := s.persistMTopCookieSessionLocked(ctx, latest, cookieSession)
+12 -1
View File
@@ -9,6 +9,7 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"
@@ -218,7 +219,7 @@ func TestRunItemPublishBatch_FailureMarksRowFailed(t *testing.T) {
}
func TestCancelItemPublishBatchMarksUnfinishedRowsFailed(t *testing.T) {
srv, _, cleanup := newTestServer(t)
srv, store, cleanup := newTestServer(t)
defer cleanup()
srv.MTop = withMTopTransport(roundTripFunc(func(req *http.Request) (*http.Response, error) {
select {
@@ -279,6 +280,16 @@ func TestCancelItemPublishBatchMarksUnfinishedRowsFailed(t *testing.T) {
if got["failed"] != got["total"] {
t.Fatalf("取消后未完成行应全部失败: total=%v failed=%v body=%+v", got["total"], got["failed"], got)
}
batch, err := store.PublishBatches.Get(context.Background(), 1, batchID)
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(batch.UploadDir) == "" {
t.Fatal("取消运行中的任务后应保留 upload_dir 供失败项重试")
}
if _, err := os.Stat(batch.UploadDir); err != nil {
t.Fatalf("取消运行中的任务后应保留图片目录: %v", err)
}
}
// TestRunItemPublishBatch_Success 启动批次,mock mtop 全程返回 SUCCESS,验证行最终成功。
+27
View File
@@ -280,6 +280,33 @@ func TestCancelItemPublishBatchNotFound(t *testing.T) {
}
}
func TestCancelPreviewBatchRetainsUploadDirectoryForRetry(t *testing.T) {
srv, store, cleanup := newTestServer(t)
defer cleanup()
h := srv.Router()
cookie := loginHelper(t, h)
batchID := previewPublishBatch(t, h, cookie)
batch, err := store.PublishBatches.Get(context.Background(), 1, batchID)
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodPost, "/items/publish-batches/"+batchID+"/cancel", nil)
req.AddCookie(cookie)
rec := httptest.NewRecorder()
h.ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("cancel status=%d body=%s", rec.Code, rec.Body.String())
}
if _, err := os.Stat(batch.UploadDir); err != nil {
t.Fatalf("取消后应保留上传目录供重试: %v", err)
}
retained, err := store.PublishBatches.Get(context.Background(), 1, batchID)
if err != nil || retained.UploadDir != batch.UploadDir {
t.Fatalf("取消后应保留 upload_dir: batch=%+v err=%v", retained, err)
}
}
func TestDeletePreviewBatchRemovesUploadDirectory(t *testing.T) {
srv, store, cleanup := newTestServer(t)
defer cleanup()