封装 axios+promise通用请求函数操作

看,田野里的玉米露出了笑脸,秋风吹过,闪烁着太阳的光芒,秋天是金色的。看满山的枫叶,红得就像一团火焰在熊熊燃烧,散发着生机,秋天是红色的。呵呵,我们都错了,秋天不是金黄的,也不是红色的,秋天是五颜六色的。

我就废话不多说了,大家还是直接看代码吧~

import axios from "axios";
import baseUrl from "../../setBaseUrl";
axios.defaults.baseURL = baseUrl;
import { Loading, Message } from "element-ui";
const loadingOptions = {
 lock: true,
 text: "拼命加载中",
 spinner: "el-icon-loading",
 background: "rgba(0, 0, 0, 0.7)"
};
if (localStorage.token) {
 axios.defaults.headers.common["x-token"] = localStorage.token;
}
const Http = {
 get: function(url: string, data: any, isLoading: boolean) {
  if (localStorage.token) {
   axios.defaults.headers.common["x-token"] = localStorage.token;
  }
  let loading: any;
  if (isLoading) {
   loading = Loading.service(loadingOptions);
  }
  return new Promise<any>((resolve, reject) => {
   axios
    .get(url, { params: data })
    .then(function(res) {
     if (isLoading) {
      loading.close();
     }
     if (res.data.code === 200) {
      resolve(res.data.data);
     } else {
      // console.warn("Http error Info ===> ", res.data);
      reject(res.data.message);
     }
    })
    .catch(function(err) {
     if (isLoading) {
      loading.close();
     }
     // console.warn("Http error Info ===> ", err);
     Message({
      message: err.message,
      type: "error",
      duration: 2500,
      showClose: true
     });
    });
  }).catch(function(err) {
   Message({
    message: err,
    type: "error",
    duration: 2500,
    showClose: true
   });
  });
 },
 post: function(url: string, data: any, isLoading: boolean) {
  if (localStorage.token) {
   axios.defaults.headers.common["x-token"] = localStorage.token;
  }
  let loading: any;
  if (isLoading) {
   loading = Loading.service(loadingOptions);
  }
  return new Promise<any>((resolve, reject) => {
   axios
    .post(url, data)
    .then(function(res) {
     if (isLoading) {
      loading.close();
     }
     if (res.data.code === 200) {
      resolve(res.data.data);
     } else {
      reject(res.data.message);
     }
    })
    .catch(function(err) {
     if (isLoading) {
      loading.close();
     }
     // console.warn("Http error Info===>", err);
     Message({
      message: err.message,
      type: "error",
      duration: 2500,
      showClose: true
     });
    });
  }).catch(function(err) {
   Message({
    message: err,
    type: "error",
    duration: 2500,
    showClose: true
   });
  });
 },
 put: function(url: string, data: any, isLoading: boolean) {
  if (localStorage.token) {
   axios.defaults.headers.common["x-token"] = localStorage.token;
  }
  let loading: any;
  if (isLoading) {
   loading = Loading.service(loadingOptions);
  }
  return new Promise<any>((resolve, reject) => {
   axios
    .put(url, data)
    .then(function(res) {
     if (isLoading) {
      loading.close();
     }
     if (res.data.code === 200) {
      resolve(res.data.data);
     } else {
      reject(res.data.message);
     }
    })
    .catch(function(err) {
     if (isLoading) {
      loading.close();
     }
     // console.warn("Http error Info===>", err);
     Message({
      message: err.message,
      type: "error",
      duration: 2500,
      showClose: true
     });
    });
  }).catch(function(err) {
   Message({
    message: err,
    type: "error",
    duration: 2500,
    showClose: true
   });
  });
 },
 delete: function(url: string, data: any, isLoading: boolean) {
  if (localStorage.token) {
   axios.defaults.headers.common["x-token"] = localStorage.token;
  }
  let loading: any;
  if (isLoading) {
   loading = Loading.service(loadingOptions);
  }
  return new Promise<any>((resolve, reject) => {
   axios
    .delete(url, data)
    .then(function(res) {
     if (isLoading) {
      loading.close();
     }
     if (res.data.code === 200) {
      resolve(res.data.data);
     } else {
      reject(res.data.message);
     }
    })
    .catch(function(err) {
     if (isLoading) {
      loading.close();
     }
     // console.warn("Http error Info===>", err);
     Message({
      message: err.message,
      type: "error",
      duration: 2500,
      showClose: true
     });
    });
  }).catch(function(err) {
   Message({
    message: err,
    type: "error",
    duration: 2500,
    showClose: true
   });
  });
 }
};
export default Http;

补充知识:vuex刷新页面是store被更新,如何解决数据备份?

我用的方法很简单,在App.vue中的生命周期函数created中,监听页面刷新,在页面刷新的时候,将store的数据及时存储在sessionStorage中,然后刷新完毕后就可以将sessionStorage中的数据再取出来用

created () { //页面刷新store数据备份
  if (localStorage.cmstoken) {
   this.$router.replace("/data");
  }else {
   this.$router.replace("/");
  }
  if (sessionStorage.getItem("store") ) { //在页面加载时读取sessionStorage里的状态信息
    this.$store.replaceState(Object.assign({}, this.$store.state,JSON.parse(sessionStorage.getItem("store"))))
  }
  window.addEventListener("beforeunload",()=>{ //在页面刷新时将vuex里的信息保存到sessionStorage里
    sessionStorage.setItem("store",JSON.stringify(this.$store.state))
  })
 },

以上这篇封装 axios+promise通用请求函数操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能有感兴趣的文章
vue中axios的二次封装实例讲解

封装一下vue中的axios示例代码详解

axios取消请求与避免重复请求

vue中如何简单封装axios浅析

项目中如何如何使用axios过滤多次重复请求详解