Browse Source

feat(user-form): 更新用户限制并增强错误处理

sususu 3 months ago
parent
commit
2efc4739a7

+ 2 - 2
messages/zh-CN/dashboard.json

@@ -560,12 +560,12 @@
     "rpm": {
       "label": "RPM限制",
       "placeholder": "每分钟请求数限制",
-      "description": "默认值: {default},范围: 1-10000"
+      "description": "默认值: {default},范围: 1-1000000"
     },
     "dailyQuota": {
       "label": "每日额度",
       "placeholder": "每日消费额度限制",
-      "description": "默认值: ${default},范围: $0.01-$1000"
+      "description": "默认值: ${default},范围: $0.01-$100000"
     }
   },
   "deleteKeyConfirm": {

+ 5 - 5
src/app/[locale]/dashboard/_components/user/forms/user-form.tsx

@@ -7,7 +7,7 @@ import { DialogFormLayout } from "@/components/form/form-layout";
 import { TextField, TagInputField } from "@/components/form/form-field";
 import { useZodForm } from "@/lib/hooks/use-zod-form";
 import { CreateUserSchema } from "@/lib/validation/schemas";
-import { USER_DEFAULTS } from "@/lib/constants/user.constants";
+import { USER_DEFAULTS, USER_LIMITS } from "@/lib/constants/user.constants";
 import { toast } from "sonner";
 import { setZodErrorMap } from "@/lib/utils/zod-i18n";
 import { getErrorMessage } from "@/lib/utils/error-messages";
@@ -146,8 +146,8 @@ export function UserForm({ user, onSuccess }: UserFormProps) {
         label={tForm("rpm.label")}
         type="number"
         required
-        min={1}
-        max={10000}
+        min={USER_LIMITS.RPM.MIN}
+        max={USER_LIMITS.RPM.MAX}
         placeholder={tForm("rpm.placeholder")}
         description={tForm("rpm.description", { default: USER_DEFAULTS.RPM })}
         {...form.getFieldProps("rpm")}
@@ -157,8 +157,8 @@ export function UserForm({ user, onSuccess }: UserFormProps) {
         label={tForm("dailyQuota.label")}
         type="number"
         required
-        min={0.01}
-        max={1000}
+        min={USER_LIMITS.DAILY_QUOTA.MIN}
+        max={USER_LIMITS.DAILY_QUOTA.MAX}
         step={0.01}
         placeholder={tForm("dailyQuota.placeholder")}
         description={tForm("dailyQuota.description", { default: USER_DEFAULTS.DAILY_QUOTA })}

+ 2 - 2
src/lib/constants/user.constants.ts

@@ -9,10 +9,10 @@ export const USER_DEFAULTS = {
 export const USER_LIMITS = {
   RPM: {
     MIN: 1,
-    MAX: 10_000,
+    MAX: 1_000_000, // 提升到 100 万
   },
   DAILY_QUOTA: {
     MIN: 0.01,
-    MAX: 1_000,
+    MAX: 100_000, // 提升到 10 万美元
   },
 } as const;

+ 6 - 2
src/lib/utils/error-messages.ts

@@ -220,8 +220,10 @@ export function zodErrorToCode(
 
     case "too_small":
       if (typeof minimum === "number") {
+        // 区分字符串长度和数值范围
+        const isStringType = type === "string";
         return {
-          code: ERROR_CODES.MIN_LENGTH,
+          code: isStringType ? ERROR_CODES.MIN_LENGTH : ERROR_CODES.MIN_VALUE,
           params: { field: field || "field", min: minimum },
         };
       }
@@ -229,8 +231,10 @@ export function zodErrorToCode(
 
     case "too_big":
       if (typeof maximum === "number") {
+        // 区分字符串长度和数值范围
+        const isStringType = type === "string";
         return {
-          code: ERROR_CODES.MAX_LENGTH,
+          code: isStringType ? ERROR_CODES.MAX_LENGTH : ERROR_CODES.MAX_VALUE,
           params: { field: field || "field", max: maximum },
         };
       }