index.js
6.1 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
* @Author: 张一一
* @Date: 2020-3-6 17:29:44
* @Last Modified by: 张一一
* @Last Modified time: 2020-3-6 17:29:44
*/
import qs from 'qs';
import config from '@/lib/config'
import {
getPlatform // 获取平台信息
} from '@/lib/utils/index'
/**
* 通用uni-app网络请求
* 基于 Promise 对象实现更简单的 request 使用方式,支持请求和响应拦截
*/
export class Http {
constructor() {
this.config = {
baseUrl: config.api, // 请求域名
header: {
// multipart/form-data
// 'Content-Type': 'multipart/form-data',
// application/x-www-form-urlencoded
'Content-Type': 'application/x-www-form-urlencoded',
},
data: {},
method: 'POST',
// dataType: 'json' /* 如设为json,会对返回的数据做一次 JSON.parse */,
// responseType: 'text',
success() {},
fail() {},
complete() {}
}
this.interceptor = {
before: null,
after: null
}
this.instance = null
}
static getInstance() {
if (!this.instance) {
this.instance = new Http()
}
return this.instance
}
request(options) {
let that = this
if (!options) {
options = {}
}
options.baseUrl = options.baseUrl ||
this.config.baseUrl
//if (process.env.NODE_ENV ===
// 'development') {
// options.url = this.addTemp(options.baseUrl +
// 'devapi/' + options.url) // 增加时间戳
//} else {
options.url = this.addTemp(options.baseUrl +
options.url) // 增加时间戳
//}
/* options.url = this.addPlatform(
options.url) // 增加使用平台*/
options.data = qs.stringify(options.data ||
{})
options.method = options.method ||
this.config.method
if (options.method === 'get' ||
options.method === 'GET') {
options.url = objParseUrlAndParam(
options.url, options.data)
}
// TODO 加密数据
// TODO 数据签名
return new Promise((resolve, reject) => {
let _config = null
options.complete = response => {
let statusCode = response.statusCode
response.config = _config
// 请求后的拦截器
if (this.interceptor.after) {
let newResponse = this.interceptor
.after(response)
if (newResponse) {
response = newResponse
}
}
// 统一的响应日志记录
that._reslog(response)
if (statusCode === 200) {
// 成功
resolve(response.data)
} else {
reject({code: 0, msg: '请求出错'})
}
}
_config = Object.assign({}, this.config,
options)
_config.requestId = new Date().getTime()
// 请求前的拦截器
if (this.interceptor.before) {
let newConfig = this.interceptor
.before(_config)
if (newConfig) {
_config = newConfig
} else {
return
}
}
// 统一的请求日志记录
that._reqlog(_config)
uni.request(_config)
})
}
get(options) {
if (!options) {
options = {}
}
options.method = options.method || 'GET';
return this.request(options)
}
post(options) {
if (!options) {
options = {}
}
options.method = options.method || 'POST';
return this.request(options)
}
/**
* 统一的响应日志记录
* @param {*} req
*/
_reqlog(req) {
if (process.env.NODE_ENV == 'development') {
if (req.data) {
}
}
// TODO 调接口异步写入日志数据库
}
/**
* 统一的请求日志记录
* @param {*} res
*/
_reslog(res) {
let _statusCode = res.statusCode
if (process.env.NODE_ENV == 'development') {
// console.log(`请求类型: ${res.config.method}`);
// console.log(`请求地址: ${res.config.url}`);
// console.log(`请求参数: ${res.config.data}`);
}
// TODO 除了接口服务错误外,其他日志调接口异步写入日志数据库
switch (_statusCode) {
case 200:
// console.log(`响应: ${JSON.stringify(res.data)}`);
break
case 401:
break
case 404:
console.log(`请求地址错误404`);
break
default:
break
}
}
/**
* 增加时间戳,用于防止请求缓存
* @param {String} url
* @return {String}
*/
addTemp(url) {
let new_url;
if (url.indexOf('?') === -1) {
new_url = `${url}?_now_=${new Date().getTime()}`;
} else {
new_url = url.replace('?', `?_now_=${new Date().getTime()}&`);
}
return new_url;
}
/**
* 增加使用平台信息
* @param {String} url
* @return {String}
*/
/* addPlatform(url) {
let new_url;
if (url.indexOf('?') === -1) {
new_url = `${url}?_platform_=${getPlatform()}`;
} else {
new_url = url.replace('?', `?_platform_=${getPlatform()}&`);
}
return new_url;
}*/
/**
* 增加参数到地址
* */
addParams(url, params) {
let new_url;
if (url.indexOf('?') === -1) {
new_url = `${url}?${params}`;
} else {
new_url = url.replace('?', `?${params}&`);
}
return new_url;
}
}
let _myHttp = Http.getInstance();
export default _myHttp;