提交 | 用户 | age
|
3ac5f2
|
1 |
/** |
J |
2 |
* http事件委托 |
|
3 |
* |
|
4 |
* udData 说明 |
|
5 |
* udData.noloading 不需要请求 |
|
6 |
* udData.fullData 使用 reponse 返回的对象进行 success 回调 (jun_http.js) |
|
7 |
* udData.nodomain 仅限于web端使用,不适用 baseUrl 前置(即当前网址根路径) (jun_http.js) |
|
8 |
* udData.nokey 不需要mpToken参数 |
|
9 |
*/ |
|
10 |
|
|
11 |
import Login from './jun_login' |
|
12 |
// import { MessageBox, Message, Loading } from 'element-ui' |
|
13 |
import fn from './fn' |
|
14 |
import resStatusCode from './jun_httpStatus' |
|
15 |
// 过滤 html |
|
16 |
function filterHtml (str) { |
|
17 |
if (typeof str !== 'string') { |
|
18 |
return str |
|
19 |
} |
|
20 |
return str.replace(/\n|\t|\s/g, '').replace(/<script\s?.+><\/script>/g, '').replace(/<[^>]+>/g, '') |
|
21 |
} |
|
22 |
|
|
23 |
/** |
|
24 |
* 使用 element-ui loading service |
|
25 |
*/ |
|
26 |
// let loadingInstance // 定义loading变量 |
|
27 |
// // 开始loading |
|
28 |
// function startLoading(){ |
|
29 |
// loadingInstance = Loading.service({ |
|
30 |
// lock: true, |
|
31 |
// text: '加载中...', |
|
32 |
// background: 'rgba(255,255,255,0.7)', |
|
33 |
// fullscreen: true, |
|
34 |
// }) |
|
35 |
// } |
|
36 |
// // 结束loading |
|
37 |
// function endLoading(){ |
|
38 |
// loadingInstance && loadingInstance.close() |
|
39 |
// } |
|
40 |
|
|
41 |
// 开始loading |
|
42 |
function startLoading(request_option){ |
|
43 |
// |
|
44 |
// console.log(request_option.udData) |
|
45 |
// request_option.udData && typeof request_option.udData.loading === 'function' && request_option.udData.loading() |
|
46 |
typeof window.appLoading === 'function' && window.appLoading() |
|
47 |
} |
|
48 |
|
|
49 |
// 结束loading |
|
50 |
function endLoading(request_option){ |
|
51 |
// |
|
52 |
// request_option.udData && typeof request_option.udData.hideLoading === 'function' && request_option.udData.hideLoading() |
|
53 |
typeof window.appHideLoading === 'function' && window.appHideLoading() |
|
54 |
} |
|
55 |
|
|
56 |
// 请求前处理参数 - get |
|
57 |
function getChangeRequestOption (get_option) { |
|
58 |
if (!get_option.udData || !get_option.udData.nokey) { |
|
59 |
if (!get_option.params) { |
|
60 |
get_option.params = {} |
|
61 |
} |
|
62 |
// 补充参数 |
|
63 |
// 需要 we_session |
|
64 |
var we_session = fn.getLocalStorage('we_session') |
|
65 |
if (we_session && we_session.we_session) { |
|
66 |
get_option.params.key = we_session.we_session |
|
67 |
get_option.params.mpToken = we_session.we_session |
|
68 |
} |
|
69 |
} |
|
70 |
return get_option |
|
71 |
} |
|
72 |
// 请求前处理参数 - post |
|
73 |
function postChangeRequestOption (post_option) { |
|
74 |
if (!post_option.udData || !post_option.udData.nokey) { |
|
75 |
if (!post_option.data) { |
|
76 |
post_option.data = {} |
|
77 |
} |
|
78 |
// 补充参数 |
|
79 |
// 需要 we_session |
|
80 |
var we_session = fn.getLocalStorage('we_session') |
|
81 |
if (we_session && we_session.we_session) { |
|
82 |
post_option.data.key = we_session.we_session |
|
83 |
post_option.data.mpToken = we_session.we_session |
|
84 |
} |
|
85 |
} |
|
86 |
return post_option |
|
87 |
} |
|
88 |
|
|
89 |
var httpEventCode = { |
|
90 |
code200 (data) { |
|
91 |
// console.log(data, 200) |
|
92 |
}, |
|
93 |
code404 (data, url) { |
|
94 |
// element-ui |
|
95 |
// Message.error('无法访问接口,状态404:' + url) |
|
96 |
}, |
|
97 |
code500 (data, url) { |
|
98 |
// element-ui |
|
99 |
// Message.error('请求失败,状态500:' + url) |
|
100 |
} |
|
101 |
} |
|
102 |
|
|
103 |
var g_login_counter = 0 // 登录请求次数 |
|
104 |
const g_LOGIN_MAX = 10 // 最大请求次数 |
|
105 |
|
|
106 |
var g_flag_loading = false // 是否已经loading |
|
107 |
var g_flag_login_requested = false // 是否正在请求登录 |
|
108 |
var g_login_result = null // 登录返回数据 |
|
109 |
var g_flag_config_requested = false // 其余配置请求 |
|
110 |
var g_config_result = null // 配置请求返回 |
|
111 |
// 请求前 |
|
112 |
function beforeRequest (res) { |
|
113 |
// 开启loading |
|
114 |
if (!g_flag_loading && (!res.request_option.udData || !res.request_option.udData.noLoading)) { |
|
115 |
res.http_option.debug && console.log('jun_httpEvent beforeRequest loading') |
|
116 |
// wx.showLoading({ |
|
117 |
// title: '加载中', |
|
118 |
// mask: true |
|
119 |
// }) |
|
120 |
// loading |
|
121 |
startLoading(res.request_option) |
|
122 |
g_flag_loading = true |
|
123 |
} |
|
124 |
|
|
125 |
// 重置登录请求标记 |
|
126 |
var we_session = fn.getLocalStorage('we_session') |
|
127 |
if (we_session && we_session.we_session) { |
|
128 |
g_flag_login_requested = false |
|
129 |
} |
|
130 |
} |
|
131 |
|
|
132 |
// 请求后 |
|
133 |
function afterRequest (res) { |
|
134 |
// console.log('请求后') |
|
135 |
} |
|
136 |
|
|
137 |
// 处理返回数据 |
|
138 |
// @return {Object} 处理过的数据 |
|
139 |
function successChangeData (res) { |
|
140 |
return res |
|
141 |
} |
|
142 |
|
|
143 |
// 批量请求完成 |
|
144 |
function afterMultiRequests (request_option) { |
|
145 |
// console.log("多个请求结束之后") |
|
146 |
// 关闭loading |
|
147 |
if (g_flag_loading) { |
|
148 |
// wx.hideLoading() |
|
149 |
endLoading(request_option) |
|
150 |
g_flag_loading = false |
|
151 |
} |
|
152 |
} |
|
153 |
|
|
154 |
function updateKey (res, key) { |
|
155 |
// 更新 key 值 |
|
156 |
if (res.request_option.method === 'GET' && (!res.request_option.udData || !res.request_option.udData.nokey)) { |
|
157 |
res.request_option.url = res.request_option.url.replace(/mpToken=[^&]*/g, `mpToken=${key}`) |
|
158 |
} |
|
159 |
if (res.request_option.method === 'POST' && (!res.request_option.udData || !res.request_option.udData.nokey)) { |
|
160 |
res.request_option.data.mpToken = key |
|
161 |
} |
|
162 |
return res |
|
163 |
} |
|
164 |
|
|
165 |
// 模拟数据处理流程 |
|
166 |
function mockFlow (res) { |
|
167 |
return new Promise(async (resolve, reject) => { |
|
168 |
// 打印请求信息 |
|
169 |
res.http_option.debug && console.log('模拟请求', res.request_option) |
|
170 |
|
|
171 |
var mockData = res.request_option.mockData || {code: 100, data: {}, message: 'success'} |
|
172 |
|
|
173 |
// 打印返回信息 |
|
174 |
res.http_option.debug && console.log('模拟返回', {data: mockData}) |
|
175 |
res.http_option.debug && console.log('开始模拟等待800ms') |
|
176 |
var timer = setTimeout(()=>{ |
|
177 |
clearTimeout(timer) |
|
178 |
// 关闭loading |
|
179 |
if (g_flag_loading) { |
|
180 |
// wx.hideLoading() |
|
181 |
endLoading(res.request_option) |
|
182 |
g_flag_loading = false |
|
183 |
} |
|
184 |
res.http_option.debug && console.log('结束模拟等待800ms') |
|
185 |
if (res.request_option.udData && res.request_option.fullData) { |
|
186 |
resolve({data: mockData}) |
|
187 |
} else { |
|
188 |
resolve(mockData) |
|
189 |
} |
|
190 |
}, 800) |
|
191 |
}) |
|
192 |
} |
|
193 |
|
|
194 |
// 请求前处理流程 |
|
195 |
function beforeFlow (res) { |
|
196 |
return new Promise(async (resolve, reject) => { |
|
197 |
// 预留请求前处理 |
|
198 |
// 设置为true,下次处理跳过 beforeFlow,避免死循环 |
|
199 |
res.request_option.skip_before_flow = true |
|
200 |
// 再次请求 |
|
201 |
resolve(res.Request(res.request_option)) |
|
202 |
}) |
|
203 |
} |
|
204 |
|
|
205 |
// 预留其余处理 |
|
206 |
function configRequest(){ |
|
207 |
return new Promise((resolve, reject) => { |
|
208 |
resolve({}) |
|
209 |
}) |
|
210 |
} |
|
211 |
|
|
212 |
// 登录处理 |
|
213 |
function appLogin (option={}) { |
|
214 |
return new Promise((resolve, reject) => { |
|
215 |
// Login.checkLogin({ |
|
216 |
// force: !!option.forceLogin, |
|
217 |
// callback: (key)=>{ |
|
218 |
// resolve(key) |
|
219 |
// } |
|
220 |
// }) |
|
221 |
Login.toLongUrl() |
|
222 |
}) |
|
223 |
} |
|
224 |
|
|
225 |
// 请求后处理 |
|
226 |
function afterFlow (res) { |
|
227 |
return new Promise((resolve, reject)=>{ |
|
228 |
var data = res.res.data |
|
229 |
|
|
230 |
// 登录超时 |
|
231 |
if ( |
|
232 |
(data && data.res && data.res.status == 2) |
|
233 |
|| (data && data.code == 603) |
|
234 |
) { |
|
235 |
res.http_option.debug && console.log('登录超时,需要重新登录', res.res) |
|
236 |
// 登录超时,需要重新登录 |
|
237 |
// 清空we_session |
|
238 |
// getApp().globalData.we_session = null |
|
239 |
fn.removeLocalStorage('we_session') |
|
240 |
resolve(retryLogin(res)) |
|
241 |
return |
|
242 |
} |
|
243 |
|
|
244 |
// status不为0 |
|
245 |
if ( |
|
246 |
(data && data.res && data.res.status != 0) |
|
247 |
|| (data && data.code != 100) |
|
248 |
) { |
|
249 |
if (data.res) { |
|
250 |
console.error('status不为0:' + (res.res.errMsg || ''), res.res) |
|
251 |
// 弹出提示 |
|
252 |
// wx.showModal({ |
|
253 |
// title: '请求提示', |
|
254 |
// content: data.res.errMsg || `请求有误,status=${data.res.status}`, |
|
255 |
// confirmText: '确定', |
|
256 |
// confirmColor: '#576B95', |
|
257 |
// showCancel: false |
|
258 |
// }) |
|
259 |
|
|
260 |
// element-ui |
|
261 |
// Message.error(data.res.errMsg || `请求有误,status=${data.res.status}`) |
|
262 |
|
|
263 |
// 根据status处理 |
|
264 |
if (typeof resStatusCode['status' + data.res.status] === 'function') { |
|
265 |
resStatusCode['status' + data.res.status](data) |
|
266 |
} |
|
267 |
} |
|
268 |
|
|
269 |
if (typeof data.code !== 'undefined') { |
|
270 |
console.error('code不为100:' + data.code, data.msg, res.res) |
|
271 |
} |
|
272 |
|
|
273 |
reject(res.res) |
|
274 |
return |
|
275 |
} |
|
276 |
|
|
277 |
|
|
278 |
// status为空 |
|
279 |
if (data && !data.res && typeof data.code === 'undefined') { |
|
280 |
console.error('status为空', res.res) |
|
281 |
let tips = '' |
|
282 |
if (res.res.statusCode=='500') { |
|
283 |
tips = '当前网络状态不佳,请稍后再试:statusCode=500;' |
|
284 |
} else if (res.res.statusCode=='200') { |
|
285 |
if (typeof res.data === 'string') { |
|
286 |
tips = filterHtml(res.data) |
|
287 |
} |
|
288 |
if (typeof res.data === 'object') { |
|
289 |
tips = filterHtml(JSON.stringify(res.data)) |
|
290 |
} |
|
291 |
tips = '网络状况不佳,点击确定重试:statusCode=200;' + tips |
|
292 |
} else { |
|
293 |
tips = `连接服务器失败,点击确定重试:statusCode=${res.res.statusCode};` |
|
294 |
} |
|
295 |
// 请求提示 |
|
296 |
|
|
297 |
// 小程序 |
|
298 |
// wx.showModal({ |
|
299 |
// title: '请求提示', |
|
300 |
// content: tips, |
|
301 |
// confirmText: '确定', |
|
302 |
// confirmColor: '#576B95', |
|
303 |
// showCancel: false, |
|
304 |
// success (res) { |
|
305 |
// if (res.confirm) { |
|
306 |
// // 点击确认后重新请求 |
|
307 |
// resolve(res.Request(res.request_option)) |
|
308 |
// } |
|
309 |
// } |
|
310 |
// }) |
|
311 |
|
|
312 |
// element-ui |
|
313 |
// MessageBox.alert(tips, '网络提示', { |
|
314 |
// confirmButtonText: '确定', |
|
315 |
// type: 'error' |
|
316 |
// }).then(()=>{ |
|
317 |
// resolve(res.Request(res.request_option)) |
|
318 |
// }).catch(()=>{ |
|
319 |
// // 点击取消 |
|
320 |
// }) |
|
321 |
return |
|
322 |
} |
|
323 |
|
|
324 |
// 打印返回信息 |
|
325 |
res.http_option.debug && console.log('返回', res.res) |
|
326 |
if (res.request_option.udData) { |
|
327 |
// 返回全部数据 |
|
328 |
if (res.request_option.udData.fullData === true) { |
|
329 |
resolve(res.res) |
|
330 |
} |
|
331 |
} |
|
332 |
g_login_counter && (g_login_counter = 0) |
|
333 |
resolve(data) |
|
334 |
}) |
|
335 |
} |
|
336 |
|
|
337 |
// 重新登录 |
|
338 |
async function retryLogin (res) { |
|
339 |
// 次数+1 |
|
340 |
g_login_counter++ |
|
341 |
return new Promise(async (resolve, reject) => { |
|
342 |
// 等待登录 |
|
343 |
var login_result = await appLogin({forceLogin: true}) |
|
344 |
// 登录失败 |
|
345 |
if (!login_result && g_login_counter < g_LOGIN_MAX) { |
|
346 |
// 2秒后重试 |
|
347 |
var timer = setTimeout(()=>{ |
|
348 |
clearTimeout(timer) |
|
349 |
resolve(retryLogin(res)) |
|
350 |
}, 2000) |
|
351 |
} |
|
352 |
// 登录成功 |
|
353 |
if (login_result) { |
|
354 |
// 更新key值 |
|
355 |
res = updateKey(res, login_result) |
|
356 |
// 再次请求 |
|
357 |
resolve(res.Request(res.request_option)) |
|
358 |
} |
|
359 |
}) |
|
360 |
} |
|
361 |
|
|
362 |
export default { |
|
363 |
httpEventCode, |
|
364 |
|
|
365 |
getChangeRequestOption, |
|
366 |
postChangeRequestOption, |
|
367 |
successChangeData, |
|
368 |
|
|
369 |
beforeRequest, |
|
370 |
afterRequest, |
|
371 |
afterMultiRequests, |
|
372 |
|
|
373 |
mockFlow, |
|
374 |
beforeFlow, |
|
375 |
afterFlow, |
|
376 |
} |