refactor: 解耦code2Session,去掉请求的isLogin标识
This commit is contained in:
2
build/module/durationReporter.d.ts
vendored
2
build/module/durationReporter.d.ts
vendored
@@ -1,7 +1,7 @@
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function start(obj: IRequestOption | IUploadFileOption): void;
|
||||
declare function end(obj: IRequestOption | IUploadFileOption): void;
|
||||
declare function report(name: string, start: number, end: number): void;
|
||||
declare function report(name: string, startTime: number, endTime: number): void;
|
||||
declare const _default: {
|
||||
start: typeof start;
|
||||
end: typeof end;
|
||||
|
||||
5
build/module/errorHandler.d.ts
vendored
5
build/module/errorHandler.d.ts
vendored
@@ -1,10 +1,15 @@
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function systemError(obj: IRequestOption | IUploadFileOption, res: wx.GeneralCallbackResult): void;
|
||||
declare function logicError(obj: IRequestOption | IUploadFileOption, res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult): void;
|
||||
declare function getErrorMsg(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult): {
|
||||
title: string;
|
||||
content: string;
|
||||
};
|
||||
declare function doError(title: string, content: string): void;
|
||||
declare const _default: {
|
||||
systemError: typeof systemError;
|
||||
logicError: typeof logicError;
|
||||
doError: typeof doError;
|
||||
getErrorMsg: typeof getErrorMsg;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
2
build/module/requestHandler.d.ts
vendored
2
build/module/requestHandler.d.ts
vendored
@@ -1,7 +1,9 @@
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function format(originUrl: string): string;
|
||||
declare function request(obj: IRequestOption): void;
|
||||
declare function uploadFile(obj: IUploadFileOption): void;
|
||||
declare const _default: {
|
||||
format: typeof format;
|
||||
request: typeof request;
|
||||
uploadFile: typeof uploadFile;
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -20,7 +20,7 @@ export interface IInitOption {
|
||||
/* 请求返回时的时间戳 */
|
||||
endTime: number,
|
||||
/* 请求方法,可用于上报 */
|
||||
request?: () => void
|
||||
request: Function
|
||||
) => void);
|
||||
/* 可为接口提供mock数据 */
|
||||
mockJson?: any;
|
||||
@@ -76,7 +76,7 @@ export interface IRequestOption extends IRequestObject {
|
||||
/* 当启用缓存时,决定除了返回缓存内容外,是否还返回接口实时内容,以防止页面多次渲染的抖动 */
|
||||
noCacheFlash?: boolean;
|
||||
/* 接口调用成功的回调函数 */
|
||||
success: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
success?: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
/* 接口调用结束的回调函数(调用成功、失败都会执行) */
|
||||
complete?: ()=> void;
|
||||
/** 接口调用失败 或 逻辑失败 的回调函数 */
|
||||
@@ -90,8 +90,6 @@ export interface IRequestObject extends wx.RequestOption{
|
||||
count: number;
|
||||
/* 重登陆次数 */
|
||||
reLoginLimit: number;
|
||||
/* 该请求是否是登陆请求 */
|
||||
isLogin?: boolean;
|
||||
/* 请求发起的时间戳 */
|
||||
_reportStartTime: number;
|
||||
/* 请求返回的时间戳 */
|
||||
@@ -106,7 +104,7 @@ export interface IUploadFileOption extends IUploadFileObject {
|
||||
/* 接口请求成功后将自动执行init()中配置的reportCGI函数,其中的name字段值为这里配置的值 */
|
||||
report?: string;
|
||||
/* 接口调用成功的回调函数 */
|
||||
success: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
success?: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
/* 接口调用结束的回调函数(调用成功、失败都会执行) */
|
||||
complete?: ()=> void;
|
||||
/** 接口调用失败 或 逻辑失败 的回调函数 */
|
||||
@@ -120,8 +118,6 @@ export interface IUploadFileObject extends wx.UploadFileOption {
|
||||
count: number;
|
||||
/* 重登陆次数 */
|
||||
reLoginLimit: number;
|
||||
/* 该请求是否是登陆请求 */
|
||||
isLogin?: boolean;
|
||||
/* 请求发起的时间戳 */
|
||||
_reportStartTime: number;
|
||||
/* 请求返回的时间戳 */
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import config from '../store/config'
|
||||
import request from '../api/request'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
|
||||
function start(obj: IRequestOption | IUploadFileOption) {
|
||||
@@ -7,12 +8,14 @@ function start(obj: IRequestOption | IUploadFileOption) {
|
||||
|
||||
function end(obj: IRequestOption | IUploadFileOption) {
|
||||
obj._reportEndTime = new Date().getTime();
|
||||
if(obj.report) {
|
||||
report(obj.report as string, obj._reportStartTime, obj._reportEndTime);
|
||||
}
|
||||
}
|
||||
|
||||
function report(name: string, startTime: number, endTime: number) {
|
||||
if (typeof config.reportCGI === "function") {
|
||||
config.reportCGI(name, startTime, endTime);
|
||||
config.reportCGI(name, startTime, endTime, request);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -59,5 +59,6 @@ function doError(title: string, content: string) {
|
||||
export default {
|
||||
systemError,
|
||||
logicError,
|
||||
doError
|
||||
doError,
|
||||
getErrorMsg
|
||||
}
|
||||
|
||||
@@ -205,6 +205,7 @@ function uploadFile(obj: IUploadFileOption): void {
|
||||
}
|
||||
|
||||
export default {
|
||||
format,
|
||||
request,
|
||||
uploadFile
|
||||
}
|
||||
|
||||
@@ -25,19 +25,7 @@ function response(
|
||||
|
||||
durationReporter.end(obj);
|
||||
|
||||
if (obj.isLogin) {
|
||||
// 登录请求
|
||||
let s = "";
|
||||
try {
|
||||
s = config.codeToSession.success(res.data);
|
||||
} catch (e) {
|
||||
}
|
||||
if (s) {
|
||||
obj.success(s);
|
||||
} else {
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
} else if (config.loginTrigger!(res.data) && obj.reLoginLimit < config.reLoginLimit!) {
|
||||
if (config.loginTrigger!(res.data) && obj.reLoginLimit < config.reLoginLimit!) {
|
||||
// 登录态失效,且重试次数不超过配置
|
||||
status.session = '';
|
||||
status.sessionIsFresh = true;
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
import flow from '../util/flow'
|
||||
import status from '../store/status'
|
||||
import config from '../store/config'
|
||||
import requestHandler from './requestHandler'
|
||||
import errorHandler from './errorHandler'
|
||||
import durationReporter from './durationReporter'
|
||||
import {IRequestOption, IUploadFileOption} from "../interface";
|
||||
import requestHandler from './requestHandler'
|
||||
import {IRequestOption, IUploadFileOption} from "../interface"
|
||||
|
||||
let checkSessionPromise: any = null;
|
||||
function checkSession() {
|
||||
return new Promise((resolve)=>{
|
||||
if (!status.sessionIsFresh && status.session) {
|
||||
if(!checkSessionPromise) {
|
||||
checkSessionPromise = new Promise((resolve)=>{
|
||||
if(config.doNotCheckSession) {
|
||||
resolve();
|
||||
} else if (!status.sessionIsFresh && status.session) {
|
||||
console.log("wx.checkSession()");
|
||||
const start = new Date().getTime();
|
||||
wx.checkSession({
|
||||
@@ -23,7 +27,7 @@ function checkSession() {
|
||||
},
|
||||
complete () {
|
||||
const end = new Date().getTime();
|
||||
durationReporter.report('checkSession', start, end);
|
||||
durationReporter.report('wx_checkSession', start, end);
|
||||
}
|
||||
})
|
||||
} else {
|
||||
@@ -31,14 +35,11 @@ function checkSession() {
|
||||
}
|
||||
})
|
||||
}
|
||||
return checkSessionPromise;
|
||||
}
|
||||
|
||||
function doLogin(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
if (obj.isLogin) {
|
||||
// 登录接口,直接放过
|
||||
if(typeof callback === "function"){
|
||||
callback();
|
||||
}
|
||||
} else if (status.session) {
|
||||
if (status.session) {
|
||||
// 缓存中有session
|
||||
if (status.sessionExpireTime && new Date().getTime() > status.sessionExpire) {
|
||||
// 如果有设置本地session缓存时间,且缓存时间已到
|
||||
@@ -67,7 +68,7 @@ function getCode(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
wx.login({
|
||||
complete () {
|
||||
const end = new Date().getTime();
|
||||
durationReporter.report('login', start, end);
|
||||
durationReporter.report('wx_login', start, end);
|
||||
},
|
||||
success (res) {
|
||||
if (res.code) {
|
||||
@@ -105,13 +106,26 @@ function code2Session(code: string) {
|
||||
data[config.codeToSession.codeName!] = code;
|
||||
|
||||
return new Promise((resolve)=>{
|
||||
requestHandler.request({
|
||||
url: config.codeToSession.url,
|
||||
let start = new Date().getTime();
|
||||
wx.request({
|
||||
url: requestHandler.format(config.codeToSession.url),
|
||||
data,
|
||||
method: config.codeToSession.method || 'GET',
|
||||
isLogin: true,
|
||||
report: config.codeToSession.report || config.codeToSession.url,
|
||||
success (s: string) {
|
||||
success (res: wx.RequestSuccessCallbackResult) {
|
||||
if (res.statusCode === 200) {
|
||||
// 耗时上报
|
||||
if(config.codeToSession.report) {
|
||||
let end = new Date().getTime();
|
||||
durationReporter.report(config.codeToSession.report, start, end)
|
||||
}
|
||||
|
||||
let s = "";
|
||||
try {
|
||||
s = config.codeToSession.success(res.data);
|
||||
} catch (e) {
|
||||
}
|
||||
|
||||
if (s) {
|
||||
status.session = s;
|
||||
status.sessionIsFresh = true;
|
||||
// 如果有设置本地session过期时间
|
||||
@@ -126,11 +140,21 @@ function code2Session(code: string) {
|
||||
key: config.sessionName,
|
||||
data: status.session
|
||||
});
|
||||
} else {
|
||||
let {title, content} = errorHandler.getErrorMsg(res);
|
||||
errorHandler.doError(title, content)
|
||||
}
|
||||
} else {
|
||||
errorHandler.doError("登录失败", "请稍后重试")
|
||||
}
|
||||
return resolve();
|
||||
},
|
||||
complete () {},
|
||||
fail: config.codeToSession.fail || null
|
||||
} as IRequestOption)
|
||||
fail: ()=> {
|
||||
errorHandler.doError("登录失败", "请稍后重试");
|
||||
return resolve();
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user