var wst = wst || {}; wst.v = 'a1.6.0'; wst.checkbrowser = function(){ return { mozilla : /firefox/.test(navigator.useragent.tolowercase()), webkit : /webkit/.test(navigator.useragent.tolowercase()), opera : /opera/.test(navigator.useragent.tolowercase()), msie : /msie/.test(navigator.useragent.tolowercase()) } } wst.pageheight = function(){ if(wst.checkbrowser().msie){ return document.compatmode == "css1compat"? document.documentelement.clientheight : document.body.clientheight; }else{ return self.innerheight; } }; //返回当前页面宽度 wst.pagewidth = function(){ if(wst.checkbrowser().msie){ return document.compatmode == "css1compat"? document.documentelement.clientwidth : document.body.clientwidth; }else{ return self.innerwidth; } }; wst.treeselector = function(item,data,rootid,defaultvalue){ this._data = data; this._item = item; this._rootid = rootid; if(defaultvalue)this.defaultvalue = defaultvalue; } wst.treeselector.prototype.createtree = function(){ var len =this._data.length; for( var i= 0;i 31 && (charcode < 48 || charcode > 57)){ return false; }else{ return true; } } //只能輸入數字和小數點 wst.isnumberdotekey = function(evt){ var e = evt || window.event; var srcelement = e.srcelement || e.target; var charcode = (evt.which) ? evt.which : event.keycode; if (charcode > 31 && ((charcode < 48 || charcode > 57) && charcode!=46)){ return false; }else{ if(charcode==46){ var s = srcelement.value; if(s.length==0 || s.indexof(".")!=-1){ return false; } } return true; } } //只能輸入數字和字母 wst.isnumbercharkey = function(evt){ var e = evt || window.event; var srcelement = e.srcelement || e.target; var charcode = (evt.which) ? evt.which : event.keycode; if((charcode>=48 && charcode<=57) || (charcode>=65 && charcode<=90) || (charcode>=97 && charcode<=122) || charcode==8){ return true; }else{ return false; } } wst.ischinese = function(obj,isreplace){ var pattern = /[\u4e00-\u9fa5]|[\ufe30-\uffa0]/i if(pattern.test(obj.value)){ if(isreplace)obj.value=obj.value.replace(/[\u4e00-\u9fa5]|[\ufe30-\uffa0]/ig,""); return true; } return false; } number.prototype.tofixed = function(exponent){ return parseint(this * math.pow(10, exponent)+0.5 )/math.pow(10,exponent); } //用户名判断 (可输入"_",".","@", 数字,字母) wst.isusername = function(evt){ var evt = evt || window.event; var charcode = (evt.which) ? evt.which : evt.keycode; if((charcode==95 || charcode==46 || charcode==64) || (charcode>=48 && charcode<=57) || (charcode>=65 && charcode<=90) || (charcode>=97 && charcode<=122) || charcode==8){ return true; }else{ return false; } } wst.isemail =function(v){ var tel = new regexp("^\\w+((-\\w+)|(\\.\\w+))*\\@[a-za-z0-9]+((\\.|-)[a-za-z0-9]+)*\\.[a-za-z0-9]+$"); return(tel.test(v)); } //判断是否电话 wst.istel = function(v){ var tel = new regexp("^[[0-9]{3}-|\[0-9]{4}-]?(\[0-9]{8}|[0-9]{7})?$"); return(tel.test(v)); } wst.isphone = function(v){ var tel = new regexp("^[1][0-9]{10}$"); return(tel.test(v)); } //判断url wst.isurl = function(str){ if(str==null||str=="") return false; var result=str.match(/^http:\/\/[a-za-z0-9]+\.[a-za-z0-9]+[\/=\?%\-&_~`@[\]\’:+!]*([^<>\"])*$/); if(result==null)return false; return true; } //比较时间差 wst.gettimediff = function(starttime,endtime,difftype){ //将xxxx-xx-xx的时间格式,转换为 xxxx/xx/xx的格式 starttime = starttime.replace(/-/g, "/"); endtime = endtime.replace(/-/g, "/"); //将计算间隔类性字符转换为小写 difftype = difftype.tolowercase(); var stime = new date(starttime); //开始时间 var etime = new date(endtime); //结束时间 //作为除数的数字 var divnum = 1; switch (difftype) { case "second": divnum = 1000; break; case "minute": divnum = 1000 * 60; break; case "hour": divnum = 1000 * 3600; break; case "day": divnum = 1000 * 3600 * 24; break; default: break; } return parseint((etime.gettime() - stime.gettime()) / parseint(divnum)); } /** * 截取字符串 */ wst.cutstr = function (str,len) { if(!str || str=='')return ''; var strlen = 0; var s = ""; for(var i = 0;i < str.length;i++) { if(strlen >= len){ return s + "..."; } if(str.charcodeat(i) > 128) strlen += 2; else strlen++; s += str.charat(i); } return s; } wst.checkchks = function(obj,cobj){ $(cobj).each(function(){ $(this)[0].checked = obj.checked; }) } wst.getchks = function(obj){ var ids = []; $(obj).each(function(){ if($(this)[0].checked)ids.push($(this).val()); }); return ids; } wst.showhide = function(t,str){ var s = str.split(','); if(t){ for(var i=0;i-1){ if((s.length - s.indexof(".")-1)>len){ obj.value = s.substring(0,s.indexof(".")+len+1); } } s = null; } wst.getparams = function(obj){ var params = {}; var chk = {},s; $(obj).each(function(){ if($(this)[0].type=='hidden' || $(this)[0].type=='number' || $(this)[0].type=='tel' || $(this)[0].type=='password' || $(this)[0].type=='select-one' || $(this)[0].type=='textarea' || $(this)[0].type=='text'){ params[$(this).attr('id')] = $.trim($(this).val()); }else if($(this)[0].type=='radio'){ if($(this).attr('name')){ params[$(this).attr('name')] = $('input[name='+$(this).attr('name')+']:checked').val(); } }else if($(this)[0].type=='checkbox'){ if($(this).attr('name') && !chk[$(this).attr('name')]){ s = []; chk[$(this).attr('name')] = 1; $('input[name='+$(this).attr('name')+']:checked').each(function(){ s.push($(this).val()); }); params[$(this).attr('name')] = s.join(','); } } }); chk=null,s=null; return params; } wst.setvalue = function(name, value){ var first = name.substr(0,1), input, i = 0, val; if("#" === first || "." === first){ input = $(name); } else { input = $("[name='" + name + "']"); } if(input.eq(0).is(":radio")) { //单选按钮 input.filter("[value='" + value + "']").each(function(){this.checked = true}); } else if(input.eq(0).is(":checkbox")) { //复选框 if(!$.isarray(value)){ val = new array(); val[0] = value; } else { val = value; } for(i = 0, len = val.length; i < len; i++){ input.filter("[value='" + val[i] + "']").each(function(){this.checked = true}); } } else { //其他表单选项直接设置值 input.val(value); } } wst.setvalues = function(obj){ var input,value,val; for(var key in obj){ if($('#'+key)[0]){ wst.setvalue('#'+key,obj[key]); }else if($("[name='" + key + "']")[0]){ wst.setvalue(key,obj[key]); } } } $(function(){ /** * 获取wstmart基础配置 * @type {object} */ wst.conf = window.conf; /* 基础对象检测 */ wst.conf || $.error("wstmart基础配置没有正确加载!"); if(wst.conf.routes)wst.conf.routes = eval("("+wst.conf.routes+")"); /** * 解析url * @param {string} url 被解析的url * @return {object} 解析后的数据 */ wst.parse_url = function(url){ var parse = url.match(/^(?:([a-z]+):\/\/)?([\w-]+(?:\.[\w-]+)+)?(?::(\d+))?([\w-\/]+)?(?:\?((?:\w+=[^#&=\/]*)?(?:&\w+=[^#&=\/]*)*))?(?:#([\w-]+))?$/i); parse || $.error("url格式不正确!"); return { "scheme" : parse[1], "host" : parse[2], "port" : parse[3], "path" : parse[4], "query" : parse[5], "fragment" : parse[6] }; } wst.parse_str = function(str){ var value = str.split("&"), vars = {}, param; for(var i=0;i/g); var tmpv = null; for(var v in vars){ tmpv = '<'+v+'>'; if($.inarray(tmpv,urlparams)>-1){ newurl = newurl.replace(tmpv,vars[v]); delete vars[v]; } } tmpv = urlparams = null; if(false !== wst.conf.suffix){ newurl += "." + wst.conf.suffix; } if($.isplainobject(vars)){ var tmp = $.param(vars); if(tmp!='')newurl += "?"+tmp; tmp = null; } //url = url.replace(new regexp("%2f","gm"),"+"); newurl = wst.conf.app + "/"+newurl; return newurl; } wst.u0 = function(url, vars){ if(!url || url=='')return ''; var info = this.parse_url(url), path = [], reg; /* 验证info */ info.path || $.error("url格式错误!"); url = info.path; /* 解析url */ path = url.split("/"); path = [path.pop(), path.pop(), path.pop()].reverse(); path[1] || $.error("wst.u(" + url + ")没有指定控制器"); /* 解析参数 */ if(typeof vars === "string"){ vars = this.parse_str(vars); } /* 解析url自带的参数 */ info.query && $.extend(vars, this.parse_str(info.query)); if(false !== wst.conf.suffix){ url += "." + wst.conf.suffix; } if($.isplainobject(vars)){ var tmp = $.param(vars); if(tmp!='')url += "?"+tmp; tmp = null; } //url = url.replace(new regexp("%2f","gm"),"+"); url = wst.conf.app + "/"+url; return url; } wst.u = function(url,vars){ if(wst.conf.routes && wst.conf.routes[url]){ return wst.initu(url,vars); }else{ return wst.u0(url, vars); } } wst.au = function(url, vars){ if(!url || url=='')return ''; var info = this.parse_url(url); url = info.path; path = url.split("/"); url = "addon/"; path = [path.pop(), path.pop()].reverse(); path[0] || $.error("wst.au(" + url + ")没有指定控制器"); path[1] || $.error("wst.au(" + url + ")没有指定接口"); url = url + info.scheme + "-" + path.join('-'); /* 解析参数 */ if(typeof vars === "string"){ vars = this.parse_str(vars); } info.query && $.extend(vars, this.parse_str(info.query)); if(false !== wst.conf.suffix){ url += "." + wst.conf.suffix; } if($.isplainobject(vars)){ var tmp = $.param(vars); if(tmp!='')url += "?"+tmp; tmp = null; } return wst.conf.app + "/"+url; } }); wst.replaceimg = function(v,str){ var vs = v.split('.'); return v.replace("."+vs[1],str+"."+vs[1]); }