nvsmessage.js 7.27 KB

//获取div的高度
function getHt(){
   var all_height=$(window).height();
   var div_height = all_height -  $("#headFrames").height()-4 ;
    $("#message_control").css("height",div_height+"px");
}


var g_groupTopic = '/nvsweb/message/group';
var g_onlineTopic = '/nvsweb/message/online';
var g_userList = null;
var g_groupId = "101";


		

function getUserList(){
	
	var tempData = getUserListFromCache();
	if (tempData) {
		//console.log('tempData:'+tempData); 
		g_userList = JSON.parse(""+tempData);
		return true;
	} 
	
	
	var xhr = $.ajax({
		url: "/nvsthird/getuserlist",
		type: "post",
		async: true,
		dataType: "json",
		contentType: "application/json",
		data: JSON.stringify({
			token: "",
			fromindex:0,
			toindex:-1
		}),
		timeout: 5000,
		success: function(res) {
			if (res.group != null) {
				var objToStr = JSON.stringify(res.group);
				setUserListToCache(objToStr);
				
				g_userList = res.group;
			
			} else {
				layer.msg('获取用户列表失败',{icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			layer.msg('获取用户列表超时:' + errorThrown, {icon: 2, title:'获取列表',time: 2000,skin: 'layer-ext-moon' });  
			return false;
		},
		complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
    	if(status == 'timeout'){//超时,status还有success,error等值的情况
      	 	xhr.abort();
    	} else if(status == 'error') {
				xhr.abort();
				layer.msg('获取用户列表失败', {icon: 2, time: 2000,title:'获取列表',skin: 'layer-ext-moon' });
			}
  	}
	});
	
}



function getUserObject(id) {
	for(var i = 0; i < g_userList.length; i++) {
		if(g_userList[i].id === id) {
			return g_userList[i];
		}
	}
	
	return null;
}


function buildFriendList(){
	var friendArray = new Array(); 
	var friendListArray = new Array();
	
	var myId = getUserId();
	
	for (var i = 0; i < g_userList.length; i++) {
		var uNode = g_userList[i];
		
		if(uNode.id == myId){
			//continue;
		}
		
		friendListArray.push({
				"username":"" + uNode.alias
				,"id": "" + uNode.id
				,"avatar": "img/qq.jpg"
				,"sign": uNode.id
			});
	}
	
	friendArray.push({
		"groupname":"发布公告"
		,"id": g_groupId
		,"status": "hide"
		,"list": friendListArray
	});
	
	return friendArray;
}




function startMqtt(layim,myId) {
	var randName =  Math.floor(Math.random()*(1000000-1))+1;
	var tid = "" + myId + randName;
	var srvIp = getDefaultSrvIp();
	
  	var port = '8083';
	var useSSL = false;
	if(window.location.protocol === 'https:') {
		port = ''+window.location.port;
		useSSL = true;
	}
  
	mqttX.init({
		id:tid,
		ip:srvIp,
		port:port,
		useSSL:useSSL,
		success:function(){
			console.log("mqttx连接成功");
			mqttX.subscribe(g_groupTopic,0);
			mqttX.subscribe(g_onlineTopic,0);
		},
		error:function(){
			console.log("mqttx连接失败");
		},
		connectLost:function(){
			console.log("mqttx连接丢失");
		},
		onMessage:function(message){
			var recvTopic = "" + message.destinationName;
			console.log("recvTopic:"+recvTopic);
			
			///实时告警主题
			if(recvTopic == g_groupTopic) {
				var recvMsg = "" + message.payloadString;
				var obj = JSON.parse(recvMsg);

				var mine = false;
				if(obj.mine.id == myId) {
					mine = true;
				}
				
				console.log("recvMsg:"+recvMsg);
				
				var showMsg = null;
				
				if(obj.to.type == "friend") {
					showMsg = {
						username: obj.mine.username
						,avatar: obj.to.avatar
						,id: obj.mine.id
						,type: obj.to.type
						,content: obj.mine.content
						,mine: mine //是否我发送的消息,如果为true,则会显示在右方
						,fromid: obj.mine.id //消息的发送者id(比如群组中的某个消息发送者),可用于自动解决浏览器多窗口时的一些问题
					};
				} else{
					showMsg = {
						username: obj.mine.username
						,avatar: obj.to.avatar
						,id: obj.to.id
						,type: obj.to.type
						,content: obj.mine.content
						,mine: mine //是否我发送的消息,如果为true,则会显示在右方
						,fromid: obj.mine.id //消息的发送者id(比如群组中的某个消息发送者),可用于自动解决浏览器多窗口时的一些问题
					};
				}
				
				///组消息或者自己的消息才显示
				if(obj.to.id == myId || obj.to.id == g_groupId){
					if(!mine){
						layim.getMessage(showMsg); 
					}
				}
			}
		}
	});
}

	
$(function(){
	
	getHt();	
	
	layui.use('layer', function(){
		var layer = layui.layer; 
    });
	
	getUserList();
	var friendArray = buildFriendList();
	
	var myObj = getUserObject(getUserId());
	if(isEmpty(myObj)){
		layer.msg('获取用户对象失败',{icon: 2, title:'获取用户',time: 2000,skin: 'layer-ext-moon' });
		return;
	}
	
	var randName =  Math.floor(Math.random()*(1000000-1))+1;
	var myId = "" + myObj.id;
	
	console.log('myId:'+myId); 
	
	layui.use('layim', function(layim){
		
	    //基础配置
		layim.config({
			init: {
			    mine: {
					"username": "" + myObj.alias //我的昵称
					,"id": myId //我的ID
					,"status": "online" //在线状态 online:在线、hide:隐身
					,"sign": myObj.id  //我的签名
					,"avatar": "img/qq.jpg" //我的头像
				}
				,friend: friendArray
			    ,group: [
					{
						"groupname": "发布公告"
						,"id": g_groupId
						,"avatar": "img/qq.jpg"
					}
				]
			}
			
			//查看群员接口
			/*,members: {
			  url: '<a href="javascript:buildGroupMembers()"></a>'
			  ,data: {}
			}*/
             
			
			//,msgbox: layui.cache.dir + 'css/modules/layim/html/msgbox.html' //消息盒子页面地址,若不开启,剔除该项即可
			 
			,brief: false //是否简约模式(若开启则不显示主面板)
			,title: '发布公告' //自定义主面板最小化时的标题
			//,right: '100px' //主面板相对浏览器右侧距离
			//,minRight: '90px' //聊天面板最小化时相对浏览器右侧距离
			//,initSkin: '5.jpg' //1-5 设置初始背景
			//,skin: ['aaa.jpg'] //新增皮肤
			,isfriend: true //是否开启好友
			//,isgroup: false //是否开启群组
			,min: false //是否始终最小化主面板,默认false
			//,notice: true //是否开启桌面消息提醒,默认false
			//,voice: false //声音提醒,默认开启,声音文件为:default.wav
			,copyright:false
		});

		  
		layim.chat({
			name: '发布公告'
			,type: 'group' //群组类型
			,avatar: 'img/qq.jpg'
			,id: g_groupId //定义唯一的id方便你处理信息
			,members: 0 //成员数,不好获取的话,可以设置为0
		});



	    //监听在线状态的切换事件
	    layim.on('online', function(data){
		  //console.log(data);
	    });
	 	
		  
		//监听layim建立就绪
		//layim.on('ready', function(res){


		//监听发送消息
		layim.on('sendMessage', function(data){
			var objToStr = JSON.stringify(data);
			console.log("sendMessage:" +objToStr);
			mqttX.sendMessage(g_groupTopic,data);
		});

		//监听查看群员
		layim.on('members', function(data){
		   console.log(data);
		});
		
	  
	   //监听聊天窗口的切换
		layim.on('chatChange', function(res){
		   var type = res.data.type;
		   if(type === 'friend'){
		
		   } else if(type === 'group'){
		   }
		});
		
		
		
		//监听签名
		layim.on('sign', function(value){
			console.log(value); 
		});
		
		startMqtt(layim,myId);
		
	});

});