messenger.js 3.04 KB
var _messenger = (function(){
	
	//定时到离站信使清理掉(不包含80)
	!function(){
		var f = arguments.callee
			,ct = Date.parse(new Date()) / 1000
			,list, time, lineCode;
		
		try {
			var lineArray = _data.getLines();
			
			$.each(lineArray, function(){
				lineCode = this.lineCode;
				
				list = $('#messengerList' + lineCode).find('.log-item.fache,.log-item.zhongdian');
				$.each(list, function(){
					time = parseInt($(this).data('time'));
					if(ct - time >= 30)
						removeLogItem($(this));
				});
				//更新未处理的消息数量
				setUntreatedNum(lineCode);
			});
		} catch (e) {
			console.log(e);
		}
		setTimeout(f, 3000);
	}();
	
	//未处理消息数量
	function setUntreatedNum(lineCode){
		var len = $('#messengerList' + lineCode).find('.log-item').length;
		var $e = $('#top-tabs-wrap .top-nav li a[data-id='+lineCode+'] span zz:eq(0)');
		$e.text(len);
	}
	
	//应发未发 班次数量
	function setYFWFNum(lineCode){
		var size = _data.getYfwfNumByLine(lineCode);
		var $e = $('#top-tabs-wrap .top-nav li a[data-id='+lineCode+'] span zz:eq(1)');
		$e.text(size);
	}
	
	/** 信使操作 */
	$('.portlet-fullscreen').on('click', '.log-item-handle a.log-close', function(){
		removeLogItem($(this).parents('.log-item'));
	});
	
	//80上报事件处理
	//同意
	$('.portlet-fullscreen').on('click','.log-item-handle .confirm', function(){
		send80Reply(this, 0);
	});
	
	//不同意
	$('.portlet-fullscreen').on('click','.log-item-handle .dissent', function(){
		send80Reply(this, -1);
	});
	
	function send80Reply(that, reply){
		var $handle = $(that).parents('.log-item.handle');
		var id = $handle.data('id');
		var $run = $handle.find('.log-item-runing');
		$run.show();
		
		$.post('/directive/reply80', {id: id, reply: reply}, function(rs){
			if(rs.status == 'ERROR')
				$run.hide();
			
			if(rs.msg)
				layer.alert(rs.msg, {icon: 0});
		});
	}
	
	
	var messengerObj = {
		init: function(){
			$.get('/directive/findNoCofm80', {lineCodes: lineCodes}
				, function(rs){
				//填充未确认的80数据
				for(var lineCode in rs){
					$.each(rs[lineCode], function(){
						this.dateStr = moment(this.timestamp).format('HH:mm.ss');
						this.text = reqCodeMap[this.data.requestCode];
					});
					
					rs[lineCode].sort(function(a, b){
						return b.timestamp - a.timestamp;
					});
					
					var htmlStr = template('console_80_temp', {list: rs[lineCode]});
					$('#messengerList' + lineCode).html(htmlStr);
				}
			});

			//消息框滚动条
			$('.console-log .log-item-list').slimscroll({
				height: '100%'
			});
		},
		setUntreatedNum: setUntreatedNum,
		setYFWFNum: setYFWFNum
	}
	
	
	function removeLogItem($that){
		var lineCode = $that.parents('.log-item-list').data('code');
		console.log('removeLogItem', lineCode);
		$that.fadeOut('normal', function(){
			$that.remove();
			//重新计算信使数量
			setUntreatedNum(lineCode);
		});
	}
	
	countDown('messenger.js');
	return messengerObj;
})();