File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Expand file tree Collapse file tree 1 file changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { useLoading } from '@sa/hooks' ;
2
+
3
+ import { useRouter } from '@/features/router' ;
4
+ import { getIsLogin , login } from '@/store/slice/auth' ;
5
+
6
+ /**
7
+ * - 登录
8
+ *
9
+ * 这里为什么封装成一个hook 因为在 系统功能 中的 切换权限页面也是需要用的
10
+ */
11
+ export function useLogin ( ) {
12
+ const { endLoading, loading, startLoading } = useLoading ( ) ;
13
+
14
+ const [ searchParams ] = useSearchParams ( ) ;
15
+
16
+ const redirectUrl = searchParams . get ( 'redirect' ) ;
17
+
18
+ const isLogin = useAppSelector ( getIsLogin ) ;
19
+
20
+ const { t } = useTranslation ( ) ;
21
+
22
+ const dispatch = useAppDispatch ( ) ;
23
+
24
+ const { navigate } = useRouter ( ) ;
25
+
26
+ async function toLogin ( params : { password : string ; userName : string } , redirect = true ) {
27
+ startLoading ( ) ;
28
+ const res = await dispatch ( login ( params ) ) ;
29
+
30
+ const info = res . payload as Api . Auth . UserInfo ;
31
+
32
+ if ( info . userName ) {
33
+ if ( redirectUrl && redirect ) {
34
+ await navigate ( redirectUrl ) ;
35
+ }
36
+
37
+ window . $notification ?. success ( {
38
+ description : t ( 'page.login.common.welcomeBack' , { userName : info . userName } ) ,
39
+ message : t ( 'page.login.common.loginSuccess' )
40
+ } ) ;
41
+ }
42
+
43
+ endLoading ( ) ;
44
+ }
45
+
46
+ return {
47
+ loading,
48
+ toLogin
49
+ } ;
50
+ }
You can’t perform that action at this time.
0 commit comments