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
import axios from 'axios';
import Qs from 'qs';
const JSONbig = require('json-bigint')({"storeAsString": true});
import cookie from './cookie';
import httpErrorHandler from './httpErrorHandler';
const instance = axios.create({
baseURL: '/attendance',
headers: {
post: {
'Content-Type': 'application/json;charset=UTF-8',
'dataType': 'json',
}
},
transformResponse: [data=>{
try {
JSON.parse(data);
return JSONbig.parse(data);
} catch (error) {
return data;
}
}],
});
instance.interceptors.request.use(config => {
// 获取当前页面的主机名和端口号
const hostname = location.hostname;
// const hostname = location.hostname;
const baseURL =config.baseURL;
const port = location.port;
// const port =process.env.VUE_APP_PORTAL_PORT=='undefined'?'11078':process.env.VUE_APP_PORTAL_PORT;
// 测试用
//http://192.168.0.98:11039
// 温
// http://192.168.0.116/17500
// const hostname = '192.168.0.116'
// const port = '17500'
// const hostname = '192.168.0.98'
// const port = '11039'
// 动态修改请求地址
if (config.url.startsWith('/')) {
// 字符串以 / 开头
config.url = `http://${hostname}:${port}${baseURL}${config.url}`;
} else {
config.url = `http://${hostname}:${port}${baseURL}/${config.url}`;
// 字符串不以 / 开头
}
//config.url = `http://${hostname}:${port}${baseURL}/${config.url}`;
//config.data = Qs.stringify(config.data, {arrayFormat: 'repeat', allowDots: true});
//config.data = Qs.stringify(config.data, {arrayFormat: 'indices', allowDots: true});
//brackets
// 也可以在这里给请求添加token之类的字段
// config.headers['Content-Type'] = 'application/json;charset=UTF-8'
// config.headers.timestamp = Math.floor(new Date().getTime() / 1000)
// console.log("sessionStorage",window.sessionStorage)
config.headers.Authorization = window.sessionStorage.getItem('token') || ''
//console.log("request config and session",config,window.sessionStorage);
return config;
}, err => {
return Promise.reject(err);
});
instance.interceptors.response.use(response=>{
return response.data
},httpErrorHandler);
/**
* 封装后的axios post方法
*
* @param {string} url 请求路径
* @param {object} option 请求参数
* @param {object} [config] 特殊配置项(选填)
* @returns
*/
export function post(url, option, config = {}) {
const data = Object.assign({}, option, {
// __mortals_token__: cookie.getItem('__mortals_token__'),
})
return instance.post(url, data, config)
/* //判断object是数组还是对象
if(Object.getPrototypeOf(option) === Object.prototype){
const data = Object.assign({}, option, {
// __mortals_token__: cookie.getItem('__mortals_token__'),
})
return instance.post(url, data, config)
}else{
return instance.post(url, option, config)
}*/
}
/**
* 封装后的axios get方法
*
* @param {string} url 请求路径
* @param {object} option 请求参数
* @param {object} [config] 特殊配置项(选填)
* @returns
*/
export function get(url, option, config = {}) {
const data = Object.assign({}, option, {
//__mortals_token__: cookie.getItem('__mortals_token__'),
})
return instance.get(url, { params: data }, config)
}
// 文件上传
const uploadInstance = axios.create({
baseURL: '/m',
headers: {
post: {
'Content-Type': 'multipart/form-data',
}
}
});
uploadInstance.interceptors.response.use(response=>{
return response.data
},httpErrorHandler);
/**
* 封装后的axios upload方法
*
* @param {string} url 请求路径
* @param {object} option 请求参数
* @param {object} [config] 特殊配置项(选填)
* @returns
*/
export function upload(url, option, config = {}) {
let formdata = new FormData();
Object.keys(option).forEach(key=>{
formdata.append(key, option[key])
})
return uploadInstance.post(url, formdata, config)
}