|
@@ -3,13 +3,18 @@ import {yupResolver} from '@hookform/resolvers/yup';
|
|
|
import {object, string} from 'yup';
|
|
|
import {useMutation} from '@tanstack/react-query';
|
|
|
import {userLogin} from '@apis';
|
|
|
-import {useBoolean} from 'ahooks';
|
|
|
+import {useBoolean, useLocalStorageState} from 'ahooks';
|
|
|
import {message} from 'antd';
|
|
|
import {useStore} from 'zustand';
|
|
|
import {userStore} from '@stores';
|
|
|
import {useNavigate} from 'react-router-dom';
|
|
|
import {HOME_PATH} from '@routes';
|
|
|
-import {USER_ALL_LOGIN_STORAGE, USER_NAME_LOGIN_STORAGE, USER_TOKEN_STORAGE} from '@utils';
|
|
|
+import {
|
|
|
+ MEMO_USER_STORAGE,
|
|
|
+ USER_ALL_LOGIN_STORAGE,
|
|
|
+ USER_NAME_LOGIN_STORAGE,
|
|
|
+ USER_TOKEN_STORAGE,
|
|
|
+} from '@utils';
|
|
|
|
|
|
type FormState = {
|
|
|
name: string;
|
|
@@ -36,13 +41,10 @@ function useLogin({user, all}: {user: boolean; all: boolean}) {
|
|
|
{
|
|
|
onSuccess(data, {name, password}) {
|
|
|
if (data.msg === '200') {
|
|
|
- user || all
|
|
|
- ? localStorage.setItem(USER_NAME_LOGIN_STORAGE, name)
|
|
|
- : localStorage.removeItem(USER_NAME_LOGIN_STORAGE);
|
|
|
-
|
|
|
- all
|
|
|
- ? localStorage.setItem(USER_ALL_LOGIN_STORAGE, password)
|
|
|
- : localStorage.removeItem(USER_ALL_LOGIN_STORAGE);
|
|
|
+ localStorage.setItem(
|
|
|
+ MEMO_USER_STORAGE,
|
|
|
+ `{"userName": "${user || all ? name : ''}", "password": "${all ? password : ''}"}`,
|
|
|
+ );
|
|
|
|
|
|
message.success('登录成功');
|
|
|
sessionStorage.setItem(USER_TOKEN_STORAGE, JSON.stringify(data.data));
|
|
@@ -58,6 +60,10 @@ function useLogin({user, all}: {user: boolean; all: boolean}) {
|
|
|
}
|
|
|
|
|
|
export function useFormState(memo: {user: boolean; all: boolean}) {
|
|
|
+ const [{userName, password}] = useLocalStorageState(MEMO_USER_STORAGE, {
|
|
|
+ defaultValue: {userName: '', password: ''},
|
|
|
+ });
|
|
|
+
|
|
|
const {
|
|
|
handleSubmit,
|
|
|
formState: {errors},
|
|
@@ -66,8 +72,8 @@ export function useFormState(memo: {user: boolean; all: boolean}) {
|
|
|
resolver: yupResolver(validate),
|
|
|
defaultValues: {
|
|
|
type: '1',
|
|
|
- name: localStorage.getItem(USER_NAME_LOGIN_STORAGE) ?? '',
|
|
|
- password: localStorage.getItem(USER_ALL_LOGIN_STORAGE) ?? '',
|
|
|
+ name: userName,
|
|
|
+ password,
|
|
|
},
|
|
|
});
|
|
|
const {isLoading, mutate, isDisable} = useLogin(memo);
|
|
@@ -79,3 +85,24 @@ export function useFormState(memo: {user: boolean; all: boolean}) {
|
|
|
|
|
|
return [{errors, control, isLoading, isDisable}, {onSubmit}] as const;
|
|
|
}
|
|
|
+
|
|
|
+export function useUserMemo() {
|
|
|
+ const [memoUserName, {set: setMemoUserName}] = useBoolean(
|
|
|
+ Boolean(localStorage.getItem(USER_NAME_LOGIN_STORAGE)),
|
|
|
+ );
|
|
|
+ const [memoAll, {set: setMemoAll}] = useBoolean(
|
|
|
+ Boolean(localStorage.getItem(USER_ALL_LOGIN_STORAGE)),
|
|
|
+ );
|
|
|
+
|
|
|
+ function onChange(key: 'userName' | 'all') {
|
|
|
+ return function (val: boolean) {
|
|
|
+ const storageKey = key === 'userName' ? USER_NAME_LOGIN_STORAGE : USER_ALL_LOGIN_STORAGE;
|
|
|
+ const setFn = key === 'userName' ? setMemoUserName : setMemoAll;
|
|
|
+
|
|
|
+ localStorage.setItem(storageKey, val ? '1' : '');
|
|
|
+ setFn(val);
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ return [{memoUserName, memoAll}, onChange] as const;
|
|
|
+}
|