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