`

秒杀倒计时控件

阅读更多
/**
 * User: yongwang
 * Date: 12-01-17
 * Time: 上午9:54
 */
define(function(require,exports,module){
	require("../../common/utils");
	var Detail=require("./detail");
	var PatList=require("./list");
	function CountDown(){
	this.options = {
			//countdown元素id
			countId : '',
			//outerCountId元素id
			outerCountId : '',

			//销售开始倒计时是否结束
			isSellBeginEnd : false,
			//销售结束倒计时是否结束
			isSellEndEnd : false,
			
			//销售开始时间点
			sellBeginTimeStemp:null,
			
			//销售结束时间点
			sellEndTimeStemp:null,

			//输入的服务器当前时间对象
			currStemp:null,
			//销售开始倒计时时间结束点 以ms为单位
			countDownBeginStemp:0,//ms
			
			//销售结束倒计时时间结束点 以ms为单位
			countDownEndStemp:0,//ms
			
			type:0,//默认倒计时   1为商品销售倒计时  
			
			autStatus:null,//出价状态  00为未出价  01为出局   02为领先
			
			minAmount:0,//当前最低价			
			validateCodeStats:null,//验证码验证状态  0为未验证 1为验证过  
			activityStatus:0,//活动状态  0有效  1结束
			inputStatus:0,//输入状态
			
			mdseDetailJson:null,//商品详情
			
			rootPath:"",//链接根目录
			
			isDutchEnd:false,//总倒计时已经结束
			/**
			 * 初始化倒计时
			 * @param currnt
			 * @param end
			 */
			init : function(o){
				this.countId = o.countId || 'CountDown';	
				this.outerCountId = o.outerCountId||'';	
				this.rootPath=o.rootPath||'';
				this.sellBeginTimeStemp=o.sellBeginTimeStemp||null;
				this.sellEndTimeStemp=o.sellEndTimeStemp||null;
				this.currStemp=o.currStemp||null;
				if(o.mdseDetailJson){
					this.mdseDetailJson=(new Function("","return "+o.mdseDetailJson))();;					
				}
				if(this.currStemp){
					this.currStemp=HP.parseDateMilliseconds(this.currStemp);					
				}else{
					console.error("传入的服务器时间不符合规则!请检查");
					return;
				}
				if(this.sellBeginTimeStemp){
					this.sellBeginTimeStemp=HP.parseDateMilliseconds(this.sellBeginTimeStemp);
					this.countDownBeginStemp=this.sellBeginTimeStemp- this.currStemp;
				}else{
					console.error("传入的活动开始时间不符合规则!请检查");
					return;
				}
				if(this.sellEndTimeStemp){
					this.sellEndTimeStemp=HP.parseDateMilliseconds(this.sellEndTimeStemp);
					if(this.sellBeginTimeStemp<this.currStemp){
						this.countDownEndStemp=this.sellEndTimeStemp- this.currStemp;
					}else{
						this.countDownEndStemp=this.sellEndTimeStemp- this.sellBeginTimeStemp;
					}
				}else{
					console.error("传入的活动结束时间不符合规则!请检查");
					return;
				}
				//测试
				//this.countDownEndStemp=0;
				
				
				this.type=o.type||0;
				this.validateCodeStats=o.validateCodeStats||0;
				this.autStatus=o.autStatus||"00";				
				this._CountDownLoop();				
			},						
			_CountDownLoop : function(){
				if(this.activityStatus==1){
				//转向抢拍结果页
					Detail.gotoAuctionResult(this);
				}
				if(this.countDownBeginStemp<= 0){
					this.isSellBeginEnd = true;					
				}
				if(this.countDownEndStemp<= 0){
					this.isSellEndEnd = true;
				}		
				//如果结束则调用结束回调
				if(this.isSellBeginEnd === true){
					console.log('销售开始时间已经到了');
					if(this.isSellEndEnd===true){
						console.log('销售结束时间已经到了');
						this._renderEndEnd();
						return;
					}else{
						//console.log('销售结束倒计时开始');
						switch(this.type){
							case 1:
								this._renderSellEnd(this.countDownEndStemp);
								break;
							case 2:
								this._renderSellEnd(this.countDownEndStemp);
								break;
							case 3:
								this._renderSellEnd(this.countDownEndStemp);
								break;
							default:
								this._renderSellEnd(this.countDownEndStemp);
								break;
						}
					}					
				}else{
					console.log('销售开始倒计时开始');
					switch(this.type){
						case 1:
							this._renderSellerGoods(this.countDownBeginStemp);
							break;
						case 2:
							this._renderSellerGoods(this.countDownBeginStemp);
							break;
						case 3:
							this._renderSellerGoods(this.countDownBeginStemp);
							break;
						default:
							this._renderSellerGoods(this.countDownBeginStemp);
							break;
					}                     
				}
				
				if(this.isDutchEnd===true){//已经完全结束
					return;
				}
					var that=this;
					requestAnimation(function(){
						that._CountDownLoop();
					});
			},	
			//活动开始倒计时方法			
			_renderSellerGoods:function(){
				this.countDownBeginStemp=this.countDownBeginStemp-1000;		
				var t =this.countDownBeginStemp;
					//小时
				var remain_hour=parseInt(t/(1000*60*60));
				if (remain_hour < 10){
					remain_hour = "0" + remain_hour; 
				}
					//分钟
				var remain_minute=parseInt((t-(remain_hour*1000*60*60))/(1000*60));
				if (remain_minute < 10){
					remain_minute = "0" + remain_minute;
				}	
				//秒数
				var remain_sec=parseInt((t-(remain_hour*1000*60*60)-(remain_minute*1000*60))/1000);
				if (remain_sec < 10){
					remain_sec = "0" + remain_sec; 
				}
				if(this.type==0){//列表页处理
					PatList.showListActNoCountDown(this,remain_hour,remain_minute,remain_sec);		
				}else if(this.type==2){//详情页处理
					Detail.showAuctStatus(this,null);					
					Detail.actNoInfoShow(this);
					Detail.showAuctStatus(this,null);
					//提前10分钟显示验证码输入  //根据验证码状态显示验证码输入框
					this.inputStatus=Detail.showValidateCodeInputBefore10Min(this);
					Detail.showDetailActNoCountDown(this,remain_hour,remain_minute,remain_sec);
				}				
			},	
			//活动结束倒计时调用方法
			_renderSellEnd:function(){
				this.countDownEndStemp=this.countDownEndStemp-1000;
				var t = this.countDownEndStemp;		
				//小时
				var remain_hour=parseInt(t/(1000*60*60));
				if (remain_hour < 10){
					remain_hour = "0" + remain_hour; 
				}
					//分钟
				var remain_minute=parseInt((t-(remain_hour*1000*60*60))/(1000*60));
				if (remain_minute < 10){
					remain_minute = "0" + remain_minute;
				}	
				//秒数
				var remain_sec=parseInt((t-(remain_hour*1000*60*60)-(remain_minute*1000*60))/1000);
				if (remain_sec < 10){
					remain_sec = "0" + remain_sec; 
				}
				if(this.type==0){//列表页处理
					PatList.showListActStartCountDown(this,remain_hour,remain_minute,remain_sec);				
				//活动详情页  活动开始   出价列表
				}else if(this.type==2){ //type==2为详情页使用				
					Detail.actStartInfoShow(this);
					//根据验证码状态显示验证码输入框
					Detail.showValidateCodeInput(this);
					Detail.showAuctStatus(this,this.autStatus);
					Detail.showDetailActStartCountDown(this,remain_hour,remain_minute,remain_sec);
					Detail.setAuctionPriceListEnable(this);
				}
			},
						
			//活动开始调用方法
			_renderBeginEnd:function(){		
				this.isDutchEnd=true;//倒计时完全结束
				if(this.type==0){//列表页处理
					PatList.showListActNoEndCountDown(this);	
				//活动详情页  活动未开始   出价列表
				}else if(this.type==2){//type==2为详情页使用
					Detail.showAuctStatus(this,null);
					Detail.showDetailActNoEndCountDown(this);					
					Detail.setAuctionPriceListEnable(this);
				}
			},
			_renderEndEnd:function(){		
				this.isDutchEnd=true;//倒计时完全结束
				if(this.type==0){//列表页处理
					PatList.showListActStartEndCountDown(this);				
				//活动详情页  活动结束   
				}else if(this.type==2){				
					Detail.showAuctStatus(this,null);
					Detail.showDetailActStartEndCountDown(this);
					//type==2为详情页使用  出价列表
					Detail.setAuctionPriceListDisable(this);
				}				
			}
	    };
	};
	
	var requestAnimation = (function(callback,time){
		return function(callback,time) {
				if(time){
					window.setTimeout(callback, time);
				}else{
					window.setTimeout(callback, 1000);
			   }
			};
	})();
	
	module.exports=CountDown;
});

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics