1
1
import { useGlobal } from '@/lib/global'
2
+ import { useRouter } from 'next/router'
2
3
import { useEffect , useRef } from 'react'
3
4
4
5
/**
@@ -11,24 +12,40 @@ import { useEffect, useRef } from 'react'
11
12
export const ArticleLock = props => {
12
13
const { validPassword } = props
13
14
const { locale } = useGlobal ( )
14
- console . log ( 'locale' , locale )
15
+ const router = useRouter ( )
16
+ const passwordInputRef = useRef ( null )
15
17
18
+ /**
19
+ * 输入提交密码
20
+ */
16
21
const submitPassword = ( ) => {
17
22
const p = document . getElementById ( 'password' )
23
+
24
+ // 验证失败提示
18
25
if ( ! validPassword ( p ?. value ) ) {
19
26
const tips = document . getElementById ( 'tips' )
20
27
if ( tips ) {
21
28
tips . innerHTML = ''
22
29
tips . innerHTML = `<div class='text-red-500 animate__shakeX animate__animated'>${ locale . COMMON . PASSWORD_ERROR } </div>`
23
30
}
31
+ } else {
32
+ // 输入密码存入localStorage,下次自动提交
33
+ localStorage . setItem ( 'password_' + router . asPath , p ?. value )
24
34
}
25
35
}
26
36
27
- const passwordInputRef = useRef ( null )
28
37
useEffect ( ( ) => {
29
38
// 选中密码输入框并将其聚焦
30
39
passwordInputRef . current . focus ( )
31
- } , [ ] )
40
+
41
+ // 从localStorage中读取上次记录 自动提交密码
42
+ const p = localStorage . getItem ( 'password_' + router . asPath )
43
+ console . log ( 'pp' , p , document . getElementById ( 'password' ) )
44
+ if ( p ) {
45
+ document . getElementById ( 'password' ) . value = p
46
+ submitPassword ( )
47
+ }
48
+ } , [ router ] )
32
49
33
50
return (
34
51
< div
0 commit comments