fix: 增加 AI 匹配失败弹窗并补齐部署配置 (#229)
This commit is contained in:
@@ -46,7 +46,7 @@ integrations:
|
||||
|
||||
ai:
|
||||
# AI API Key 只能写入这个独立密钥文件。生产建议通过
|
||||
# CMAUTOBUY_AI_SECRETS_PATH 指向 /etc/cmautobuy/ai-secrets.yaml,权限 600。
|
||||
# CMAUTOBUY_AI_SECRETS_PATH 指向 /etc/cmautobuy/secrets/ai-secrets.yaml,权限 600。
|
||||
# 留空时 Admin 正常启动,但不能保存密钥或测试 AI 连接。
|
||||
secrets_path: ""
|
||||
|
||||
|
||||
@@ -18,9 +18,9 @@ UMask=0077
|
||||
|
||||
NoNewPrivileges=true
|
||||
PrivateTmp=true
|
||||
ProtectSystem=strict
|
||||
ProtectSystem=true
|
||||
ProtectHome=true
|
||||
ReadWritePaths=/opt/cmautobuy/data
|
||||
ReadWriteDirectories=/opt/cmautobuy/data /etc/cmautobuy/secrets
|
||||
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
||||
|
||||
[Install]
|
||||
|
||||
@@ -18,7 +18,7 @@ func (h *Handler) SybAIMatchCreate(c *gin.Context) {
|
||||
actor := currentUser(c)
|
||||
snapshot, err := service.LoadActiveAIMatchSnapshot(c.Request.Context(), h.db, h.aiSecrets, h.aiPolicy)
|
||||
if err != nil {
|
||||
h.sybRedirect(c, "AI 规格匹配未开始:"+err.Error())
|
||||
h.sybRedirectAIMatchError(c, "AI 规格匹配未开始:"+err.Error())
|
||||
return
|
||||
}
|
||||
batch, err := service.CreateAIMatchBatch(h.db, actor, c.PostFormArray("ids"), snapshot, time.Now())
|
||||
@@ -27,7 +27,7 @@ func (h *Handler) SybAIMatchCreate(c *gin.Context) {
|
||||
if service.IsValidationError(err) {
|
||||
message = "AI 规格匹配未开始:" + err.Error()
|
||||
}
|
||||
h.sybRedirect(c, message)
|
||||
h.sybRedirectAIMatchError(c, message)
|
||||
return
|
||||
}
|
||||
actorCopy := *actor
|
||||
@@ -42,6 +42,17 @@ func (h *Handler) SybAIMatchCreate(c *gin.Context) {
|
||||
h.sybRedirectAIMatch(c, batch.BatchID, "AI 规格匹配已开始,可在进度窗口查看逐条结果")
|
||||
}
|
||||
|
||||
// sybRedirectAIMatchError 使用独立参数返回列表,让页面明确弹出 AI 失败提示。
|
||||
// 普通列表状态仍使用 msg,避免同步、采集等操作结果被误当成 AI 错误。
|
||||
func (h *Handler) sybRedirectAIMatchError(c *gin.Context, message string) {
|
||||
params := url.Values{}
|
||||
appendSybFormState(params, c)
|
||||
if message != "" {
|
||||
params.Set("ai_error", strings.TrimSpace(message))
|
||||
}
|
||||
c.Redirect(http.StatusSeeOther, "/syb?"+params.Encode())
|
||||
}
|
||||
|
||||
func (h *Handler) SybAIMatchStatus(c *gin.Context) {
|
||||
view, err := service.GetAIMatchBatchView(h.db, currentUser(c), c.Param("batch_id"))
|
||||
if errors.Is(err, service.ErrForbidden) {
|
||||
|
||||
@@ -279,6 +279,7 @@ func (h *Handler) renderSybListWithLoginReason(c *gin.Context, keyword, pageRaw,
|
||||
"PurchaseClientCount": purchaseClients.SelectableCount,
|
||||
"EnabledSyncShopCount": enabledShopCount,
|
||||
"AIMatchBatch": aiBatch,
|
||||
"AIMatchError": strings.TrimSpace(c.Query("ai_error")),
|
||||
"AIMatchBatchFetchURL": func() string {
|
||||
if aiBatch == nil {
|
||||
return ""
|
||||
|
||||
@@ -87,6 +87,31 @@ func TestSybRedirectAfterStart_清除本次日期范围(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSybRedirectAIMatchError_独立错误参数并保留列表状态(t *testing.T) {
|
||||
context, recorder := sybPostContext(t, url.Values{
|
||||
"order_no": {"ORDER-1"}, "shop": {"店铺A"}, "stage": {"mapping_pending"},
|
||||
"page": {"2"}, "date_from": {"2026-08-13"}, "date_to": {"2026-08-14"},
|
||||
})
|
||||
(&Handler{}).sybRedirectAIMatchError(context, "AI 规格匹配未开始:密钥存储不可用")
|
||||
location, err := url.Parse(recorder.Header().Get("Location"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
query := location.Query()
|
||||
for name, want := range map[string]string{
|
||||
"order_no": "ORDER-1", "shop": "店铺A", "stage": "mapping_pending", "page": "2",
|
||||
"date_from": "2026-08-13", "date_to": "2026-08-14",
|
||||
"ai_error": "AI 规格匹配未开始:密钥存储不可用",
|
||||
} {
|
||||
if got := query.Get(name); got != want {
|
||||
t.Errorf("AI 错误跳转丢失 %s:got=%q want=%q Location=%s", name, got, want, location.String())
|
||||
}
|
||||
}
|
||||
if query.Has("msg") {
|
||||
t.Fatalf("AI 错误不能混入普通状态消息:%s", location.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestSybHistoryPagination_保留筛选并重开弹窗(t *testing.T) {
|
||||
view := sybHistoryPagination(2, 3, "ORDER-1", "店铺A", "pdd_missing", "2026-08-01", "2026-08-09")
|
||||
for _, raw := range []string{view.FirstURL, view.PrevURL, view.NextURL, view.LastURL} {
|
||||
|
||||
@@ -405,6 +405,11 @@
|
||||
var open = document.querySelectorAll(".modal-backdrop:not([hidden])");
|
||||
if (open.length > 0) closeModal(open[open.length - 1]);
|
||||
});
|
||||
|
||||
/* 服务端确认需要立即提醒的错误,页面加载后使用同一套弹窗焦点和关闭规则。 */
|
||||
document.querySelectorAll("[data-auto-open-modal]").forEach(function (modal) {
|
||||
openModal(modal);
|
||||
});
|
||||
}
|
||||
|
||||
/* ── 双击行打开详情弹窗 ────────────────────
|
||||
|
||||
@@ -167,6 +167,36 @@ func TestSybListTemplate_订单号错误就地显示(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSybListTemplate_AI匹配失败自动弹窗且转义(t *testing.T) {
|
||||
tmpl, err := template.ParseFS(templateFS, "templates/*/*.html")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var output bytes.Buffer
|
||||
data := map[string]any{
|
||||
"Title": "顺运宝数据", "Active": "syb", "CSRFToken": "test-csrf",
|
||||
"AIMatchError": `<script>alert("secret")</script>`,
|
||||
}
|
||||
if err := tmpl.ExecuteTemplate(&output, "syb/list", data); err != nil {
|
||||
t.Fatalf("渲染 AI 匹配错误弹窗失败: %v", err)
|
||||
}
|
||||
body := output.String()
|
||||
for _, want := range []string{
|
||||
`id="syb-ai-match-error-modal" data-auto-open-modal hidden`,
|
||||
`role="alertdialog"`,
|
||||
`id="syb-ai-match-error-message"`,
|
||||
`<script>alert("secret")</script>`,
|
||||
`规格映射和采购任务都没有被改动`,
|
||||
} {
|
||||
if !strings.Contains(body, want) {
|
||||
t.Errorf("AI 匹配错误弹窗缺少 %q", want)
|
||||
}
|
||||
}
|
||||
if strings.Contains(body, `<script>alert("secret")</script>`) {
|
||||
t.Fatal("AI 匹配错误未经 html/template 转义")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSybDetailTemplate_单条采集可选执行客户端(t *testing.T) {
|
||||
tmpl, err := template.ParseFS(templateFS, "templates/*/*.html")
|
||||
if err != nil {
|
||||
|
||||
@@ -194,6 +194,25 @@
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{{if .AIMatchError}}
|
||||
<div class="modal-backdrop" id="syb-ai-match-error-modal" data-auto-open-modal hidden>
|
||||
<div class="modal" role="alertdialog" aria-modal="true"
|
||||
aria-labelledby="syb-ai-match-error-title" aria-describedby="syb-ai-match-error-message">
|
||||
<div class="modal-head">
|
||||
<h2 id="syb-ai-match-error-title">AI 规格匹配失败</h2>
|
||||
<button type="button" class="modal-x" data-modal-close aria-label="关闭">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p class="missing" id="syb-ai-match-error-message">{{.AIMatchError}}</p>
|
||||
<p class="hint">规格映射和采购任务都没有被改动。请按提示处理后重新勾选并匹配。</p>
|
||||
</div>
|
||||
<div class="modal-foot">
|
||||
<button type="button" data-modal-close autofocus>关闭</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
||||
<div class="modal-backdrop" id="syb-ai-match-create-modal" hidden>
|
||||
<div class="modal" role="dialog" aria-modal="true" aria-labelledby="syb-ai-match-create-title">
|
||||
<div class="modal-head">
|
||||
|
||||
Reference in New Issue
Block a user