fetch-api.js
3.07 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
import axios from 'axios';
import { Message, MessageBox } from 'element-ui'
import store from '../store'
import { getToken } from '@/utils/auth'
// import {
// Toast
// } from 'vant';
function Toast(msg) {
console.log("msg:", msg);
}
// axios的默认url
// axios.defaults.baseURL = ""
// 服务器地址
let requestDomain = "https://ow.go.qudone.com";
if (location.href.indexOf("://k.wxpai.cn") > 0 || location.href.indexOf("://h5.k.wxpai.cn") > 0) {
requestDomain = "https://api.k.wxpai.cn/bizproxy";
}
/**
* header不加session的白名单
*/
let headerWhiteList = [
"kdapi/file/upload"
]
// request拦截器
/*axios.interceptors.request.use(
config => {
let addSession = true;
for (let index = 0; index < headerWhiteList.length; index++) {
if (config.url.indexOf(headerWhiteList[index]) > -1) {
addSession = false;
break;
}
}
if (addSession) {
//config.headers['adminSessionId'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
}
return config
},
error => {
// Do something with request error
console.log(error) // for debug
Promise.reject(error)
}
)*/
// 响应拦截器
axios.interceptors.response.use(
response => {
let res = response.data;
console.log(res);
if (response.data.code === 200) {
return Promise.resolve(response.data);
} else {
if (res.code == 404) {
store.dispatch('FedLogOut').then(() => {
location.reload() // 为了重新实例化vue-router对象 避免bug
})
} else {
if (res.errMsg) {
Message({
message: res.errMsg,
type: 'error',
duration: 5 * 1000
})
}
return Promise.resolve(response.data);
}
}
},
error => {
return Promise.reject(error);
}
);
//formDataHeaders设置
let formDataHeaders = {
headers: {
"Content-Type": "multipart/form-data",
}
}
/**
* 封装post方法
* @param {*} params
* data数据是 formdata格式
* 例如:
* this.file = file
let data = new FormData() //使用formData对象
data.append('path', '/pro/mzczcradmin/')
data.append('file', file.file)
*/
export const formdata = (url, data) => {
return axios.post(`${requestDomain}${url}`, data, formDataHeaders).then(res => res);
}
export const request = {
post(url, data) {
return axios.post(`${requestDomain}${url}`, data);
},
get(url, data) {
return axios.get(`${requestDomain}${url}`, { params: data });
},
form(url, params) {
let formData = new FormData(); //使用formData对象
for (let key in params) {
formData.append(key, params[key]);
}
let requestUrl = url.indexOf("://") >= 0 ? `${url}` : `${requestDomain}${url}`;
return axios.post(requestUrl, formData, formDataHeaders)
},
build(url, params){
let fullUrl = `${requestDomain}${url}`;
let split = "";
for(let key in params){
if(split){
split = "&";
} else {
split = "?"
}
fullUrl += split + key +"="+params[key];
}
return fullUrl;
},
/*test*/
};