Просмотр исходного кода

feat: 优化数据权限下拉列表存储方式

lanjianrong 5 лет назад
Родитель
Сommit
a0f9276566

+ 11 - 13
src/app.tsx

@@ -1,23 +1,21 @@
-import { apiGetRoles, getDataPerm } from './services/user'
-import { setAuthCache } from './utils/auth'
+import { apiGetRoles } from './services/user'
 import { setAuthority } from './utils/auth/authority'
-import { PERM_LIST_KEY } from './utils/cache/cacheEnum'
 
-const PermDataList = [
-  {
-    key: '/customer',
-    value: 2
-  }
-]
+// const PermDataList = [
+//   {
+//     key: '/customer',
+//     value: 2
+//   }
+// ]
 export function render(oldRender) {
   const path = window.location.pathname
   if (path !== '/user/login') {
     apiGetRoles().then(roles => {
       setAuthority(roles.data)
-      const permVal = PermDataList.find(item => path.startsWith(item.key))?.value
-      if (permVal) {
-        getDataPerm().then(prem => setAuthCache(PERM_LIST_KEY, prem?.data || []))
-      }
+      // const permVal = PermDataList.find(item => path.startsWith(item.key))?.value
+      // if (permVal) {
+      //   getDataPerm().then(prem => setAuthCache(PERM_LIST_KEY, prem?.data || []))
+      // }
     })
   }
   oldRender()

+ 4 - 1
src/layouts/SecurityLayout.jsx

@@ -6,7 +6,7 @@ import { stringify } from 'querystring'
 class SecurityLayout extends React.Component {
   state = {
     isReady: false
-  };
+  }
 
   componentDidMount() {
     this.setState({
@@ -18,6 +18,9 @@ class SecurityLayout extends React.Component {
       dispatch({
         type: 'user/fetchCurrent'
       })
+      dispatch({
+        type: 'global/fetchPermData'
+      })
     }
   }
 

+ 2 - 2
src/models/district.js

@@ -9,7 +9,7 @@ export default {
   effects: {
     *fetch({ payload }, { call, put }) {
       const response = yield call(queryDistrict, payload)
-      if (response && response.code === consts.RET_CODE.SUCCESS) {
+      if (response?.code === consts.RET_CODE.SUCCESS) {
         yield put({
           type: 'changeState',
           payload: {
@@ -31,7 +31,7 @@ export default {
         const { code = -1, data = [] } = yield call(queryDistrict, { parentId: provinceId })
         if (code === consts.RET_CODE.SUCCESS) {
           const response = yield call(queryDistrict, { parentId: cityId })
-          if (response && response.code === consts.RET_CODE.SUCCESS) {
+          if (response?.code === consts.RET_CODE.SUCCESS) {
             const newData = data.map(item => {
               const newItem = { ...item }
               if (item.id === cityId) {

+ 17 - 79
src/models/global.js

@@ -1,68 +1,25 @@
 // import { queryNotices } from '@/services/user'
 
+import consts from '@/basic/consts'
+import { getDataPerm } from '@/services/user'
+
 const GlobalModel = {
   namespace: 'global',
   state: {
     collapsed: false,
-    notices: []
+    notices: [],
+    premData: {}
   },
   effects: {
-    // *fetchNotices(_, { call, put, select }) {
-    //   const data = yield call(queryNotices)
-    //   yield put({
-    //     type: 'saveNotices',
-    //     payload: data
-    //   })
-    //   const unreadCount = yield select(
-    //     (state) => state.global.notices.filter((item) => !item.read).length
-    //   )
-    //   yield put({
-    //     type: 'user/changeNotifyCount',
-    //     payload: {
-    //       totalCount: data.length,
-    //       unreadCount
-    //     }
-    //   })
-    // },
-    // *clearNotices({ payload }, { put, select }) {
-    //   yield put({
-    //     type: 'saveClearedNotices',
-    //     payload
-    //   })
-    //   const count = yield select((state) => state.global.notices.length)
-    //   const unreadCount = yield select(
-    //     (state) => state.global.notices.filter((item) => !item.read).length
-    //   )
-    //   yield put({
-    //     type: 'user/changeNotifyCount',
-    //     payload: {
-    //       totalCount: count,
-    //       unreadCount
-    //     }
-    //   })
-    // },
-    // *changeNoticeReadState({ payload }, { put, select }) {
-    //   const notices = yield select((state) =>
-    //     state.global.notices.map((item) => {
-    //       const notice = { ...item }
-    //       if (notice.id === payload) {
-    //         notice.read = true
-    //       }
-    //       return notice
-    //     })
-    //   )
-    //   yield put({
-    //     type: 'saveNotices',
-    //     payload: notices
-    //   })
-    //   yield put({
-    //     type: 'user/changeNotifyCount',
-    //     payload: {
-    //       totalCount: notices.length,
-    //       unreadCount: notices.filter((item) => !item.read).length
-    //     }
-    //   })
-    // }
+    *fetchPermData(_, { put }) {
+      const response = yield getDataPerm()
+      if (response?.code === consts.RET_CODE.SUCCESS) {
+        yield put({
+          type: 'ChangePermData',
+          payload: response.data || {}
+        })
+      }
+    }
   },
   reducers: {
     changeLayoutCollapsed(
@@ -73,29 +30,10 @@ const GlobalModel = {
       { payload }
     ) {
       return { ...state, collapsed: payload }
+    },
+    ChangePermData(state, { payload }) {
+      return { ...state, premData: payload }
     }
-
-    // saveNotices(state, { payload }) {
-    //   return {
-    //     collapsed: false,
-    //     ...state,
-    //     notices: payload
-    //   }
-    // },
-
-    // saveClearedNotices(
-    //   state = {
-    //     notices: [],
-    //     collapsed: true
-    //   },
-    //   { payload }
-    // ) {
-    //   return {
-    //     ...state,
-    //     collapsed: false,
-    //     notices: state.notices.filter((item) => item.type !== payload)
-    //   }
-    // }
   }
 }
 export default GlobalModel

+ 7 - 6
src/pages/Customer/Client/index.jsx

@@ -20,9 +20,8 @@ import { useTableScroll } from '@/hooks/useAutoTable'
 import { apiAddClient, queryClient } from '@/services/customer'
 import { apiBatchLink } from '@/services/customer'
 import styles from './index.less'
-import { getPermDataList } from '@/utils/auth'
 
-const Contact = props => {
+const Client = props => {
   const { toggleDrawer, setDrawerProps } = useModal()
   const {
     personTagColorMap,
@@ -31,6 +30,7 @@ const Contact = props => {
     teamTags,
     dispatch,
     shouldUpdate,
+    permData,
     loading
   } = props
 
@@ -600,7 +600,7 @@ const Contact = props => {
             onChange={e => {
               setState({ ...state, params: { ...state.params, dataPermission: e } })
             }}
-            options={getPermDataList()?.customer || []}
+            options={permData?.customer || []}
           />
         }
         onLoadingChange={loadingStatus => {
@@ -614,11 +614,12 @@ const Contact = props => {
   )
 }
 
-export default connect(({ client, refresh, loading }) => ({
+export default connect(({ client, refresh, global, loading }) => ({
   personTagColorMap: client.personTagColorMap,
   personTags: client.personTags,
   teamTagColorMap: client.teamTagColorMap,
   teamTags: client.teamTags,
   loading: loading.models.client,
-  shouldUpdate: refresh.client
-}))(Contact)
+  shouldUpdate: refresh.client,
+  permData: global.premData
+}))(Client)

+ 4 - 3
src/pages/Customer/Company/index.jsx

@@ -14,7 +14,6 @@ import PersonLabel from './components/PersonLabel'
 import { TagTypeEnum, TagDataTypeEnum, LabelModeType } from './components/PersonLabel/const'
 import { Add, Down, Check } from '@icon-park/react'
 import { apiBatchLink } from '@/services/customer'
-import { getPermDataList } from '@/utils/auth'
 import styles from './index.less'
 
 const Company = props => {
@@ -25,6 +24,7 @@ const Company = props => {
     teamTags,
     dispatch,
     shouldUpdate,
+    permData,
     loading
   } = props
   const { toggleDrawer, setDrawerProps } = useModal()
@@ -471,7 +471,7 @@ const Company = props => {
             onChange={e => {
               setState({ ...state, params: { ...state.params, dataPermission: e } })
             }}
-            options={getPermDataList()?.customer || []}
+            options={permData?.customer || []}
           />
         }
         rowSelection={{
@@ -492,11 +492,12 @@ const Company = props => {
   )
 }
 
-export default connect(({ company, refresh, loading }) => ({
+export default connect(({ company, refresh, global, loading }) => ({
   personTagColorMap: company.personTagColorMap,
   personTags: company.personTags,
   teamTagColorMap: company.teamTagColorMap,
   teamTags: company.teamTags,
   loading: loading.models.company,
+  permData: global.premData,
   shouldUpdate: refresh.company
 }))(Company)

+ 1 - 5
src/utils/auth/index.ts

@@ -2,7 +2,7 @@
 
 import { Persistent, BasicKeys } from '@/utils/cache/persistent'
 import projectSetting from '@/settings/projectSetting'
-import { TOKEN_KEY, CacheTypeEnum, PERM_LIST_KEY } from '@/utils/cache/cacheEnum'
+import { TOKEN_KEY, CacheTypeEnum } from '@/utils/cache/cacheEnum'
 
 const { permissionCacheType } = projectSetting
 const isLocal = permissionCacheType === CacheTypeEnum.LOCAL
@@ -11,10 +11,6 @@ export function getToken() {
   return getAuthCache(TOKEN_KEY)
 }
 
-export function getPermDataList() {
-  return getAuthCache(PERM_LIST_KEY)
-}
-
 export function getAuthCache<T>(key: BasicKeys) {
   const fn = isLocal ? Persistent.getLocal : Persistent.getSession
   return fn(key) as T

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

@@ -5,8 +5,6 @@ export const TOKEN_KEY = 'TOKEN__'
 // role role key
 export const ROLES_KEY = 'ROLES__KEY__'
 
-export const PERM_LIST_KEY = 'PERM_LIST_KEY'
-
 // base global local key
 export const APP_LOCAL_CACHE_KEY = 'COMMON__LOCAL__KEY__'