2018-12-19 19:26:38 +08:00
|
|
|
|
import config from '../store/config'
|
|
|
|
|
|
import requestHandler from './requestHandler'
|
2020-03-27 17:26:25 +08:00
|
|
|
|
import errorHandler from './errorHandler'
|
2018-12-19 19:26:38 +08:00
|
|
|
|
import cacheManager from './cacheManager'
|
|
|
|
|
|
import durationReporter from './durationReporter'
|
2019-01-11 22:27:54 +08:00
|
|
|
|
import sessionManager from './sessionManager'
|
2019-01-04 18:41:46 +08:00
|
|
|
|
import { IRequestOption, IUploadFileOption } from "../interface";
|
2020-03-27 17:26:25 +08:00
|
|
|
|
import jsonSuperset from '../util/jsonSuperset'
|
2018-12-19 19:26:38 +08:00
|
|
|
|
|
2020-03-27 17:26:25 +08:00
|
|
|
|
function responseForRequest(
|
|
|
|
|
|
res: wx.RequestSuccessCallbackResult,
|
|
|
|
|
|
obj: IRequestOption
|
2019-01-04 18:41:46 +08:00
|
|
|
|
): any {
|
2018-12-19 19:26:38 +08:00
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
|
|
2020-03-27 17:26:25 +08:00
|
|
|
|
durationReporter.end(obj);
|
2021-01-05 10:39:53 +08:00
|
|
|
|
|
2020-03-27 17:26:25 +08:00
|
|
|
|
// 请求格式为json,但返回了string,说明内容中可能存在导致使得JavaScript异常的字符
|
|
|
|
|
|
if (obj.dataType === 'json' && typeof res.data === 'string') {
|
|
|
|
|
|
res.data = jsonSuperset(res.data);
|
2018-12-19 19:26:38 +08:00
|
|
|
|
try {
|
|
|
|
|
|
res.data = JSON.parse(res.data);
|
|
|
|
|
|
} catch (e) {
|
2020-03-27 17:26:25 +08:00
|
|
|
|
if(obj.catchError) {
|
|
|
|
|
|
throw new Error(e);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
errorHandler.logicError(obj, res);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2019-03-12 18:08:47 +08:00
|
|
|
|
if (config.loginTrigger!(res.data) && obj.reLoginCount !== undefined && obj.reLoginCount < config.reLoginLimit!) {
|
2018-12-19 19:26:38 +08:00
|
|
|
|
// 登录态失效,且重试次数不超过配置
|
2019-01-11 22:27:54 +08:00
|
|
|
|
sessionManager.delSession();
|
2020-03-27 17:26:25 +08:00
|
|
|
|
return requestHandler.request(obj);
|
2019-01-04 18:41:46 +08:00
|
|
|
|
} else if (config.successTrigger(res.data)) {
|
2018-12-19 19:26:38 +08:00
|
|
|
|
// 接口返回成功码
|
2019-01-04 18:41:46 +08:00
|
|
|
|
let realData: string | IAnyObject | ArrayBuffer = "";
|
2018-12-19 19:26:38 +08:00
|
|
|
|
try {
|
2019-04-04 16:32:51 +08:00
|
|
|
|
if (typeof config.successData === 'function') {
|
|
|
|
|
|
realData = config.successData(res.data);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
realData = res.data;
|
|
|
|
|
|
}
|
2021-01-05 10:39:53 +08:00
|
|
|
|
} catch (e) {}
|
2020-03-27 17:26:25 +08:00
|
|
|
|
if (!obj.noCacheFlash) {
|
2018-12-19 19:26:38 +08:00
|
|
|
|
// 如果为了保证页面不闪烁,则不回调,只是缓存最新数据,待下次进入再用
|
2019-07-04 17:47:25 +08:00
|
|
|
|
if (typeof obj.success === "function") {
|
2019-01-07 19:32:53 +08:00
|
|
|
|
obj.success(realData);
|
2019-01-24 10:26:51 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
return realData;
|
2019-01-07 19:32:53 +08:00
|
|
|
|
}
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 缓存存储
|
|
|
|
|
|
cacheManager.set(obj, realData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 接口返回失败码
|
2019-07-04 17:47:25 +08:00
|
|
|
|
throw { type: 'logic-error', res }
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2019-01-24 10:26:51 +08:00
|
|
|
|
// https返回状态码非200
|
2019-07-04 17:47:25 +08:00
|
|
|
|
throw { type: 'http-error', res }
|
2018-12-19 19:26:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 17:26:25 +08:00
|
|
|
|
function responseForUploadFile(
|
|
|
|
|
|
res: wx.UploadFileSuccessCallbackResult,
|
|
|
|
|
|
obj: IUploadFileOption
|
|
|
|
|
|
): any {
|
|
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
|
|
|
|
|
|
|
|
durationReporter.end(obj);
|
|
|
|
|
|
|
|
|
|
|
|
// 内容中可能存在导致使得JavaScript异常的字符
|
|
|
|
|
|
if (typeof res.data === 'string') {
|
|
|
|
|
|
res.data = jsonSuperset(res.data);
|
|
|
|
|
|
try {
|
|
|
|
|
|
res.data = JSON.parse(res.data);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
if(obj.catchError) {
|
|
|
|
|
|
throw new Error(e);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
errorHandler.logicError(obj, res);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (config.loginTrigger!(res.data) && obj.reLoginCount !== undefined && obj.reLoginCount < config.reLoginLimit!) {
|
|
|
|
|
|
// 登录态失效,且重试次数不超过配置
|
|
|
|
|
|
sessionManager.delSession();
|
|
|
|
|
|
return requestHandler.uploadFile(obj);
|
|
|
|
|
|
} else if (config.successTrigger(res.data)) {
|
|
|
|
|
|
// 接口返回成功码
|
|
|
|
|
|
let realData: string | IAnyObject | ArrayBuffer = "";
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (typeof config.successData === 'function') {
|
|
|
|
|
|
realData = config.successData(res.data);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
realData = res.data;
|
|
|
|
|
|
}
|
2021-01-05 10:39:53 +08:00
|
|
|
|
} catch (e) {}
|
2020-03-27 17:26:25 +08:00
|
|
|
|
|
|
|
|
|
|
if (typeof obj.success === "function") {
|
|
|
|
|
|
obj.success(realData);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return realData;
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 接口返回失败码
|
|
|
|
|
|
throw { type: 'logic-error', res }
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// https返回状态码非200
|
|
|
|
|
|
throw { type: 'http-error', res }
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
responseForRequest,
|
|
|
|
|
|
responseForUploadFile
|
|
|
|
|
|
};
|