request.js

import axios from 'axios'
import { message } from 'ant-design-vue'
const service = axios.create({
      baseURL: 'http://127.0.0.1:8088'.// The timeout period is in ms. The timeout period is set to 6s
      timeout: 6 * 1000
})
service.interceptors.request.use(config= > {
  // Do some processing before sending a request, such as data conversion, request header configuration, token setting, loading setting, etc
  // Data conversion, also can use QS conversion
  config.data = JSON.stringify(config.data); 
  config.headers = {
        'Content-Type':'application/x-www-form-urlencoded' // Configure the request header
  }
  const token = localStorage.getItem('loginToken');
  if(token){
    // config.params = {'token':token} // If required to carry in the parameters
    config.headers.token= token; // If the request is carried in the request header
  }
  return config
}, error= > {
  Promise.reject(error)
})

// Response interceptor
service.interceptors.response.use(response= > {
  // Some common processing after receiving the response data and succeeding, such as closing loading, etc
  return response
}, error= > {
  if (error && error.response) {
    // Public error handling
    // Process according to the response code
    switch (error.response.status) {
      case 400:
        error.message = 'Error request'
        break;
      case 401:
        error.message = 'Not authorized, please log in again'
        break;
      case 403:
        error.message = 'Access denied'
        break;
      case 404:
        error.message = 'Request error, resource not found'
        break;
      case 405:
        error.message = 'Requested method not allowed'
        break;
      case 408:
        error.message = 'Request timed out'
        break;
      case 500:
        error.message = 'Server side error'
        break;
      case 501:
        error.message = 'Network not implemented'
        break;
      case 502:
        error.message = 'Network error'
        break;
      case 503:
        error.message = 'Service unavailable'
        break;
      case 504:
        error.message = 'Network timeout'
        break;
      case 505:
        error.message = 'The HTTP version does not support this request'
        break;
      default:
        error.message = 'Connection error${error.response.status}`}}else {
    // Timeout processing
    if (JSON.stringify(error).includes('timeout')) {
      message.error('Server response timed out, please refresh current page')
    }
    error.message = 'Failed to connect to server'
  }
  message.error(error.message)
  /***** No further action is required *****/
  // If no error handling is required, the above processing can be omitted
  return Promise.resolve(error.response)
})
export default service;
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

http.js


import service from './request'

const http ={
  /** * methods: request *@param Url Request address *@param Params requests the parameter */
   
  get(url,params){
    const config = {
      method: 'get'.url:url
    }
    if(params) config.params = params
    return service(config)
  },
  
  post(url,params){
    const config = {
      method: 'post'.url:url
    }
    if(params) config.data = params
    return service(config)
  },
  
  put(url,params){
    const config = {
      method: 'put'.url:url
    }
    if(params) config.params = params
    return service(config)
  },
  
  delete(url,params){
    const config = {
      method: 'delete'.url:url
    }
    if(params) config.params = params
    return service(config)
  }
}
export default http;
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

api.js


import http from '@/utils/http.js';
const fmModelList = (params) = >http.get("/filemanage/base/fmModel/list",params);
export {
  fmModelList,
}
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

a.vue


import {fmModelList} from '@/utils/api.js';
let res = fmModelList({ isUsed:1 });
Copy the code