在src文件夹中新创建一个utils文件夹,创建index.js来实现axios实例,并配置拦截器
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
| import axios from 'axios' import {ElLoading,ElMessage} from 'element-plus' import router from "../router" import store from "../store" import CHAT from "../client"
var instance=axios.create({ baseURL:"http://127.0.0.1:3007", timeout: 10000, headers:{ 'Content-Type':'application/x-www-form-urlencoded', }
}) let loading;
let requestCount=0;
const showLoading=()=>{ if(requestCount===0&&!loading){ loading=ElLoading.service({ text:'Loading', background:'rgba(0,0,0,0.7)', spinner:'el-icon-loading', }) } requestCount++; }
const hideLoading=()=>{ requestCount--; if(requestCount===0){ loading.close() } }
instance.interceptors.request.use((config)=>{ showLoading() const token=window.localStorage.getItem('token'); token&&(config.headers.Authorization=token) return config; },(error)=>{ Promise.reject(error); })
instance.interceptors.response.use((response)=>{ hideLoading() const status=response.data.status; if(status!=1){ switch(status){ case 0: if(response.data.token!=undefined)window.localStorage.setItem('token',response.data.token); break; case 200: console.log(response.data); store.commit("saveUserInfo",(response.data).data); window.localStorage.setItem('userInfo',JSON.stringify((response.data).data)); break; case 401: case 201: window.localStorage.removeItem('token'); window.localStorage.removeItem('userInfo') CHAT.logout(); router.push("/login"); } if(response.data.message)ElMessage.success(response.data.message) return Promise.resolve(response); } else { ElMessage.error(response.data.message); return Promise.reject(response); } },(error)=>{ console.log(error); if(error.response&&error.response.status){ return Promise.reject(error) } return Promise.reject(error);
}) export default instance;
|
request.js
request.js中使用axios实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import instance from "./index" const axios=({ method, url, data, config })=>{ method=method.toLowerCase(); if(method=='post'){ return instance.post(url,data,{...config}) }else if(method=='get'){ return instance.get(url,{ params:data, ...config }) }else if(method=='delete'){ return instance.delete(url,{ params:data, ...config }) }else if(method=='put'){ return instance.put(url,data,{...config}) }else{ console.log('未知的方法'+method) return false } } export default axios
|
api.js
api.js用来封装各种类型的请求
默认情况下,axios 将 JavaScript 对象序列化为JSON
. 要改为以格式发送数据application/x-www-form-urlencoded
,我用的是qs.stringfy将数据转换,其他方法可以参考官网
[]: https://axios-http.com/docs/urlencoded
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| import axios from "./request" import qs from "qs" export const login=(data)=>{ return axios({ url:'/api/login', method:'post', data:qs.stringify(data), }) } export const register=(data)=>{ return axios({ url:'/api/register', method:'post', data:qs.stringify(data) }) } export const logout=()=>{ return axios({ url:'/api/logout', method:'post', }) } export const getUserInfo=()=>{ return axios({ url:'/my/getUserInfo', method:'get',
}) } export const updatePassword=(data)=>{ return axios({ url:'/my/updatePassword', method:'post', data:qs.stringify(data) }) }
|