/* Array.js v0.0.3 | https://github.com/iwind/Array.js */ Array.$nil={};Array.prototype.$contains=function(a){var c=this;if(c==null){return false}for(var b=0;bthis.length){a=this.length}return this.splice(this.length-a,a)};Array.prototype.$keepIf=function(b){var c=this;if(c==null){return 0}var a=c.length;var d=c.$findAll(b);c.$replace(d);return a-c.length};Array.prototype.$replace=function(a){var b=this;if(b==null){return false}if(!Array.isArray(a)){return false}b.splice.apply(b,[0,b.length].concat(a));return true};Array.prototype.$clear=function(){var a=this;if(a==null){return true}if(a.length==0){return true}a.splice(0,a.length);return true};Array.prototype.$each=function(b){var d=this;if(d==null){return true}if(typeof(b)!="function"){return true}var c=d.length;for(var a=0;ab.length-1){return null}return b[a]};Array.prototype.$getAll=function(e){var d=this;if(d==null){return[]}var b=[];for(var c=0;cb.length-1){return false}b[a]=c;return true};Array.prototype.$copy=function(){var c=this;if(c==null){return c}var a=[];for(var b=0;b-1){return}if(c.call(d,f,e)){b=f;a=e}});return a};Array.prototype.$findAll=function(b){var c=this;if(c==null){return[]}if(typeof(b)=="undefined"){return c.$copy()}var a=[];c.$each(function(e,d){if(b.call(c,e,d)){a.push(d)}});return a};Array.prototype.$filter=function(a){var b=this;if(b==null){return[]}return b.$findAll(a)};Array.prototype.$exist=function(a){return this.$findAll(a).length>0};Array.prototype.$reject=function(b){var c=this;if(c==null){return[]}if(typeof(b)=="undefined"){return[]}var a=[];c.$each(function(e,d){if(!b.call(c,e,d)){a.push(d)}});return a};Array.prototype.$grep=function(b){var a=this;if(a==null){return[]}return a.$findAll(function(d,c){if(c==null){return false}return b.test(c.toString())})};Array.prototype.$keys=function(e,a){var d=this;if(d==null){return[]}if(arguments.length==0){return Array.$range(0,d.length-1)}var c=[];if(typeof(a)=="undefined"){a=false}for(var b=0;bc){return 1}else{if(d==c){return 0}else{return -1}}}}b.sort(a);return true};Array.prototype.$rsort=function(a){var b=this;if(b==null){return false}this.$sort(a);b.reverse();return true};Array.prototype.$asort=function(a){var e=this;if(e==null){return[]}var c=[];for(var d=0;df){return 1}return 0}}for(d=0;d0&&a(e[b-1],e[b])>0){e.$swap(b,b-1);c.$swap(b,b-1)}}}return c};Array.prototype.$arsort=function(a){var c=this;if(c==null){return[]}var b=c.$asort(a);c.reverse();b.reverse();return b};Array.prototype.$diff=function(c){var b=this;if(b==null){return[]}var a=[];b.$each(function(e,d){if(!c.$contains(d)){a.push(d)}});return a};Array.prototype.$intersect=function(c){var b=this;if(b==null){return[]}var a=[];b.$each(function(e,d){if(c.$contains(d)){a.push(d)}});return a};Array.prototype.$max=function(a){var c=this;if(c==null){return null}if(c.length>0){var b=c.$copy();b.$rsort(a);return b.$get(0)}return null};Array.prototype.$min=function(a){var c=this;if(c==null){return null}if(c.length>0){var b=c.$copy();b.$sort(a);return b.$get(0)}return null};Array.prototype.$swap=function(e,d){var c=this;if(c==null){return false}var b=c.$get(e);var a=c.$get(d);c.$set(e,a);c.$set(d,b);return true};Array.prototype.$sum=function(b){var c=this;if(c==null){return 0}var a=0;c.$each(function(e,d){if(typeof(b)=="function"){d=b.call(c,e,d)}if(typeof(d)=="number"){a+=d}else{if(typeof(d)=="string"){var f=parseFloat(d);if(!isNaN(f)){a+=f}}}});return a};Array.prototype.$product=function(b){var c=this;if(c==null){return 0}var a=1;c.$each(function(e,d){if(typeof(b)=="function"){d=b.call(c,e,d)}if(typeof(d)=="number"){a*=d}else{if(typeof(d)=="string"){var f=parseFloat(d);if(!isNaN(f)){a*=f}}}});return a};Array.prototype.$chunk=function(c){var d=this;if(d==null){return[]}if(typeof(c)=="undefined"){c=1}c=parseInt(c);if(isNaN(c)||c<1){return[]}var a=[];for(var b=0;bc[b]){return 1}if(d[b]==c[b]){return 0}return -1}return 0})};Array.prototype.$desc=function(b){var a=this;if(a==null){return false}return a.$sort(function(d,c){if(typeof(d)=="object"&&typeof(c)=="object"){if(d[b]>c[b]){return -1}if(d[b]==c[b]){return 0}return 1}return 0})};Array.prototype.$equal=function(c){var b=this;if(b==null){return false}if(!Array.$isArray(c)){return false}if(b.length!=c.length){return false}for(var a=0;ab.length-1){this.index=0}a.call(b,this.index,b[this.index],this);return this.index},sleep:function(c){var d=this;setTimeout(function(){d.next()},c)}});return true};Array.prototype.$asJSON=function(){return JSON.stringify(this)};Array.$range=function(e,a,c){var d=[];if(typeof(c)=="undefined"){c=1}if(e=a;b-=c){d.push(b)}}return d};Array.$isArray=function(a){return Object.prototype.toString.call(a)==="[object Array]"};if(!Array.from){Array.from=(function(){var d=Object.prototype.toString;var e=function(g){return typeof g==="function"||d.call(g)==="[object Function]"};var c=function(h){var g=Number(h);if(isNaN(g)){return 0}if(g===0||!isFinite(g)){return g}return(g>0?1:-1)*Math.floor(Math.abs(g))};var b=Math.pow(2,53)-1;var a=function(h){var g=c(h);return Math.min(Math.max(g,0),b)};return function f(p){var g=this;var o=Object(p);if(p==null){throw new TypeError("Array.from requires an array-like object - not null or undefined")}var m=arguments.length>1?arguments[1]:void undefined;var i;if(typeof m!=="undefined"){if(!e(m)){throw new TypeError("Array.from: when provided, the second argument must be a function")}if(arguments.length>2){i=arguments[2]}}var n=a(o.length);var h=e(g)?Object(new g(n)):new Array(n);var j=0;var l;while(j * @license MIT */ e.exports=function(e){return null!=e&&(n(e)||r(e)||!!e._isBuffer)}},function(e,t,n){"use strict";function r(e){this.defaults=e,this.interceptors={request:new s,response:new s}}var o=n(6),i=n(2),s=n(17),u=n(18);r.prototype.request=function(e){"string"==typeof e&&(e=i.merge({url:arguments[0]},arguments[1])),e=i.merge(o,{method:"get"},this.defaults,e),e.method=e.method.toLowerCase();var t=[u,void 0],n=Promise.resolve(e);for(this.interceptors.request.forEach(function(e){t.unshift(e.fulfilled,e.rejected)}),this.interceptors.response.forEach(function(e){t.push(e.fulfilled,e.rejected)});t.length;)n=n.then(t.shift(),t.shift());return n},i.forEach(["delete","get","head","options"],function(e){r.prototype[e]=function(t,n){return this.request(i.merge(n||{},{method:e,url:t}))}}),i.forEach(["post","put","patch"],function(e){r.prototype[e]=function(t,n,r){return this.request(i.merge(r||{},{method:e,url:t,data:n}))}}),e.exports=r},function(e,t,n){"use strict";function r(e,t){!i.isUndefined(e)&&i.isUndefined(e["Content-Type"])&&(e["Content-Type"]=t)}function o(){var e;return"undefined"!=typeof XMLHttpRequest?e=n(8):"undefined"!=typeof process&&(e=n(8)),e}var i=n(2),s=n(7),u={"Content-Type":"application/x-www-form-urlencoded"},a={adapter:o(),transformRequest:[function(e,t){return s(t,"Content-Type"),i.isFormData(e)||i.isArrayBuffer(e)||i.isBuffer(e)||i.isStream(e)||i.isFile(e)||i.isBlob(e)?e:i.isArrayBufferView(e)?e.buffer:i.isURLSearchParams(e)?(r(t,"application/x-www-form-urlencoded;charset=utf-8"),e.toString()):i.isObject(e)?(r(t,"application/json;charset=utf-8"),JSON.stringify(e)):e}],transformResponse:[function(e){if("string"==typeof e)try{e=JSON.parse(e)}catch(e){}return e}],timeout:0,xsrfCookieName:"XSRF-TOKEN",xsrfHeaderName:"X-XSRF-TOKEN",maxContentLength:-1,validateStatus:function(e){return e>=200&&e<300}};a.headers={common:{Accept:"application/json, text/plain, */*"}},i.forEach(["delete","get","head"],function(e){a.headers[e]={}}),i.forEach(["post","put","patch"],function(e){a.headers[e]=i.merge(u)}),e.exports=a},function(e,t,n){"use strict";var r=n(2);e.exports=function(e,t){r.forEach(e,function(n,r){r!==t&&r.toUpperCase()===t.toUpperCase()&&(e[t]=n,delete e[r])})}},function(e,t,n){"use strict";var r=n(2),o=n(9),i=n(12),s=n(13),u=n(14),a=n(10),c="undefined"!=typeof window&&window.btoa&&window.btoa.bind(window)||n(15);e.exports=function(e){return new Promise(function(t,f){var p=e.data,d=e.headers;r.isFormData(p)&&delete d["Content-Type"];var l=new XMLHttpRequest,h="onreadystatechange",m=!1;if("undefined"==typeof window||!window.XDomainRequest||"withCredentials"in l||u(e.url)||(l=new window.XDomainRequest,h="onload",m=!0,l.onprogress=function(){},l.ontimeout=function(){}),e.auth){var y=e.auth.username||"",w=e.auth.password||"";d.Authorization="Basic "+c(y+":"+w)}if(l.open(e.method.toUpperCase(),i(e.url,e.params,e.paramsSerializer),!0),l.timeout=e.timeout,l[h]=function(){if(l&&(4===l.readyState||m)&&(0!==l.status||l.responseURL&&0===l.responseURL.indexOf("file:"))){var n="getAllResponseHeaders"in l?s(l.getAllResponseHeaders()):null,r=e.responseType&&"text"!==e.responseType?l.response:l.responseText,i={data:r,status:1223===l.status?204:l.status,statusText:1223===l.status?"No Content":l.statusText,headers:n,config:e,request:l};o(t,f,i),l=null}},l.onerror=function(){f(a("Network Error",e,null,l)),l=null},l.ontimeout=function(){f(a("timeout of "+e.timeout+"ms exceeded",e,"ECONNABORTED",l)),l=null},r.isStandardBrowserEnv()){var g=n(16),v=(e.withCredentials||u(e.url))&&e.xsrfCookieName?g.read(e.xsrfCookieName):void 0;v&&(d[e.xsrfHeaderName]=v)}if("setRequestHeader"in l&&r.forEach(d,function(e,t){"undefined"==typeof p&&"content-type"===t.toLowerCase()?delete d[t]:l.setRequestHeader(t,e)}),e.withCredentials&&(l.withCredentials=!0),e.responseType)try{l.responseType=e.responseType}catch(t){if("json"!==e.responseType)throw t}"function"==typeof e.onDownloadProgress&&l.addEventListener("progress",e.onDownloadProgress),"function"==typeof e.onUploadProgress&&l.upload&&l.upload.addEventListener("progress",e.onUploadProgress),e.cancelToken&&e.cancelToken.promise.then(function(e){l&&(l.abort(),f(e),l=null)}),void 0===p&&(p=null),l.send(p)})}},function(e,t,n){"use strict";var r=n(10);e.exports=function(e,t,n){var o=n.config.validateStatus;n.status&&o&&!o(n.status)?t(r("Request failed with status code "+n.status,n.config,null,n.request,n)):e(n)}},function(e,t,n){"use strict";var r=n(11);e.exports=function(e,t,n,o,i){var s=new Error(e);return r(s,t,n,o,i)}},function(e,t){"use strict";e.exports=function(e,t,n,r,o){return e.config=t,n&&(e.code=n),e.request=r,e.response=o,e}},function(e,t,n){"use strict";function r(e){return encodeURIComponent(e).replace(/%40/gi,"@").replace(/%3A/gi,":").replace(/%24/g,"$").replace(/%2C/gi,",").replace(/%20/g,"+").replace(/%5B/gi,"[").replace(/%5D/gi,"]")}var o=n(2);e.exports=function(e,t,n){if(!t)return e;var i;if(n)i=n(t);else if(o.isURLSearchParams(t))i=t.toString();else{var s=[];o.forEach(t,function(e,t){null!==e&&"undefined"!=typeof e&&(o.isArray(e)?t+="[]":e=[e],o.forEach(e,function(e){o.isDate(e)?e=e.toISOString():o.isObject(e)&&(e=JSON.stringify(e)),s.push(r(t)+"="+r(e))}))}),i=s.join("&")}return i&&(e+=(e.indexOf("?")===-1?"?":"&")+i),e}},function(e,t,n){"use strict";var r=n(2),o=["age","authorization","content-length","content-type","etag","expires","from","host","if-modified-since","if-unmodified-since","last-modified","location","max-forwards","proxy-authorization","referer","retry-after","user-agent"];e.exports=function(e){var t,n,i,s={};return e?(r.forEach(e.split("\n"),function(e){if(i=e.indexOf(":"),t=r.trim(e.substr(0,i)).toLowerCase(),n=r.trim(e.substr(i+1)),t){if(s[t]&&o.indexOf(t)>=0)return;"set-cookie"===t?s[t]=(s[t]?s[t]:[]).concat([n]):s[t]=s[t]?s[t]+", "+n:n}}),s):s}},function(e,t,n){"use strict";var r=n(2);e.exports=r.isStandardBrowserEnv()?function(){function e(e){var t=e;return n&&(o.setAttribute("href",t),t=o.href),o.setAttribute("href",t),{href:o.href,protocol:o.protocol?o.protocol.replace(/:$/,""):"",host:o.host,search:o.search?o.search.replace(/^\?/,""):"",hash:o.hash?o.hash.replace(/^#/,""):"",hostname:o.hostname,port:o.port,pathname:"/"===o.pathname.charAt(0)?o.pathname:"/"+o.pathname}}var t,n=/(msie|trident)/i.test(navigator.userAgent),o=document.createElement("a");return t=e(window.location.href),function(n){var o=r.isString(n)?e(n):n;return o.protocol===t.protocol&&o.host===t.host}}():function(){return function(){return!0}}()},function(e,t){"use strict";function n(){this.message="String contains an invalid character"}function r(e){for(var t,r,i=String(e),s="",u=0,a=o;i.charAt(0|u)||(a="=",u%1);s+=a.charAt(63&t>>8-u%1*8)){if(r=i.charCodeAt(u+=.75),r>255)throw new n;t=t<<8|r}return s}var o="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";n.prototype=new Error,n.prototype.code=5,n.prototype.name="InvalidCharacterError",e.exports=r},function(e,t,n){"use strict";var r=n(2);e.exports=r.isStandardBrowserEnv()?function(){return{write:function(e,t,n,o,i,s){var u=[];u.push(e+"="+encodeURIComponent(t)),r.isNumber(n)&&u.push("expires="+new Date(n).toGMTString()),r.isString(o)&&u.push("path="+o),r.isString(i)&&u.push("domain="+i),s===!0&&u.push("secure"),document.cookie=u.join("; ")},read:function(e){var t=document.cookie.match(new RegExp("(^|;\\s*)("+e+")=([^;]*)"));return t?decodeURIComponent(t[3]):null},remove:function(e){this.write(e,"",Date.now()-864e5)}}}():function(){return{write:function(){},read:function(){return null},remove:function(){}}}()},function(e,t,n){"use strict";function r(){this.handlers=[]}var o=n(2);r.prototype.use=function(e,t){return this.handlers.push({fulfilled:e,rejected:t}),this.handlers.length-1},r.prototype.eject=function(e){this.handlers[e]&&(this.handlers[e]=null)},r.prototype.forEach=function(e){o.forEach(this.handlers,function(t){null!==t&&e(t)})},e.exports=r},function(e,t,n){"use strict";function r(e){e.cancelToken&&e.cancelToken.throwIfRequested()}var o=n(2),i=n(19),s=n(20),u=n(6),a=n(21),c=n(22);e.exports=function(e){r(e),e.baseURL&&!a(e.url)&&(e.url=c(e.baseURL,e.url)),e.headers=e.headers||{},e.data=i(e.data,e.headers,e.transformRequest),e.headers=o.merge(e.headers.common||{},e.headers[e.method]||{},e.headers||{}),o.forEach(["delete","get","head","post","put","patch","common"],function(t){delete e.headers[t]});var t=e.adapter||u.adapter;return t(e).then(function(t){return r(e),t.data=i(t.data,t.headers,e.transformResponse),t},function(t){return s(t)||(r(e),t&&t.response&&(t.response.data=i(t.response.data,t.response.headers,e.transformResponse))),Promise.reject(t)})}},function(e,t,n){"use strict";var r=n(2);e.exports=function(e,t,n){return r.forEach(n,function(n){e=n(e,t)}),e}},function(e,t){"use strict";e.exports=function(e){return!(!e||!e.__CANCEL__)}},function(e,t){"use strict";e.exports=function(e){return/^([a-z][a-z\d\+\-\.]*:)?\/\//i.test(e)}},function(e,t){"use strict";e.exports=function(e,t){return t?e.replace(/\/+$/,"")+"/"+t.replace(/^\/+/,""):e}},function(e,t){"use strict";function n(e){this.message=e}n.prototype.toString=function(){return"Cancel"+(this.message?": "+this.message:"")},n.prototype.__CANCEL__=!0,e.exports=n},function(e,t,n){"use strict";function r(e){if("function"!=typeof e)throw new TypeError("executor must be a function.");var t;this.promise=new Promise(function(e){t=e});var n=this;e(function(e){n.reason||(n.reason=new o(e),t(n.reason))})}var o=n(23);r.prototype.throwIfRequested=function(){if(this.reason)throw this.reason},r.source=function(){var e,t=new r(function(t){e=t});return{token:t,cancel:e}},e.exports=r},function(e,t){"use strict";e.exports=function(e){return function(t){return e.apply(null,t)}}}])}); //# sourceMappingURL=axios.min.map !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.ES6Promise=e()}(this,function(){"use strict";function t(t){var e=typeof t;return null!==t&&("object"===e||"function"===e)}function e(t){return"function"==typeof t}function n(t){B=t}function r(t){G=t}function o(){return function(){return process.nextTick(a)}}function i(){return"undefined"!=typeof z?function(){z(a)}:c()}function s(){var t=0,e=new J(a),n=document.createTextNode("");return e.observe(n,{characterData:!0}),function(){n.data=t=++t%2}}function u(){var t=new MessageChannel;return t.port1.onmessage=a,function(){return t.port2.postMessage(0)}}function c(){var t=setTimeout;return function(){return t(a,1)}}function a(){for(var t=0;t 0) { var context = {}; context.Tea = window.Tea; // 内置方法 for (var methodName in innerMethods) { if (innerMethods.hasOwnProperty(methodName)) { context[methodName] = innerMethods[methodName]; } } for (key in data) { if (!data.hasOwnProperty(key)) { continue; } context[key] = data[key]; } for (var i = 0; i < contextFunctions.length; i ++) { var contextFn = contextFunctions[i]; if (typeof(contextFn) != "function") { continue; } contextFn.call(context); for (var key in context) { if (!context.hasOwnProperty(key)) { continue; } if (typeof(key) != "string") { continue; } // 跳过自定义方法 if (key.length > 0 && key[0] == "$") { continue; } var value = context[key]; if (typeof(value) === "function") { context[key] = function (value) { return function () { if (window.Tea.Vue == null) { return value.apply(innerMethods, arguments); } else { return value.apply(window.Tea.Vue, arguments); } }; }(value); } } } // 清除context中的预定义变量 for (var methodName in innerMethods) { if (innerMethods.hasOwnProperty(methodName)) { delete(context[methodName]); } } window.Tea.Vue = new Vue({ el: vueElement, data: context, // 内置方法 methods: innerMethods }); } else { var context = { Tea: window.Tea }; for (key in data) { if (!data.hasOwnProperty(key)) { continue; } context[key] = data[key]; } window.Tea.Vue = new Vue({ el: vueElement, data: context, // 内置方法 methods: { $delay: Tea.delay, $get: function (action) { return Tea.action(action).get(); }, $post: function (action) { return Tea.action(action).post(); }, $go: Tea.go, $url: Tea.url, $find: Tea.element } }); } }; document.addEventListener("DOMContentLoaded", function () { that.load(); if (document.body) { Tea.activate(document.body); } }); })(); /** * 序列化参数为可传递的字符串 * * 代码来自jQuery:https://jquery.com/download/ * * @param a 要序列化的参数集 * @param traditional * @returns {*} */ window.Tea.serialize = function (a, traditional) { var prefix, s = [], add = function (key, valueOrFunction) { // If value is a function, invoke it and use its return value var value = (typeof(valueOrFunction) === "function") ? valueOrFunction() : valueOrFunction; s[s.length] = encodeURIComponent(key) + "=" + encodeURIComponent(value == null ? "" : value); }; var rbracket = /\[]$/; var buildParams = function (prefix, obj, traditional, add) { var name; if (Array.isArray(obj)) { // Serialize array item. for (var i in obj) { if (!obj.hasOwnProperty(i)) { continue; } var v = obj[i]; if (traditional || rbracket.test(prefix)) { // Treat each array item as a scalar. add(prefix, v); } else { // Item is non-scalar (array or object), encode its numeric index. buildParams( prefix + "[" + (typeof v === "object" && v != null ? i : "") + "]", v, traditional, add ); } } } else if (!traditional && typeof(obj) === "object") { // Serialize object item. for (name in obj) { buildParams(prefix + "[" + name + "]", obj[name], traditional, add); } } else { // Serialize scalar item. add(prefix, obj); } }; // If an array was passed in, assume that it is an array of form elements. if (Array.isArray(a)) { // Serialize the form elements for (key in a) { if (!a.hasOwnProperty(key)) { continue; } add(key, a[key]); } } else { // If traditional, encode the "old" way (the way 1.3.2 or older // did it), otherwise encode params recursively. for (prefix in a) { if (!a.hasOwnProperty(prefix)) { continue; } buildParams(prefix, a[prefix], traditional, add); } } // Return the resulting serialization return s.join("&"); }; /** * 取得Action对应的URL * * @param action Action * @param params 参数 * @param hashParams 锚点参数 * @returns {*} */ window.Tea.url = function (action, params, hashParams) { // $ if (action == "$") { action = window.location.pathname; } var config = window.TEA.ACTION; var controller = config.parent; var module = config.module; var base = config.base; var actionParam = config.actionParam; var url; if (action.match(/\//)) {//支持URL url = action; if (typeof(params) === "object") { var query = Tea.serialize(params); if (query.length > 0) { url += "?" + query; } } if (!url.match(/^(http|https|ftp):/i)) { url = base + ((url.substr(0, 1) === "/") ? "" : "/") + url; } } else { if (action.substr(0, 2) === "..") { var pos = controller.lastIndexOf("."); if (pos === -1) { action = action.substr(2); } else { action = controller.substr(0, pos) + action.substr(1); } if (module !== "") { action = "@" + module + "." + action; } } else if (action.substr(0, 1) === ".") { action = controller + action; if (module !== "") { action = "@" + module + "." + action; } } else if (module !== "") { if (action === "@") { action = "@" + module; } else { action = action.replace("@.", "@" + module + "."); } } action = action.replace(/\.$/, ""); if (actionParam) { var path = action.replace(/[.\/]+/g, "/"); if (path.substr(0, 1) !== "/") { path = "/" + path; } url = base + "?__ACTION__=" + path; } else { url = base + "/" + action.replace(/[.\/]+/g, "/").replace(/^\//, ""); } if (typeof(params) === "object") { params = Tea.serialize(params); if (params.length > 0) { if (url.indexOf("?") === -1) { url += "?" + params; } else { url += "&" + params; } } } if (typeof(hashParams) === "string") { url += "#" + hashParams; } else if (typeof(hashParams) === "object") { url += "#" + Tea.serialize(hashParams); } } return url; }; /** * 跳转 * * @param action 要跳转到的action * @param params 附带的参数 * @param hash 附带的锚点参数 */ window.Tea.go = function (action, params, hash) { var url = Tea.url(action, params); if (hash && hash.length > 0) { url += "#" + hash; } window.location.href = url; }; /** * 格式化字节数 * * @param bytes 字节数 * @returns {*} */ window.Tea.formatBytes = function (bytes) { if (bytes < 1024) { return "< 1kb"; } else if (bytes < 1024 * 1024) { return Math.round(bytes / 1024 * 100) / 100 + " kb"; } else if (bytes < 1024 * 1024 * 1024) { return Math.round(bytes / 1024 / 1024 * 100) / 100 + " mb"; } return Math.round(bytes / 1024 / 1024 / 1024 * 100) / 100 + " gb"; }; /** * 版本号对比 * * 代码来自 http://stackoverflow.com/questions/6832596/how-to-compare-software-version-number-using-js-only-number * * @param a * @param b * @returns {number} */ window.Tea.versionCompare = function compare(a, b) { if (a === b) { return 0; } var a_components = a.split("."); var b_components = b.split("."); var len = Math.min(a_components.length, b_components.length); // loop while the components are equal for (var i = 0; i < len; i++) { // A bigger than B if (parseInt(a_components[i]) > parseInt(b_components[i])) { return 1; } // B bigger than A if (parseInt(a_components[i]) < parseInt(b_components[i])) { return -1; } } // If one's a prefix of the other, the longer one is greater. if (a_components.length > b_components.length) { return 1; } if (a_components.length < b_components.length) { return -1; } // Otherwise they are the same. return 0; }; /** * 延时执行 * * @param fn 要执行的函数 * @param ms 延时长度 */ window.Tea.delay = function (fn, ms) { if (typeof(ms) === "undefined") { ms = 10; } setTimeout(function () { fn.call(Tea.Vue); }, ms); }; /** * 定义Action对象 * * @param action Action * @param params 参数集 * @constructor */ window.Tea.Action = function (action, params) { var _action = action; var _params = params; var _successFn; var _failFn; var _errorFn; var _doneFn; var _method = "POST"; var _timeout = 30; var _delay = 0; var _progressFn; var _refresh = false; this.params = function (params) { _params = params; return this; }; this.form = function (form) { _params = new FormData(form); return this; }; this.success = function (successFn) { _successFn = successFn; return this; }; this.fail = function (failFn) { _failFn = failFn; return this; }; this.error = function (errorFn) { _errorFn = errorFn; return this; }; this.done = function (doneFn) { _doneFn = doneFn; return this; }; this.timeout = function (timeout) { _timeout = timeout; return this; }; this.delay = function (delay) { _delay = delay; return this; }; this.progress = function (progressFn) { _progressFn = progressFn; return this; }; this.refresh = function () { _refresh = true; return this; }; this.post = function () { _method = "POST"; setTimeout(this._post); return this; }; this.get = function () { _method = "GET"; setTimeout(this._post); return this; }; this._post = function () { var params = _params; // 参数配置:https://github.com/axios/axios#request-config var config = { method: _method, url: Tea.url(_action), timeout: _timeout * 1000, headers: { "X-Requested-With": "XMLHttpRequest" } }; if (_progressFn != null && typeof(_progressFn) == "function") { config["onUploadProgress"] = function (event) { _progressFn.call(Tea.Vue, event.loaded, event.total, event); }; } if (_method === "GET") { config["params"] = params; } else { if (typeof(params) === "object" && params instanceof FormData) { Array.from(params).$each(function (name, object) { if (object != null && object instanceof File) { if (object.size === 0 && object.name.length === 0) { params.delete(name); } } }); config["data"] = params; } else { var formData = new FormData(); for (var key in params) { if (!params.hasOwnProperty(key)) { continue; } if (params[key] == null) { formData.append(key, ""); } else { if (typeof(params[key]) == "object" && (params[key] instanceof Array)) { for (var i = 0; i < params[key].length; i ++) { formData.append(key, params[key][i]); } } else { formData.append(key, params[key]); } } } config["data"] = formData; } } axios(config) .then(function (response) { response = response.data; setTimeout(function () { if (typeof(response) !== "object" || typeof(response.code) === "undefined") { if (typeof(_errorFn) === "function") { _errorFn.call(Tea.Vue, {}); } return; } var code = parseInt(response.code, 10); if (code === 200) { if (typeof(_successFn) === "function") { var result = _successFn.call(Tea.Vue, response); if (typeof(result) === "boolean" && !result) { return; } } if (response.message != null && response.message.length > 0) { alert(response.message); } if (response.next != null && typeof(response.next) === "object") { if (response.next.action === "*refresh") { window.location.reload(); } else { Tea.go(response.next.action, response.next.params, response.next.hash); } } // 自动刷新 if (_refresh) { window.location.reload(); } } else { if (typeof(_failFn) === "function") { _failFn.call(Tea.Vue, response); } else { Tea.failResponse(response); } } }); }) .catch(function (error) { console.log(error); if (typeof(_errorFn) === "function") { _errorFn.call(Tea.Vue, {}); } }) .then(function () { // console.log("done"); if (typeof(_doneFn) == "function") { _doneFn.call(Tea.Vue, {}); } }); }; }; /** * 取得Action对象 * * @param action Action * @returns {Window.Tea.Action} */ window.Tea.action = function (action) { return new this.Action(action); }; /** * 激活元素中的Action * * 支持 * - data-tea-action * - data-tea-confirm * - data-tea-timeout * - data-tea-before * - data-tea-success * - data-tea-fail * - data-tea-error * - data-tea-done * - data-tea-progress */ window.Tea.activate = function (element) { var nodes = Tea.element("*[data-tea-action]", element); if (nodes.length === 0) { return; } for (var i = 0; i < nodes.length; i++) { var node = nodes[i]; if (node.tagName.toUpperCase() === "FORM") { Tea.element(node).unbind("submit").bind("submit", function (e) { Tea.runActionOn(this); e.preventDefault(); e.stopPropagation(); }); } else { Tea.element(node).unbind("click").bind("click", function (e) { Tea.runActionOn(this); e.preventDefault(); e.stopPropagation(); return false; }); } } }; /** * 执行绑定data-tea-*的元素 * * @param element 元素 */ window.Tea.runActionOn = function (element) { var form = Tea.element(element); var action = form.attr("data-tea-action"); var timeout = form.attr("data-tea-timeout"); var confirm = form.attr("data-tea-confirm"); var beforeFn = form.attr("data-tea-before"); var successFn = form.attr("data-tea-success"); var failFn = form.attr("data-tea-fail"); var errorFn = form.attr("data-tea-error"); var progressFn = form.attr("data-tea-progress"); var doneFn = form.attr("data-tea-done"); if (confirm != null && confirm.length > 0 && !window.confirm(confirm)) { return; } //执行前调用beforeFn if (beforeFn != null && beforeFn.length > 0) { beforeFn = beforeFn.split("(")[0].trim(); if (typeof(Tea.Vue[beforeFn]) === "function") { var result = Tea.Vue[beforeFn].call(Tea.Vue, form); if (typeof(result) === "boolean" && !result) { return; } } } //请求对象 var actionObject = Tea.action(action) .post(); if (successFn != null && successFn.length > 0) { if (typeof(Tea.Vue[successFn]) === "function") { actionObject.success(function (resp) { Tea.Vue[successFn].call(Tea.Vue, resp); }); } } if (failFn != null && failFn.length > 0) { if (typeof(Tea.Vue[failFn]) === "function") { actionObject.fail(function (resp) { Tea.Vue[failFn].call(Tea.Vue, resp); }); } } if (errorFn != null && errorFn.length > 0) { if (typeof(Tea.Vue[errorFn]) === "function") { actionObject.error(function () { Tea.Vue[errorFn].call(Tea.Vue); }); } } if (progressFn != null && progressFn.length > 0) { if (typeof(Tea.Vue[progressFn]) === "function") { actionObject.progress(function () { Tea.Vue[progressFn].apply(Tea.Vue, arguments); }); } } if (doneFn != null && doneFn.length > 0) { if (typeof(Tea.Vue[doneFn]) === "function") { actionObject.done(function () { Tea.Vue[doneFn].apply(Tea.Vue, arguments); }); } } //超时时间 if (timeout != null) { timeout = parseFloat(timeout); if (!isNaN(timeout)) { actionObject.timeout(timeout); } } //参数 if (element.tagName.toUpperCase() === "FORM") { actionObject.form(element); } else { var attributes = element.attributes; var params = {}; for (var i = 0; i < attributes.length; i++) { var attr = attributes[i]; var match = attr.name.toString().match(/^data-(.+)$/); if (match && !match[1].match(/^tea-/)) { var pieces = match[1].split("-"); for (var j = 1; j < pieces.length; j++) { pieces[j] = pieces[j][0].toUpperCase() + pieces[j].substr(1); } var name = pieces.join(""); params[name] = attr.value; } } actionObject.params(params); } }; var teaEventListeners = {}; // element => { event => [ callback1, ... ] } function TeaElementObjects(elements) { var that = this; elements.$each(function (index, element) { that[index] = element; }); this.bind = function (event, listener) { elements.$each(function (_, element) { if (typeof(teaEventListeners[element]) == "undefined") { teaEventListeners[element] = {}; } if (typeof(teaEventListeners[element][event]) == "undefined") { teaEventListeners[element][event] = []; } teaEventListeners[element][event].push(listener); element.addEventListener(event, listener) }); return this; }; this.unbind = function (event) { elements.$each(function (_, element) { if (typeof(teaEventListeners[element]) == "undefined") { return; } if (typeof(teaEventListeners[element][event]) == "undefined") { return; } teaEventListeners[element][event].$each(function (_, listener) { element.removeEventListener(event, listener); }); teaEventListeners[element][event] = []; var hasListeners = false; for (var k in teaEventListeners[element]) { if (!teaEventListeners[element].hasOwnProperty(k)) { continue; } if (teaEventListeners[element][k] instanceof Array && teaEventListeners[element][k].length > 0) { hasListeners = true; } } if (!hasListeners) { delete(teaEventListeners[element]); } }); return this; }; this.first = function () { var first = elements.$first(); if (first != null) { return Tea.element(first); } return new TeaElementObjects([]); }; this.last = function () { var last = elements.$last(); if (last != null) { return Tea.element(last); } return new TeaElementObjects([]); }; this.attrs = function () { var first = this.first(); if (first.length === 0) { return {}; } var attrs = {}; var node = first[0]; for (var i = 0; i < node.attributes.length; i ++) { var attr = node.attributes[i]; attrs[attr.name] = attr.value; } return attrs; }; this.attr = function (name, value) { if (arguments.length === 0) { return ""; } if (arguments.length === 1) { var attrs = this.attrs(); if (typeof(attrs[name]) !== "undefined") { return attrs[name]; } return ""; } var first = this.first(); if (first.length > 0) { first[0].setAttribute(name, value); } return this; }; this.tagName = function () { var first = this.first(); if (first.length === 0) { return ""; } return first[0].tagName; }; this.focus = function () { var first = this.first(); if (first.length === 0) { return; } first[0].focus(); }; this.blur = function () { this.each(function (k, v) { v.blur(); }); }; this.each = function (iterator) { elements.$each(function (index, element) { iterator(index, element); }); return this; }; this.find = function (selector) { if (this.length == 0) { return new TeaElementObjects([]); } return Tea.element(selector, this.first()[0]); }; this.hide = function () { this.each(function (_, element) { element.style.display = "none"; }); return this; }; this.show = function () { this.each(function (_, element) { element.style.display = "block"; }); return this; }; this.text = function () { if (arguments.length > 0) { var text = arguments[0]; this.each(function (_, element) { if (typeof(element.textContent) != "undefined") { element.textContent = text; } if (typeof(element.innerText) != "undefined") { element.innerText = text; } }); return this; } if (this.length == 0) { return ""; } if (typeof(elements[0].textContent) == "string") { return elements[0].textContent; } return elements[0].innerText; }; this.html = function () { if (arguments.length > 0) { var html = arguments[0]; this.each(function (_, element) { element.innerHTML = html; }); return this; } if (this.length == 0) { return ""; } return elements[0].innerHTML; }; this.val = function () { if (arguments.length > 0) { var value = arguments[0]; this.each(function (_, element) { element.value = value; }); return this; } if (this.length == 0) { return ""; } return elements[0].value; }; this.remove = function () { this.each(function (_, element) { var parent = element.parentNode; if (parent != null) { parent.removeChild(element); } }); return this; }; this.length = elements.length; } /** * 获取元素 * * @param selector 选择器 * @param parent 父节点 * @returns {*} */ window.Tea.element = function (selector, parent) { var elements = []; if (typeof(selector) === "object" && /(function|object) \w+Element\b/.test(selector.constructor.toString())) { elements = [selector]; } else if (typeof(selector) === "object" && /function TeaElementObjects/.test(TeaElementObjects.constructor.toString())) { return selector; } else if (typeof(selector) === "string") { if (typeof(parent) === "object") { elements = Array.from(parent.querySelectorAll(selector)); } else { elements = Array.from(document.querySelectorAll(selector)); } } return new TeaElementObjects(elements); }; /** * 生成vue for用的key * @returns {number} */ window.Tea.key = function () { return Math.random() }; // 失败的响应处理 window.Tea.failResponse = function (response) { //消息提示 var hasMessage = false; if (response.message != null && response.message.length > 0) { hasMessage = true; Tea.alert(response.message); return; } if (typeof(response.errors) === "object" && response.errors != null && response.errors.length > 0) { /** * errors: [ * [field1, [ error1, error2, ....] * ... * ] * error: [ rule, message ] */ var fieldName = response.errors[0].param; var error = response.errors[0].messages[0]; var callback = function () { var element = Tea.element("*[name='" + fieldName + "']"); if (element) { element.focus(); } else { var match = fieldName.match(/^(.+)\[(\d+)]$/); if (match != null) { var index = parseInt(match[2], 10); var fields = Tea.element("*[name='" + match[1].trim() + "[]']"); if (fields.length > 0 && index < fields.length) { fields[index].focus(); } } } }; if (!hasMessage) { Tea.alert(error, callback); } else { callback(); } } }; window.Tea.alert = function (message, callback) { if (typeof(teaweb) != null) { teaweb.warn(message, function () { if (typeof(callback) == "function") { callback(); } }); } else { alert(message); if (typeof(callback) == "function") { callback(); } } }; if (typeof(window.console) === "undefined") { window.console = { log: function () {}, error: function () {}, group: function () {} }; }