From 5ac12ae5c3b7dcb7f691a3c2765e787fdbf2ba0d Mon Sep 17 00:00:00 2001 From: yml2213 Date: Sat, 27 Jun 2026 09:38:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=AB=99=E5=86=85=E4=BF=A1?= =?UTF-8?q?=E5=9B=BE=E6=A0=87=E5=92=8C=E5=93=88=E5=A4=AB=E5=B8=81=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../internal/modules/listing/handler_user.go | 16 +++++++-- .../modules/listing/handler_user_test.go | 33 +++++++++++++++++++ .../features/auth/views/MobileProfileView.vue | 2 +- .../seller/composables/usePublishForm.ts | 8 ++++- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 backend/internal/modules/listing/handler_user_test.go diff --git a/backend/internal/modules/listing/handler_user.go b/backend/internal/modules/listing/handler_user.go index 67eb1ba..b236cce 100644 --- a/backend/internal/modules/listing/handler_user.go +++ b/backend/internal/modules/listing/handler_user.go @@ -2,6 +2,7 @@ package listing import ( "hfb_sys/backend/pkg/response" + "strings" "github.com/gin-gonic/gin" ) @@ -14,7 +15,7 @@ func (h *Handler) Create(c *gin.Context) { } var req CreateRequest if err := c.ShouldBindJSON(&req); err != nil { - response.BadRequest(c, "发布信息不完整") + response.BadRequest(c, listingBindErrorMessage(err)) return } item, err := h.service.Create(c.Request.Context(), ownerID, req) @@ -37,7 +38,7 @@ func (h *Handler) Update(c *gin.Context) { } var req UpdateRequest if err := c.ShouldBindJSON(&req); err != nil { - response.BadRequest(c, "发布信息不完整") + response.BadRequest(c, listingBindErrorMessage(err)) return } item, err := h.service.Update(c.Request.Context(), ownerID, id, req) @@ -48,6 +49,17 @@ func (h *Handler) Update(c *gin.Context) { response.OK(c, item) } +func listingBindErrorMessage(err error) string { + if err == nil { + return "发布信息不完整" + } + message := strings.ToLower(err.Error()) + if strings.Contains(message, "hafcoinamount") || strings.Contains(message, "haf_coin_amount") { + return "哈夫币数量格式不正确,请重新填写哈夫币/M后再提交" + } + return "发布信息不完整" +} + func (h *Handler) SubmitReview(c *gin.Context) { ownerID, ok := currentUserID(c) if !ok { diff --git a/backend/internal/modules/listing/handler_user_test.go b/backend/internal/modules/listing/handler_user_test.go new file mode 100644 index 0000000..4535b69 --- /dev/null +++ b/backend/internal/modules/listing/handler_user_test.go @@ -0,0 +1,33 @@ +package listing + +import ( + "errors" + "testing" +) + +func TestListingBindErrorMessage(t *testing.T) { + tests := []struct { + name string + err error + want string + }{ + { + name: "哈夫币字段格式错误", + err: errors.New("json: cannot unmarshal number 197100000.1 into Go struct field CreateRequest.haf_coin_amount of type int64"), + want: "哈夫币数量格式不正确,请重新填写哈夫币/M后再提交", + }, + { + name: "普通绑定错误", + err: errors.New("Key: 'CreateRequest.Title' Error:Field validation for 'Title' failed"), + want: "发布信息不完整", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := listingBindErrorMessage(tt.err); got != tt.want { + t.Fatalf("listingBindErrorMessage() = %q, want %q", got, tt.want) + } + }) + } +} diff --git a/frontend/src/features/auth/views/MobileProfileView.vue b/frontend/src/features/auth/views/MobileProfileView.vue index 28ed941..2b69abc 100644 --- a/frontend/src/features/auth/views/MobileProfileView.vue +++ b/frontend/src/features/auth/views/MobileProfileView.vue @@ -378,7 +378,7 @@ function resolveAvatarURL(url: string | undefined | null) {