skyouc
2025-06-22 fee38f39e36bcda9924f5b26dca609dda6b331e0
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
100
101
102
103
104
105
106
107
108
109
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* eslint-disable */
import axios from 'axios'
import {MessageBox, Message} from 'element-ui'
import utilVue from '../utils/utilVue.js'
 
const baseUrl = process.env.VUE_APP_BASE_API
// create an axios instance
const service = axios.create({
  baseURL: baseUrl + 'wms/', // url = base url + request url
  timeout: 120000 // request timeout
})
 
// request interceptor
service.interceptors.request.use(
  (config) => {
    // do something before request is sent
 
    // if (store.getters.currentUser) {
    //   // let each request carry token
    //   // ['X-Token'] is a custom headers key
    //   // please modify it according to the actual situation
    //   config.headers['Authorization'] = sessionStorage.getItem('Authorization')
    //   // config.headers['warehouseId'] = store.getters.currentWarehouse.id
    //   // config.headers['requestType'] = 'spdpc'
    // }
    if (config.method != 'get' && window.appendParam && config.data) {
      Object.keys(window.appendParam).forEach((key) => {
        if (Object.prototype.hasOwnProperty.call(window.appendParam, key)) {
          if (window.appendParam[key]) {
            if (Array.isArray(config.data)) {
              config.data.forEach(data => {
                if (typeof data == 'object') {
                  data[key] = window.appendParam[key]
                }
              })
            } else {
              config.data[key] = window.appendParam[key]
            }
          }
        }
      })
      window.appendParam = null
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
 
const download = (resp) => {
  //这里res.data是返回的blob对象
  var blob = new Blob([resp.data], {type: resp.headers['content-type']});
 
  // 自定义响应头
  let fileName = resp.headers['content-filename'] && decodeURIComponent(resp.headers['content-filename'])
  if (fileName === undefined || fileName === null || fileName === "") {
      fileName = new Date().getTime() + '.xlsx'
  }
 
  if (window.navigator.msSaveOrOpenBlob) {
      // 如果是IE浏览器
      navigator.msSaveBlob(blob, fileName);//filename文件名包括扩展名,下载路径为浏览器默认路径
      return
  }
 
  // chrome、Firefox
  var downloadElement = document.createElement('a');
  var href = window.URL.createObjectURL(blob); //创建下载的链接
  downloadElement.href = href;
  downloadElement.download = fileName
  document.body.appendChild(downloadElement);
  downloadElement.click(); //点击下载
  document.body.removeChild(downloadElement); //下载完成移除元素a
  window.URL.revokeObjectURL(href); //释放掉blob对象
}
 
// response interceptor
service.interceptors.response.use(
  /**
   * If you want to get http information such as headers or status
   * Please return  response => response
   */
 
  /**
   * Determine the request status by custom code
   * Here is just an example
   * You can also judge the status by HTTP Status Code
   */
  (response) => {
    let {headers, data} = response
 
    // 处理文件下载
    let contentType = headers['content-type']
    if (headers && contentType
        && (contentType.indexOf('application/x-msdownload') != -1
            || contentType.indexOf('application/octet-stream') != -1
            || contentType.indexOf('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') != -1)) {
        download(response)
        return response
    }
 
    if (contentType && contentType.indexOf('application/json') == -1) {
      return response
    }
 
    let isValidexception = headers && '1' == headers.validexception
 
    const res = data
    // if the custom code is not 0, it is judged as an error.
    if (res.success) {
      return res
    }
    utilVue.loadHide()
    if (res.success == undefined) { //如果未定义,直接返回
      return res
    }
 
    Message({
      message: '<div class="el-notification__content ">'+res.msg || 'Error'+'</div>',
      duration: 5000,
      dangerouslyUseHTMLString: true,
      iconClass: isValidexception ? 'el-notification__icon el-icon-warning' : 'el-notification__icon el-icon-error',
      customClass: isValidexception ? 'warn-ajax' : 'error-ajax',
      showClose: true,
      showIcon: true,
      offset: 1
    })
 
    //下面功能尚未实现,暂时注释掉
    // return Promise.reject(new Error(res.msg || 'Error'))
    // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
    if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
      // to re-login
      // MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
      //   confirmButtonText: 'Re-Login',
      //   cancelButtonText: 'Cancel',
      //   type: 'warning'
      // }).then(() => {
      //   store.dispatch('user/resetToken').then(() => {
      //     location.reload()
      //   })
      // })
    }
    return res
    // return Promise.reject(new Error(res.msg || 'Error'))
  },
  (error) => {
    utilVue.loadHide()
    let response = error?error.response:null
    let status = response?response.status:null
    let data = response?response.data:null
    let isValidexception = response.headers && '1' == response.headers.validexception
    if (data && data.data && data.data.needLogin) {
      try {
        MessageBox.close()
      }catch (e) {
 
      }
      MessageBox.alert('会话超时,请重新登录!', '系统提示', {
        confirmButtonText: '确定',
        type: 'warning',
        callback: action => {
          // router.push('/login', () => {
          //   // Message.error(error.response.data.msg)
          //   location.reload()
          // })
        }
      })
 
      return Promise.reject(error)
    }
    let msg='未知异常'
    if(!status){
      msg= '网络连接异常,请检查本地网络是否连接'
    }else if(status == 500){
      if(response.data.msg){
        msg= response.data.msg
      }else if(response.data.message){
        msg= response.data.message
        if(msg.indexOf('服务器异常') == -1) msg='服务器异常,原因:'+msg
      }else if(typeof(response.data) == 'string'  && response.data.indexOf('Proxy error') != -1){
        msg= '服务器暂时无法访问,请稍候重试'
      }
    }else if(status == 502){
      msg= '代理服务器暂时不可以使用,请稍候重试'
    }
    Message({
      message: '<div class="el-notification__content ">'+msg+'</div>',
      duration: 5000,
      dangerouslyUseHTMLString: true,
      iconClass: isValidexception ? 'el-notification__icon el-icon-warning' : 'el-notification__icon el-icon-error',
      customClass: isValidexception ? 'warn-ajax' : 'error-ajax',
      showClose: true,
      showIcon: true,
      offset: 1
    })
    return Promise.reject(error)
  }
)
 
export default service