a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
1
|
import axios from 'axios'
|
6db3ce8f
wuxw
优化 post 显示动画
|
2
|
import { Message, Loading } from 'element-ui'
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
3
4
5
|
import config from '@/conf/config'
import { getHeader } from './header'
|
6db3ce8f
wuxw
优化 post 显示动画
|
6
7
8
|
// 创建loading实例
let loadingInstance = null
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
9
10
11
12
13
14
|
// 创建axios实例
const service = axios.create({
baseURL: config.baseApi, // 基础URL
timeout: config.apiTimeout // 请求超时时间
})
|
6db3ce8f
wuxw
优化 post 显示动画
|
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// 显示加载动画
const showLoading = () => {
loadingInstance = Loading.service({
lock: true,
text: '加载中...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
})
}
// 隐藏加载动画
const hideLoading = () => {
if (loadingInstance) {
loadingInstance.close()
loadingInstance = null
}
}
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
32
33
34
35
36
37
38
39
40
41
|
// 请求拦截器
service.interceptors.request.use(
config => {
let _header = getHeader();
// 在发送请求之前做些什么
config.headers['Content-Type'] = 'application/json'
config.headers['Accept'] = 'application/json'
config.headers['app-id'] = _header['app-id']
config.headers['TRANSACTION-ID'] = _header['TRANSACTION-ID']
|
6db3ce8f
wuxw
优化 post 显示动画
|
42
43
44
|
config.headers['REQ-TIME'] = _header['REQ-TIME']
config.headers['SIGN'] = _header['SIGN']
config.headers['user-id'] = _header['user-id']
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
45
46
47
|
config.headers['X-Requested-With'] = 'XMLHttpRequest'
// 这里可以添加token等认证信息
config.headers['Authorization'] = _header['Authorization']
|
6db3ce8f
wuxw
优化 post 显示动画
|
48
|
if (config.url.indexOf('/app/') == -1 && config.url.indexOf('/callComponent/') == -1) {
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
49
50
|
config.baseURL = "/app"
}
|
6db3ce8f
wuxw
优化 post 显示动画
|
51
52
53
54
55
|
// GET请求不显示加载动画,其他请求显示
if (config.method !== 'get' && config.showLoading !== false) {
showLoading()
}
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
56
57
58
59
|
return config
},
error => {
console.log(error)
|
6db3ce8f
wuxw
优化 post 显示动画
|
60
61
|
// 请求错误时也要隐藏加载动画
hideLoading()
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
62
63
64
65
66
67
68
|
return Promise.reject(error)
}
)
// 响应拦截器
service.interceptors.response.use(
response => {
|
6db3ce8f
wuxw
优化 post 显示动画
|
69
70
71
|
// 隐藏加载动画
hideLoading()
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
72
73
74
75
76
77
78
79
|
const res = response.data
if (res.code && res.code != 0) {
Message({
message: res.msg || 'Error',
type: 'error',
duration: 5 * 1000
})
|
6db3ce8f
wuxw
优化 post 显示动画
|
80
|
return Promise.reject()
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
81
82
83
84
85
86
|
} else {
return response
}
},
error => {
console.log(error)
|
6db3ce8f
wuxw
优化 post 显示动画
|
87
88
89
90
|
// 隐藏加载动画
hideLoading()
|
5e59e7c3
wuxw
支持回话过期自动登录
|
91
92
93
94
95
96
|
// 判断是否为401未授权错误
if (error.response && error.response.status === 401) {
// 清除本地存储的token
localStorage.removeItem('token')
// 跳转到登录页面
window.location.href = '/#/views/user/login'
|
6db3ce8f
wuxw
优化 post 显示动画
|
97
|
|
468b84d2
wuxw
searchOwner 删除多余的
|
98
99
100
101
102
|
// Message({
// message: '登录已过期,请重新登录',
// type: 'error',
// duration: 5 * 1000
// })
|
5e59e7c3
wuxw
支持回话过期自动登录
|
103
|
}
|
6db3ce8f
wuxw
优化 post 显示动画
|
104
|
|
a42b3256
wuxw
HC小区管理系统前段vue版正在开发中
|
105
106
107
108
109
|
return Promise.reject(error)
}
)
export default service
|