简单封装axios请求

import axios from "axios";
import { MessageBox } from "element-ui";

function handleError(message, reject) {
  if (message) {
    MessageBox.alert(message, "提示");
  } else {
    MessageBox.alert("网络或系统错误!", "提示");
  }
  reject();
}

export default {
  get(path, data = {}) {
    let p = new Promise(async function(resolve, reject) {
      try {
        let resp = await axios.get(path, data);
        if (resp.data.errcode == 0) {
          resolve(resp.data);
        } else {
          handleError(resp.data.errmsg, reject);
        }
      } catch (e) {
        handleError(e, reject);
      }
    });
    return p;
  },

  post(path, data = {}) {
    let p = new Promise(async function(resolve, reject) {
      try {
        let resp = await axios.post(path, data);
        if (resp.data.errcode == 0) {
          resolve(resp.data);
        } else {
          handleError(resp.data.errmsg, reject);
        }
      } catch (e) {
        handleError(e, reject);
      }
    });
    return p;
  }
};

你可能感兴趣的:(简单封装axios请求)