index.js
1.77 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
import axios from 'axios';
import qs from 'qs';
import Util from "../tool";
/**
* 封装axios的通用请求
* @param {string} method get\post\put\delete
* @param {string} url 请求的接口URL
* @param {object} param 传的参数,没有则传空对象
*/
const envs = process.env.NODE_ENV;
export let NODE_ENVS = "/";
export let NODE_ENVS_MSG = "/";//消息系统
switch (envs) {
case 'development':
//开发环境
NODE_ENVS = 'http://cloud.liexindev.net';
// NODE_ENVS = 'http://cloud.hd.liexinlocal.com';
NODE_ENVS_MSG = 'http://192.168.1.252:16543';
break;
case 'test':
//测试环境
NODE_ENVS = 'http://cloud.liexindev.net';
NODE_ENVS_MSG = 'http://192.168.1.252:16543';
break;
case 'production':
//线上环境
NODE_ENVS = '//cloud.ichunt.com';
NODE_ENVS_MSG = 'https://msg.ichunt.net';
break;
}
export const http = (method, url, param, loading, responseType) => {
//axios.defaults.withCredentials = true; // 携带cookie
//loading 默认加载loading 传1 不加载
param = param && typeof param === 'object' ? param : {};
let token = Util.getCookie('token') || '';
const config = {
url: `${NODE_ENVS}${url}`,
method: method,
transformRequest: [function (param) {
return qs.stringify(param);
}],
loading: (loading ? false : true)
};
//param.token=token
if (url != "/auth/cp" && url != "/auth/login") {
config.headers = {
'Authorization': 'Bearer ' + token
}
}
// post请求时需要设定Content-Type
if (method == 'post' || method == 'POST') {
config.data = param;
} else if (method === 'get' || method == 'GET') {
config.params = param;
}
if ( responseType ){
config.responseType = responseType;
}
return axios(config);
}