http.js
4.82 KB
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
import axios from 'axios'
import qs from 'qs'
import * as Cookies from 'js-cookie'
import router from '@/router'
import Vue from 'vue'
import {
loading,
Message
} from 'element-ui'
import { param2 } from '@/utils'
import md5 from 'blueimp-md5'
// 创建 axios 的一个实例
const Axios = axios.create({
baseURL: window._vionConfig.apiUrl, // 因为我本地做了反向代理
timeout: 0,
// responseType: "json",
// changeOrigin: true,//允许跨域
withCredentials: true, // 是否允许带cookie这些
headers: {
"Content-Type": "application/json;charset=UTF-8"
// 'Content-Type': 'application/x-www-form-urlencoded'
}
})
window.axios = Axios;
var loadingStatus;
let store = {
source: {
token: null,
cancel: null
}
}
// 请求拦截器
Axios.interceptors.request.use(
config => {
const suffix = '4c413628731691abc99eb2fca5f69aab'
const { method, params, data, url } = config
let language = localStorage.getItem('lang'),
aToken = Cookies.get('atoken');
if (language) {
let languageObj = {
'mall_CN': 'zh-CN,zh;q=0.9',
'zh_CN': 'zh-CN,zh;q=0.9',
'en_US': 'en-US,en;q=0.5',
'zh_TW': 'zh-TW,zh;q=0.3',
// 'zh_ar': 'zh-AR,zh;q=0.9'
}
languageObj[language] && (config.headers['Accept-Language'] = languageObj[language]);
}
aToken && (config.headers.Authorization = aToken);
config.cancelToken = store.source.token
const signalStr = method.toUpperCase() +
(params ? param2(params) : '') +
(data ? JSON.stringify(data) : '') +
suffix + (config.headers.Authorization ? config.headers.Authorization : '');
const hashSignature = md5(signalStr)
config.headers['signature'] = hashSignature;
let pathList = router.app.$route.path.split('/');
let index = pathList.length - 1,
path = pathList[index];
let ignoreList = ['daily', 'weekly', 'monthly', 'yearly'];
if (ignoreList.includes(path)) {
path = pathList[index - 1];
}
if (pathList.length > 2) {
path = pathList[2];
}
if (!path && router.app.$route.name == 'login') {
path = 'login';
}
config.headers.path = path
return config;
}, error => {
Message({
showClose: true,
message: error,
type: 'error.data.error.message'
})
return Promise.reject(error.data.msg)
}
)
// 跳转路由打断请求
router.beforeEach((to, from, next) => {
if (!to.matched.some(item => item.path === from.fullPath)) {
let CancelToken = axios.CancelToken;
store.source.cancel && store.source.cancel();
store.source = CancelToken.source();
}
next()
})
// 响应拦截器
Axios.interceptors.response.use(
res => {
if (res.data && !res.data.success) {
if (res.data.ecode && res.data.ecode != 200) {
Vue.prototype.removeStorge();
let path = painter.$route.path;
router.push({
path: '/?path=' + path
})
Message({
showClose: true,
message: res.data.enote,
type: 'error'
})
return
} else {
// 非标准返回体
if (res.status == 200) {
return res;
}
}
if (res.data.code == 500) {
Message({
showClose: true,
message: res.data.msg,
type: 'error'
})
}
return Promise.reject(res.data.msg);
} else if (!res.data && res.status == 200) {
return res;
} else if (res.data.success && res.config.method != 'get') {
//
}
return res;
}
// , error => {
// return Promise.reject(error)
// }
)
export default function(method, url, data = null, config = null, bodyDelete) {
method = method.toLowerCase()
if (method == 'post') {
return Axios.post(url, data, config)
} else if (method == 'get') {
let defaultParam = {
_t: Date.parse(new Date()) / 1000
}
return Axios.get(url, { params: {...defaultParam, ...data }, config })
} else if (bodyDelete) {
return Axios.delete(url, { data: data }, config)
} else if (method == 'delete') {
return Axios.delete(url, { params: data }, config)
} else if (method == 'put') {
return Axios.put(url, data, config)
} else {
console.error('unknown method' + method)
return false
}
}