Bladeren bron

feat: 请求相关处理

touchitvoid 3 jaren geleden
bovenliggende
commit
22c0ca4faa
4 gewijzigde bestanden met toevoegingen van 19 en 8 verwijderingen
  1. 4 6
      .idea/workspace.xml
  2. 0 0
      src/services/api.ts
  3. 10 0
      src/services/request.ts
  4. 5 2
      src/views/User/Login.tsx

+ 4 - 6
.idea/workspace.xml

@@ -2,12 +2,8 @@
 <project version="4">
   <component name="ChangeListManager">
     <list default="true" id="41f85978-32cc-4348-8909-ad50d32d8ad8" name="Default Changelist" comment="">
-      <change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/router/config.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/router/config.tsx" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/services/user.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/services/user.ts" afterDir="false" />
-      <change beforePath="$PROJECT_DIR$/src/views/Role/list.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/views/Role/list.tsx" afterDir="false" />
+      <change beforePath="$PROJECT_DIR$/src/services/request.ts" beforeDir="false" afterPath="$PROJECT_DIR$/src/services/request.ts" afterDir="false" />
       <change beforePath="$PROJECT_DIR$/src/views/User/Login.tsx" beforeDir="false" afterPath="$PROJECT_DIR$/src/views/User/Login.tsx" afterDir="false" />
     </list>
     <option name="SHOW_DIALOG" value="false" />
@@ -29,7 +25,9 @@
     <property name="WebServerToolWindowFactoryState" value="false" />
     <property name="node.js.detected.package.eslint" value="true" />
     <property name="node.js.selected.package.eslint" value="(autodetect)" />
+    <property name="nodejs_package_manager_path" value="yarn" />
     <property name="settings.editor.selected.configurable" value="web-ide.project.structure" />
+    <property name="ts.external.directory.path" value="$PROJECT_DIR$/node_modules/typescript/lib" />
     <property name="vue.rearranger.settings.migration" value="true" />
   </component>
   <component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
@@ -40,7 +38,7 @@
       <option name="number" value="Default" />
       <option name="presentableId" value="Default" />
       <updated>1630475566284</updated>
-      <workItem from="1630475569580" duration="72000" />
+      <workItem from="1630475569580" duration="2565000" />
     </task>
     <servers />
   </component>

+ 0 - 0
src/services/user.ts → src/services/api.ts


+ 10 - 0
src/services/request.ts

@@ -1,4 +1,5 @@
 import axios from 'axios'
+import { message } from 'antd'
 
 const prefix = '/api'
 
@@ -8,10 +9,19 @@ export const request = axios.create({
 })
 
 request.interceptors.request.use(config => {
+  if (localStorage.getItem('area-token')) {
+    config.headers['Authorization'] = localStorage.getItem('area-token')
+  }
   return config
 })
 
 request.interceptors.response.use(response => {
+  if (response.data.code) {
+    const error = response.data.message
+    message.error(error)
+    throw error
+    return
+  }
   return response.data
 })
 

+ 5 - 2
src/views/User/Login.tsx

@@ -1,6 +1,7 @@
 import * as React from 'react'
 import './index.scss'
-import { PostLogin } from '../../services/user'
+import { PostLogin } from '../../services/api'
+import { message } from 'antd'
 
 // @ts-ignore
 import sha256 from 'sha256'
@@ -13,7 +14,9 @@ const Login: React.FC = () => {
   const handleLogin = async () => {
     const name = inputName.current.value
     const password = inputPassword.current.value || 'zaq123edc'
-    const res = await PostLogin({ user: name, password: sha256(password) })
+    const { data } = await PostLogin({ user: name, password: sha256(password) })
+    message.success('登录成功!')
+    localStorage.setItem('area-token', data.token)
   }
 
   return (