Files
weRequest/build/weRequest.min.js

1 line
7.2 KiB
JavaScript

module.exports=function(e){var t={};function n(o){if(t[o])return t[o].exports;var i=t[o]={i:o,l:!1,exports:{}};return e[o].call(i.exports,i,i.exports,n),i.l=!0,i.exports}return n.m=e,n.c=t,n.d=function(e,t,o){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:o})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var o=Object.create(null);if(n.r(o),Object.defineProperty(o,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var i in e)n.d(o,i,function(t){return e[t]}.bind(null,i));return o},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t,n){const o=n(1),i=n(2);var c="session",r=function(){return!1},a={},u=function(){return!0},f="",s=function(e){return e},l="操作失败",d=function(e){return e},p=3,m=null,g=!1,h=!1,y=!1,w="",T=!1,S=!1,x=!1;function v(e,t){x?i.wait("checkSessionFinished",function(){v(e,t)}):!T&&w?(x=!0,t.count++,t._checkSessionStartTime=(new Date).getTime(),console.log("wx.checkSession"),wx.checkSession({success:function(){T=!0},fail:function(){w=""},complete:function(){x=!1,t.count--,t._checkSessionEndTime=(new Date).getTime(),"function"==typeof g&&g("wx_checkSession",t._checkSessionStartTime,t._checkSessionEndTime,_),L(e,t),i.emit("checkSessionFinished")}})):L(e,t)}function L(e,t){w||t.isLogin?"function"==typeof e&&e():S?i.wait("doLoginFinished",function(){L(e,t)}):(S=!0,t.count++,t._loginStartTime=(new Date).getTime(),console.log("wx.login"),wx.login({complete:function(){t.count--,t._loginEndTime=(new Date).getTime(),"function"==typeof g&&g("wx_login",t._loginStartTime,t._loginEndTime,_),"function"==typeof t.complete&&0==t.count&&t.complete()},success:function(n){var o;n.code?((o="function"==typeof a.data?a.data():a.data||{})[a.codeName]=n.code,t.count++,D({url:a.url,data:o,method:a.method,isLogin:!0,report:a.report||a.url,success:function(t){w=t,T=!0,"function"==typeof e&&e(),wx.setStorage({key:c,data:w})},complete:function(){t.count--,"function"==typeof t.complete&&0==t.count&&t.complete(),S=!1,i.emit("doLoginFinished")},fail:a.fail||null})):(k(t,n),console.error(n),S=!1,i.emit("doLoginFinished"))},fail:function(e){k(t,e),console.error(e),S=!1,i.emit("doLoginFinished")}}))}function b(e){return"function"==typeof e.beforeSend&&e.beforeSend(),void 0===e.reLoginLimit?e.reLoginLimit=0:e.reLoginLimit++,void 0===e.count&&(e.count=0),e.showLoading&&(o.show(e.showLoading),e.complete=function(e){return function(){o.hide(),"function"==typeof e&&e.apply(this,arguments)}}(e.complete)),e}function _(e){e.count++,e.data||(e.data={}),e.url!=a.url&&w&&(e.data[c]=w);var t={};"function"==typeof y?t=y():"object"==typeof y&&(t=y),e.data=Object.assign({},t,e.data),e.method=e.method||"GET";var n=e.url.startsWith("http")?e.url:f+e.url;if("GET"!=e.method)for(var o in w&&(n.indexOf("?")>=0?n+="&"+c+"="+encodeURIComponent(w):n+="?"+c+"="+encodeURIComponent(w)),t)n.indexOf("?")>=0?n+="&"+o+"="+t[o]:n+="?"+o+"="+t[o];e.report&&(e._reportStartTime=(new Date).getTime()),wx.request({url:n,data:e.data,method:e.method,header:e.header||{},dataType:e.dataType||"json",success:function(t){if(200==t.statusCode)if(e.report&&"function"==typeof g&&(e._reportEndTime=(new Date).getTime(),g(e.report,e._reportStartTime,e._reportEndTime,_)),e.isLogin){var n="";try{n=a.success(t.data)}catch(e){}n?e.success(n):k(e,t)}else if(r(t.data)&&e.reLoginLimit<p)w="",wx.removeStorage({key:c,complete:function(){L(function(){D(e)},e)}});else if(u(t.data)&&"function"==typeof e.success){var o=null;try{o=s(t.data)}catch(e){console.error("Function successData occur error: "+e)}e.noCacheFlash||e.success(o),(!0===e.cache||"function"==typeof e.cache&&e.cache(o))&&wx.setStorage({key:e.url,data:o})}else k(e,t);else k(e,t)},fail:function(t){k(e,t),console.error(t)},complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()}})}function k(e,t){if("function"==typeof e.fail)e.fail(t);else{var n="";if("function"==typeof l)try{n=l(t.data)}catch(e){}else"string"==typeof l&&(n=l);var o="";if("function"==typeof d)try{o=d(t.data)}catch(e){}else"string"==typeof d&&(o=d);wx.showModal({title:n,content:o||"网络或服务异常,请稍后重试",showCancel:!1})}"function"==typeof m&&m(e,t),console.error(t)}function D(e){e=b(e),h&&h[e.url]?function(e){var t={data:h[e.url]};u(t.data)&&"function"==typeof e.success?e.success(s(t.data)):k(e,t);"function"==typeof e.complete&&e.complete()}(e):function(e,t){e.cache?wx.getStorage({key:e.url,success:function(n){"function"==typeof e.beforeSend&&e.beforeSend(),"function"==typeof e.cache&&e.cache(n.data)?"function"==typeof e.success&&e.success(n.data,{isCache:!0}):1==e.cache&&"function"==typeof e.success&&e.success(n.data,{isCache:!0}),"function"==typeof e.complete&&e.complete(),t(e)},fail:function(){e.noCacheFlash=!1,t(e)}}):t(e)}(e,function(e){v(function(){_(e)},e)})}function j(e){v(function(){!function(e){e.count++,e.formData||(e.formData={}),e.formData[c]=w;var t={};"function"==typeof y?t=y():"object"==typeof y&&(t=y),e.formData=Object.assign({},t,e.formData),e.dataType=e.dataType||"json";var n=e.url.startsWith("http")?e.url:f+e.url;for(var o in w&&(n.indexOf("?")>=0?n+="&"+c+"="+w:n+="?"+c+"="+w),t)n.indexOf("?")>=0?n+="&"+o+"="+t[o]:n+="?"+o+"="+t[o];e.report&&(e._reportStartTime=(new Date).getTime()),wx.uploadFile({url:n,filePath:e.filePath||"",name:e.name||"",formData:e.formData,success:function(t){if(200==t.statusCode&&"uploadFile:ok"==t.errMsg){if(e.report&&"function"==typeof g&&(e.endTime=(new Date).getTime(),g(e.report,e._reportStartTime,e._reportEndTime,_)),"json"==e.dataType)try{t.data=JSON.parse(t.data)}catch(n){return k(e,t),!1}r(t.data)&&e.reLoginLimit<p?(w="",wx.removeStorage({key:c,complete:function(){L(function(){j(e)},e)}})):u(t.data)&&"function"==typeof e.success?e.success(s(t.data)):k(e,t)}else k(e,t)},fail:function(t){k(e,t),console.error(t)},complete:function(){e.count--,"function"==typeof e.complete&&0==e.count&&e.complete()}})}(e)},e=b(e))}e.exports={init:function(e){c=e.sessionName||"session",r=e.loginTrigger||function(){return!1},a=e.codeToSession||{},u=e.successTrigger||function(){return!0},f=e.urlPerfix||"",s=e.successData||function(e){return e},l=e.errorTitle||"操作失败",d=e.errorContent||!1,p=e.reLoginLimit||3,m=e.errorCallback||null,T=e.doNotCheckSession||!1,g=e.reportCGI||!1,h=e.mockJson||!1,y=e.globalData||!1;try{w=wx.getStorageSync(c)||""}catch(e){}},request:D,uploadFile:j,setSession:function(e){w=e,T=!0},login:function(e){v(e,{})},getSession:function(){return w},getConfig:function(){return{urlPerfix:f}}}},function(e,t){e.exports={show:function(e){wx.showToast({title:"boolean"==typeof e?"加载中":e,icon:"loading",mask:!0,duration:6e4})},hide:function(){wx.hideToast()}}},function(e,t){var n={};function o(e){return n[e]||(n[e]={waitingList:[]}),n[e]}e.exports={wait:function(e,t){o(e).waitingList.push(t)},emit:function(e){var t=o(e);console.log("waitingList Length: "+t.waitingList.length);for(var n=t.waitingList.length,i=0;i<n;i++){var c=t.waitingList.shift();"function"==typeof c&&c()}}}}]);