Explorar o código

feat: PermSelect增加本地存储化

lanjianrong %!s(int64=5) %!d(string=hai) anos
pai
achega
931d3503bc

+ 0 - 40
src/pages/Customer/Company/components/PermSelect.jsx

@@ -1,40 +0,0 @@
-import { useModal } from '@/components/Modal'
-import { useModel } from 'umi'
-import { Select } from 'antd'
-import CustomPerm from './CustomPerm'
-
-export const PermDataTypeEunm = {
-  CUSTOMER: 'customer',
-  PRODUCT: 'product',
-  HR: 'hr',
-  WORKBENCH: 'workbench'
-}
-
-const PermSelect = ({ dataType, onConfirm, ...resetProps }) => {
-  const { initialState: { permData } = { permData: {} } } = useModel('@@initialState')
-  const { toggleModal, setModalProps } = useModal()
-  const options = [...(permData[dataType] || [])]
-
-  return (
-    <Select
-      {...resetProps}
-      defaultValue="oneself"
-      dropdownMatchSelectWidth={false}
-      onChange={e => {
-        if (e === 'custom') {
-          setModalProps({
-            width: '60vw',
-            modalRender: () => <CustomPerm onConfirm={onConfirm} />,
-            onCancel: toggleModal
-          })
-          toggleModal()
-          return
-        }
-        onConfirm({ staffIds: null, dataPermission: e })
-      }}
-      options={options}
-    />
-  )
-}
-
-export default PermSelect

+ 60 - 0
src/pages/Customer/Company/components/PermSelect.tsx

@@ -0,0 +1,60 @@
+import { useModal } from '@/components/Modal'
+import { useModel } from 'umi'
+import { Select } from 'antd'
+import CustomPerm from './CustomPerm'
+import { getAuthCache, setAuthCache } from '@/utils/auth'
+import { PERMISSION_SELECT_KEY } from '@/utils/cache/cacheEnum'
+import type { SelectProps } from 'antd'
+import type { PERMISSION_KEY } from '@/utils/cache/cacheEnum'
+
+export const PermDataTypeEunm = {
+  CUSTOMER: 'customer',
+  PRODUCT: 'product',
+  HR: 'hr',
+  WORKBENCH: 'workbench'
+}
+interface PermSelectProps<T> extends SelectProps {
+  dataType: T
+  onConfirm: () => void
+}
+
+const PermSelect: React.FC<PermSelectProps<PERMISSION_KEY>> = ({
+  dataType,
+  onConfirm,
+  ...resetProps
+}) => {
+  const { initialState: { permData } = { permData: {} } } = useModel('@@initialState')
+  const { toggleModal, setModalProps } = useModal()
+  let defaultValue = 'oneself'
+  const options = [...(permData[dataType] || [])]
+  const permAuthCache = getAuthCache(PERMISSION_SELECT_KEY)
+
+  if (permAuthCache && permAuthCache[dataType]) {
+    defaultValue = permAuthCache[dataType]
+  }
+
+  const handleOnChange = e => {
+    if (e === 'custom') {
+      setModalProps({
+        width: '60vw',
+        modalRender: () => <CustomPerm onConfirm={onConfirm} />,
+        onCancel: toggleModal
+      })
+      toggleModal()
+      return
+    }
+    onConfirm({ staffIds: null, dataPermission: e })
+    setAuthCache(PERMISSION_SELECT_KEY, { ...permAuthCache, [dataType]: e })
+  }
+  return (
+    <Select
+      {...resetProps}
+      defaultValue={defaultValue}
+      dropdownMatchSelectWidth={false}
+      onChange={handleOnChange}
+      options={options}
+    />
+  )
+}
+
+export default PermSelect

+ 5 - 0
src/utils/cache/cacheEnum.ts

@@ -11,6 +11,11 @@ export const APP_SESSION_CACHE_KEY = 'COMMON__SESSION__KEY__'
 // base global casader components history value
 export const CASCADER_HISTORY_KEY = 'CASCADER_HISTORY_KEY__'
 
+// permission select component key
+export const PERMISSION_SELECT_KEY = 'PERMISSION_SELECT_KEY'
+// permission select component defualtValue key
+export type PERMISSION_KEY = 'customer' | 'product' | 'workbench' | 'hr'
+
 export enum CacheTypeEnum {
   SESSION,
   LOCAL

+ 4 - 1
src/utils/cache/persistent.ts

@@ -7,7 +7,9 @@ import {
   TOKEN_KEY,
   APP_LOCAL_CACHE_KEY,
   APP_SESSION_CACHE_KEY,
-  CASCADER_HISTORY_KEY
+  CASCADER_HISTORY_KEY,
+  PERMISSION_SELECT_KEY,
+  PERMISSION_KEY
 } from './cacheEnum'
 import { DEFAULT_CACHE_TIME } from '@/settings/encryptionSetting'
 import { pick, omit } from 'lodash-es'
@@ -15,6 +17,7 @@ import { pick, omit } from 'lodash-es'
 interface BasicStore {
   [TOKEN_KEY]: string | null | undefined
   [CASCADER_HISTORY_KEY]: { key: string; value: string[]; label: string }[]
+  [PERMISSION_SELECT_KEY]: { [key in PERMISSION_KEY]: string } | null | underfined
 }
 
 type LocalStore = BasicStore