Skip to content

Commit f9b61ff

Browse files
committed
feat: 添加登录的最新hook
1 parent d67f155 commit f9b61ff

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

src/pages/(blank)/login/hook.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
}

0 commit comments

Comments
 (0)