(function() { // jQuery if (window.jQuery) { // ajax 默认设置 $.ajaxSetup({ async: true, // 异步调用 cache: false // 禁用缓存 }); // 扩展功能 $.extend({ // 倒计时 countDown: function(options) { var defaults = { name: "default", time: 60, onTimer: null, onTimeout: null }; options = $.extend(defaults, options); var timer = function() { options.onTimer && options.onTimer.call(null, options.time); if (options.time <= 0) { options.onTimeout && options.onTimeout.call(null); } else { $["countDownTimer_" + options.name] = setTimeout(function() { options.time--; timer(); }, 1000); } }; timer(); }, // 取消倒计时 cancelCountDown: function(name) { clearTimeout(this["countDownTimer_" + (name || "default")]); } }); } }) (); // 返回当前时间(秒值) function now() { var dt = new Date(); dt.setMilliseconds(0); return dt.getTime() / 1000; } // 返回今天日期(秒值) function today() { var dt = new Date(); dt.setHours(0, 0, 0, 0); return dt.getTime() / 1000; } // 返回日期,按 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss 格式参数 function getDate(dtStr) { dtStr = $.trim(dtStr); var a; var date, time; if (dtStr.indexOf(" ") != -1) { a = dtStr.split(" "); date = a[0]; time = a[1]; } else { date = dtStr; time = "00:00:00"; } a = date.split("-"); var y = parseInt(a[0]); var M = parseInt(a[1]) - 1; var d = parseInt(a[2]); a = time.split(":"); var h = a[0]; var m = a[1]; var s = a.length == 3 ? a[2] : 0; return new Date(y, M, d, h, m, s); } // 返回日期时间(秒值),按 yyyy-MM-dd 或 yyyy-MM-dd HH:mm:ss 格式参数 function getDateTime(dtStr) { var dt = getDate(dtStr); return dt.getTime() / 1000; } // 格式化日期(参数一:日期对象或秒值;参数二:格式[可选]) function formatDate() { var date = arguments[0]; var format = arguments[1] || "yyyy-MM-dd HH:mm:ss"; if (typeof date == "number") { date = new Date(date * 1000); } var paddNum = function(num) { num += ""; return num.replace(/^(\d)$/, "0$1"); }; var config = { yyyy : date.getFullYear(), yy : date.getFullYear().toString().substring(2), M : date.getMonth() + 1, MM : paddNum(date.getMonth() + 1), d : date.getDate(), dd : paddNum(date.getDate()), HH : paddNum(date.getHours()), mm : paddNum(date.getMinutes()), ss : paddNum(date.getSeconds()) }; return format.replace(/([a-z])(\1)*/ig, function(m){return config[m];}); } // 返回一个变量是否数组 function isArray(value) { if (typeof Array.isArray === "function") { return Array.isArray(value); } else { return Object.prototype.toString.call(value) === "[object Array]"; } } // 返回 min - max 之间的随机整数 function getRandomInt(min, max) { return parseInt((Math.random() * (max - min + 1)) + min); } // 返回一个指定长度的随机字符串 function getRandomString(len) { var base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var str = ""; for (var i=0; i0x0000&&code<=0x007F){res.push(str.charAt(i))}else if(code>=0x0080&&code<=0x07FF){var byte1=0xC0|((code>>6)&0x1F);var byte2=0x80|(code&0x3F);res.push(String.fromCharCode(byte1),String.fromCharCode(byte2))}else if(code>=0x0800&&code<=0xFFFF){var byte1=0xE0|((code>>12)&0x0F);var byte2=0x80|((code>>6)&0x3F);var byte3=0x80|(code&0x3F);res.push(String.fromCharCode(byte1),String.fromCharCode(byte2),String.fromCharCode(byte3))}else if(code>=0x00010000&&code<=0x001FFFFF){}else if(code>=0x00200000&&code<=0x03FFFFFF){}else{}};return res.join('')},UTF8ToUTF16:function(str){var res=[],len=str.length;var i=0;for(var i=0;i>7)&0xFF)==0x0){res.push(str.charAt(i))}else if(((code>>5)&0xFF)==0x6){var code2=str.charCodeAt(++i);var byte1=(code&0x1F)<<6;var byte2=code2&0x3F;var utf16=byte1|byte2;res.push(Sting.fromCharCode(utf16))}else if(((code>>4)&0xFF)==0xE){var code2=str.charCodeAt(++i);var code3=str.charCodeAt(++i);var byte1=(code<<4)|((code2>>2)&0x0F);var byte2=((code2&0x03)<<6)|(code3&0x3F);var utf16=((byte1&0x00FF)<<8)|byte2;res.push(String.fromCharCode(utf16))}else if(((code>>3)&0xFF)==0x1E){}else if(((code>>2)&0xFF)==0x3E){}else{}};return res.join('')},encode:function(str){if(!str){return''};var utf8=this.UTF16ToUTF8(str);var i=0;var len=utf8.length;var res=[];while(i>2]);if(i==len){res.push(this.table[(c1&0x3)<<4]);res.push('==');break};var c2=utf8.charCodeAt(i++);if(i==len){res.push(this.table[((c1&0x3)<<4)|((c2>>4)&0x0F)]);res.push(this.table[(c2&0x0F)<<2]);res.push('=');break};var c3=utf8.charCodeAt(i++);res.push(this.table[((c1&0x3)<<4)|((c2>>4)&0x0F)]);res.push(this.table[((c2&0x0F)<<2)|((c3&0xC0)>>6)]);res.push(this.table[c3&0x3F])};return res.join('')},decode:function(str){if(!str){return''};var len=str.length;var i=0;var res=[];while(i>4);c2=((code2&0xF)<<4)|(code3>>2);c3=((code3&0x3)<<6)|code4;res.push(String.fromCharCode(c1));if(code3!=64){res.push(String.fromCharCode(c2))};if(code4!=64){res.push(String.fromCharCode(c3))}};return this.UTF8ToUTF16(res.join(''))}};