Przeglądaj źródła

feat: 登录后获取用户信息

lanjianrong 5 lat temu
rodzic
commit
469d9bbe93
6 zmienionych plików z 36 dodań i 25 usunięć
  1. 8 6
      mock/user.js
  2. 1 1
      src/basic
  3. 2 1
      src/layouts/SecurityLayout.jsx
  4. 1 2
      src/models/login.js
  5. 16 13
      src/models/user.js
  6. 8 2
      src/services/user.js

+ 8 - 6
mock/user.js

@@ -14,7 +14,7 @@ async function getFakeCaptcha(req, res) {
 
 export default {
   // 支持值为 Object 和 Array
-  'GET /api/currentUser': {
+  'GET /api/login/staff': {
     name: 'testName',
     avatar: 'https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png',
     userid: '00000001',
@@ -86,19 +86,21 @@ export default {
     },
   ],
   'POST /api/login': async (req, res) => {
-    const { password, userName, type } = req.body;
+    const { password, username, type } = req.body;
     await waitTime(2000);
 
-    if (password === '123456' && userName === '蔡频') {
+    if (password === '123456' && username === '蔡频') {
       res.send({
-        status: 'ok',
-        type,
+        code: 0,
+        data: {
+          token: ''
+        },
         currentAuthority: 'admin',
       });
       return;
     }
 
-    if (password === 'ant.design' && userName === 'user') {
+    if (password === 'ant.design' && username === 'user') {
       res.send({
         status: 'ok',
         type,

+ 1 - 1
src/basic

@@ -1 +1 @@
-Subproject commit d8887e51c41989105fadf51adbd1c33797845666
+Subproject commit b75c263d6afbff67343d688b7eecd6ad95e0acb0

+ 2 - 1
src/layouts/SecurityLayout.jsx

@@ -26,7 +26,8 @@ class SecurityLayout extends React.Component {
     const { children, loading, currentUser } = this.props // You can replace it to your authentication rule (such as check token exists)
     // 你可以把它替换成你自己的登录认证规则(比如判断 token 是否存在)
 
-    const isLogin = currentUser && currentUser.userid
+    const isLogin = currentUser && currentUser.staffId
+
     const queryString = stringify({
       redirect: window.location.href
     })

+ 1 - 2
src/models/login.js

@@ -22,9 +22,8 @@ const Model = {
         payload: response
       }) // Login successfully
 
-      if (response.code === consts.RET_CODE.SUCCESS) {
+      if (response && response.code === consts.RET_CODE.SUCCESS) {
         saveTokenId(response.data.token)
-        saveUserAccount(response.data.username)
         const urlParams = new URL(window.location.href)
         const params = getPageQuery()
         message.success('🎉 🎉 🎉  登录成功!')

+ 16 - 13
src/models/user.js

@@ -1,4 +1,5 @@
-import { queryCurrent, query as queryUsers } from '@/services/user'
+import consts from '@/consts'
+import { apiCurrent } from '@/services/user'
 
 const UserModel = {
   namespace: 'user',
@@ -6,20 +7,22 @@ const UserModel = {
     currentUser: {}
   },
   effects: {
-    *fetch(_, { call, put }) {
-      const response = yield call(queryUsers)
-      yield put({
-        type: 'save',
-        payload: response
-      })
-    },
+    // *fetch(_, { call, put }) {
+    //   const response = yield call(queryUsers)
+    //   yield put({
+    //     type: 'save',
+    //     payload: response
+    //   })
+    // },
 
     *fetchCurrent(_, { call, put }) {
-      const response = yield call(queryCurrent)
-      yield put({
-        type: 'saveCurrentUser',
-        payload: response
-      })
+      const response = yield call(apiCurrent)
+      if (response && response.code === consts.RET_CODE.SUCCESS) {
+        yield put({
+          type: 'saveCurrentUser',
+          payload: response.data
+        })
+      }
     }
   },
   reducers: {

+ 8 - 2
src/services/user.js

@@ -3,9 +3,15 @@ import request from '@/basic/utils/request'
 export async function query() {
   return request('/api/users')
 }
-export async function queryCurrent() {
-  return request('/api/currentUser')
+
+/**
+ * 获取当前用户信息
+ */
+export async function apiCurrent() {
+  const data = await request.get('/api/login/staff')
+  return data
 }
+
 export async function queryNotices() {
   return request('/api/notices')
 }