支持cache参数;支持setSession接口;修复多个请求同时发起时重复调用登录接口的问题
This commit is contained in:
@@ -1 +1 @@
|
||||
module.exports=function(t){function e(n){if(o[n])return o[n].exports;var c=o[n]={i:n,l:!1,exports:{}};return t[n].call(c.exports,c,c.exports,e),c.l=!0,c.exports}var o={};return e.m=t,e.c=o,e.i=function(t){return t},e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=1)}([function(t,e){function o(){wx.showToast({title:"加载中",icon:"loading",mask:!0,duration:6e4})}function n(){wx.hideToast()}t.exports={show:o,hide:n}},function(t,e,o){function n(t,e){C?c(t,e):(e.count++,wx.checkSession({success:function(){C=!0},fail:function(){S=""},complete:function(){e.count--,c(t,e)}}))}function c(t,e){if(!S)try{S=wx.getStorageSync(p)||""}catch(t){}S||e.isLogin?"function"==typeof t&&t():(e.count++,wx.login({complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()},success:function(o){if(o.code){var n;n="function"==typeof h.data?h.data():h.data||{},n[h.codeName]=o.code,e.count++,f({url:h.url,data:n,method:h.method,isLogin:!0,success:function(e){S=e,C=!0,"function"==typeof t&&t(),wx.setStorage({key:p,data:S})},complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()}})}else wx.showModal({title:"登录失败",content:"请稍后重试",showCancel:!1}),console.error(o)},fail:function(t){wx.showModal({title:"登录失败",content:t.errMsg||"请稍后重试",showCancel:!1}),console.error(t)}}))}function i(t){return"function"==typeof t.beforeSend&&t.beforeSend(),void 0===t.reLoginLimit?t.reLoginLimit=0:t.reLoginLimit++,void 0===t.count&&(t.count=0),t.showLoading&&(d.show(),t.complete=function(t){return function(){d.hide(),"function"==typeof t&&t.apply(this,arguments)}}(t.complete)),t}function u(t){t.count++,t.data||(t.data={}),t.url!=h.url&&(t.data[p]=S),t.method=t.method||"GET";var e=t.url.startsWith("http")?t.url:g+t.url;"GET"!=t.method&&(e.indexOf("?")>=0?e+="&"+p+"="+S:e+="?"+p+"="+S),wx.request({url:e,data:t.data,method:t.method,header:t.header||{},dataType:t.dataType||"json",success:function(e){if(200==e.statusCode)if(t.isLogin){var o="";try{o=h.success(e.data)}catch(t){}o?t.success(o):a(t,e)}else m(e.data)&&t.reLoginLimit<T?(S="",wx.removeStorage({key:p,complete:function(){c(function(){f(t)},t)}})):y(e.data)&&"function"==typeof t.success?t.success(w(e.data)):a(t,e);else a(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),a(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function r(t){t.count++,t.formData||(t.formData={}),t.formData[p]=S,t.dataType=t.dataType||"json",wx.uploadFile({url:g+t.url,filePath:t.filePath||"",name:t.name||"",formData:t.formData,success:function(e){if(200==e.statusCode&&"uploadFile:ok"==e.errMsg){if("json"==t.dataType)try{e.data=JSON.parse(e.data)}catch(o){return a(t,e),!1}m(e.data)&&t.reLoginLimit<T?(S="",wx.removeStorage({key:p,complete:function(){c(function(){l(t)},t)}})):y(e.data)&&"function"==typeof t.success?t.success(w(e.data)):a(t,e)}else a(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),a(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function a(t,e){if("function"==typeof t.fail)t.fail(e);else{var o="";if("function"==typeof x)try{o=x(e.data)}catch(t){}else"string"==typeof x&&(o=x);var n="";if("function"==typeof L)try{n=L(e.data)}catch(t){}else"string"==typeof L&&(n=L);wx.showModal({title:o||"操作失败",content:n||"服务器异常,请稍后重试",showCancel:!1})}"function"==typeof v&&v(t,e),console.error(e)}function s(t){p=t.sessionName||"session",m=t.loginTrigger||function(){return!1},h=Object.assign({},{method:"GET",codeName:"code"},t.codeToSession),y=t.successTrigger||function(){return!0},g=t.urlPerfix||"",w=t.successData||function(t){return t},x=t.errorTitle||"操作失败",L=t.errorContent||!1,T=t.reLoginLimit||3,v=t.errorCallback||null,C=t.doNotCheckSession||!1;try{S=wx.getStorageSync(p)||""}catch(t){}}function f(t){t=i(t),n(function(){u(t)},t)}function l(t){t=i(t),n(function(){r(t)},t)}const d=o(0);var p="session",m=function(){return!1},h={},y=function(){return!0},g="",w=function(t){return t},x="操作失败",L=function(t){return t},T=3,v=null,S="",C=!1;t.exports={init:s,request:f,uploadFile:l}}]);
|
||||
module.exports=function(t){function e(n){if(o[n])return o[n].exports;var c=o[n]={i:n,l:!1,exports:{}};return t[n].call(c.exports,c,c.exports,e),c.l=!0,c.exports}var o={};return e.m=t,e.c=o,e.i=function(t){return t},e.d=function(t,o,n){e.o(t,o)||Object.defineProperty(t,o,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var o=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(o,"a",o),o},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=1)}([function(t,e){function o(){wx.showToast({title:"加载中",icon:"loading",mask:!0,duration:6e4})}function n(){wx.hideToast()}t.exports={show:o,hide:n}},function(t,e,o){function n(t,e){M?c(t,e):(e.count++,wx.checkSession({success:function(){M=!0},fail:function(){C=""},complete:function(){e.count--,c(t,e)}}))}function c(t,e){C||e.isLogin?"function"==typeof t&&t():b?setTimeout(function(){c(t,e)},300):(b=!0,e.count++,wx.login({complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()},success:function(o){if(o.code){var n;n="function"==typeof g.data?g.data():g.data||{},n[g.codeName]=o.code,e.count++,l({url:g.url,data:n,method:g.method,isLogin:!0,success:function(e){C=e,M=!0,"function"==typeof t&&t(),wx.setStorage({key:m,data:C})},complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete(),b=!1}})}else wx.showModal({title:"登录失败",content:"请稍后重试",showCancel:!1}),console.error(o),b=!1},fail:function(t){wx.showModal({title:"登录失败",content:t.errMsg||"请稍后重试",showCancel:!1}),console.error(t),b=!1}}))}function i(t){return"function"==typeof t.beforeSend&&t.beforeSend(),void 0===t.reLoginLimit?t.reLoginLimit=0:t.reLoginLimit++,void 0===t.count&&(t.count=0),t.showLoading&&(h.show(),t.complete=function(t){return function(){h.hide(),"function"==typeof t&&t.apply(this,arguments)}}(t.complete)),t}function u(t){t.count++,t.data||(t.data={}),t.url!=g.url&&(t.data[m]=C),t.method=t.method||"GET";var e=t.url.startsWith("http")?t.url:x+t.url;"GET"!=t.method&&(e.indexOf("?")>=0?e+="&"+m+"="+C:e+="?"+m+"="+C),wx.request({url:e,data:t.data,method:t.method,header:t.header||{},dataType:t.dataType||"json",success:function(e){if(200==e.statusCode)if(t.isLogin){var o="";try{o=g.success(e.data)}catch(t){}o?t.success(o):r(t,e)}else if(y(e.data)&&t.reLoginLimit<S)C="",wx.removeStorage({key:m,complete:function(){c(function(){l(t)},t)}});else if(w(e.data)&&"function"==typeof t.success){var n=null;try{n=L(e.data)}catch(t){console.error("Function successData occur error: "+t)}t.success(n),(!0===t.cache||"function"==typeof t.cache&&t.cache(n))&&wx.setStorage({key:t.url,data:n})}else r(t,e);else r(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),r(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function a(t){t.count++,t.formData||(t.formData={}),t.formData[m]=C,t.dataType=t.dataType||"json",wx.uploadFile({url:x+t.url,filePath:t.filePath||"",name:t.name||"",formData:t.formData,success:function(e){if(200==e.statusCode&&"uploadFile:ok"==e.errMsg){if("json"==t.dataType)try{e.data=JSON.parse(e.data)}catch(o){return r(t,e),!1}y(e.data)&&t.reLoginLimit<S?(C="",wx.removeStorage({key:m,complete:function(){c(function(){d(t)},t)}})):w(e.data)&&"function"==typeof t.success?t.success(L(e.data)):r(t,e)}else r(t,e)},fail:function(e){wx.showModal({title:"请求失败",content:e.errMsg,showCancel:!1}),r(t,e)},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete()}})}function r(t,e){if("function"==typeof t.fail)t.fail(e);else{var o="";if("function"==typeof T)try{o=T(e.data)}catch(t){}else"string"==typeof T&&(o=T);var n="";if("function"==typeof v)try{n=v(e.data)}catch(t){}else"string"==typeof v&&(n=v);wx.showModal({title:o||"操作失败",content:n||"服务器异常,请稍后重试",showCancel:!1})}"function"==typeof k&&k(t,e),console.error(e)}function s(t,e){t.cache?wx.getStorage({key:t.url,success:function(e){"function"==typeof t.cache&&t.cache(e.data)?"function"==typeof t.success&&t.success(e.data):!0===t.cache&&"function"==typeof t.success&&t.success(e.data)},fail:function(){e()}}):e()}function f(t){m=t.sessionName||"session",y=t.loginTrigger||function(){return!1},g=Object.assign({},{method:"GET",codeName:"code"},t.codeToSession),w=t.successTrigger||function(){return!0},x=t.urlPerfix||"",L=t.successData||function(t){return t},T=t.errorTitle||"操作失败",v=t.errorContent||!1,S=t.reLoginLimit||3,k=t.errorCallback||null,M=t.doNotCheckSession||!1;try{C=wx.getStorageSync(m)||""}catch(t){}}function l(t){t=i(t),s(t,function(){n(function(){u(t)},t)})}function d(t){t=i(t),n(function(){a(t)},t)}function p(t){C=t,M=!0}const h=o(0);var m="session",y=function(){return!1},g={},w=function(){return!0},x="",L=function(t){return t},T="操作失败",v=function(t){return t},S=3,k=null,C="",M=!1,b=!1;t.exports={init:f,request:l,uploadFile:d,setSession:p}}]);
|
||||
@@ -15,6 +15,8 @@ var errorCallback = null;
|
||||
//global data
|
||||
var session = '';
|
||||
var sessionIsFresh = false;
|
||||
// 正在登录中,其他请求轮询稍后,避免重复调用登录接口
|
||||
var logining = false;
|
||||
|
||||
function checkSession(callback, obj) {
|
||||
if (!sessionIsFresh) {
|
||||
@@ -41,19 +43,17 @@ function checkSession(callback, obj) {
|
||||
}
|
||||
|
||||
function doLogin(callback, obj) {
|
||||
// 如果运行时Storage中通过其他业务种下了session,但是本控件不知道的,这时候尝试读取Storage
|
||||
if(!session) {
|
||||
try {
|
||||
session = wx.getStorageSync(sessionName) || '';
|
||||
} catch (e) {}
|
||||
}
|
||||
//TODO: 不能这样,否则session失效时无法清掉
|
||||
|
||||
if (session || obj.isLogin) {
|
||||
// 缓存中有session,或者是登录接口
|
||||
typeof callback == "function" && callback();
|
||||
} else if(logining) {
|
||||
// 正在登录中,请求轮询稍后,避免重复调用登录接口
|
||||
setTimeout(function() {
|
||||
doLogin(callback, obj);
|
||||
}, 300)
|
||||
} else {
|
||||
// 缓存中无session
|
||||
logining = true;
|
||||
obj.count ++;
|
||||
wx.login({
|
||||
complete: function () {
|
||||
@@ -89,6 +89,7 @@ function doLogin(callback, obj) {
|
||||
complete: function() {
|
||||
obj.count --;
|
||||
typeof obj.complete == "function" && obj.count == 0 && obj.complete();
|
||||
logining = false;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -98,6 +99,8 @@ function doLogin(callback, obj) {
|
||||
showCancel: false
|
||||
})
|
||||
console.error(res);
|
||||
// 登录失败,解除锁,防止死循环
|
||||
logining = false;
|
||||
}
|
||||
},
|
||||
fail: function (res) {
|
||||
@@ -107,6 +110,8 @@ function doLogin(callback, obj) {
|
||||
showCancel: false
|
||||
})
|
||||
console.error(res);
|
||||
// 登录失败,解除锁,防止死循环
|
||||
logining = false;
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -195,7 +200,19 @@ function request(obj) {
|
||||
})
|
||||
} else if (successTrigger(res.data) && typeof obj.success == "function") {
|
||||
// 接口返回成功码
|
||||
obj.success(successData(res.data));
|
||||
var realData = null;
|
||||
try {
|
||||
realData = successData(res.data);
|
||||
} catch (e) {
|
||||
console.error("Function successData occur error: " + e);
|
||||
}
|
||||
obj.success(realData);
|
||||
if(obj.cache === true || (typeof obj.cache == "function" && obj.cache(realData))) {
|
||||
wx.setStorage({
|
||||
key: obj.url,
|
||||
data: realData
|
||||
})
|
||||
}
|
||||
} else {
|
||||
// 接口返回失败码
|
||||
fail(obj, res);
|
||||
@@ -318,6 +335,26 @@ function fail(obj, res) {
|
||||
console.error(res);
|
||||
}
|
||||
|
||||
function getCache(obj, callback) {
|
||||
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);
|
||||
}
|
||||
},
|
||||
fail: function() {
|
||||
callback();
|
||||
}
|
||||
})
|
||||
} else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
function init(params) {
|
||||
sessionName = params.sessionName || 'session';
|
||||
loginTrigger = params.loginTrigger || function () { return false };
|
||||
@@ -341,9 +378,11 @@ function init(params) {
|
||||
|
||||
function requestWrapper(obj) {
|
||||
obj = preDo(obj);
|
||||
checkSession(function () {
|
||||
request(obj);
|
||||
}, obj)
|
||||
getCache(obj, function() {
|
||||
checkSession(function () {
|
||||
request(obj);
|
||||
}, obj)}
|
||||
)
|
||||
}
|
||||
|
||||
function uploadFileWrapper(obj) {
|
||||
@@ -353,8 +392,14 @@ function uploadFileWrapper(obj) {
|
||||
}, obj)
|
||||
}
|
||||
|
||||
function setSession(s) {
|
||||
session = s;
|
||||
sessionIsFresh = true;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
init: init,
|
||||
request: requestWrapper,
|
||||
uploadFile: uploadFileWrapper
|
||||
uploadFile: uploadFileWrapper,
|
||||
setSession: setSession
|
||||
};
|
||||
Reference in New Issue
Block a user