entrustAllSocket.js 6.86 KB
/**
 * 买卖--全部商品
 */
var EntrustingAll = function() {
	var _sock = null;
	var _attentions = null; //用户关注列表
	var _clickTag = 0;
	var _searchText = '';
	var _entrusts = null;
	var _searchTip = '请输入代码或名称';

	return {
		init : function() {
			EntrustingAll.userAttentions();
			EntrustingAll.connect();
			
			$('#txtSearch').val(_searchTip);
			$('#txtSearch').focus(function() {
				if ($('#txtSearch').val() == _searchTip) {
					$('#txtSearch').val('');
				}
			});
			$('#txtSearch').blur(function() {
				if ($('#txtSearch').val() == '') {
					$('#txtSearch').val(_searchTip);
				}
			});
			$('#txtSearch').keyup(function() {
				EntrustingAll.search();
			});
		},

		/** 连接 */
		connect : function() {
			_sock = new SockJS("/sockjs/ec/entrust/all");

			_sock.onopen = function() {
				//console.log('open');
			};

			_sock.onmessage = function(event) {
				_entrusts = $.parseJSON(event.data);
				EntrustingAll.showEntrust();
			};

			_sock.onclose = function() {
				EntrustingAll.reconnect();
			}
		},

		/** 发送消息 */
		send : function() {
			if (_sock != null) {
				_sock.send($('#txtMessage').val());
			}
		},

		/** 断开连接 */
		disconnect : function() {
			if (_sock != null) {
				_sock.close();
				_sock = null;
			}
		},

		/** 断线重连 */
		reconnect : function() {
			// console.log('reconnect');
			setTimeout("EntrustingAll.connect()", 5000);
		},

		/**显示全部商品*/
		showEntrust : function() {
			var entrusts = _entrusts;
			if (_attentions == null) {
				setTimeout(function() {
					EntrustingAll.showEntrust();
				}, 1000);
				return;
			}
			var text = new Array();
			var attentions = _attentions.split(',');
		$('#entrustList').html('');
			
			if (entrusts == null || entrusts.length == 0) {
				return;
			}
			
			for (var i = 0; i < entrusts.length; i++) {
				var entrust = entrusts[i];
				//alert("rrrr"+entrusts.length);
				var attentioned = false;
				var otcCode = entrust.otcCode;
				var otcName = entrust.otcName;
				var rose =EntrustingAll.toPercents(entrust.rose);
				//var rose =entrust.rose;
				var ups = entrust.ups;
				//var businessAmount = entrust.businessAmount;
				var businessBalance = entrust.businessBalance;  
				var openPrice = parseFloat(entrust.openPrice);
				var lastPrice = parseFloat(entrust.lastPrice);
				var trendClass = "";
				businessBalance = Math.round((businessBalance /10000) * 100) / 100;
				//businessBalance = businessBalance + "万";
				
				
			


				
				
				//涨跌标识
				if (openPrice > 0 && lastPrice > 0) {
					if (openPrice < lastPrice) {
						trendClass = " red-font";
					} else if (openPrice > lastPrice) {
						trendClass = " green-font";
					}
				}
				if(rose>0){//涨跌幅颜色
					trendClass = " red-font";
				}else if(rose<0){
					trendClass = " green-font";
				}
				if(ups>0){//涨跌颜色
					trendClass = " red-font";
				}else if(ups<0){
					trendClass = " green-font";
				}
				//搜索
				if (_searchText.length > 0) {
					var searchText = isNaN(_searchText) ? otcName : otcCode;
					if (searchText.indexOf(_searchText) < 0) {
						continue;
					}
				}
				
				lastPrice = entrust.lastPrice == null || entrust.lastPrice == 0.00 ?  '--' : entrust.lastPrice.toMoney(2, "", ",");
				rose=entrust.lastPrice == null || entrust.lastPrice == 0.00 ?  '--' : rose;
				ups=entrust.lastPrice == null || entrust.lastPrice == 0.00  ?  '--' : ups.toFixed(2);
				var buyPrice = entrust.buyPrice == null ? '--' : entrust.buyPrice.toMoney(2, "", ",");
				var buyAmount = entrust.buyAmount == null || entrust.buyAmount == 0 ? '--' : entrust.buyAmount;
				var sellPrice = entrust.sellPrice == null ? '--' : entrust.sellPrice.toMoney(2, "", ",");
				var sellAmount = entrust.sellAmount == null || entrust.sellAmount == 0 ? '--' : entrust.sellAmount;
				var businessAmount = entrust.businessAmount==null || entrust.businessAmount == 0  ? '--':entrust.businessAmount;
				businessBalance=businessBalance==null || businessBalance == 0 ? '--':businessBalance;
				if(entrust.businessAmount!=null || entrust.businessAmount != 0){
					businessAmountOne = Math.round((businessAmount /10000) * 100) / 100;
					businessAmount=(businessAmount.toString().length<5) ? businessAmount : businessAmountOne + "万";
				}
				text.push('<tr>');
				text.push('<td>' + (i + 1) + '</a></td>');
				text.push('<td class="clearHover"><a href="/ec/entrust/detail/' + otcCode + '">' + otcCode + '</td>');
				text.push('<td style="width:150px" class="left-font padding-left clearHover"><a href="/ec/entrust/detail/' + otcCode + '">' + otcName + '</td>');
				text.push('<td style="width:60px" class="right-font padding-right' + trendClass + '">' + lastPrice + '</td>');
				text.push('<td style="width:60px" class="right-font padding-right' + trendClass + '">' +rose+ '</td>');
				text.push('<td style="width:70px" class="right-font padding-right '+ trendClass +'">' + ups + '</td>');
				text.push('<td style="width:60px" class="right-font padding-right">' + businessAmount + '</td>');
				text.push('<td style="width:70px" class="right-font padding-right"> ' + businessBalance + '</td>');
				text.push('<td style="width:80px" class="padding-left">');
				for (var p in attentions) {
					if (attentions[p] == otcCode) {
						text.push('已关注');
						attentioned = true;
						break;
					}
				}
				if (!attentioned) {
					text.push('<a class="pay-attenion" onclick="EntrustingAll.attention(\'' + 
						otcCode + '\', \'' + otcName + '\', this)">+</a>');
				}
				text.push('</td>');
				text.push('<td  ><a href="/ec/entrust/detail/' + otcCode + '" class="trade-now">立即买卖</a></td>');
				text.push('</tr>');
			}
			$('#entrustList').html(text.join(''));
		},
	
	  toPercents : function (point){
			    var str=Number(point*100).toFixed(2);
			    str+="%";
			    return str;
			},
		
		/**关注*/
		attention : function(otcCode, otcName, link) {
			//防止重复点击
			if (_clickTag == 1) {
				return false;
			}
			_clickTag = 1;
			$.post('/user/attention/save', { otcCode : otcCode, otcName : otcName }, 
				function(data) {
					if (data != '') {
						$('#attentionsResult').html(data);
						$(link).prop('outerHTML', '已关注');
						$(link).onclick = function() {
							EntrustingAll.attention(otcCode, otcName, link);
						};
						EntrustingAll.userAttentions();
						easyDialog.open({
							container : 'sureAttention'
						});
					}
					_clickTag = 0;
			}, 'text');
		},
		
		/**获取用户关注列表*/
		userAttentions : function() {
			$.get('/entrust/userAttentions', function(data) {
				_attentions = data;
			}, 'text');
		},
		
		search : function() {
			_searchText = $('#txtSearch').val();
			EntrustingAll.showEntrust();
		}
	}
}();

$(function() {
	$("#navbar").find("a").removeClass("headBar-seleced");
	$("#navbar").find(".em").css("display","none");
	
	$("#deal").siblings(".em").css("display","block");
	$("#deal").addClass("headBar-seleced");
	EntrustingAll.init();
})