fix: 简化真实采购创建并恢复客户端选择 (#112)

This commit is contained in:
chengma
2026-08-10 18:15:28 +08:00
parent 40cd8afefd
commit a26219091a
14 changed files with 96 additions and 129 deletions
+9 -11
View File
@@ -66,7 +66,7 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
"读取顺运宝同步记录失败,数据没有被改动。刷新页面重试;一直失败请把这句话报给维护者。")
return
}
assignableClients, err := service.ListLivePurchaseClients(h.db, currentUser(c), h.onlineThreshold)
purchaseClients, err := service.ListPurchaseClientOptions(h.db, currentUser(c), h.onlineThreshold)
if err != nil {
fail(c, http.StatusInternalServerError, "读取可分配客户端失败,数据没有被改动。")
return
@@ -181,9 +181,10 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
keyword, dateFrom, dateTo),
"HistoryFetchPagination": sybHistoryPartialPagination(history.Page, history.TotalPages,
keyword, dateFrom, dateTo),
"Pagination": service.NewPaginationView(result.Page, result.TotalPages, values.Encode()),
"DetailURL": "/syb/detail?" + detailValues.Encode(),
"AssignableClients": assignableClients,
"Pagination": service.NewPaginationView(result.Page, result.TotalPages, values.Encode()),
"DetailURL": "/syb/detail?" + detailValues.Encode(),
"AssignableClients": purchaseClients.Rows,
"LivePurchaseClientCount": purchaseClients.SelectableCount,
}))
}
@@ -643,7 +644,7 @@ func (h *Handler) SybCreateTask(c *gin.Context) {
}
result, err := service.CreatePurchaseTasksWithOptions(
h.db, currentUser(c), requests,
purchaseTaskOptionsFromForm(c, time.Now().UTC().Add(-h.onlineThreshold).Format(model.TimeLayout)),
purchaseTaskOptionsFromForm(c),
)
if err != nil {
h.sybRedirect(c, "采购任务没有创建:"+err.Error())
@@ -653,15 +654,12 @@ func (h *Handler) SybCreateTask(c *gin.Context) {
h.sybRedirect(c, purchaseTaskResultMessage(result))
}
func purchaseTaskOptionsFromForm(c *gin.Context, clientSeenAfter string) service.PurchaseTaskOptions {
func purchaseTaskOptionsFromForm(c *gin.Context) service.PurchaseTaskOptions {
return service.PurchaseTaskOptions{
ClientID: c.PostForm("client_id"),
// Admin 新建采购任务固定为 live。不能相信浏览器提交的 execution_mode,
// 否则篡改表单就能绕过真实采购确认和审计。
ExecutionMode: model.TaskExecutionLive,
LiveAcknowledged: c.PostForm("live_acknowledged") == "1",
LiveConfirmation: c.PostForm("live_confirmation"),
ClientSeenAfter: clientSeenAfter,
// 否则篡改表单就能改变任务的不可变执行模式。
ExecutionMode: model.TaskExecutionLive,
}
}
+5 -10
View File
@@ -152,20 +152,15 @@ func TestSybHistoryPartialPagination_翻页继续使用片段路由(t *testing.T
func TestPurchaseTaskOptionsFromForm_忽略篡改模式并固定Live(t *testing.T) {
context, _ := sybPostContext(t, url.Values{
"client_id": {"CLIENT-LIVE"},
"execution_mode": {"dry_run"},
"live_acknowledged": {"1"},
"live_confirmation": {"创建未付款订单"},
"client_id": {"CLIENT-LIVE"},
"execution_mode": {"dry_run"},
})
options := purchaseTaskOptionsFromForm(context, "2026-08-10T09:00:00Z")
options := purchaseTaskOptionsFromForm(context)
if options.ExecutionMode != model.TaskExecutionLive {
t.Fatalf("篡改 execution_mode 不得创建演练任务,实际 %q", options.ExecutionMode)
}
if options.ClientID != "CLIENT-LIVE" || !options.LiveAcknowledged || options.LiveConfirmation != "创建未付款订单" {
t.Fatalf("真实采购确认字段读取错误: %+v", options)
}
if options.ClientSeenAfter != "2026-08-10T09:00:00Z" {
t.Fatalf("在线截止时间未传给服务层: %+v", options)
if options.ClientID != "CLIENT-LIVE" {
t.Fatalf("采购客户端读取错误: %+v", options)
}
}