hf-pda/src/util/request.js
2022-08-04 15:53:50 +08:00

87 lines
2.3 KiB
JavaScript

/*
* @Author: gtz
* @Date: 2022-07-25 14:18:00
* @LastEditors: gtz
* @LastEditTime: 2022-08-04 15:53:41
* @Description: file content
* @FilePath: \hf-pda\src\util\request.js
*/
import axios from 'axios'
import { Toast, Notify } from 'vant'
import qs from 'qs'
// import store from '@/vuex'
const service = axios.create({
// baseURL: process.env.VUE_APP_BASE_API, // api的base_url
timeout: 5000 // request timeout
})
let toast = null
// 提交表单的
// token加入header
service.interceptors.request.use((config) => {
// console.log(store.state)
toast = Toast.loading({
mask: true,
message: '加载中...'
})
// if (config.url !== '/wms/login/verify' && JSON.parse(sessionStorage.getItem('userInfo')).token) {
// config.headers['token'] = JSON.parse(sessionStorage.getItem('userInfo')).token
// }
// 输出到安卓版本的时候开启下面的配置项
config.url = 'http://hfxny.mes.picaiba.com' + config.url
// ***************
if (config.formState) {
config.data = qs.stringify(config.data)
config.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
} else if (config.file) {
config.headers['Content-Type'] = 'multipart/form-data;charset=UTF-8'
}
return config
}, error => {
Promise.reject(error)
})
// axios全局拦截配置
service.interceptors.response.use(
response => {
// doing sth
// console.log('request!')
// setTimeout(() => {
if (toast) {
toast.clear()
}
toast = null
// }, 500)
// console.log(response)
if (response.data.code === 0) {
return {
success: true,
data: response.data.data,
pagetotal: response.data.pagetotal
}
} else if (response.config.responseType === 'blob') {
return {
success: true,
data: response
}
} else {
Notify(response.data.msg)
return {
success: false,
data: response.data,
msg: response.data.msg
}
}
// return response
},
error => {
toast.clear()
toast = null
if (error.response) {
Toast.fail('网络异常')
}
return Promise.reject(error) // 返回接口返回的错误信息
// return Promise.reject(error.response.data) // 返回接口返回的错误信息
})
export default service