From 5f3c1daeab3a26d8df8e58f593bca8580b3c3693 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sun, 19 Jan 2025 16:40:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E4=BC=97=E5=8F=B7=E8=A7=A3=E9=94=81?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BB=84=E5=90=8D=E5=8D=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/OpenWrite.js | 63 +++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/components/OpenWrite.js b/components/OpenWrite.js index a219830ddcb..8866ff61641 100644 --- a/components/OpenWrite.js +++ b/components/OpenWrite.js @@ -22,8 +22,10 @@ const OpenWrite = () => { ) // 验证一次后的有效时长,单位小时 const cookieAge = siteConfig('OPEN_WRITE_VALIDITY_DURATION', 1) - // 白名单 + // 白名单,想要放行的页面 const whiteList = siteConfig('OPEN_WRITE_WHITE_LIST', '') + // 黄名单,优先级最高,设置后只有这里的路径会被上锁,其他页面自动全部放行 + const yellowList = siteConfig('OPEN_WRITE_YELLOW_LIST', '') // 登录信息 const { isLoaded, isSignedIn } = useGlobal() @@ -71,30 +73,38 @@ const OpenWrite = () => { } } useEffect(() => { - const existWhite = existedWhiteList(router.asPath, whiteList) - // 白名单中,免检 - if (existWhite) { + const isInYellowList = isPathInList(router.asPath, yellowList) + const isInWhiteList = isPathInList(router.asPath, whiteList) + + // 优先判断黄名单 + if (yellowList && yellowList.length > 0) { + if (!isInYellowList) { + console.log('当前路径不在黄名单中,放行') + return + } + } else if (isInWhiteList) { + // 白名单中,免检 + console.log('当前路径在白名单中,放行') return } + if (isSignedIn) { // 用户已登录免检 - console.log('用户已登录') + console.log('用户已登录,放行') return } - - // 开发环境免检 + if (process.env.NODE_ENV === 'development') { + // 开发环境免检 console.log('开发环境:屏蔽OpenWrite') return } - + if (isBrowser && blogId && !isSignedIn) { toggleTocItems(true) // 禁止目录项的点击 - - // Check if the element with id 'read-more-wrap' already exists + + // 检查是否已加载 const readMoreWrap = document.getElementById('read-more-wrap') - - // Only load the script if the element doesn't exist if (!readMoreWrap) { loadOpenWrite() } @@ -121,34 +131,27 @@ const toggleTocItems = disable => { } /** - * 检查白名单 + * 检查路径是否在名单中 * @param {*} path 当前url的字符串 - * @param {*} whiteListStr 白名单字符串 + * @param {*} listStr 名单字符串,逗号分隔 */ -function existedWhiteList(path, whiteListStr) { - // 参数检查 - if (!path || !whiteListStr) { - return true +function isPathInList(path, listStr) { + if (!path || !listStr) { + return false } - // 提取 path 最后一个斜杠后的内容,去掉前面的斜杆 - // 移除查询参数(从 '?' 开始的部分)和 `.html` 后缀 + // 提取 path 最后一个斜杠后的内容,并移除查询参数和 .html 后缀 const processedPath = path .replace(/\?.*$/, '') // 移除查询参数 - .replace(/.*\/([^/]+)(?:\.html)?$/, '$1') // 去掉前面的路径和 .html - - // 严格检查白名单字符串中是否包含处理后的 path - // const whiteListArray = whiteListStr.split(',') - // return whiteListArray.includes(processedPath) + .replace(/.*\/([^/]+)(?:\.html)?$/, '$1') // 提取最后部分 - // 放宽判断 - const isWhite = whiteListStr.includes(processedPath) + const isInList = listStr.includes(processedPath) - if (isWhite) { - console.log('OpenWrite白名单', processedPath) + if (isInList) { + // console.log(`当前路径在名单中: ${processedPath}`) } - return isWhite + return isInList } export default OpenWrite