package httpapi import ( "context" "data-terminal-cloud/internal/identity" "errors" "net/http" ) type profileUpdater interface { UpdateDisplayName(context.Context, identity.Principal, string) error } func UpdateCustomerProfileHandler(repository profileUpdater) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { principal, ok := webPrincipal(w, r) if !ok { return } var input struct { DisplayName string `json:"display_name"` } if !decodeWebJSON(w, r, &input, "请填写有效昵称") { return } name, err := identity.ValidateDisplayName(input.DisplayName) if err != nil { writeValidation(w, r, "昵称须为1–80个字符,不能包含控制字符", FieldError{Field: "display_name", Message: "请填写有效昵称"}) return } if err = repository.UpdateDisplayName(r.Context(), principal, name); errors.Is(err, identity.ErrAccountNotFound) || errors.Is(err, identity.ErrAccessDenied) { WriteError(w, r, http.StatusForbidden, "RESOURCE_ACCESS_DENIED", "当前账号不可修改") return } else if err != nil { WriteError(w, r, http.StatusInternalServerError, "INTERNAL_ERROR", "保存失败,请重试") return } w.Header().Set("Cache-Control", "no-store") WriteJSON(w, r, http.StatusOK, map[string]string{"display_name": name}) }) }