common_1.2.js 3.39 KB
/**
 * @desc website公用JavaScript
 * Website.Util and jquery扩展
 * @author jiangchengyong
 */
(function(){
	window.NAMESPACE = function(str){
		var arr = str.split('.'),o = this;
		for (var i = (arr[0] == 'GLOBAL')? 1 : 0; i < arr.length; i++) {
			o[arr[i]] = o[arr[i]] || {};
			o = o[arr[i]];
		}
	}
	//公用系统域名
	window.DiliPath = {
			homePath : "http://www.nong12.com/",
			userPath : "http://user.nong12.com/",
			staticPath : "http://static.nong12.com/",
			shopPath : "http://shop.nong12.com/",
			loginPath : "https://passport.nong12.com/uc/login.aspx",
			registPath : "https://passport.nong12.com/regedit/regedit.aspx",
			loadingImgPath : "http://static.nong12.com/static/common/images/gg/loading.gif",
            payPath:"http://pay.nong12.com/pay/payment/index.do",
			packageProductPath : function(productId) {
				return DiliPath.homePath + "product/" + productId + ".html";
			}
	};
	window.Money = {
		centToYuan : function(cent) {
			cent = parseInt(cent);
			if (cent <= 0) {
	            return "0.00";
	        }
			yuan = (cent / 100).toFixed(2);
			var result = yuan.toString();
			if(result.indexOf(".") <= 0) {
				result = result + ".00";
			}
	        return result;
		}
	};
	window.Website = {};
	Website.Util = {
		/**
		 * @param fn 
		 * @returns
		 * @author jiangchengyong
		 */
		checkLogin : function(fn){
			var flag = false;
			jQuery.ajax({
				type : "POST",
				url : DiliPath.homePath + "common/checkLogin.html",
				dataType : "json",
				success : function(data) {
					if(fn && typeof(fn) == 'function'){
						fn(data);
					}
					if(data.code == "000"){
						flag = true;
					}
				}
			});
			return flag;
		},
		/**
		 * 控制文本框只能输入数字
		 * @param e
		 */
		controlInput : function(e){
			var evt = e || window.event;
			var keyCode = evt.keyCode ? evt.keyCode : (evt.which ? evt.which :evt.charCode);
			if(arguments.length>1){
				for (var i = 1; i < arguments.length; i++) {
					if(keyCode == arguments[i]){
						return true;
					}
				}
			}
			if(keyCode != 9 && keyCode != 8 &&  keyCode != 127 && (keyCode<48 || keyCode>57)) {
				if(window.event){
					evt.returnValue=false;
				}else{
					e.preventDefault();
				}
			}
		},
		
		/**
		 * 校验输入的数字是否合法
		 * @param obj
		 */
		validateNum : function(obj,isDecimal){
			var reg=isDecimal?/^[0-9]+([.]{1}[0-9]{1,2})?$/ : /^[0-9]+$/;
			if(!reg.test($(obj).val())){
				$(obj).val('');
			}
		},
		/**
		 * @desc  将图片url按照type进行转换
		 * @param url
		 * @param type
		 * @param type 30*30 type=5
	     *            50*50 type=4
	     *            160*160 type=3
	     *            170*170 type=2
	     *            400*400 type=1
	     *            800*800 type=0
		 * @returns
		 */
		transformImageUrl : function(url,type){
			return url?url.replace(/\/i[0-5]/g,'/i'+type):'';
		}
	};

	/**
	 *  jQuery 扩展方法
	 *
	 *  $.Object.count( p )
	 *  获取一个对象的长度,需要指定上下文,通过 call/apply 调用
	 *  示例: $.Object.count.call( obj, true );
	 *  @param  {p} 是否跳过 null / undefined / 空值
	 *  @author jiangchengyong
	 */
	$.extend({
	    //  获取对象的长度,需要指定上下文 this
	    Object:     {
	        count: function( p ) {
	            p = p || false;
	            return $.map( this, function(o) {
	                if( !p ) return o;
	                return true;
	            }).length;
	        }
	    }
	});
})(this);