[fixed]mock模式下没执行complete

This commit is contained in:
TENCENT\ivinwu
2017-12-04 11:38:14 +08:00
parent d60c6325cd
commit e3eaf0b535

View File

@@ -1,36 +1,44 @@
const loading = require('./loading');
//params
var sessionName = "session";
var loginTrigger = function () { return false };
var codeToSession = {};
var successTrigger = function () { return true };
var urlPerfix = "";
var successData = function (res) { return res };
var errorTitle = "操作失败";
var errorContent = function(res) {return res};
var reLoginLimit = 3;
var errorCallback = null;
var reportCGI = false;
var mockJson = false;
var globalData = false;
var sessionName = "session";
var loginTrigger = function () {
return false
};
var codeToSession = {};
var successTrigger = function () {
return true
};
var urlPerfix = "";
var successData = function (res) {
return res
};
var errorTitle = "操作失败";
var errorContent = function (res) {
return res
};
var reLoginLimit = 3;
var errorCallback = null;
var reportCGI = false;
var mockJson = false;
var globalData = false;
//global data
var session = '';
var sessionIsFresh = false;
var session = '';
var sessionIsFresh = false;
// 正在登录中,其他请求轮询稍后,避免重复调用登录接口
var logining = false;
var logining = false;
// 正在查询session有效期中避免重复调用接口
var isCheckingSession = false;
function checkSession(callback, obj) {
if(isCheckingSession) {
setTimeout(function() {
if (isCheckingSession) {
setTimeout(function () {
checkSession(callback, obj)
}, 500);
} else if (!sessionIsFresh && session) {
isCheckingSession = true;
obj.count ++;
obj.count++;
// 如果还没检验过session是否有效则需要检验一次
obj._checkSessionStartTime = new Date().getTime();
wx.checkSession({
@@ -44,9 +52,9 @@ function checkSession(callback, obj) {
},
complete: function () {
isCheckingSession = false;
obj.count --;
obj.count--;
obj._checkSessionEndTime = new Date().getTime();
if(typeof reportCGI == "function") {
if (typeof reportCGI === "function") {
reportCGI('wx_checkSession', obj._checkSessionStartTime, obj._checkSessionEndTime, request);
}
doLogin(callback, obj);
@@ -61,40 +69,40 @@ function checkSession(callback, obj) {
function doLogin(callback, obj) {
if (session || obj.isLogin) {
// 缓存中有session或者是登录接口
typeof callback == "function" && callback();
} else if(logining) {
typeof callback === "function" && callback();
} else if (logining) {
// 正在登录中,请求轮询稍后,避免重复调用登录接口
setTimeout(function() {
setTimeout(function () {
doLogin(callback, obj);
}, 500)
} else {
// 缓存中无session
logining = true;
obj.count ++;
obj.count++;
// 记录调用wx.login前的时间戳
obj._loginStartTime = new Date().getTime();
wx.login({
complete: function () {
obj.count --;
obj.count--;
// 记录wx.login返回数据后的时间戳用于上报
obj._loginEndTime = new Date().getTime();
if(typeof reportCGI == "function") {
if (typeof reportCGI === "function") {
reportCGI('wx_login', obj._loginStartTime, obj._loginEndTime, request);
}
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
typeof obj.complete === "function" && obj.count == 0 && obj.complete();
},
success: function (res) {
if (res.code) {
var data;
// codeToSession.data支持函数
if(typeof codeToSession.data == "function") {
if (typeof codeToSession.data === "function") {
data = codeToSession.data();
} else {
data = codeToSession.data || {};
}
data[codeToSession.codeName] = res.code;
obj.count ++;
obj.count++;
requestWrapper({
url: codeToSession.url,
data: data,
@@ -102,17 +110,17 @@ function doLogin(callback, obj) {
isLogin: true,
report: codeToSession.report || codeToSession.url,
success: function (s) {
session = s;
session = s;
sessionIsFresh = true;
typeof callback == "function" && callback();
typeof callback === "function" && callback();
wx.setStorage({
key: sessionName,
data: session
})
},
complete: function() {
obj.count --;
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
complete: function () {
obj.count--;
typeof obj.complete === "function" && obj.count == 0 && obj.complete();
logining = false;
},
fail: codeToSession.fail || null
@@ -135,25 +143,25 @@ function doLogin(callback, obj) {
}
function preDo(obj) {
typeof obj.beforeSend == "function" && obj.beforeSend();
typeof obj.beforeSend === "function" && obj.beforeSend();
// 登录态失效,重复登录计数
if (typeof obj.reLoginLimit == "undefined") {
if (typeof obj.reLoginLimit === "undefined") {
obj.reLoginLimit = 0;
} else {
obj.reLoginLimit ++;
obj.reLoginLimit++;
}
if(typeof obj.count == "undefined") {
if (typeof obj.count === "undefined") {
obj.count = 0;
}
if(obj.showLoading) {
if (obj.showLoading) {
loading.show();
obj.complete = (function(fn) {
return function() {
obj.complete = (function (fn) {
return function () {
loading.hide();
typeof fn == "function" && fn.apply(this, arguments);
typeof fn === "function" && fn.apply(this, arguments);
}
})(obj.complete)
}
@@ -162,7 +170,7 @@ function preDo(obj) {
}
function request(obj) {
obj.count ++;
obj.count++;
if (!obj.data) {
obj.data = {};
@@ -174,9 +182,9 @@ function request(obj) {
// 如果有全局参数,则添加
var gd = {};
if(typeof globalData == "function") {
if (typeof globalData === "function") {
gd = globalData();
} else if(typeof globalData == "object") {
} else if (typeof globalData === "object") {
gd = globalData;
}
obj.data = Object.assign({}, gd, obj.data);
@@ -186,10 +194,10 @@ function request(obj) {
// 如果请求的URL中不是http开头的则自动添加配置中的前缀
var url = obj.url.startsWith('http') ? obj.url : (urlPerfix + obj.url);
// 如果请求不是GET则在URL中自动加上登录态和全局参数
if(obj.method != "GET") {
if (obj.method != "GET") {
if(session) {
if(url.indexOf('?') >= 0) {
if (session) {
if (url.indexOf('?') >= 0) {
url += '&' + sessionName + '=' + session;
} else {
url += '?' + sessionName + '=' + session;
@@ -197,8 +205,8 @@ function request(obj) {
}
// 如果有全局参数则在URL中添加
for(var i in gd) {
if(url.indexOf('?') >= 0) {
for (var i in gd) {
if (url.indexOf('?') >= 0) {
url += '&' + i + '=' + gd[i];
} else {
url += '?' + i + '=' + gd[i];
@@ -207,7 +215,7 @@ function request(obj) {
}
// 如果有上报字段配置,则记录请求发出前的时间戳
if(obj.report) {
if (obj.report) {
obj._reportStartTime = new Date().getTime();
}
@@ -221,7 +229,7 @@ function request(obj) {
if (res.statusCode == 200) {
// 如果有上报字段配置,则记录请求返回后的时间戳,并进行上报
if(obj.report && typeof reportCGI == "function") {
if (obj.report && typeof reportCGI === "function") {
obj._reportEndTime = new Date().getTime();
reportCGI(obj.report, obj._reportStartTime, obj._reportEndTime, request);
}
@@ -231,8 +239,9 @@ function request(obj) {
var s = "";
try {
s = codeToSession.success(res.data);
} catch (e) {}
if(s) {
} catch (e) {
}
if (s) {
obj.success(s);
} else {
fail(obj, res);
@@ -242,13 +251,13 @@ function request(obj) {
session = '';
wx.removeStorage({
key: sessionName,
complete: function() {
complete: function () {
doLogin(function () {
requestWrapper(obj);
}, obj)
}
})
} else if (successTrigger(res.data) && typeof obj.success == "function") {
} else if (successTrigger(res.data) && typeof obj.success === "function") {
// 接口返回成功码
var realData = null;
try {
@@ -257,7 +266,7 @@ function request(obj) {
console.error("Function successData occur error: " + e);
}
obj.success(realData);
if(obj.cache === true || (typeof obj.cache == "function" && obj.cache(realData))) {
if (obj.cache === true || (typeof obj.cache === "function" && obj.cache(realData))) {
wx.setStorage({
key: obj.url,
data: realData
@@ -276,14 +285,14 @@ function request(obj) {
console.error(res);
},
complete: function () {
obj.count --;
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
obj.count--;
typeof obj.complete === "function" && obj.count == 0 && obj.complete();
}
})
}
function uploadFile(obj) {
obj.count ++;
obj.count++;
if (!obj.formData) {
obj.formData = {};
@@ -292,9 +301,9 @@ function uploadFile(obj) {
// 如果有全局参数,则添加
var gd = {};
if(typeof globalData == "function") {
if (typeof globalData === "function") {
gd = globalData();
} else if(typeof globalData == "object") {
} else if (typeof globalData === "object") {
gd = globalData;
}
obj.formData = Object.assign({}, gd, obj.formData);
@@ -305,8 +314,8 @@ function uploadFile(obj) {
var url = obj.url.startsWith('http') ? obj.url : (urlPerfix + obj.url);
// 在URL中自动加上登录态和全局参数
if(session) {
if(url.indexOf('?') >= 0) {
if (session) {
if (url.indexOf('?') >= 0) {
url += '&' + sessionName + '=' + session;
} else {
url += '?' + sessionName + '=' + session;
@@ -314,8 +323,8 @@ function uploadFile(obj) {
}
// 如果有全局参数则在URL中添加
for(var i in gd) {
if(url.indexOf('?') >= 0) {
for (var i in gd) {
if (url.indexOf('?') >= 0) {
url += '&' + i + '=' + gd[i];
} else {
url += '?' + i + '=' + gd[i];
@@ -323,7 +332,7 @@ function uploadFile(obj) {
}
// 如果有上报字段配置,则记录请求发出前的时间戳
if(obj.report) {
if (obj.report) {
obj._reportStartTime = new Date().getTime();
}
@@ -336,12 +345,12 @@ function uploadFile(obj) {
if (res.statusCode == 200 && res.errMsg == 'uploadFile:ok') {
// 如果有上报字段配置,则记录请求返回后的时间戳,并进行上报
if(obj.report && typeof reportCGI == "function") {
if (obj.report && typeof reportCGI === "function") {
obj.endTime = new Date().getTime();
reportCGI(obj.report, obj._reportStartTime, obj._reportEndTime, request);
}
if(obj.dataType == 'json') {
if (obj.dataType == 'json') {
try {
res.data = JSON.parse(res.data);
} catch (e) {
@@ -354,13 +363,13 @@ function uploadFile(obj) {
session = '';
wx.removeStorage({
key: sessionName,
complete: function() {
complete: function () {
doLogin(function () {
uploadFileWrapper(obj);
}, obj)
}
})
} else if (successTrigger(res.data) && typeof obj.success == "function") {
} else if (successTrigger(res.data) && typeof obj.success === "function") {
// 接口返回成功码
obj.success(successData(res.data));
} else {
@@ -376,31 +385,33 @@ function uploadFile(obj) {
console.error(res);
},
complete: function () {
obj.count --;
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
obj.count--;
typeof obj.complete === "function" && obj.count == 0 && obj.complete();
}
})
}
function fail(obj, res) {
if(typeof obj.fail == "function") {
if (typeof obj.fail === "function") {
obj.fail(res);
} else {
var title = "";
if(typeof errorTitle == "function") {
if (typeof errorTitle === "function") {
try {
title = errorTitle(res.data)
} catch (e) {}
} else if (typeof errorTitle == "string") {
} catch (e) {
}
} else if (typeof errorTitle === "string") {
title = errorTitle;
}
var content = "";
if(typeof errorContent == "function") {
if (typeof errorContent === "function") {
try {
content = errorContent(res.data)
} catch (e) {}
} else if(typeof errorContent == "string") {
} catch (e) {
}
} else if (typeof errorContent === "string") {
content = errorContent;
}
@@ -412,7 +423,7 @@ function fail(obj, res) {
}
// 如果有配置统一错误回调函数,则执行它
if(typeof errorCallback == "function") {
if (typeof errorCallback === "function") {
errorCallback(obj, res);
}
@@ -420,17 +431,17 @@ function fail(obj, res) {
}
function getCache(obj, callback) {
if(obj.cache) {
if (obj.cache) {
wx.getStorage({
key: obj.url,
success: function(res) {
if(typeof obj.cache == "function" && obj.cache(res.data)) {
typeof obj.success == "function" && obj.success(res.data);
} else if(obj.cache === true) {
typeof obj.success == "function" && obj.success(res.data);
success: function (res) {
if (typeof obj.cache === "function" && obj.cache(res.data)) {
typeof obj.success === "function" && obj.success(res.data);
} else if (obj.cache == true) {
typeof obj.success === "function" && obj.success(res.data);
}
},
fail: function() {
fail: function () {
callback();
}
})
@@ -444,36 +455,44 @@ function login(callback) {
}
function init(params) {
sessionName = params.sessionName || 'session';
loginTrigger = params.loginTrigger || function () { return false };
codeToSession = params.codeToSession || {};
successTrigger = params.successTrigger || function () { return true };
urlPerfix = params.urlPerfix || "";
successData = params.successData || function (res) { return res };
errorTitle = params.errorTitle || "操作失败";
errorContent = params.errorContent || false;
reLoginLimit = params.reLoginLimit || 3;
errorCallback = params.errorCallback || null;
sessionName = params.sessionName || 'session';
loginTrigger = params.loginTrigger || function () {
return false
};
codeToSession = params.codeToSession || {};
successTrigger = params.successTrigger || function () {
return true
};
urlPerfix = params.urlPerfix || "";
successData = params.successData || function (res) {
return res
};
errorTitle = params.errorTitle || "操作失败";
errorContent = params.errorContent || false;
reLoginLimit = params.reLoginLimit || 3;
errorCallback = params.errorCallback || null;
sessionIsFresh = params.doNotCheckSession || false;
reportCGI = params.reportCGI || false;
mockJson = params.mockJson || false;
globalData = params.globalData || false;
reportCGI = params.reportCGI || false;
mockJson = params.mockJson || false;
globalData = params.globalData || false;
try {
session = wx.getStorageSync(sessionName) || '';
} catch (e) {}
} catch (e) {
}
}
function requestWrapper(obj) {
obj = preDo(obj);
if(mockJson && mockJson[obj.url]) {
if (mockJson && mockJson[obj.url]) {
// mock 模式
mock(obj);
} else {
getCache(obj, function() {
checkSession(function () {
request(obj);
}, obj)}
getCache(obj, function () {
checkSession(function () {
request(obj);
}, obj)
}
)
}
}
@@ -486,7 +505,7 @@ function uploadFileWrapper(obj) {
}
function setSession(s) {
session = s;
session = s;
sessionIsFresh = true;
}
@@ -494,13 +513,16 @@ function mock(obj) {
var res = {
data: mockJson[obj.url]
};
if (successTrigger(res.data) && typeof obj.success == "function") {
if (successTrigger(res.data) && typeof obj.success === "function") {
// 接口返回成功码
obj.success(successData(res.data));
} else {
// 接口返回失败码
fail(obj, res);
}
if (typeof obj.complete === "function") {
obj.complete();
}
}
module.exports = {