@@ -22,8 +22,10 @@ const OpenWrite = () => {
2222 )
2323 // 验证一次后的有效时长,单位小时
2424 const cookieAge = siteConfig ( 'OPEN_WRITE_VALIDITY_DURATION' , 1 )
25- // 白名单
25+ // 白名单,想要放行的页面
2626 const whiteList = siteConfig ( 'OPEN_WRITE_WHITE_LIST' , '' )
27+ // 黄名单,优先级最高,设置后只有这里的路径会被上锁,其他页面自动全部放行
28+ const yellowList = siteConfig ( 'OPEN_WRITE_YELLOW_LIST' , '' )
2729
2830 // 登录信息
2931 const { isLoaded, isSignedIn } = useGlobal ( )
@@ -71,30 +73,38 @@ const OpenWrite = () => {
7173 }
7274 }
7375 useEffect ( ( ) => {
74- const existWhite = existedWhiteList ( router . asPath , whiteList )
75- // 白名单中,免检
76- if ( existWhite ) {
76+ const isInYellowList = isPathInList ( router . asPath , yellowList )
77+ const isInWhiteList = isPathInList ( router . asPath , whiteList )
78+
79+ // 优先判断黄名单
80+ if ( yellowList && yellowList . length > 0 ) {
81+ if ( ! isInYellowList ) {
82+ console . log ( '当前路径不在黄名单中,放行' )
83+ return
84+ }
85+ } else if ( isInWhiteList ) {
86+ // 白名单中,免检
87+ console . log ( '当前路径在白名单中,放行' )
7788 return
7889 }
90+
7991 if ( isSignedIn ) {
8092 // 用户已登录免检
81- console . log ( '用户已登录' )
93+ console . log ( '用户已登录,放行 ' )
8294 return
8395 }
84-
85- // 开发环境免检
96+
8697 if ( process . env . NODE_ENV === 'development' ) {
98+ // 开发环境免检
8799 console . log ( '开发环境:屏蔽OpenWrite' )
88100 return
89101 }
90-
102+
91103 if ( isBrowser && blogId && ! isSignedIn ) {
92104 toggleTocItems ( true ) // 禁止目录项的点击
93-
94- // Check if the element with id 'read-more-wrap' already exists
105+
106+ // 检查是否已加载
95107 const readMoreWrap = document . getElementById ( 'read-more-wrap' )
96-
97- // Only load the script if the element doesn't exist
98108 if ( ! readMoreWrap ) {
99109 loadOpenWrite ( )
100110 }
@@ -121,34 +131,27 @@ const toggleTocItems = disable => {
121131}
122132
123133/**
124- * 检查白名单
134+ * 检查路径是否在名单中
125135 * @param {* } path 当前url的字符串
126- * @param {* } whiteListStr 白名单字符串
136+ * @param {* } listStr 名单字符串,逗号分隔
127137 */
128- function existedWhiteList ( path , whiteListStr ) {
129- // 参数检查
130- if ( ! path || ! whiteListStr ) {
131- return true
138+ function isPathInList ( path , listStr ) {
139+ if ( ! path || ! listStr ) {
140+ return false
132141 }
133142
134- // 提取 path 最后一个斜杠后的内容,去掉前面的斜杆
135- // 移除查询参数(从 '?' 开始的部分)和 `.html` 后缀
143+ // 提取 path 最后一个斜杠后的内容,并移除查询参数和 .html 后缀
136144 const processedPath = path
137145 . replace ( / \? .* $ / , '' ) // 移除查询参数
138- . replace ( / .* \/ ( [ ^ / ] + ) (?: \. h t m l ) ? $ / , '$1' ) // 去掉前面的路径和 .html
139-
140- // 严格检查白名单字符串中是否包含处理后的 path
141- // const whiteListArray = whiteListStr.split(',')
142- // return whiteListArray.includes(processedPath)
146+ . replace ( / .* \/ ( [ ^ / ] + ) (?: \. h t m l ) ? $ / , '$1' ) // 提取最后部分
143147
144- // 放宽判断
145- const isWhite = whiteListStr . includes ( processedPath )
148+ const isInList = listStr . includes ( processedPath )
146149
147- if ( isWhite ) {
148- console . log ( 'OpenWrite白名单' , processedPath )
150+ if ( isInList ) {
151+ // console.log(`当前路径在名单中: ${ processedPath}` )
149152 }
150153
151- return isWhite
154+ return isInList
152155}
153156
154157export default OpenWrite
0 commit comments