From 726092c9b8d00c2df3e587e3a45edaddbedaebbf Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 8 May 2024 11:20:50 +0800 Subject: [PATCH] =?UTF-8?q?gitbook=20=E6=96=B0=E5=A2=9E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/gitbook/components/ArticleLock.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/themes/gitbook/components/ArticleLock.js b/themes/gitbook/components/ArticleLock.js index dd63992c..03d11d1a 100644 --- a/themes/gitbook/components/ArticleLock.js +++ b/themes/gitbook/components/ArticleLock.js @@ -1,4 +1,5 @@ import { useGlobal } from '@/lib/global' +import { useRouter } from 'next/router' import { useEffect, useRef } from 'react' /** @@ -11,24 +12,40 @@ import { useEffect, useRef } from 'react' export const ArticleLock = props => { const { validPassword } = props const { locale } = useGlobal() - console.log('locale', locale) + const router = useRouter() + const passwordInputRef = useRef(null) + /** + * 输入提交密码 + */ const submitPassword = () => { const p = document.getElementById('password') + + // 验证失败提示 if (!validPassword(p?.value)) { const tips = document.getElementById('tips') if (tips) { tips.innerHTML = '' tips.innerHTML = `
${locale.COMMON.PASSWORD_ERROR}
` } + } else { + // 输入密码存入localStorage,下次自动提交 + localStorage.setItem('password_' + router.asPath, p?.value) } } - const passwordInputRef = useRef(null) useEffect(() => { // 选中密码输入框并将其聚焦 passwordInputRef.current.focus() - }, []) + + // 从localStorage中读取上次记录 自动提交密码 + const p = localStorage.getItem('password_' + router.asPath) + console.log('pp', p, document.getElementById('password')) + if (p) { + document.getElementById('password').value = p + submitPassword() + } + }, [router]) return (