//npm install flyio --save  下载依赖
import { VUE_APP_BASE_API } from '@/config/config'
import { getToken } from '@/utils/auth'
import Fly from 'flyio/dist/npm/wx'
//创建实例
const request = new Fly()
request.config.baseURL = VUE_APP_BASE_API
request.config.timeout = 3000 

//请求拦截
request.interceptors.request.use((request) => {
 //给所有请求添加自定义header
  request.headers["content-type"] = "application/json";
  if (getToken()) {
     request.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
   }
   return request;
})

//响应拦截
request.interceptors.response.use(
    (response) => {
        return response
    },
    (err) => {
        if (err.status == 0) {
            console.log("网络连接异常");
            return
        } else if (err.status == 1) {
            console.log("网络连接超时");
            return
        } else if (err.status == 401) {
            console.log("用户未登录");
            return
        } else {
            if (err.response.data.message) {
                return err.response.data.message
            } else {
                return '请求数据失败,请稍后再试'
            }
        };
        // Do something with response error
    }
)

export default request

调用flyio

import request from '@/utils/request'

export function login(data) {
    return request.post("auth/login",data)
}

export function getInfo() {
  return request.get('auth/info')
}
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐