refactor: 解决残留的TODO类型
This commit is contained in:
28
build/api/init.d.ts
vendored
28
build/api/init.d.ts
vendored
@@ -1,29 +1,3 @@
|
||||
/// <reference types="wx" />
|
||||
export interface IInitOption {
|
||||
codeToSession: ICodeToSessionOptions;
|
||||
sessionName: string;
|
||||
urlPerfix?: string | (() => string);
|
||||
doNotCheckSession?: boolean;
|
||||
reLoginLimit?: number;
|
||||
errorCallback?: null | Function;
|
||||
reportCGI?: boolean | ((name: string, startTime: number, endTime: number, request?: () => void) => void);
|
||||
mockJson?: TODO;
|
||||
globalData?: boolean | object | Function;
|
||||
sessionExpireKey: string;
|
||||
loginTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
successTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
successData?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
errorTitle?: string | ((res: string | IAnyObject | ArrayBuffer) => string);
|
||||
errorContent?: string | ((res: string | IAnyObject | ArrayBuffer) => string);
|
||||
}
|
||||
export interface ICodeToSessionOptions {
|
||||
url?: string;
|
||||
method?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT' | 'string';
|
||||
codeName?: string;
|
||||
data?: string | IAnyObject | ArrayBuffer;
|
||||
success?: Function;
|
||||
fail?: Function;
|
||||
report?: string;
|
||||
}
|
||||
import { IInitOption } from '../interface';
|
||||
declare const _default: (params: IInitOption) => void;
|
||||
export default _default;
|
||||
|
||||
6
build/api/request.d.ts
vendored
6
build/api/request.d.ts
vendored
@@ -1,7 +1,3 @@
|
||||
export interface IRequestOption extends wx.RequestOption {
|
||||
beforeSend?: Function;
|
||||
showLoading?: boolean | string;
|
||||
report?: string;
|
||||
}
|
||||
import { IRequestOption } from '../interface';
|
||||
declare const _default: (obj: IRequestOption) => void;
|
||||
export default _default;
|
||||
|
||||
2
build/api/setSession.d.ts
vendored
2
build/api/setSession.d.ts
vendored
@@ -1,2 +1,2 @@
|
||||
declare const _default: (session: any) => void;
|
||||
declare const _default: (session: string) => void;
|
||||
export default _default;
|
||||
|
||||
3
build/api/uploadFile.d.ts
vendored
3
build/api/uploadFile.d.ts
vendored
@@ -1,2 +1,3 @@
|
||||
declare const _default: (obj: any) => void;
|
||||
import { IUploadFileOption } from "../interface";
|
||||
declare const _default: (obj: IUploadFileOption) => void;
|
||||
export default _default;
|
||||
|
||||
23
build/index.d.ts
vendored
23
build/index.d.ts
vendored
@@ -1,24 +1,3 @@
|
||||
import { IInitOption } from "./api/init";
|
||||
import { IRequestOption } from "./api/request";
|
||||
export interface IUploadFileOption extends wx.UploadFileOption {
|
||||
beforeSend?: Function;
|
||||
showLoading?: boolean | string;
|
||||
report?: string;
|
||||
}
|
||||
export interface IGetConfigResult {
|
||||
urlPerfix?: string | (() => string);
|
||||
sessionExpireTime?: number;
|
||||
sessionExpireKey?: string;
|
||||
sessionExpire?: number;
|
||||
}
|
||||
export interface weRequest {
|
||||
init?: (obj: IInitOption) => void;
|
||||
request?: (option: IRequestOption) => void;
|
||||
uploadFile?: (option: IUploadFileOption) => void;
|
||||
getSession?: () => string;
|
||||
getConfig?: () => IGetConfigResult;
|
||||
login?: (callback: Function) => void;
|
||||
setSession?: (x: string) => void;
|
||||
}
|
||||
import { weRequest } from "./interface";
|
||||
declare const weRequestObject: weRequest;
|
||||
export default weRequestObject;
|
||||
|
||||
6
build/module/cacheManager.d.ts
vendored
6
build/module/cacheManager.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
/// <reference types="wx" />
|
||||
declare function get(obj: TODO): void;
|
||||
declare function set(obj: TODO, realData: TODO): void;
|
||||
import { IRequestOption } from "../interface";
|
||||
declare function get(obj: IRequestOption): void;
|
||||
declare function set(obj: IRequestOption, realData: string | object): void;
|
||||
declare const _default: {
|
||||
get: typeof get;
|
||||
set: typeof set;
|
||||
|
||||
8
build/module/durationReporter.d.ts
vendored
8
build/module/durationReporter.d.ts
vendored
@@ -1,8 +1,10 @@
|
||||
/// <reference types="wx" />
|
||||
declare function start(obj: TODO, name?: string): void;
|
||||
declare function end(obj: TODO, name?: string): void;
|
||||
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 const _default: {
|
||||
start: typeof start;
|
||||
end: typeof end;
|
||||
report: typeof report;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
10
build/module/errorHandler.d.ts
vendored
10
build/module/errorHandler.d.ts
vendored
@@ -1,2 +1,10 @@
|
||||
declare const _default: (obj: any, res: any) => void;
|
||||
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 doError(title: string, content: string): void;
|
||||
declare const _default: {
|
||||
systemError: typeof systemError;
|
||||
logicError: typeof logicError;
|
||||
doError: typeof doError;
|
||||
};
|
||||
export default _default;
|
||||
|
||||
4
build/module/mockManager.d.ts
vendored
4
build/module/mockManager.d.ts
vendored
@@ -1,5 +1,5 @@
|
||||
/// <reference types="wx" />
|
||||
declare function get(obj: TODO, method: "request" | "uploadFile"): any;
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function get(obj: IRequestOption | IUploadFileOption, method: "request" | "uploadFile"): any;
|
||||
declare const _default: {
|
||||
get: typeof get;
|
||||
};
|
||||
|
||||
6
build/module/requestHandler.d.ts
vendored
6
build/module/requestHandler.d.ts
vendored
@@ -1,6 +1,6 @@
|
||||
/// <reference types="wx" />
|
||||
declare function request(obj: TODO): TODO;
|
||||
declare function uploadFile(obj: TODO): TODO;
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function request(obj: IRequestOption): void;
|
||||
declare function uploadFile(obj: IUploadFileOption): void;
|
||||
declare const _default: {
|
||||
request: typeof request;
|
||||
uploadFile: typeof uploadFile;
|
||||
|
||||
4
build/module/responseHandler.d.ts
vendored
4
build/module/responseHandler.d.ts
vendored
@@ -1,3 +1,3 @@
|
||||
/// <reference types="wx" />
|
||||
declare function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult, obj: TODO, method: "request" | "uploadFile"): any;
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult, obj: IRequestOption | IUploadFileOption, method: "request" | "uploadFile"): any;
|
||||
export default response;
|
||||
|
||||
6
build/module/sessionManager.d.ts
vendored
6
build/module/sessionManager.d.ts
vendored
@@ -1,3 +1,3 @@
|
||||
/// <reference types="wx" />
|
||||
declare function checkSession(callback: Function, obj: TODO): void;
|
||||
export default checkSession;
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
declare const _default: (fn: Function, obj: IRequestOption | IUploadFileOption) => void;
|
||||
export default _default;
|
||||
|
||||
2
build/store/config.d.ts
vendored
2
build/store/config.d.ts
vendored
@@ -1,3 +1,3 @@
|
||||
import { IInitOption } from '../api/init';
|
||||
import { IInitOption } from '../interface';
|
||||
declare const defaultConfig: IInitOption;
|
||||
export default defaultConfig;
|
||||
|
||||
5
build/util/flow.d.ts
vendored
5
build/util/flow.d.ts
vendored
@@ -1,6 +1,5 @@
|
||||
/// <reference types="wx" />
|
||||
declare function emit(key: TODO): void;
|
||||
declare function wait(key: TODO, callback: Function): void;
|
||||
declare function emit(key: string): void;
|
||||
declare function wait(key: string, callback: Function): void;
|
||||
declare const _default: {
|
||||
wait: typeof wait;
|
||||
emit: typeof emit;
|
||||
|
||||
2
build/util/url.d.ts
vendored
2
build/util/url.d.ts
vendored
@@ -1,4 +1,4 @@
|
||||
declare function setParams(url: string, params: object): string;
|
||||
declare function setParams(url: string | undefined, params: object): string;
|
||||
declare const _default: {
|
||||
setParams: typeof setParams;
|
||||
};
|
||||
|
||||
File diff suppressed because one or more lines are too long
2
build/weRequest.min.js
vendored
2
build/weRequest.min.js
vendored
File diff suppressed because one or more lines are too long
@@ -3,7 +3,7 @@ import status from '../store/status'
|
||||
import { IInitOption } from '../interface'
|
||||
|
||||
export default (params: IInitOption) => {
|
||||
const config: IInitOption = {...params, ...defaultConfig}
|
||||
const config: IInitOption = {...params, ...defaultConfig};
|
||||
// 如果配置更改了session的存储名字,则重新获取一次session
|
||||
if (params.sessionName) {
|
||||
try {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import checkSession from '../module/sessionManager'
|
||||
import { IRequestOption } from "../interface"
|
||||
|
||||
export default (callback: Function) => {
|
||||
return checkSession(callback, {})
|
||||
return checkSession(callback, {} as IRequestOption)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import status from '../store/status'
|
||||
|
||||
export default (session: TODO) => {
|
||||
export default (session: string) => {
|
||||
status.session = session;
|
||||
status.sessionIsFresh = true;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import requestHandler from '../module/requestHandler'
|
||||
import { IUploadFileOption } from "../interface";
|
||||
|
||||
export default (obj: TODO) => {
|
||||
export default (obj: IUploadFileOption) => {
|
||||
requestHandler.uploadFile(obj)
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export interface IInitOption {
|
||||
request?: () => void
|
||||
) => void);
|
||||
/* 可为接口提供mock数据 */
|
||||
mockJson?: TODO;
|
||||
mockJson?: any;
|
||||
/** 所有请求都会自动带上这里的参数 */
|
||||
globalData?: boolean | object | Function;
|
||||
/** session在本地缓存的key */
|
||||
@@ -31,9 +31,9 @@ export interface IInitOption {
|
||||
/* 触发重新登录的条件;参数为CGI返回的数据,返回需要重新登录的条件 */
|
||||
loginTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
/* 触发请求成功的条件;参数为CGI返回的数据,返回接口逻辑成功的条件 */
|
||||
successTrigger?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
successTrigger: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
/* 成功之后返回数据;参数为CGI返回的数据,返回逻辑需要使用的数据 */
|
||||
successData?: (res: string | IAnyObject | ArrayBuffer) => boolean;
|
||||
successData: (res: string | IAnyObject | ArrayBuffer) => string | IAnyObject | ArrayBuffer;
|
||||
/* 接口逻辑失败时,错误弹窗的标题 */
|
||||
errorTitle?: string | ((res: string | IAnyObject | ArrayBuffer) => string);
|
||||
/* 接口逻辑失败时,错误弹窗的内容 */
|
||||
@@ -42,7 +42,7 @@ export interface IInitOption {
|
||||
|
||||
export interface ICodeToSessionOptions{
|
||||
/* CGI的url */
|
||||
url?: string;
|
||||
url: string;
|
||||
/* 调用该CGI的方法 */
|
||||
method?: 'OPTIONS'
|
||||
| 'GET'
|
||||
@@ -57,33 +57,75 @@ export interface ICodeToSessionOptions{
|
||||
/* 登录接口需要的其他参数 */
|
||||
data?: string | Function | IAnyObject | ArrayBuffer;
|
||||
/* 接口返回成功的函数;需要返回session的值 */
|
||||
success?: Function;
|
||||
success: Function;
|
||||
/* code换取session的接口逻辑出错时,执行的函数,若配置了此函数,则不再默认弹窗报错 */
|
||||
fail?: Function;
|
||||
/* codeToSession的上报字段名 */
|
||||
report?: string;
|
||||
}
|
||||
|
||||
export interface IRequestOption extends wx.RequestOption {
|
||||
export interface IRequestOption extends IRequestObject {
|
||||
/* 发起请求前执行的函数 */
|
||||
beforeSend?: Function;
|
||||
/* 请求过程页面是否展示全屏的loading */
|
||||
showLoading?: boolean | string;
|
||||
/* 接口请求成功后将自动执行init()中配置的reportCGI函数,其中的name字段值为这里配置的值 */
|
||||
report?: string;
|
||||
|
||||
/* 是否需要缓存 */
|
||||
cache?: boolean | Function;
|
||||
|
||||
noCacheFlash?: boolean
|
||||
/* 当启用缓存时,决定除了返回缓存内容外,是否还返回接口实时内容,以防止页面多次渲染的抖动 */
|
||||
noCacheFlash?: boolean;
|
||||
/* 接口调用成功的回调函数 */
|
||||
success: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
/* 接口调用结束的回调函数(调用成功、失败都会执行) */
|
||||
complete?: ()=> void;
|
||||
/** 接口调用失败 或 逻辑失败 的回调函数 */
|
||||
fail?: (res: string | IAnyObject | ArrayBuffer)=> void;
|
||||
}
|
||||
|
||||
export interface IUploadFileOption extends wx.UploadFileOption {
|
||||
export interface IRequestObject extends wx.RequestOption{
|
||||
/* 业务请求的原始url */
|
||||
originUrl: string;
|
||||
/* 业务请求的计数器 */
|
||||
count: number;
|
||||
/* 重登陆次数 */
|
||||
reLoginLimit: number;
|
||||
/* 该请求是否是登陆请求 */
|
||||
isLogin?: boolean;
|
||||
/* 请求发起的时间戳 */
|
||||
_reportStartTime: number;
|
||||
/* 请求返回的时间戳 */
|
||||
_reportEndTime: number;
|
||||
}
|
||||
|
||||
export interface IUploadFileOption extends IUploadFileObject {
|
||||
/* 发起请求前执行的函数 */
|
||||
beforeSend?: Function;
|
||||
/* 请求过程页面是否展示全屏的loading */
|
||||
showLoading?: boolean | string;
|
||||
/* 接口请求成功后将自动执行init()中配置的reportCGI函数,其中的name字段值为这里配置的值 */
|
||||
report?: string;
|
||||
/* 接口调用成功的回调函数 */
|
||||
success: (res: string | IAnyObject | ArrayBuffer, cacheInfo?: object) => void;
|
||||
/* 接口调用结束的回调函数(调用成功、失败都会执行) */
|
||||
complete?: ()=> void;
|
||||
/** 接口调用失败 或 逻辑失败 的回调函数 */
|
||||
fail?: (res: string | IAnyObject | ArrayBuffer)=> void;
|
||||
}
|
||||
|
||||
export interface IUploadFileObject extends wx.UploadFileOption {
|
||||
/* 业务请求的原始url */
|
||||
originUrl: string;
|
||||
/* 业务请求的计数器 */
|
||||
count: number;
|
||||
/* 重登陆次数 */
|
||||
reLoginLimit: number;
|
||||
/* 该请求是否是登陆请求 */
|
||||
isLogin?: boolean;
|
||||
/* 请求发起的时间戳 */
|
||||
_reportStartTime: number;
|
||||
/* 请求返回的时间戳 */
|
||||
_reportEndTime: number;
|
||||
}
|
||||
|
||||
export interface IGetConfigResult {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
function get(obj: TODO) {
|
||||
import { IRequestOption } from "../interface"
|
||||
|
||||
function get(obj: IRequestOption) {
|
||||
wx.getStorage({
|
||||
key: obj.originUrl,
|
||||
success: function (res) {
|
||||
@@ -16,7 +18,7 @@ function get(obj: TODO) {
|
||||
})
|
||||
}
|
||||
|
||||
function set(obj: TODO , realData: TODO) {
|
||||
function set(obj: IRequestOption , realData: string | object) {
|
||||
if (obj.cache === true || (typeof obj.cache === "function" && obj.cache(realData))) {
|
||||
wx.setStorage({
|
||||
key: obj.originUrl,
|
||||
|
||||
@@ -1,37 +1,13 @@
|
||||
import config from '../store/config'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
|
||||
function start(obj: TODO, name?: string) {
|
||||
switch (name) {
|
||||
case 'checkSession':
|
||||
obj._checkSessionStartTime = new Date().getTime();
|
||||
break;
|
||||
case 'login':
|
||||
obj._loginStartTime = new Date().getTime();
|
||||
break;
|
||||
default:
|
||||
function start(obj: IRequestOption | IUploadFileOption) {
|
||||
obj._reportStartTime = new Date().getTime();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function end(obj: TODO, name?: string) {
|
||||
switch (name) {
|
||||
case 'checkSession':
|
||||
// wx.checkSession 耗时上报
|
||||
obj._checkSessionEndTime = new Date().getTime();
|
||||
report('wx_checkSession', obj._checkSessionStartTime, obj._checkSessionEndTime);
|
||||
break;
|
||||
case 'login':
|
||||
// wx.login 耗时上报
|
||||
obj._loginEndTime = new Date().getTime();
|
||||
report('wx_login', obj._loginStartTime, obj._loginEndTime);
|
||||
break;
|
||||
default:
|
||||
// 其他CGI接口
|
||||
function end(obj: IRequestOption | IUploadFileOption) {
|
||||
obj._reportEndTime = new Date().getTime();
|
||||
report(obj.report, obj._reportStartTime, obj._reportEndTime);
|
||||
break;
|
||||
}
|
||||
report(<string>obj.report, obj._reportStartTime, obj._reportEndTime);
|
||||
}
|
||||
|
||||
function report(name: string, start: number, end: number) {
|
||||
|
||||
@@ -1,34 +1,19 @@
|
||||
import config from '../store/config'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
|
||||
export default (obj: TODO, res: TODO) => {
|
||||
function systemError(obj: IRequestOption | IUploadFileOption, res: wx.GeneralCallbackResult) {
|
||||
doError("", res.errMsg);
|
||||
if (typeof obj.fail === "function") {
|
||||
obj.fail("");
|
||||
}
|
||||
}
|
||||
|
||||
function logicError(obj: IRequestOption | IUploadFileOption, res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult) {
|
||||
if (typeof obj.fail === "function") {
|
||||
obj.fail(res);
|
||||
} else {
|
||||
let title = "";
|
||||
if (typeof config.errorTitle === "function") {
|
||||
try {
|
||||
title = config.errorTitle(res.data || res.errMsg)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof config.errorTitle === "string") {
|
||||
title = config.errorTitle;
|
||||
}
|
||||
|
||||
let content = "";
|
||||
if (typeof config.errorContent === "function") {
|
||||
try {
|
||||
content = config.errorContent(res.data || res.errMsg)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof config.errorContent === "string") {
|
||||
content = config.errorContent;
|
||||
}
|
||||
|
||||
wx.showModal({
|
||||
title: title,
|
||||
content: content || "网络或服务异常,请稍后重试",
|
||||
showCancel: false
|
||||
})
|
||||
let {title, content} = getErrorMsg(res);
|
||||
doError(title, content);
|
||||
}
|
||||
|
||||
// 如果有配置统一错误回调函数,则执行它
|
||||
@@ -38,3 +23,41 @@ export default (obj: TODO, res: TODO) => {
|
||||
|
||||
console.error(res);
|
||||
}
|
||||
|
||||
function getErrorMsg(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult) {
|
||||
let title = "";
|
||||
if (typeof config.errorTitle === "function") {
|
||||
try {
|
||||
title = config.errorTitle(res.data)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof config.errorTitle === "string") {
|
||||
title = config.errorTitle;
|
||||
}
|
||||
|
||||
let content = "";
|
||||
if (typeof config.errorContent === "function") {
|
||||
try {
|
||||
content = config.errorContent(res.data)
|
||||
} catch (e) {
|
||||
}
|
||||
} else if (typeof config.errorContent === "string") {
|
||||
content = config.errorContent;
|
||||
}
|
||||
|
||||
return {title, content}
|
||||
}
|
||||
|
||||
function doError(title: string, content: string) {
|
||||
wx.showModal({
|
||||
title: title,
|
||||
content: content || "网络或服务异常,请稍后重试",
|
||||
showCancel: false
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
systemError,
|
||||
logicError,
|
||||
doError
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import config from '../store/config'
|
||||
import responseHandler from './responseHandler'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
|
||||
function get(obj: TODO, method: "request" | "uploadFile"): any {
|
||||
function get(obj: IRequestOption | IUploadFileOption, method: "request" | "uploadFile"): any {
|
||||
|
||||
if(!config.mockJson[obj.url] && !config.mockJson[obj.originUrl]) {
|
||||
// mock 没有对应接口的数据
|
||||
|
||||
@@ -8,6 +8,7 @@ import errorHandler from './errorHandler'
|
||||
import responseHandler from './responseHandler'
|
||||
import durationReporter from "./durationReporter"
|
||||
import url from '../util/url'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface"
|
||||
|
||||
// 格式化url
|
||||
function format(url: string) {
|
||||
@@ -23,7 +24,7 @@ function format(url: string) {
|
||||
}
|
||||
|
||||
// 所有请求发出前需要做的事情
|
||||
function preDo(obj: TODO) {
|
||||
function preDo<T extends IRequestOption | IUploadFileOption>(obj: T): T {
|
||||
if(typeof obj.beforeSend === "function") {
|
||||
obj.beforeSend();
|
||||
}
|
||||
@@ -40,7 +41,7 @@ function preDo(obj: TODO) {
|
||||
|
||||
if (obj.showLoading) {
|
||||
loading.show(obj.showLoading);
|
||||
obj.complete = ((fn: Function, ...args) => {
|
||||
obj.complete = ((fn: any, ...args) => {
|
||||
return ()=> {
|
||||
// TODO 使用Promise方式后,可能不需要这些了
|
||||
loading.hide();
|
||||
@@ -59,23 +60,19 @@ function preDo(obj: TODO) {
|
||||
}
|
||||
|
||||
// 格式化处理请求的obj内容
|
||||
function initialize(obj: TODO, container: TODO) {
|
||||
if (!obj[container]) {
|
||||
obj[container] = {};
|
||||
function initializeRequestObj(obj: IRequestOption) {
|
||||
|
||||
if (!obj.data) {
|
||||
obj.data = {};
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj[container][config.sessionName!] = status.session;
|
||||
obj.data = Object.assign({}, obj.data, {[config.sessionName]: status.session})
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
let gd: any = {};
|
||||
if (typeof config.globalData === "function") {
|
||||
gd = config.globalData();
|
||||
} else if (typeof config.globalData === "object") {
|
||||
gd = config.globalData;
|
||||
}
|
||||
obj[container] = Object.assign({}, gd, obj[container]);
|
||||
let gd = getGlobalData();
|
||||
obj.data = Object.assign({}, gd, obj.data);
|
||||
|
||||
obj.method = obj.method || 'GET';
|
||||
obj.dataType = obj.dataType || 'json';
|
||||
@@ -83,9 +80,7 @@ function initialize(obj: TODO, container: TODO) {
|
||||
// 如果请求不是GET,则在URL中自动加上登录态和全局参数
|
||||
if (obj.method !== "GET") {
|
||||
if (status.session) {
|
||||
let params: any = {};
|
||||
params[config.sessionName] = status.session;
|
||||
obj.url = url.setParams(obj.url, params);
|
||||
obj.url = url.setParams(obj.url, {[config.sessionName]: status.session});
|
||||
}
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
}
|
||||
@@ -95,8 +90,44 @@ function initialize(obj: TODO, container: TODO) {
|
||||
return obj;
|
||||
}
|
||||
|
||||
function doRequest(obj: TODO) {
|
||||
obj = initialize(obj, 'data');
|
||||
// 格式化处理上传文件的obj内容
|
||||
function initializeUploadFileObj(obj: IUploadFileOption) {
|
||||
if (!obj.formData) {
|
||||
obj.formData = {};
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj.formData = Object.assign({}, obj.formData, {[config.sessionName]: status.session})
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
let gd = getGlobalData();
|
||||
obj.formData = Object.assign({}, gd, obj.formData);
|
||||
|
||||
// 将登陆态也带在url上
|
||||
if (status.session) {
|
||||
obj.url = url.setParams(obj.url, {[config.sessionName]: status.session});
|
||||
}
|
||||
// 全局参数同时放在url上
|
||||
obj.url = url.setParams(obj.url, gd);
|
||||
|
||||
durationReporter.start(obj);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function getGlobalData() {
|
||||
let gd: any = {};
|
||||
if (typeof config.globalData === "function") {
|
||||
gd = config.globalData();
|
||||
} else if (typeof config.globalData === "object") {
|
||||
gd = config.globalData;
|
||||
}
|
||||
return gd;
|
||||
}
|
||||
|
||||
function doRequest(obj: IRequestOption) {
|
||||
obj = initializeRequestObj(obj);
|
||||
obj.count++;
|
||||
wx.request({
|
||||
url: obj.url,
|
||||
@@ -108,7 +139,7 @@ function doRequest(obj: TODO) {
|
||||
responseHandler(res, obj, 'request')
|
||||
},
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
complete: function () {
|
||||
@@ -118,8 +149,8 @@ function doRequest(obj: TODO) {
|
||||
})
|
||||
}
|
||||
|
||||
function doUploadFile(obj: TODO) {
|
||||
obj = initialize(obj, 'formData');
|
||||
function doUploadFile(obj: IUploadFileOption) {
|
||||
obj = initializeUploadFileObj(obj);
|
||||
obj.count++;
|
||||
wx.uploadFile({
|
||||
url: obj.url,
|
||||
@@ -130,7 +161,7 @@ function doUploadFile(obj: TODO) {
|
||||
responseHandler(res, obj, 'uploadFile')
|
||||
},
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
complete: function () {
|
||||
@@ -140,11 +171,11 @@ function doUploadFile(obj: TODO) {
|
||||
})
|
||||
}
|
||||
|
||||
function request(obj: TODO): TODO {
|
||||
function request(obj: IRequestOption): void {
|
||||
obj = preDo(obj);
|
||||
if(config.mockJson) {
|
||||
mockManager.get(obj, 'request');
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
if(obj.cache) {
|
||||
cacheManager.get(obj);
|
||||
@@ -155,14 +186,11 @@ function request(obj: TODO): TODO {
|
||||
}, obj)
|
||||
}
|
||||
|
||||
function uploadFile(obj: TODO): TODO {
|
||||
obj = preDo(obj);
|
||||
function uploadFile(obj: IUploadFileOption): void {
|
||||
obj = preDo(obj) as IUploadFileOption;
|
||||
if(config.mockJson) {
|
||||
mockManager.get(obj, 'uploadFile');
|
||||
return false;
|
||||
}
|
||||
if(obj.cache) {
|
||||
cacheManager.get(obj);
|
||||
return;
|
||||
}
|
||||
|
||||
sessionManager(()=>{
|
||||
|
||||
@@ -4,8 +4,13 @@ import requestHandler from './requestHandler'
|
||||
import errorHandler from './errorHandler'
|
||||
import cacheManager from './cacheManager'
|
||||
import durationReporter from './durationReporter'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface";
|
||||
|
||||
function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult, obj: TODO, method: "request" | "uploadFile"): any {
|
||||
function response(
|
||||
res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCallbackResult,
|
||||
obj: IRequestOption | IUploadFileOption,
|
||||
method: "request" | "uploadFile"
|
||||
): any {
|
||||
if (res.statusCode === 200) {
|
||||
|
||||
// 兼容uploadFile返回的res.data可能是字符串
|
||||
@@ -13,7 +18,7 @@ function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCal
|
||||
try {
|
||||
res.data = JSON.parse(res.data);
|
||||
} catch (e) {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.logicError(obj, res);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -24,13 +29,13 @@ function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCal
|
||||
// 登录请求
|
||||
let s = "";
|
||||
try {
|
||||
s = config.codeToSession.success!(res.data);
|
||||
s = config.codeToSession.success(res.data);
|
||||
} catch (e) {
|
||||
}
|
||||
if (s) {
|
||||
obj.success(s);
|
||||
} else {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
} else if (config.loginTrigger!(res.data) && obj.reLoginLimit < config.reLoginLimit!) {
|
||||
// 登录态失效,且重试次数不超过配置
|
||||
@@ -39,18 +44,22 @@ function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCal
|
||||
wx.removeStorage({
|
||||
key: config.sessionName!,
|
||||
complete: function () {
|
||||
requestHandler[method](obj)
|
||||
if(method === "request") {
|
||||
requestHandler.request(<IRequestOption>obj);
|
||||
} else if(method === "uploadFile") {
|
||||
requestHandler.uploadFile(<IUploadFileOption>obj);
|
||||
}
|
||||
}
|
||||
})
|
||||
} else if (config.successTrigger!(res.data)) {
|
||||
} else if (config.successTrigger(res.data)) {
|
||||
// 接口返回成功码
|
||||
let realData = null;
|
||||
let realData: string | IAnyObject | ArrayBuffer = "";
|
||||
try {
|
||||
realData = config.successData!(res.data);
|
||||
realData = config.successData(res.data);
|
||||
} catch (e) {
|
||||
console.error("Function successData occur error: " + e);
|
||||
}
|
||||
if(!obj.noCacheFlash) {
|
||||
if(!(<IRequestOption>obj).noCacheFlash) {
|
||||
// 如果为了保证页面不闪烁,则不回调,只是缓存最新数据,待下次进入再用
|
||||
typeof obj.success === "function" && obj.success(realData);
|
||||
}
|
||||
@@ -58,10 +67,10 @@ function response(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccessCal
|
||||
cacheManager.set(obj, realData);
|
||||
} else {
|
||||
// 接口返回失败码
|
||||
errorHandler(obj, res);
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
} else {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.logicError(obj, res);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import config from '../store/config'
|
||||
import requestHandler from './requestHandler'
|
||||
import errorHandler from './errorHandler'
|
||||
import durationReporter from './durationReporter'
|
||||
import {IRequestOption, IUploadFileOption} from "../interface";
|
||||
|
||||
function checkSession() {
|
||||
return new Promise((resolve)=>{
|
||||
@@ -31,7 +32,7 @@ function checkSession() {
|
||||
})
|
||||
}
|
||||
|
||||
function doLogin(callback: Function, obj: TODO) {
|
||||
function doLogin(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
if (obj.isLogin) {
|
||||
// 登录接口,直接放过
|
||||
typeof callback === "function" && callback();
|
||||
@@ -55,7 +56,7 @@ function doLogin(callback: Function, obj: TODO) {
|
||||
}
|
||||
}
|
||||
|
||||
function getCode(callback: Function,obj: TODO) {
|
||||
function getCode(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
status.logining = true;
|
||||
console.log('wx.login');
|
||||
let start = new Date().getTime();
|
||||
@@ -72,7 +73,7 @@ function getCode(callback: Function,obj: TODO) {
|
||||
flow.emit('doLoginFinished');
|
||||
})
|
||||
} else {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.doError("登录失败", "请稍后重试[code 获取失败]");
|
||||
console.error(res);
|
||||
// 登录失败,解除锁,防止死锁
|
||||
status.logining = false;
|
||||
@@ -80,7 +81,7 @@ function getCode(callback: Function,obj: TODO) {
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
errorHandler(obj, res);
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
// 登录失败,解除锁,防止死锁
|
||||
status.logining = false;
|
||||
@@ -125,11 +126,11 @@ function code2Session(code: String) {
|
||||
},
|
||||
complete: function () {},
|
||||
fail: config.codeToSession.fail || null
|
||||
})
|
||||
} as IRequestOption)
|
||||
})
|
||||
}
|
||||
|
||||
export default (fn: Function, obj: object)=>{
|
||||
export default (fn: Function, obj: IRequestOption | IUploadFileOption)=>{
|
||||
checkSession().then(()=>{
|
||||
return doLogin(fn, obj)
|
||||
});
|
||||
|
||||
@@ -5,7 +5,10 @@ const defaultConfig: IInitOption = {
|
||||
loginTrigger() {
|
||||
return false
|
||||
},
|
||||
codeToSession: {},
|
||||
codeToSession: {
|
||||
url: "",
|
||||
success: ()=> {}
|
||||
},
|
||||
successTrigger() {
|
||||
return true
|
||||
},
|
||||
@@ -25,6 +28,6 @@ const defaultConfig: IInitOption = {
|
||||
globalData: false,
|
||||
// session在本地缓存的key
|
||||
sessionExpireKey: "sessionExpireKey"
|
||||
}
|
||||
};
|
||||
|
||||
export default defaultConfig;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
let store: TODO = {};
|
||||
let store: any = {};
|
||||
|
||||
function emit(key: TODO) {
|
||||
function emit(key: string) {
|
||||
let flow = getFlow(key);
|
||||
let currentLength = flow.waitingList.length;
|
||||
for (let i = 0; i < currentLength; i++) {
|
||||
@@ -9,12 +9,12 @@ function emit(key: TODO) {
|
||||
}
|
||||
}
|
||||
|
||||
function wait(key: TODO, callback: Function) {
|
||||
function wait(key: string, callback: Function) {
|
||||
var flow = getFlow(key);
|
||||
flow.waitingList.push(callback)
|
||||
}
|
||||
|
||||
function getFlow(key: TODO) {
|
||||
function getFlow(key: string) {
|
||||
if (!store[key]) {
|
||||
store[key] = {
|
||||
waitingList: []
|
||||
|
||||
Reference in New Issue
Block a user