lanjianrong 4 rokov pred
rodič
commit
23463659ff
2 zmenil súbory, kde vykonal 91 pridanie a 55 odobranie
  1. 44 24
      src/app.tsx
  2. 47 31
      src/pages/user/Login/index.tsx

+ 44 - 24
src/app.tsx

@@ -8,6 +8,7 @@ import Footer from '@/components/Footer'
 import type { RequestOptionsInit, ResponseError } from 'umi-request'
 import { currentUser as queryCurrentUser } from './services/user/api'
 import { BookOutlined, LinkOutlined } from '@ant-design/icons'
+import consts from './utils/consts'
 
 const isDev = process.env.NODE_ENV === 'development'
 const loginPath = '/user/login'
@@ -107,41 +108,60 @@ export const request: RequestConfig = {
     throw error
   },
   middlewares: [
-    // async (ctx, next) => {
-    //   await next()
-    //   const { req, res } = ctx
-    //   // @ts-ignore
-    //   if (req.options?.skipErrorHandler) {
-    //     return
-    //   }
-    //   const errorAdaptor = req.options?.errorConfig.adaptor || (resData => resData)
-    //   const { options } = req
-    //   const { getResponse } = options
-    //   const resData = getResponse ? res.data : res
-    //   const errorInfo = errorAdaptor(resData, ctx)
-    //   if (errorInfo.success === false) {
-    //     // 抛出错误到 errorHandler 中处理
-    //     const error: RequestError = new Error(errorInfo.errorMessage)
-    //     error.name = 'BizError'
-    //     error.data = resData
-    //     error.info = errorInfo
-    //     error.response = res
-    //     throw error
-    //   }
-    // }
+    async (ctx, next) => {
+      await next()
+      const { req, res } = ctx
+      // @ts-ignore
+      if (req.options?.skipErrorHandler) {
+        return
+      }
+      const errorAdaptor = req.options?.errorConfig.adaptor || (resData => resData)
+      const { options } = req
+      const { getResponse } = options
+      const resData = getResponse ? res.data : res
+      const errorInfo = errorAdaptor(resData, ctx)
+      if (resData.code !== consts.RET_CODE.SUCCESS) {
+        // 抛出错误到 errorHandler 中处理
+        const error: RequestError = new Error(errorInfo.errorMessage)
+        error.name = 'BizError'
+        error.data = resData
+        error.info = errorInfo
+        error.response = res
+        throw error
+      }
+    }
   ],
   prefix: '/backstage',
   errorConfig: {
     adaptor: resData => {
       return {
-        success: true,
+        // success: resData.code === consts.RET_CODE.SUCCESS,
         data: resData.data,
         errorCode: resData.code,
         errorMessage: resData.msg
       }
     }
   },
-  responseInterceptors: [],
+  responseInterceptors: [
+    async (response, options) => {
+      const errorAdaptor = options?.errorConfig.adaptor || (resData => resData)
+      const res = await response.clone().json()
+      const { getResponse } = options
+      const resData = getResponse ? res.data : res
+      const errorInfo = errorAdaptor(resData, { res: response, req: options })
+
+      if (res?.code !== consts.RET_CODE.SUCCESS) {
+        // 抛出错误到 errorHandler 中处理
+        const error: RequestError = new Error(errorInfo.errorMessage)
+        error.name = 'BizError'
+        error.data = resData
+        error.info = errorInfo
+        error.response = res
+        throw error
+      }
+      return response
+    }
+  ],
   requestInterceptors: [authHeaderInterceptor]
 }
 

+ 47 - 31
src/pages/user/Login/index.tsx

@@ -1,19 +1,12 @@
-import {
-  // AlipayCircleOutlined,
-  LockOutlined,
-  // MobileOutlined,
-  // TaobaoCircleOutlined,
-  UserOutlined
-  // WeiboCircleOutlined
-} from '@ant-design/icons'
+import { LockOutlined, UserOutlined } from '@ant-design/icons'
 import { Alert, message, Tabs } from 'antd'
 import React, { useState } from 'react'
 import ProForm, { ProFormCheckbox, ProFormText } from '@ant-design/pro-form'
-import { useIntl, Link, history, FormattedMessage, SelectLang, useModel } from 'umi'
+import { useIntl, Link, history, FormattedMessage, SelectLang, useModel, useRequest } from 'umi'
 import Footer from '@/components/Footer'
 import { login } from '@/services/user/api'
 // import { getFakeCaptcha } from '@/services/ant-design-pro/login'
-
+import { currentUser as queryCurrentUser } from '@/services/user/api'
 import styles from './index.less'
 import consts from '@/utils/consts'
 
@@ -42,41 +35,64 @@ const goto = () => {
 
 const Login: React.FC = () => {
   const { setAuthToken } = useModel('user')
-  const [submitting, setSubmitting] = useState(false)
+  // const [submitting, setSubmitting] = useState(false)
   const [userLoginState, setUserLoginState] = useState<API.LoginResult>({})
   const [type, setType] = useState<string>('account')
   const { initialState, setInitialState } = useModel('@@initialState')
 
   const intl = useIntl()
 
-  const fetchUserInfo = async () => {
-    const userInfo = await initialState?.fetchUserInfo?.()
-    if (userInfo) {
-      setInitialState({
-        ...initialState,
-        currentUser: userInfo.data
-      })
+  const { run, loading } = useRequest(queryCurrentUser, {
+    manual: true,
+    onSuccess: result => {
+      setInitialState({ ...initialState, currentUser: result })
+      goto()
     }
-  }
+  })
+
+  const { run: tryLogin } = useRequest((values: API.LoginParams) => login(values), {
+    manual: true,
+    onSuccess: async result => {
+      setAuthToken(result?.token)
+      message.success('登录成功')
+      await run()
+      // await fetchUserInfo()
+    },
+    onError: () => {
+      setUserLoginState({ ...userLoginState, code: -1 })
+    }
+  })
+  // const fetchUserInfo = async () => {
+  //   const userInfo = await initialState?.fetchUserInfo?.()
+  //   if (userInfo) {
+  //     setInitialState({
+  //       ...initialState,
+  //       currentUser: userInfo
+  //     })
+  //   }
+  // }
 
   const handleSubmit = async (values: API.LoginParams) => {
-    setSubmitting(true)
+    // setSubmitting(true)
     try {
       // 登录
-      const res = await login({ ...values, type })
-      if (res?.code === consts.RET_CODE.SUCCESS) {
-        setAuthToken(res.data.token)
-        message.success('登录成功')
-        await fetchUserInfo()
-        goto()
-        return
-      }
+      const res = await tryLogin(values)
+      console.log(res)
+      // if (res?.code === consts.RET_CODE.SUCCESS) {
+
+      // setAuthToken(res.data.token)
+      // message.success('登录成功')
+      // await run()
+      // // await fetchUserInfo()
+      // goto()
+      // return
+      // }
+      // setUserLoginState({ ...userLoginState, code: -1 })
       // 如果失败去设置用户错误信息
-      setUserLoginState(res.data)
     } catch (error) {
       message.error('登录失败,请重试')
     }
-    setSubmitting(false)
+    // setSubmitting(false)
   }
   const { code } = userLoginState
 
@@ -110,7 +126,7 @@ const Login: React.FC = () => {
               },
               render: (_, dom) => dom.pop(),
               submitButtonProps: {
-                loading: submitting,
+                loading,
                 size: 'large',
                 style: {
                   width: '100%'