完善商品发布位置与 Docker 运行环境

This commit is contained in:
Christ committed 2026-08-13 09:12:22 +08:00
1 parent 9adc77df73
commit 7ec0b4a078
21 files changed
+431 -176

No files matched your search

+62
View File
@@ -23,6 +23,7 @@ func (s *Server) mountItemsReal(r chi.Router) {
r.Post("/items/get-all-from-account", s.syncItemsFromAccount)
r.Post("/items/get-by-page", s.syncItemsPageFromAccount)
r.Post("/items/publish", s.publishItem)
r.Post("/items/publish-locations", s.publishLocations)
r.Post("/items/publish-categories/recommend", s.recommendItemPublishCategory)
r.Post("/items/publish-batches/preview", s.previewItemPublishBatch)
r.Post("/items/publish-batches", s.startItemPublishBatch)
@@ -41,6 +42,57 @@ func (s *Server) mountItemsReal(r chi.Router) {
r.Put("/items/{cookie_id}/{item_id}/multi-quantity-delivery", s.setItemMultiQuantity)
}
func (s *Server) publishLocations(w http.ResponseWriter, r *http.Request) {
var req struct {
CookieID string `json:"cookie_id"`
Longitude float64 `json:"longitude"`
Latitude float64 `json:"latitude"`
}
if err := decodeJSON(r, &req); err != nil || strings.TrimSpace(req.CookieID) == "" {
writeErr(w, http.StatusBadRequest, "请选择账号并提供当前位置")
return
}
_, userID, ok := s.cookieForCurrentUser(w, r, strings.TrimSpace(req.CookieID))
if !ok {
return
}
credentialUnlock := s.Store.LockAccountCredentials(req.CookieID)
defer credentialUnlock()
latest, err := s.Store.Cookies.GetDetails(r.Context(), req.CookieID)
if err != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) {
writeErr(w, http.StatusConflict, "账号凭证已变化,请重试")
return
}
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
mtopCtx, cookieSession := withMTopCookieSnapshot(ctx, latest)
locator, ok := s.mtopClient().(interface {
PublishLocations(context.Context, string, float64, float64) ([]mtop.PublishLocation, string, error)
})
if !ok {
writeErr(w, http.StatusNotImplemented, "当前客户端不支持查询发货地")
return
}
locations, updated, callErr := locator.PublishLocations(mtopCtx, latest.Value, req.Longitude, req.Latitude)
value, changed, handled, persistErr := s.persistMTopCookieSessionLocked(r.Context(), latest, cookieSession)
if persistErr != nil {
writeErr(w, http.StatusInternalServerError, "保存账号登录状态失败")
return
}
if !handled && updated != "" && updated != latest.Value {
if err := s.Store.Cookies.UpdateValueOwned(r.Context(), req.CookieID, updated, userID); err == nil {
s.updateRunningCookie(r.Context(), req.CookieID, updated)
}
} else if handled && changed {
s.updateRunningCookie(r.Context(), req.CookieID, value)
}
if callErr != nil {
writeErr(w, http.StatusBadGateway, callErr.Error())
return
}
writeJSON(w, http.StatusOK, map[string]any{"success": true, "locations": locations})
}
func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
// 最多 9 张 10 MiB 图片,额外预留 multipart 元数据空间。
r.Body = http.MaxBytesReader(w, r.Body, maxItemPublishBytes)
@@ -89,6 +141,15 @@ func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
writeErr(w, http.StatusBadRequest, err.Error())
return
}
var location mtop.PublishLocation
var selectedLocation *mtop.PublishLocation
if rawLocation := strings.TrimSpace(r.FormValue("location")); rawLocation != "" {
if err := json.Unmarshal([]byte(rawLocation), &location); err != nil {
writeErr(w, http.StatusBadRequest, "发货地格式错误,请重新定位")
return
}
selectedLocation = &location
}
credentialUnlock := s.Store.LockAccountCredentials(cookieID)
latest, err := s.Store.Cookies.GetDetails(r.Context(), cookieID)
if err != nil || latest == nil || latest.UserID != userID || !hasStoredCookieCredential(latest) {
@@ -110,6 +171,7 @@ func (s *Server) publishItem(w http.ResponseWriter, r *http.Request) {
PostageMode: postageMode,
PostageCents: postageCents,
Virtual: true,
Location: selectedLocation,
Images: images,
})
runtimeCookie := ""
@@ -194,6 +194,15 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request)
ChannelCatID: strings.TrimSpace(r.FormValue("fallback_channel_category_id")),
TBCatID: strings.TrimSpace(r.FormValue("fallback_tb_category_id")),
}
var batchLocation mtop.PublishLocation
locationJSON := strings.TrimSpace(r.FormValue("location"))
if locationJSON != "" {
if json.Unmarshal([]byte(locationJSON), &batchLocation) != nil {
writeErr(w, http.StatusBadRequest, "发货地格式错误,请重新定位")
return
}
}
locationBytes, _ := json.Marshal(batchLocation)
hasDefaultCategory := fallbackCategory.CatID != "" || fallbackCategory.CatName != "" || fallbackCategory.ChannelCatID != "" || fallbackCategory.TBCatID != ""
if hasDefaultCategory && (fallbackCategory.CatID == "" || fallbackCategory.CatName == "" || fallbackCategory.ChannelCatID == "") {
writeErr(w, http.StatusBadRequest, "默认类目信息不完整,请重新通过关键词获取")
@@ -331,6 +340,7 @@ func (s *Server) previewItemPublishBatch(w http.ResponseWriter, r *http.Request)
DefaultCookieID: defaultCookieID,
Filename: sourceName,
UploadDir: uploadDir,
LocationJSON: string(locationBytes),
Status: "preview",
}, rows); err != nil {
writeErr(w, http.StatusInternalServerError, "保存预检结果失败")
@@ -854,6 +864,18 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop.
if err != nil {
return errors.New("批量任务不存在")
}
var location mtop.PublishLocation
locationJSON := strings.TrimSpace(batch.LocationJSON)
if locationJSON == "" {
locationJSON = "{}"
}
if err := json.Unmarshal([]byte(locationJSON), &location); err != nil {
return errors.New("批量任务发货地配置损坏,请重新创建任务")
}
var selectedLocation *mtop.PublishLocation
if strings.TrimSpace(location.DivisionID) != "" {
selectedLocation = &location
}
cookieValue, err := s.cookieValueForUser(ctx, userID, row.CookieID)
if err != nil {
return err
@@ -919,6 +941,7 @@ func (s *Server) publishBatchRow(ctx context.Context, userID int64, client mtop.
PostageMode: row.PostageMode,
PostageCents: postageCents,
Virtual: true,
Location: selectedLocation,
PreferredCategory: preferredCategory,
Images: images,
})
@@ -1308,6 +1331,10 @@ func (s *Server) validatePublishAutomation(ctx context.Context, userID int64, cf
}
func publishBatchToMap(batch *db.ItemPublishBatch, rows []db.ItemPublishBatchRow) map[string]any {
locationJSON := strings.TrimSpace(batch.LocationJSON)
if locationJSON == "" {
locationJSON = "{}"
}
outRows := make([]map[string]any, 0, len(rows))
pending := 0
running := 0
@@ -1343,6 +1370,7 @@ func publishBatchToMap(batch *db.ItemPublishBatch, rows []db.ItemPublishBatchRow
"id": batch.ID, "status": batch.Status, "filename": batch.Filename,
"total": batch.TotalCount, "success": batch.SuccessCount, "failed": batch.FailedCount,
"pending": pending, "running": running, "retryable": retryable, "rows": outRows,
"location": json.RawMessage(locationJSON),
"created_at": batch.CreatedAt, "updated_at": batch.UpdatedAt,
}
}