自动保存密码,并添加解密通知弹框

This commit is contained in:
tangly1024.com
2024-05-24 16:56:08 +08:00
parent 3b062037ce
commit 2eb95b2bf8
9 changed files with 166 additions and 38 deletions

View File

@@ -1,7 +1,10 @@
import BLOG from '@/blog.config'
import useNotification from '@/components/Notification'
import { siteConfig } from '@/lib/config'
import { getGlobalData, getPost, getPostBlocks } from '@/lib/db/getSiteData'
import { useGlobal } from '@/lib/global'
import { getPageTableOfContents } from '@/lib/notion/getPageTableOfContents'
import { getPasswordQuery } from '@/lib/password'
import { uploadDataToAlgolia } from '@/lib/plugins/algolia'
import { checkSlugHasNoSlash, getRecommendPost } from '@/lib/utils/post'
import { getLayoutByTheme } from '@/themes/theme'
@@ -19,9 +22,11 @@ import { useEffect, useState } from 'react'
const Slug = props => {
const { post } = props
const router = useRouter()
const { locale } = useGlobal()
// 文章锁🔐
const [lock, setLock] = useState(post?.password && post?.password !== '')
const { showNotification, Notification } = useNotification()
/**
* 验证文章密码
@@ -36,6 +41,7 @@ const Slug = props => {
setLock(false)
// 输入密码存入localStorage下次自动提交
localStorage.setItem('password_' + router.asPath, passInput)
showNotification(locale.COMMON.ARTICLE_UNLOCK_TIPS) // 设置解锁成功提示显示
return true
}
return false
@@ -56,10 +62,14 @@ const Slug = props => {
}
}
// 从localStorage中读取上次记录 自动提交密码
const passInput = localStorage.getItem('password_' + router.asPath)
if (passInput) {
validPassword(passInput)
// 读取上次记录 自动提交密码
const passInputs = getPasswordQuery(router.asPath)
if (passInputs.length > 0) {
for (const passInput of passInputs) {
if (validPassword(passInput)) {
break // 密码验证成功,停止尝试
}
}
}
}, [post])
@@ -83,7 +93,12 @@ const Slug = props => {
theme: siteConfig('THEME'),
router: useRouter()
})
return <Layout {...props} />
return (
<>
<Layout {...props} />
{!lock && <Notification />}
</>
)
}
export async function getStaticPaths() {