ajax.js 4.1 KB
Newer Older
C
Captain.B 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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 () {
W
''  
wenyann 已提交
17
      MessageBox.alert("The authentication information has expired, please login again", "Prompt", {
C
Captain.B 已提交
18 19 20
        callback: () => {
          window.location.href = "/login"
        }
W
''  
wenyann 已提交
21
      }).then(r => {
W
''  
wenyann 已提交
22
        window.location.href = "/login"
C
Captain.B 已提交
23 24 25 26 27 28 29 30 31 32 33
      });
    };

    axios.defaults.withCredentials = true;

    axios.interceptors.response.use(response => {
      if (response.headers["authentication-status"] === "invalid") {
        login();
      }
      return response;
    }, error => {
W
''  
wenyann 已提交
34
      return Promise.reject(error);
C
Captain.B 已提交
35 36
    });

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

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

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

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

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

C
Captain.B 已提交
108 109 110 111
    Vue.prototype.$all = function (array, callback) {
      if (array.length < 1) return;
      axios.all(array).then(axios.spread(callback));
    };
C
chenjianxing 已提交
112 113

    Vue.prototype.$fileDownload = function(url) {
C
chenjianxing 已提交
114 115 116 117 118 119 120 121
      axios.get(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();
        });
C
chenjianxing 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    };

    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 已提交
143 144
  }
}