ajax.js 4.0 KB
Newer Older
C
Captain.B 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
import {Message, MessageBox} from 'element-ui';
import axios from "axios";

export default {
  install(Vue) {
    if (!axios) {
      window.console.error('You have to install axios');
      return
    }

    if (!Message) {
      window.console.error('You have to install Message of ElementUI');
      return
    }

    let login = function () {
      MessageBox.alert("认证信息已过期,请重新登录。", {
        callback: () => {
          window.location.href = "/login"
        }
      });
    };

    axios.defaults.withCredentials = true;

    axios.interceptors.response.use(response => {
      if (response.headers["authentication-status"] === "invalid") {
        login();
      }
      return response;
    }, error => {
      return Promise.reject(error);
    });

Q
q4speed 已提交
35 36 37
    function then(success, response, result) {
      if (!response.data) {
        success(response);
C
chenjianxing 已提交
38
      } else if (response.data.success) {
Q
q4speed 已提交
39 40 41 42 43 44 45 46 47
        success(response.data);
      } else {
        window.console.warn(response.data);
        Message.warning(response.data.message);
      }
      result.loading = false;
    }

    function exception(error, result) {
Q
q4speed 已提交
48 49 50 51
      if (error.response && error.response.status === 401) {
        login();
        return;
      }
Q
q4speed 已提交
52 53
      result.loading = false;
      window.console.error(error.response || error.message);
C
Captain.B 已提交
54 55 56 57 58
      if (error.response.data) {
        Message.error({message: error.response.data.message, showClose: true});
      } else {
        Message.error({message: error.message, showClose: true});
      }
Q
q4speed 已提交
59 60
    }

C
Captain.B 已提交
61
    Vue.prototype.$get = function (url, success) {
Q
q4speed 已提交
62
      let result = {loading: true};
C
Captain.B 已提交
63 64 65 66
      if (!success) {
        return axios.get(url);
      } else {
        axios.get(url).then(response => {
Q
q4speed 已提交
67
          then(success, response, result);
C
Captain.B 已提交
68
        }).catch(error => {
Q
q4speed 已提交
69 70 71
          exception(error, result);
        });
        return result;
C
Captain.B 已提交
72 73 74
      }
    };

C
chenjianxing 已提交
75
    Vue.prototype.$post = function (url, data, success, failure) {
Q
q4speed 已提交
76
      let result = {loading: true};
C
Captain.B 已提交
77 78 79 80
      if (!success) {
        return axios.post(url, data);
      } else {
        axios.post(url, data).then(response => {
Q
q4speed 已提交
81
          then(success, response, result);
C
Captain.B 已提交
82
        }).catch(error => {
Q
q4speed 已提交
83
          exception(error, result);
C
chenjianxing 已提交
84 85 86
          if (failure) {
            then(failure, error, result);
          }
Q
q4speed 已提交
87 88
        });
        return result;
C
Captain.B 已提交
89 90 91
      }
    };

H
haifeng414 已提交
92
    Vue.prototype.$request = function (axiosRequestConfig, success) {
Q
q4speed 已提交
93
      let result = {loading: true};
H
haifeng414 已提交
94 95 96 97
      if (!success) {
        return axios.request(axiosRequestConfig);
      } else {
        axios.request(axiosRequestConfig).then(response => {
Q
q4speed 已提交
98
          then(success, response, result);
H
haifeng414 已提交
99
        }).catch(error => {
Q
q4speed 已提交
100 101 102
          exception(error, result);
        });
        return result;
H
haifeng414 已提交
103 104 105
      }
    };

C
Captain.B 已提交
106 107 108 109
    Vue.prototype.$all = function (array, callback) {
      if (array.length < 1) return;
      axios.all(array).then(axios.spread(callback));
    };
C
chenjianxing 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

    Vue.prototype.$fileDownload = function(url) {
      axios({
        method: 'get',
        url: url,
        responseType: 'blob',
      }).then(response => {
        let fileName = window.decodeURI(response.headers['content-disposition'].split('=')[1]);
        let link = document.createElement("a");
        link.href = window.URL.createObjectURL(new Blob([response.data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"}));
        link.download = fileName;
        link.click();
      })
    };

    Vue.prototype.$fileUpload = function(url, fileList, success, failure) {
      let result = {loading: true};
      let formData = new FormData();
      if (fileList.length > 0) {
        fileList.forEach(f => {
          formData.append("file", f);
        });
      }
      axios.post(url, formData, { headers: { "Content-Type": "multipart/form-data" }})
        .then(response => {
          then(success, response, result);
        }).catch(error => {
          exception(error, result);
          if (failure) {
          then(failure, error, result);
        }
      });
      return result;
    }

C
Captain.B 已提交
145 146
  }
}