Blame view

ant-design-vue-jeecg/src/utils/request.js 5.26 KB
1
2
3
4
5
import Vue from 'vue'
import axios from 'axios'
import store from '@/store'
import { VueAxios } from './axios'
import {Modal, notification} from 'ant-design-vue'
6
import { ACCESS_TOKEN, TENANT_ID } from "@/store/mutation-types"
7
8
9
10
11
12
13
/**
 * 【指定 axios baseURL
 * 如果手工指定 baseURL: '/jeecg-boot'
 * 则映射后端域名,通过 vue.config.js
 * @type {*|string}
 */
14
15
// 创建 axios 实例
const service = axios.create({
16
  baseURL: '/jeecg-boot',
17
  timeout: 9000 // 请求超时时间
18
19
20
21
})

const err = (error) => {
  if (error.response) {
22
    let that=this;
23
24
25
26
27
28
29
30
31
    let data = error.response.data
    const token = Vue.ls.get(ACCESS_TOKEN)
    console.log("------异常响应------",token)
    console.log("------异常响应------",error.response.status)
    switch (error.response.status) {
      case 403:
        notification.error({ message: '系统提示', description: '拒绝访问',duration: 4})
        break
      case 500:
32
33
34
35
36
37
38
39
        console.log("------error.response------",error.response)
        // update-begin- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
        let type=error.response.request.responseType;
        if(type === 'blob'){
          blobToJson(data);
          break;
        }
        // update-end- --- author:liusq ------ date:20200910 ---- for:处理Blob情况----
40
        //notification.error({ message: '系统提示', description:'Token失效,请重新登录!',duration: 4})
41
        if(token && data.message.includes("Token失效")){
42
43
44
45
46
47
48
49
          // update-begin- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
          Modal.error({
            title: '登录已过期',
            content: '很抱歉,登录已过期,请重新登录',
            okText: '重新登录',
            mask: false,
            onOk: () => {
              store.dispatch('Logout').then(() => {
50
                Vue.ls.remove(ACCESS_TOKEN)
51
                try {
52
53
54
                  let path = window.document.location.pathname
                  console.log("location pathname -> "+path)
                  if(path!="/" && path.indexOf('/user/login')==-1){
55
56
57
58
59
                    window.location.reload()
                  }
                }catch (e) {
                  window.location.reload()
                }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
              })
            }
          })
          // update-end- --- author:scott ------ date:20190225 ---- for:Token失效采用弹框模式,不直接跳转----
        }
        break
      case 404:
          notification.error({ message: '系统提示', description:'很抱歉,资源未找到!',duration: 4})
        break
      case 504:
        notification.error({ message: '系统提示', description: '网络超时'})
        break
      case 401:
        notification.error({ message: '系统提示', description:'未授权,请重新登录',duration: 4})
        if (token) {
          store.dispatch('Logout').then(() => {
            setTimeout(() => {
              window.location.reload()
            }, 1500)
          })
        }
        break
      default:
        notification.error({
          message: '系统提示',
          description: data.message,
          duration: 4
        })
        break
    }
  }
  return Promise.reject(error)
};

// request interceptor
service.interceptors.request.use(config => {
  const token = Vue.ls.get(ACCESS_TOKEN)
  if (token) {
    config.headers[ 'X-Access-Token' ] = token // 让每个请求携带自定义 token 请根据实际情况自行修改
  }
100
101
102
103
104
105
106
  //update-begin-author:taoyan date:2020707 for:多租户
  let tenantid = Vue.ls.get(TENANT_ID)
  if (!tenantid) {
    tenantid = 0;
  }
  config.headers[ 'tenant_id' ] = tenantid
  //update-end-author:taoyan date:2020707 for:多租户
107
  if(config.method=='get'){
108
109
110
111
112
    if(config.url.indexOf("sys/dict/getDictItems")<0){
      config.params = {
        _t: Date.parse(new Date())/1000,
        ...config.params
      }
113
114
    }
  }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  return config
},(error) => {
  return Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((response) => {
    return response.data
  }, err)

const installer = {
  vm: {},
  install (Vue, router = {}) {
    Vue.use(VueAxios, router, service)
  }
}
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * Blob解析
 * @param data
 */
function blobToJson(data) {
  let fileReader = new FileReader();
  let token = Vue.ls.get(ACCESS_TOKEN);
  fileReader.onload = function() {
    try {
      let jsonData = JSON.parse(this.result);  // 说明是普通对象数据,后台转换失败
      console.log("jsonData",jsonData)
      if (jsonData.status === 500) {
        console.log("token----------》",token)
        if(token && jsonData.message.includes("Token失效")){
          Modal.error({
            title: '登录已过期',
            content: '很抱歉,登录已过期,请重新登录',
            okText: '重新登录',
            mask: false,
            onOk: () => {
              store.dispatch('Logout').then(() => {
                Vue.ls.remove(ACCESS_TOKEN)
                window.location.reload()
              })
            }
          })
        }
      }
    } catch (err) {
      // 解析成对象失败,说明是正常的文件流
      console.log("blob解析fileReader返回err",err)
    }
  };
  fileReader.readAsText(data)
}
166
167
168
169

export {
  installer as VueAxios,
  service as axios
170
171
172
}

service