// Request interceptor import axios from 'axios' import {Toast} from 'vant'; import $router from '.. /router/index' Toast.setDefaultOptions({ duration: 1500 }); Toast.setDefaultOptions('loading', { forbidClick: true }); // export const baseURL = ""; // export const baseURL = 'http://114.118.8.241/api'; // test environment export const baseURL = "http://localhost:3006"; Const axiosInstance = axios.create({baseURL, timeout: Headers: {' x-auth0-token ': 'tokenValue'},}); // Order of execution: Request function -- -- > request interceptor > (formal request) -- -- > response interceptors -- - > then function or catch function / / add request interceptor axiosInstance interceptors. Request. Use (config = > {/ / What to do before sending the request return config; }, error => {// do something about the response error return promise.reject (error); }); / / add the response interceptor axiosInstance. Interceptors. Response. Use (response = > {/ / do something for the response data switch (response) data) code) {0 case: Toast.clear(); Return response.data case 40001: // If the token fails, log in to case-401 again: SetTimeout (()=>{$router.replace('/login')},1500) return response.data default: Toast(response.data.msg); Return response.data}}, error => {// Do something about the response error if (error.response) {switch (error.response.status) {case 400: Error. Message = 'error request: ${error.response.data}' break case 401: $router.replace('/login') break case 403: error. Message = 'denied access' break case 404: Error. Message = 'request method not allowed' break case 408: Error. Message = 'Server failed' break case 501: error. Message = 'Network failed' break case 502: Error. Message = 'network timeout' break case 503: error. Message = 'Network timeout' break case 505: Error. Message = 'HTTP version does not support this request' break default: Error. Message = 'connection error ${error. Response. status}'}} else {error. Message = 'server exception'} Toast(error. return Promise.reject(error.response); }); /** Encapsulate get request * @param {String} URL * @param {Object} params * @param {Boolean} loading * @returns {Promise} */ export Function getRequest(url, params, loading = true) {if (loading) {toast. loading({message: 'loading... ', duration: 0, }); } return new Promise((resolve, Reject) => {// axiosInstance. Request ({config}) Equivalent axiosInstance({config}) axiosinstance. request({// {config}) The configuration of axiosInstance will be merged with that of the instance. url, method: "GET", params, }). Then (res => {resolve(res)}). Catch (err => {reject(err)})} // Encapsulate POST PUT PATCH request export function PostRequest (URL, data, method = "POST", loading = true) {if (loading) {toast. loading({message: ', duration: 0, }); } return new Promise((resolve, reject) => { axiosInstance.request({ url, method, data, // 'data' is the data sent as the body of the request and only applies to these request methods 'PUT', 'POST', And 'PATCH'}). Then (res = > {resolve (res)}). The catch (err = > {reject (err)})})}Copy the code