修复了一些tslint错误提示
This commit is contained in:
@@ -3,17 +3,19 @@ import { IRequestOption } from "../interface"
|
||||
function get(obj: IRequestOption) {
|
||||
wx.getStorage({
|
||||
key: obj.originUrl,
|
||||
success: function (res) {
|
||||
success (res) {
|
||||
if (typeof obj.cache === "function" && obj.cache(res.data)) {
|
||||
if (typeof obj.success === "function") {
|
||||
obj.success(res.data, {isCache: true})
|
||||
}
|
||||
} else if (obj.cache == true) {
|
||||
} else if (obj.cache === true) {
|
||||
if (typeof obj.success === "function") {
|
||||
obj.success(res.data, {isCache: true})
|
||||
}
|
||||
}
|
||||
typeof obj.complete === "function" && obj.complete();
|
||||
if(typeof obj.complete === "function") {
|
||||
obj.complete();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,12 +7,12 @@ function start(obj: IRequestOption | IUploadFileOption) {
|
||||
|
||||
function end(obj: IRequestOption | IUploadFileOption) {
|
||||
obj._reportEndTime = new Date().getTime();
|
||||
report(<string>obj.report, obj._reportStartTime, obj._reportEndTime);
|
||||
report(obj.report as string, obj._reportStartTime, obj._reportEndTime);
|
||||
}
|
||||
|
||||
function report(name: string, start: number, end: number) {
|
||||
function report(name: string, startTime: number, endTime: number) {
|
||||
if (typeof config.reportCGI === "function") {
|
||||
config.reportCGI(name, start, end);
|
||||
config.reportCGI(name, startTime, endTime);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ function logicError(obj: IRequestOption | IUploadFileOption, res: wx.RequestSucc
|
||||
if (typeof obj.fail === "function") {
|
||||
obj.fail(res);
|
||||
} else {
|
||||
let {title, content} = getErrorMsg(res);
|
||||
const {title, content} = getErrorMsg(res);
|
||||
doError(title, content);
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ function getErrorMsg(res: wx.RequestSuccessCallbackResult | wx.UploadFileSuccess
|
||||
|
||||
function doError(title: string, content: string) {
|
||||
wx.showModal({
|
||||
title: title,
|
||||
title,
|
||||
content: content || "网络或服务异常,请稍后重试",
|
||||
showCancel: false
|
||||
})
|
||||
|
||||
@@ -13,8 +13,8 @@ function get(obj: IRequestOption | IUploadFileOption, method: "request" | "uploa
|
||||
let data = config.mockJson[obj.url] || config.mockJson[obj.originUrl];
|
||||
// deep copy
|
||||
data = JSON.parse(JSON.stringify(data));
|
||||
let res = {
|
||||
data: data,
|
||||
const res = {
|
||||
data,
|
||||
statusCode: 200
|
||||
};
|
||||
|
||||
|
||||
@@ -11,15 +11,15 @@ import url from '../util/url'
|
||||
import { IRequestOption, IUploadFileOption } from "../interface"
|
||||
|
||||
// 格式化url
|
||||
function format(url: string) {
|
||||
if (url.startsWith('http')) {
|
||||
return url
|
||||
function format(originUrl: string) {
|
||||
if (originUrl.startsWith('http')) {
|
||||
return originUrl
|
||||
} else {
|
||||
let urlPerfix = config.urlPerfix;
|
||||
if (typeof config.urlPerfix === "function") {
|
||||
urlPerfix = config.urlPerfix()
|
||||
}
|
||||
return urlPerfix + url;
|
||||
return urlPerfix + originUrl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +45,10 @@ function preDo<T extends IRequestOption | IUploadFileOption>(obj: T): T {
|
||||
return ()=> {
|
||||
// TODO 使用Promise方式后,可能不需要这些了
|
||||
loading.hide();
|
||||
// @ts-ignore
|
||||
typeof fn === "function" && fn.apply(this, ...args);
|
||||
if(typeof fn === "function"){
|
||||
// @ts-ignore
|
||||
fn.apply(this, ...args);
|
||||
}
|
||||
}
|
||||
})(obj.complete)
|
||||
}
|
||||
@@ -67,12 +69,12 @@ function initializeRequestObj(obj: IRequestOption) {
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj.data = Object.assign({}, obj.data, {[config.sessionName]: status.session})
|
||||
obj.data = {...obj.data as object, [config.sessionName]: status.session};
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
let gd = getGlobalData();
|
||||
obj.data = Object.assign({}, gd, obj.data);
|
||||
const gd = getGlobalData();
|
||||
obj.data = {...gd, ...obj.data as object};
|
||||
|
||||
obj.method = obj.method || 'GET';
|
||||
obj.dataType = obj.dataType || 'json';
|
||||
@@ -97,12 +99,12 @@ function initializeUploadFileObj(obj: IUploadFileOption) {
|
||||
}
|
||||
|
||||
if (obj.originUrl !== config.codeToSession.url && status.session) {
|
||||
obj.formData = Object.assign({}, obj.formData, {[config.sessionName]: status.session})
|
||||
obj.formData = {...obj.formData as object, [config.sessionName]: status.session};
|
||||
}
|
||||
|
||||
// 如果有全局参数,则添加
|
||||
let gd = getGlobalData();
|
||||
obj.formData = Object.assign({}, gd, obj.formData);
|
||||
const gd = getGlobalData();
|
||||
obj.formData = {...gd, ...obj.formData};
|
||||
|
||||
// 将登陆态也带在url上
|
||||
if (status.session) {
|
||||
@@ -135,16 +137,18 @@ function doRequest(obj: IRequestOption) {
|
||||
method: obj.method,
|
||||
header: obj.header || {},
|
||||
dataType: obj.dataType || 'json',
|
||||
success: function (res: wx.RequestSuccessCallbackResult) {
|
||||
success(res: wx.RequestSuccessCallbackResult) {
|
||||
responseHandler(res, obj, 'request')
|
||||
},
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
fail (res: wx.GeneralCallbackResult) {
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
complete: function () {
|
||||
complete () {
|
||||
obj.count--;
|
||||
typeof obj.complete === "function" && obj.count === 0 && obj.complete();
|
||||
if(typeof obj.complete === "function" && obj.count === 0){
|
||||
obj.complete();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -157,16 +161,18 @@ function doUploadFile(obj: IUploadFileOption) {
|
||||
filePath: obj.filePath || '',
|
||||
name: obj.name || '',
|
||||
formData: obj.formData,
|
||||
success: function (res: wx.UploadFileSuccessCallbackResult) {
|
||||
success (res: wx.UploadFileSuccessCallbackResult) {
|
||||
responseHandler(res, obj, 'uploadFile')
|
||||
},
|
||||
fail: function (res: wx.GeneralCallbackResult) {
|
||||
fail (res: wx.GeneralCallbackResult) {
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
},
|
||||
complete: function () {
|
||||
complete () {
|
||||
obj.count--;
|
||||
typeof obj.complete === "function" && obj.count === 0 && obj.complete();
|
||||
if(typeof obj.complete === "function" && obj.count === 0){
|
||||
obj.complete();
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -43,11 +43,11 @@ function response(
|
||||
status.sessionIsFresh = true;
|
||||
wx.removeStorage({
|
||||
key: config.sessionName!,
|
||||
complete: function () {
|
||||
complete () {
|
||||
if(method === "request") {
|
||||
requestHandler.request(<IRequestOption>obj);
|
||||
requestHandler.request(obj as IRequestOption);
|
||||
} else if(method === "uploadFile") {
|
||||
requestHandler.uploadFile(<IUploadFileOption>obj);
|
||||
requestHandler.uploadFile(obj as IUploadFileOption);
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -59,9 +59,11 @@ function response(
|
||||
} catch (e) {
|
||||
console.error("Function successData occur error: " + e);
|
||||
}
|
||||
if(!(<IRequestOption>obj).noCacheFlash) {
|
||||
if(!(obj as IRequestOption).noCacheFlash) {
|
||||
// 如果为了保证页面不闪烁,则不回调,只是缓存最新数据,待下次进入再用
|
||||
typeof obj.success === "function" && obj.success(realData);
|
||||
if(typeof obj.success === "function"){
|
||||
obj.success(realData);
|
||||
}
|
||||
}
|
||||
// 缓存存储
|
||||
cacheManager.set(obj, realData);
|
||||
|
||||
@@ -10,19 +10,19 @@ function checkSession() {
|
||||
return new Promise((resolve)=>{
|
||||
if (!status.sessionIsFresh && status.session) {
|
||||
console.log("wx.checkSession()");
|
||||
let start = new Date().getTime();
|
||||
const start = new Date().getTime();
|
||||
wx.checkSession({
|
||||
success: function () {
|
||||
success () {
|
||||
// 登录态有效,且在本生命周期内无须再检验了
|
||||
resolve();
|
||||
},
|
||||
fail: function () {
|
||||
fail () {
|
||||
// 登录态过期
|
||||
status.session = '';
|
||||
resolve();
|
||||
},
|
||||
complete: function () {
|
||||
let end = new Date().getTime();
|
||||
complete () {
|
||||
const end = new Date().getTime();
|
||||
durationReporter.report('checkSession', start, end);
|
||||
}
|
||||
})
|
||||
@@ -35,7 +35,9 @@ function checkSession() {
|
||||
function doLogin(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
if (obj.isLogin) {
|
||||
// 登录接口,直接放过
|
||||
typeof callback === "function" && callback();
|
||||
if(typeof callback === "function"){
|
||||
callback();
|
||||
}
|
||||
} else if (status.session) {
|
||||
// 缓存中有session
|
||||
if (status.sessionExpireTime && new Date().getTime() > status.sessionExpire) {
|
||||
@@ -43,11 +45,13 @@ function doLogin(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
status.session = '';
|
||||
doLogin(callback, obj);
|
||||
} else {
|
||||
typeof callback === "function" && callback();
|
||||
if(typeof callback === "function"){
|
||||
callback();
|
||||
}
|
||||
}
|
||||
} else if (status.logining) {
|
||||
// 正在登录中,请求轮询稍后,避免重复调用登录接口
|
||||
flow.wait('doLoginFinished', function () {
|
||||
flow.wait('doLoginFinished', () => {
|
||||
doLogin(callback, obj);
|
||||
})
|
||||
} else {
|
||||
@@ -59,13 +63,13 @@ function doLogin(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
function getCode(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
status.logining = true;
|
||||
console.log('wx.login');
|
||||
let start = new Date().getTime();
|
||||
const start = new Date().getTime();
|
||||
wx.login({
|
||||
complete: function () {
|
||||
let end = new Date().getTime();
|
||||
complete () {
|
||||
const end = new Date().getTime();
|
||||
durationReporter.report('login', start, end);
|
||||
},
|
||||
success: function (res) {
|
||||
success (res) {
|
||||
if (res.code) {
|
||||
code2Session(res.code).then(()=>{
|
||||
callback();
|
||||
@@ -80,7 +84,7 @@ function getCode(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
flow.emit('doLoginFinished');
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
fail (res) {
|
||||
errorHandler.systemError(obj, res);
|
||||
console.error(res);
|
||||
// 登录失败,解除锁,防止死锁
|
||||
@@ -90,7 +94,7 @@ function getCode(callback: Function, obj: IRequestOption | IUploadFileOption) {
|
||||
})
|
||||
}
|
||||
|
||||
function code2Session(code: String) {
|
||||
function code2Session(code: string) {
|
||||
let data: any;
|
||||
// codeToSession.data支持函数
|
||||
if (typeof config.codeToSession.data === "function") {
|
||||
@@ -103,11 +107,11 @@ function code2Session(code: String) {
|
||||
return new Promise((resolve)=>{
|
||||
requestHandler.request({
|
||||
url: config.codeToSession.url,
|
||||
data: data,
|
||||
data,
|
||||
method: config.codeToSession.method || 'GET',
|
||||
isLogin: true,
|
||||
report: config.codeToSession.report || config.codeToSession.url,
|
||||
success: function (s: String) {
|
||||
success (s: string) {
|
||||
status.session = s;
|
||||
status.sessionIsFresh = true;
|
||||
// 如果有设置本地session过期时间
|
||||
@@ -124,7 +128,7 @@ function code2Session(code: String) {
|
||||
});
|
||||
return resolve();
|
||||
},
|
||||
complete: function () {},
|
||||
complete () {},
|
||||
fail: config.codeToSession.fail || null
|
||||
} as IRequestOption)
|
||||
})
|
||||
|
||||
@@ -1,16 +1,18 @@
|
||||
let store: any = {};
|
||||
const store: any = {};
|
||||
|
||||
function emit(key: string) {
|
||||
let flow = getFlow(key);
|
||||
let currentLength = flow.waitingList.length;
|
||||
const flow = getFlow(key);
|
||||
const currentLength = flow.waitingList.length;
|
||||
for (let i = 0; i < currentLength; i++) {
|
||||
let callback = flow.waitingList.shift();
|
||||
typeof callback == "function" && callback();
|
||||
const callback = flow.waitingList.shift();
|
||||
if(typeof callback == "function"){
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function wait(key: string, callback: Function) {
|
||||
var flow = getFlow(key);
|
||||
const flow = getFlow(key);
|
||||
flow.waitingList.push(callback)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
function setParams(url: string = '', params: object) {
|
||||
let queryStringIndex: number = url.indexOf('?');
|
||||
let kvp: any = {};
|
||||
if (queryStringIndex >= 0) {
|
||||
let oldQueryString = url.substr(queryStringIndex + 1).split('&');
|
||||
for (let i = 0; i < oldQueryString.length; i++) {
|
||||
let kv: Array<string> = oldQueryString[i].split('=');
|
||||
kvp[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
function setParams(url: string = "", params: object) {
|
||||
const queryStringIndex: number = url.indexOf("?");
|
||||
let kvp: any = {};
|
||||
if (queryStringIndex >= 0) {
|
||||
const oldQueryString = url.substr(queryStringIndex + 1).split("&");
|
||||
oldQueryString.forEach((x, i) => {
|
||||
const kv: string[] = oldQueryString[i].split("=");
|
||||
kvp[kv[0]] = kv[1];
|
||||
});
|
||||
}
|
||||
|
||||
kvp = {...kvp, ...params};
|
||||
kvp = { ...kvp, ...params };
|
||||
|
||||
let queryString = Object.keys(kvp).map(key => {
|
||||
return `${key}=${encodeURI(kvp[key])}`
|
||||
}).join('&');
|
||||
|
||||
if (queryStringIndex >= 0) {
|
||||
return url.substring(0, queryStringIndex + 1) + queryString
|
||||
} else {
|
||||
return url + "?" + queryString
|
||||
}
|
||||
const queryString = Object.keys(kvp)
|
||||
.map(key => {
|
||||
return `${key}=${encodeURI(kvp[key])}`;
|
||||
})
|
||||
.join("&");
|
||||
|
||||
if (queryStringIndex >= 0) {
|
||||
return url.substring(0, queryStringIndex + 1) + queryString;
|
||||
} else {
|
||||
return url + "?" + queryString;
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
setParams
|
||||
}
|
||||
setParams
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user