ajax.js 4.2 KB
Newer Older
C
Captain.B 已提交
1 2
import {Message, MessageBox} from 'element-ui';
import axios from "axios";
W
wenyann 已提交
3 4
import i18n from '../../i18n/i18n'

C
Captain.B 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18

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 已提交
19
      MessageBox.alert(i18n.t('commons.tips'), i18n.t('commons.prompt'), {
C
Captain.B 已提交
20
        callback: () => {
C
Captain.B 已提交
21 22
          axios.get("/signout");
          localStorage.setItem('Admin-Token', "{}");
C
Captain.B 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
          window.location.href = "/login"
        }
      });
    };

    axios.defaults.withCredentials = true;

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

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

    function exception(error, result) {
Q
q4speed 已提交
52 53 54 55
      if (error.response && error.response.status === 401) {
        login();
        return;
      }
Q
q4speed 已提交
56 57
      result.loading = false;
      window.console.error(error.response || error.message);
C
chenjianxing 已提交
58
      if (error.response && error.response.data) {
C
chenjianxing 已提交
59 60 61
        if (error.response.headers["authentication-status"] != "invalid") {
          Message.error({message: error.response.data.message, showClose: true});
        }
C
Captain.B 已提交
62 63 64
      } else {
        Message.error({message: error.message, showClose: true});
      }
Q
q4speed 已提交
65 66
    }

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

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

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

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

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

    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 已提交
147 148
  }
}