Procházet zdrojové kódy

chore: 修改登录记录状态

xyh před 2 roky
rodič
revize
a63a561ed7

+ 2 - 0
packages/app/src/models/request/storage.ts

@@ -26,6 +26,8 @@ export type AddStorageParams = {
   warehouseWhere: string;
   /** 物料类型 */
   storageLocationType: string;
+  /** 是否是产成品库位 */
+  isProduct: string;
 };
 
 /** 修改仓库 */

+ 38 - 11
packages/app/src/pages/login/info/hooks.ts

@@ -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;
+}

+ 3 - 7
packages/app/src/pages/login/info/index.tsx

@@ -1,17 +1,13 @@
 import {Button, Checkbox, Input, Select, Space} from 'antd';
 import css from './index.module.css';
 import {FC} from 'react';
-import {useFormState} from './hooks';
+import {useFormState, useUserMemo} from './hooks';
 import cla from 'classnames';
 import {Controller} from 'react-hook-form';
-import {useFilterField} from '@hooks';
-import {USER_ALL_LOGIN_STORAGE, USER_NAME_LOGIN_STORAGE} from '@utils';
 
 const LoginInfo: FC = function () {
-  const [{userName: memoUserName, all: memoAll}, onChange] = useFilterField({
-    userName: Boolean(localStorage.getItem(USER_NAME_LOGIN_STORAGE)),
-    all: Boolean(localStorage.getItem(USER_ALL_LOGIN_STORAGE)),
-  });
+  const [{memoUserName, memoAll}, onChange] = useUserMemo();
+
   const [{errors, control, isLoading, isDisable}, {onSubmit}] = useFormState({
     user: memoUserName,
     all: memoAll,

+ 4 - 3
packages/app/src/utils/constants.ts

@@ -1,8 +1,9 @@
 /** 用户session token */
-export const USER_TOKEN_STORAGE = 'user_token';
+export const USER_TOKEN_STORAGE = 'userToken';
 /** 用户登录信息token */
-export const USER_NAME_LOGIN_STORAGE = 'login_user_name';
-export const USER_ALL_LOGIN_STORAGE = 'login_user_all';
+export const USER_NAME_LOGIN_STORAGE = 'memoUserLoginName';
+export const USER_ALL_LOGIN_STORAGE = 'memoAllLoginUser';
+export const MEMO_USER_STORAGE = 'userInfoData';
 /** 用户菜单收起状态 */
 export const MENU_COLLAPSED_STORAGE = 'menu_collapsed';
 /** 页码列表 */