diff --git a/docs/docs.go b/docs/docs.go index cbb8e673..bf4cde18 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1752,6 +1752,24 @@ const docTemplate = `{ "schema": { "$ref": "#/definitions/util.ResponseAny" } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } } } } @@ -2620,9 +2638,19 @@ const docTemplate = `{ "pay_key" ], "properties": { + "current_pay_key": { + "description": "CurrentPayKey 当前安全密码;已设置安全密码的用户修改时必须提供", + "type": "string", + "maxLength": 6, + "minLength": 6, + "example": "123456" + }, "pay_key": { + "description": "PayKey 新安全密码,必须为6位数字", "type": "string", - "maxLength": 6 + "maxLength": 6, + "minLength": 6, + "example": "654321" } } }, @@ -2697,6 +2725,10 @@ const docTemplate = `{ "type": "object", "properties": { "data": {}, + "error_code": { + "type": "string", + "example": "" + }, "error_msg": { "type": "string", "example": "" diff --git a/docs/swagger.json b/docs/swagger.json index 003dc0fb..278751bb 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -1743,6 +1743,24 @@ "schema": { "$ref": "#/definitions/util.ResponseAny" } + }, + "400": { + "description": "Bad Request", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } + }, + "401": { + "description": "Unauthorized", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } + }, + "500": { + "description": "Internal Server Error", + "schema": { + "$ref": "#/definitions/util.ResponseAny" + } } } } @@ -2611,9 +2629,19 @@ "pay_key" ], "properties": { + "current_pay_key": { + "description": "CurrentPayKey 当前安全密码;已设置安全密码的用户修改时必须提供", + "type": "string", + "maxLength": 6, + "minLength": 6, + "example": "123456" + }, "pay_key": { + "description": "PayKey 新安全密码,必须为6位数字", "type": "string", - "maxLength": 6 + "maxLength": 6, + "minLength": 6, + "example": "654321" } } }, @@ -2688,6 +2716,10 @@ "type": "object", "properties": { "data": {}, + "error_code": { + "type": "string", + "example": "" + }, "error_msg": { "type": "string", "example": "" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index f42a3d17..d4ea6a1e 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -516,8 +516,17 @@ definitions: type: object user.UpdatePayKeyRequest: properties: + current_pay_key: + description: CurrentPayKey 当前安全密码;已设置安全密码的用户修改时必须提供 + example: "123456" + maxLength: 6 + minLength: 6 + type: string pay_key: + description: PayKey 新安全密码,必须为6位数字 + example: "654321" maxLength: 6 + minLength: 6 type: string required: - pay_key @@ -570,6 +579,9 @@ definitions: util.ResponseAny: properties: data: {} + error_code: + example: "" + type: string error_msg: example: "" type: string @@ -1679,6 +1691,18 @@ paths: description: OK schema: $ref: '#/definitions/util.ResponseAny' + "400": + description: Bad Request + schema: + $ref: '#/definitions/util.ResponseAny' + "401": + description: Unauthorized + schema: + $ref: '#/definitions/util.ResponseAny' + "500": + description: Internal Server Error + schema: + $ref: '#/definitions/util.ResponseAny' tags: - user /f/{id}: diff --git a/frontend/components/common/settings/security.tsx b/frontend/components/common/settings/security.tsx index c13f6801..db359f4d 100644 --- a/frontend/components/common/settings/security.tsx +++ b/frontend/components/common/settings/security.tsx @@ -11,19 +11,34 @@ import { Breadcrumb, BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbP import { Button } from "@/components/ui/button" import { InputOTP, InputOTPGroup, InputOTPSlot } from "@/components/ui/input-otp" import { Spinner } from "@/components/ui/spinner" -import { UserService } from "@/lib/services/user" +import { useUser } from "@/contexts/user-context" export function SecurityMain() { const router = useRouter() - const [setupStep, setSetupStep] = React.useState<'password' | 'confirm'>('password') + const { user, updatePayKey } = useUser() + const hasPayKey = user?.is_pay_key ?? true + const [setupStep, setSetupStep] = React.useState<'current' | 'password' | 'confirm'>('current') + const [currentPayKey, setCurrentPayKey] = React.useState("") const [payKey, setPayKey] = React.useState("") const [confirmPayKey, setConfirmPayKey] = React.useState("") const [isSubmitting, setIsSubmitting] = React.useState(false) + const isCurrentPayKeyValid = currentPayKey.length === 6 && /^\d{6}$/.test(currentPayKey) const isPayKeyValid = payKey.length === 6 && /^\d{6}$/.test(payKey) const isConfirmValid = confirmPayKey.length === 6 && /^\d{6}$/.test(confirmPayKey) const passwordsMatch = payKey === confirmPayKey + React.useEffect(() => { + if (user && !user.is_pay_key) { + setSetupStep('password') + } + }, [user]) + + /* 原安全密码输入 */ + const handleCurrentPayKeyChange = (value: string) => { + setCurrentPayKey(value.replaceAll(/\D/g, '')) + } + /* 安全密码输入 */ const handlePayKeyChange = (value: string) => { const numericValue = value.replaceAll(/\D/g, '') @@ -39,7 +54,13 @@ export function SecurityMain() { const handlePayKeySubmit = async (e: React.FormEvent) => { e.preventDefault() - if (setupStep === 'password') { + if (setupStep === 'current') { + if (!isCurrentPayKeyValid) { + toast.error("原安全密码必须为6位数字") + return + } + setSetupStep('password') + } else if (setupStep === 'password') { if (!isPayKeyValid) { toast.error("安全密码必须为6位数字") return @@ -60,7 +81,10 @@ export function SecurityMain() { setIsSubmitting(true) try { - await UserService.updatePayKey(payKey) + await updatePayKey( + payKey, + hasPayKey ? currentPayKey : undefined, + ) toast.success("修改成功", { description: "您的安全密码已成功更新", }) @@ -71,7 +95,9 @@ export function SecurityMain() { toast.error("修改失败", { description: error instanceof Error ? error.message : "更新密码时发生错误,请稍后重试", }) - setSetupStep('password') + setSetupStep(hasPayKey ? 'current' : 'password') + setCurrentPayKey("") + setPayKey("") setConfirmPayKey("") } finally { setIsSubmitting(false) @@ -121,18 +147,38 @@ export function SecurityMain() { >
- {setupStep === 'password' - ? '请输入新的6位数字安全密码' - : '请再次输入密码进行确认'} + {setupStep === 'current' + ? '请输入当前的6位数字安全密码' + : setupStep === 'password' + ? '请输入新的6位数字安全密码' + : '请再次输入密码进行确认'}