Files
weRequest/src/module/responseHandler.ts

124 lines
4.1 KiB
TypeScript
Raw Normal View History

2018-12-19 19:26:38 +08:00
import config from '../store/config'
import requestHandler from './requestHandler'
import errorHandler from './errorHandler'
2018-12-19 19:26:38 +08:00
import cacheManager from './cacheManager'
import durationReporter from './durationReporter'
import sessionManager from './sessionManager'
2019-01-04 18:41:46 +08:00
import { IRequestOption, IUploadFileOption } from "../interface";
import jsonSuperset from '../util/jsonSuperset'
2018-12-19 19:26:38 +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) {
durationReporter.end(obj);
// 请求格式为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) {
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
// 登录态失效,且重试次数不超过配置
sessionManager.delSession();
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;
}
} catch (e) {}
if (!obj.noCacheFlash) {
2018-12-19 19:26:38 +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 {
// 接口返回失败码
throw { type: 'logic-error', res }
2018-12-19 19:26:38 +08:00
}
} else {
2019-01-24 10:26:51 +08:00
// https返回状态码非200
throw { type: 'http-error', res }
2018-12-19 19:26:38 +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;
}
} catch (e) {}
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
};