Kaynağa Gözat

feat: 利用运行时配置初次render解决设置权限异步存在的问题

lanjianrong 5 yıl önce
ebeveyn
işleme
525e5ed8af
3 değiştirilmiş dosya ile 122 ekleme ve 106 silme
  1. 15 0
      src/app.tsx
  2. 78 82
      src/models/global.js
  3. 29 24
      src/models/user.js

+ 15 - 0
src/app.tsx

@@ -0,0 +1,15 @@
+// export function onRouteChange({ matchedRoutes }) {
+//   console.log('222')
+// }
+
+import { apiGetRoles } from './services/user'
+import { setAuthority } from './utils/authority'
+
+export function render(oldRender) {
+  if (window.location.pathname !== '/user/login') {
+    apiGetRoles().then(value => {
+      setAuthority(value?.data || [])
+    })
+  }
+  oldRender()
+}

+ 78 - 82
src/models/global.js

@@ -1,4 +1,4 @@
-import { queryNotices } from '@/services/user'
+// import { queryNotices } from '@/services/user'
 
 const GlobalModel = {
   namespace: 'global',
@@ -7,66 +7,62 @@ const GlobalModel = {
     notices: []
   },
   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
-        }
-      })
-    }
+    // *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
+    //     }
+    //   })
+    // }
   },
   reducers: {
     changeLayoutCollapsed(
@@ -77,29 +73,29 @@ const GlobalModel = {
       { payload }
     ) {
       return { ...state, collapsed: payload }
-    },
+    }
 
-    saveNotices(state, { payload }) {
-      return {
-        collapsed: false,
-        ...state,
-        notices: 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)
-      }
-    }
+    // saveClearedNotices(
+    //   state = {
+    //     notices: [],
+    //     collapsed: true
+    //   },
+    //   { payload }
+    // ) {
+    //   return {
+    //     ...state,
+    //     collapsed: false,
+    //     notices: state.notices.filter((item) => item.type !== payload)
+    //   }
+    // }
   }
 }
 export default GlobalModel

+ 29 - 24
src/models/user.js

@@ -2,7 +2,6 @@ import { getTokenid, removeTokenId, saveTokenId } from '@/basic/utils/common'
 import consts from '@/consts'
 import { apiLogin } from '@/services/login'
 import { apiCurrent, apiGetRoles } from '@/services/user'
-import { setAuthority } from '@/utils/authority'
 import { getPageQuery } from '@/utils/utils'
 import { message } from 'antd'
 import { stringify } from 'qs'
@@ -18,14 +17,21 @@ const UserModel = {
     *login({ payload }, { call, put }) {
       // 获取登录接口数据
       const response = yield call(apiLogin, payload)
-      saveTokenId(response.data.token)
-      const { data: currentAuthority = [] } = yield call(apiGetRoles)
-      yield put({
-        type: 'changeLoginStatus',
-        payload: { status: response?.code, currentAuthority }
-      }) // Login successfully
-
-      if (response && response.code === consts.RET_CODE.SUCCESS) {
+      if (response?.code !== consts.RET_CODE.SUCCESS) {
+        yield put({
+          type: 'changeLoginStatus',
+          payload: { status: response?.code || -1 }
+        })
+        return
+      }
+      if (response?.code === consts.RET_CODE.SUCCESS) {
+        // Login successfully
+        saveTokenId(response.data.token)
+        const { data: currentAuthority = [] } = yield call(apiGetRoles)
+        yield put({
+          type: 'changeLoginStatus',
+          payload: { status: response?.code, currentAuthority }
+        })
         const urlParams = new URL(window.location.href)
         const params = getPageQuery()
         message.success('🎉 🎉 🎉  登录成功')
@@ -61,7 +67,7 @@ const UserModel = {
       }
       // window.location.reload()
     },
-    *fetchCurrent(_, { call, put, select }) {
+    *fetchCurrent(_, { call, put }) {
       if (window.location.pathname === '/user/login') {
         return
       }
@@ -76,27 +82,26 @@ const UserModel = {
           type: 'saveCurrentUser',
           payload: response.data
         })
-        const { loginStatus } = yield select(state => state.user)
-        console.log('loginStatus', loginStatus)
-        if (!loginStatus) {
-          const { data: currentAuthority = [] } = yield call(apiGetRoles)
-          yield put({
-            type: 'changeLoginStatus',
-            payload: { status: consts.RET_CODE.SUCCESS, currentAuthority }
-          })
-        }
+        // const { loginStatus } = yield select(state => state.user)
+        // if (!loginStatus) {
+        //   const { data: currentAuthority = [] } = yield call(apiGetRoles)
+        //   yield put({
+        //     type: 'changeLoginStatus',
+        //     payload: { status: consts.RET_CODE.SUCCESS, currentAuthority }
+        //   })
+        // }
       }
     }
   },
   reducers: {
     saveCurrentUser(state, action) {
       return { ...state, currentUser: action.payload || {} }
-    },
-
-    changeLoginStatus(state, { payload }) {
-      setAuthority(payload.currentAuthority)
-      return { ...state, loginStatus: payload.status }
     }
+
+    // changeLoginStatus(state, { payload }) {
+    //   setAuthority(payload.currentAuthority)
+    //   return { ...state, loginStatus: payload.status }
+    // }
   }
 }
 export default UserModel