/* 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
|