1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
'use strict'
var YX = function(accid) {
	this.accid = accid
	this.initModule()
	this.cache = new Cache()
	this.mysdk = new SDKBridge(this, this.cache)
	this.myNetcall = new NetcallBridge(this)
	this.firstLoadSysMsg = true
	this.totalUnread = 0
}
YX.fn = YX.prototype

YX.fn.initModule = function() {
	this.initBase()
	this.message()
	this.notification()
	this.personCard()
	this.sysMsg()
	this.session()
	this.friend()
	this.team()
	this.cloudMsg()
}
YX.fn.initBase = function() {
	// 初始化节点事件
	this.$mask = $('#mask')
	//左上角信息
	this.$userPic = $('#userPic')
	this.$userName = $('#userName')
	//右边面板内容节点
	this.$rightPanel = $('#rightPanel')
	this.$chatTitle = $('#chatTitle')
	this.$chatContent = $('#chatContent')
	this.$nickName = $('#nickName')
	//切换面板
	this.$switchPanel = $('#switchPanel')
	this.$switchPanel.on('click', 'a', this.switchPanel.bind(this))
	//切换的面板
	this.$sessionsWrap = $('#sessionsWrap')
	this.$friendsWrap = $('#friendsWrap')
	this.$teamsWrap = $('#teamsWrap')
	this.$panelItems = $('#left-panel .item')
	//2面板中间的圆点
	this.$chatVernier = $('#chatVernier span')
	$('#left-panel .item .list').on('scroll', this.doPoint.bind(this))

	//登出
	this.logoutEvt()
	//多端登陆
	this.multiportEvt()
}

/**
 * 同步完成后 UI显示  本demo这里显示最近会话列表 跟消息中心 新系统通知技术计数
 * @see session.js 
 */
YX.fn.initUI = function() {
	this.buildSessions()
	this.showSysMsgCount()
}
/**
 * 初始化个人信息回调 显示左上角信息
 */
YX.fn.showMe = function() {
	var user = this.cache.getUserById(userUID)
	this.$userName.text(user.nick)
	this.$userPic.attr('src', getAvatar(user.avatar))
	setCookie('nickName', user.nick)
	setCookie('avatar', user.avatar)
}
/*********************************************
 * SDK初始化结束后,拉取需要用到的用户信息,群组信息
 *********************************************/
YX.fn.initInfo = function(obj, team) {
	this.lockPerson = true
	this.lockTeam = true
	var array = Object.keys(obj),
		teamArray = []
	for(var i = team.length - 1; i >= 0; i--) {
		if(!this.cache.hasTeam(team[i])) {
			teamArray.push(team[i])
		}
	}
	if(teamArray.length > 0) {
		this.mysdk.getLocalTeams(teamArray, this.cbInitLocalTeamInfo.bind(this))
	} else {
		this.lockTeam = false
	}
	this.mysdk.getUsers(array, this.cbInitInfo.bind(this))
}
YX.fn.cbInitInfo = function(error, data) {
	if(!error) {
		this.cache.setPersonlist(data)
		this.lockPerson = false
		if(this.lockTeam === false) {
			this.initUI()
		}
	} else {
		alert("获取用户信息失败")
	}
}

YX.fn.cbInitLocalTeamInfo = function(err, data) {
	if(!err) {
		this.cache.addTeamMap(data.teams)
		this.lockTeam = false
		if(this.lockPerson === false) {
			this.initUI()
		}
	} else {
		alert("获取本地群组失败")
	}
}

/*********************************
 * 点击左边面板,打开聊天框
 *********************************/
YX.fn.openChatBox = function(account, scene) {
	var info
	var that = this
	this.mysdk.setCurrSession(scene, account)
	this.crtSession = scene + "-" + account
	this.crtSessionType = scene
	this.crtSessionAccount = account
	//隐藏其他窗口
	$('#teamInfoContainer') && $('#teamInfoContainer').addClass('hide')
	this.$devices && this.$devices.addClass('hide')
	this.$cloudMsgContainer && this.$cloudMsgContainer.addClass('hide')
	//退群的特殊UI
	this.$rightPanel.find(".u-chat-notice").addClass("hide")
	this.$rightPanel.find(".chat-mask").addClass("hide")
	
	// nim.getUser({
	// 	account: account,
	// 	sync:true,
	// 	done: getUserDone,
	// });

	// function getUserDone(error, user) {
	// 	console.log(user);
	// 	//console.log(getAvatar)
		
	// 	if(!error) {
	// 		//onUsers(user);
	// 	}

	// }
	//通过id查询用户信息	
	// $.get('/api/getUserInfo/' + account, function(res) {
	// 	if(res.code == 0) {
	// 		let userInfo = res.data
	// 		console.log(userInfo)
	// 		//localStorage.setItem('userInfo', JSON.stringify(userInfo))

	// 		if(userInfo.base) {
	// 			let uType = ''
	// 			let uTypeStr = ''
	// 			let gender = ''
	// 			let genderStr = ''
	// 			let profession_id = ''
	// 			let professionStr = ''
	// 			let phoneArr = []
	// 			let phonesHtml = ''
	// 			let lv = ''
	// 			let lvStr = ''
	// 			if(userInfo.base.icon) {
	// 				$('#showInfoBox .headBox')[0].getElementsByTagName('img')[0].src = userInfo.base.icon
	// 			}

	// 			uType = userInfo.base.utype
	// 			switch(uType) {
	// 				case 0:
	// 					uTypeStr = '普通用户';
	// 					break;
	// 				case 1:
	// 					uTypeStr = '批发商';
	// 					break;
	// 				case 2:
	// 					uTypeStr = '生产商';
	// 					break;
	// 				case 3:
	// 					uTypeStr = '终端用户';
	// 					break;
	// 			}
	// 			$('#showInfoBox .uType .conText')[0].innerHTML = uTypeStr

	// 			gender = userInfo.base.gender
	// 			switch(gender) {
	// 				case 0:
	// 					genderStr = '保密';
	// 					break;
	// 				case 1:
	// 					genderStr = '男';
	// 					break;
	// 				case 2:
	// 					genderStr = '女';
	// 					break;
	// 			}
	// 			console.log(genderStr)
	// 			$('#showInfoBox .sexBox .conText')[0].innerHTML = genderStr

	// 			if(userInfo.base.profession) {

	// 				$('#showInfoBox .jobsBox .conText')[0].innerHTML = userInfo.base.profession
	// 			}
	// 			if(userInfo.base.phones) {
	// 				phoneArr = userInfo.base.phones.split(',')
	// 				for(var i = 0; i < phoneArr.length; i++) {
	// 					phonesHtml += '<p>' + phoneArr[i] + '</p>'
	// 				}
	// 			}
	// 			if(userInfo.base.level) {
	// 				lv = userInfo.base.level
	// 				switch(lv) {
	// 					case 0:
	// 						lvStr = '未定级';
	// 						break;
	// 					case 2:
	// 						lvStr = '初级';
	// 						break;
	// 					case 3:
	// 						lvStr = '中级';
	// 						break;
	// 				}
	// 				$('#showInfoBox .certLvBox .conText')[0].innerHTML = lvStr
	// 			}
	// 			if(userInfo.base.uname) {
	// 				$('#showInfoBox .realName .conText')[0].innerHTML = userInfo.base.uname

	// 			}

	// 			$('#showInfoBox .areaBox .conText')[0].innerHTML = userInfo.base.province + ' ' + userInfo.base.city + ' ' + userInfo.base.area
	// 			$('#showInfoBox .addressBox .conText')[0].innerHTML = userInfo.base.address
	// 			$('#showInfoBox .phoneBox .conText')[0].innerHTML = userInfo.base.phone
	// 			$('#showInfoBox .assPhoneBox .conText')[0].innerHTML = phonesHtml
	// 			$('#showInfoBox .regTimeBox .conText')[0].innerHTML = userInfo.base.create_time
	// 			$('#showInfoBox .certTimeBox .conText')[0].innerHTML = userInfo.base.validity
	// 			$('#showInfoBox .companyName .conText')[0].innerHTML = userInfo.base.name

	// 		}
	// 		if(userInfo.auth) {
	// 			//渠道信息
	// 			let channelArr = []
	// 			let channelHtml = ''
	// 			let categoryArr = []
	// 			let categoryHtml = ''
	// 			let in_systemStr = ''
	// 			if(userInfo.auth.salechannels && userInfo.auth.salechannels.length > 0) {
	// 				channelArr = userInfo.auth.salechannels.split(",")
	// 				for(var i = 0; i < channelArr.length; i++) {
	// 					channelHtml += '<p>' + channelArr[i] + '</p>'
	// 				}
	// 				$('#showInfoBox .channelBox .conText')[0].innerHTML = channelHtml
	// 			}
	// 			//品类信息
	// 			if(userInfo.auth.categorys && userInfo.auth.categorys.length > 0) {
	// 				categoryArr = userInfo.auth.categorys.split(",")
	// 				for(var i = 0; i < categoryArr.length; i++) {
	// 					categoryHtml += '<p>' + categoryArr[i] + '</p>'
	// 				}
	// 				$('#showInfoBox .categoryBox .conText')[0].innerHTML = categoryHtml
	// 			}
	// 			//车辆信息

	// 			$('#showInfoBox .carBox .conText')[0].innerHTML = userInfo.auth.car
	// 			in_systemStr = userInfo.auth.in_system ? userInfo.auth.in_system : '无'

	// 			$('#showInfoBox .in_system .conText')[0].innerHTML = in_systemStr
	// 			if(userInfo.auth.annual_sales) {
	// 				$('#showInfoBox .totalSaleBox .conText')[0].innerHTML = userInfo.auth.annual_sales
	// 			}
	// 			if(userInfo.auth.ice_area) {
	// 				$('#showInfoBox .ice_areaBox .conText')[0].innerHTML = userInfo.auth.ice_area + 'm²'
	// 			}
	// 			if(userInfo.auth.all_numbers) {
	// 				$('#showInfoBox .memberBox .conText')[0].innerHTML = userInfo.auth.all_numbers + '人'
	// 			}
	// 			if(userInfo.auth.business_height) {
	// 				$('#showInfoBox .business_height .conText')[0].innerHTML = userInfo.auth.business_height
	// 			}
	// 			if(userInfo.auth.store_img) {
	// 				$('#showInfoBox .storeImgBox .conText')[0].innerHTML = '<img src="' + userInfo.auth.store_img + '">'
	// 			}

	// 		}
			
	// 		//$(".showInfoBox ").removeClass("hide")
	// 		let oBtn = $("#showInfoBox #tabTitle")[0].getElementsByTagName("span")
	// 		let con = $("#showInfoBox .conBox")
	// 		for(var i = 0; i < oBtn.length; i++) {
	// 			oBtn[i].index = i
	// 			oBtn[i].onclick = function() {
	// 				for(var j = 0; j < oBtn.length; j++) {
	// 					con[j].classList.add('hide')
	// 					oBtn[j].classList.remove("active")
	// 					con[this.index].classList.remove("hide")
	// 					this.classList.add('active')
	// 				}
	// 			}
	// 		}
	// 	}else{
	// 		that.$rightPanel.removeClass('hide')
	// 		$(".showInfoBox ").addClass("hide")
	// 	}
	// })
	that.$rightPanel.removeClass('hide')
	this.$messageText.val('')
	// 让netcall.js感知到打开聊天框的操作,做一些UI层的控制
	this.myNetcall && this.myNetcall.whenOpenChatBox(scene, account);
	if(this.WB) this.WB.checkSession()
	// 是否是群管理员
	this.cache.isCurSessionTeamManager = false
	//根据帐号跟消息类型获取消息数据
	if(scene == "p2p") {
		info = this.cache.getUserById(account)
		if(info.account == userUID) {
			this.$nickName.text("我的手机")
			this.$chatTitle.find('img').attr('src', "images/myPhone.png")
		} else {
			if(window.CONFIG.openSubscription) {
				var multiPortStatus = this.cache.getMultiPortStatus(account)
				if(multiPortStatus) {
					this.$nickName.text(this.getNick(account))
				} else {
					this.$nickName.text(this.getNick(account))
				}
			} else {
				this.$nickName.text(this.getNick(account))
			}
			this.$chatTitle.find('img').attr('src', getAvatar(info.avatar))
		}
		// 群资料入口隐藏
		this.$teamInfo && this.$teamInfo.addClass('hide')
	}
	this.doPoint()
	// 根据或取聊天记录
	this.getHistoryMsgs(scene, account)
	//如果是老板登录只开启云记录窗口
	if(readCookie('flag') == 1) {
		that.showCloudMsg()
	}
}

/**
 * 切换左边面板上方tab
 */
YX.fn.switchPanel = function(evt) {
	var node
	if(evt.target.tagName.toLowerCase() === 'span') {
		node = evt.target.parentNode
	} else {
		node = evt.target
	}
	$('.panel_tab').removeClass('cur')
	$(node).addClass('cur')
	var type = $(node).data('type')
	this.$chatVernier.addClass('hide')
	$('.item[data-type="' + type + '"]').removeClass('hide').siblings('.item').addClass('hide')
	if(type === 'sessions') {
		this.buildSessions()
	} else if(type === 'friends') {
		this.buildFriends()
	} else {
		this.buildTeams()
	}
}
/**
 * 导航圆点显示
 */
YX.fn.doPoint = function() {
	var $container
	var that = this
	if(!this.$sessionsWrap.is('.hide')) {
		$container = this.$sessionsWrap
	} else if(!this.$friendsWrap.is('.hide')) {
		$container = this.$friendsWrap
	} else {
		$container = this.$teamsWrap
	}
	var $li = $container.find(".m-panel li")
	var $active = $li.map(function() {
		$(this).removeClass("active")
		if($(this).attr('data-account') == that.crtSessionAccount) {
			$(this).addClass("active")
			return this
		}
	})
	if($active.length) {
		var top = $active.offset().top - $container.offset().top + 60
		this.$chatVernier.css('top', top).removeClass("hide")
	} else {
		this.$chatVernier.addClass("hide")
	}
}
//获取好友备注名或者昵称
YX.fn.getNick = function(account) {
	// 使用util中的工具方法
	return getNick(account, this.cache)
}

/*YX.fn.getLastCanShowMsgForInfoProvider = function(lastMsg){
    if (!lastMsg) return;
    var sessionId = lastMsg.sessionId;
    var msgs = this.cache.getMsgs(sessionId);
    var msg;
    for(var i = msgs.length - 1; i >= 0; i--) {
        msg = msgs[i];
        if(msg && (!msg.attach || msg.attach.netcallType === undefined)) {
            break;
        }
    }
    return msg;
}*/
/**
 * 列表想内容提供方法(用于ui组件)
 * @param  {Object} data 数据
 * @param  {String} type 类型
 * @return {Object} info 需要呈现的数据
 */
YX.fn.infoProvider = function(data, type) {
	var info = {}
	switch(type) {
		case "session":
			var msg = data.lastMsg
			if(!msg) {
				return;
			}
			var scene = msg.scene
			info.scene = scene
			info.account = msg.target
			info.target = msg.scene + "-" + msg.target
			info.time = transTime2(msg.time)
			info.crtSession = this.crtSession
			info.unread = data.unread > 99 ? "99+" : data.unread
			info.text = buildSessionMsg(msg)
			if(scene === "p2p") {
				//点对点
				if(msg.target === userUID) {
					info.nick = "我的手机"
					info.avatar = "images/myPhone.png"
				} else {
					var userInfo = this.cache.getUserById(msg.target)
					info.nick = this.getNick(msg.target)
					info.avatar = getAvatar(userInfo.avatar)
				}

			} else {
				//群组
				var teamInfo = this.cache.getTeamById(msg.target)
				if(teamInfo) {
					info.nick = teamInfo.name
					if(teamInfo.avatar) {
						info.avatar = teamInfo.avatar + "?imageView&thumbnail=40x40&quality=85"
					} else {
						info.avatar = "images/" + teamInfo.type + ".png"
					}
				} else {
					info.nick = msg.target
					info.avatar = "images/normal.png"
				}
			}
			break
		case "friend":
			info.target = "p2p-" + data.account
			info.account = data.account
			info.nick = this.getNick(info.account)
			info.avatar = getAvatar(data.avatar)
			info.crtSession = this.crtSession
			break
		case "team":
			info.type = data.type
			info.nick = data.name
			info.target = "team-" + data.teamId
			info.teamId = data.teamId
			if(data.avatar) {
				info.avatar = data.avatar + "?imageView&thumbnail=40x40&quality=85"
			} else {
				info.avatar = info.type === "normal" ? "images/normal.png" : "images/advanced.png"
			}
			info.crtSession = this.crtSession
			break
	}
	return info
}
/**********************************
 * 登出
 **********************************/
YX.fn.logoutEvt = function() {

	this.$logout = $('#logout')
	this.$logoutDialog = $('#logoutDialog')
	this.$logout.on('click', this.showLogoutDialog.bind(this))
	this.$logoutDialog.delegate('.j-close', 'click', this.hideLogoutDialog.bind(this))
	this.$logoutDialog.delegate('.j-ok', 'click', this.doLogout.bind(this))
}
YX.fn.doLogout = function() {
	delCookie('uid')
	delCookie('sdktoken')
	delCookie('avatar')
	delCookie('nickName')
	//window.location.href = './index.html'
}

YX.fn.showLogoutDialog = function() {
	this.$logoutDialog.removeClass('hide')
	this.$mask.removeClass('hide')
}

YX.fn.hideLogoutDialog = function() {
	this.$logoutDialog.addClass('hide')
	this.$mask.addClass('hide')
}

/**********************************************
 * 多端登录管理      
 ********************************************/
YX.fn.multiportEvt = function() {
	this.$devices = $("#devices")
	// 踢人 0:移动端 1:pc端
	$("#devices .mobile").on('click', function() {
		this.mysdk.kick(0)
	}.bind(this))
	$("#devices .pc").on('click', function() {
		this.mysdk.kick(1)
	}.bind(this))
	$("#backBtn2").on('click', this.hideDevices.bind(this))
	$(".m-devices").on('click', this.showDevices.bind(this))
}
//SDK回调
YX.fn.loginPorts = function(devices) {
		var pc, mobile
		for(var i = devices.length - 1; i >= 0; i--) {
			if(/iOS|Android|WindowsPhone/i.test(devices[i].type)) {
				mobile = devices[i]
			} else if(/Mac|PC/i.test(devices[i].type)) {
				pc = devices[i]
			}
		}
		if((pc && pc.online) || (mobile && mobile.online)) {
			if((pc && pc.online) && (mobile && mobile.online)) {
				$(".m-devices").html("正在使用云信手机版,电脑版")
				$("#devices .pc").removeClass("hide")
				$("#devices .mobile").removeClass("hide")
				this.mysdk.mobileDeviceId = mobile.deviceId
				this.mysdk.pcDeviceId = pc.deviceId
			} else if(pc && pc.online) {
				$(".m-devices").html("正在使用云信电脑版")
				$("#devices .pc").removeClass("hide")
				$("#devices .mobile").addClass("hide")
				this.mysdk.mobileDeviceId = false
				this.mysdk.pcDeviceId = pc.deviceId
			} else {
				$(".m-devices").html("正在使用云信手机版")
				$("#devices .mobile").removeClass("hide")
				$("#devices .pc").addClass("hide")
				this.mysdk.mobileDeviceId = mobile.deviceId
				this.mysdk.pcDeviceId = false
			}
			$(".m-devices").removeClass("hide")
			$("#left-panel .item").height(463)
			$("#chatVernier").css({
				marginTop: '41px'
			})
		} else {
			$(".m-devices").addClass("hide")
			$("#devices").addClass("hide")
			$("#devices .pc").addClass("hide")
			$("#devices .mobile").addClass("hide")
			this.mysdk.mobileDeviceId = false
			this.mysdk.pcDeviceId = false
			$("#left-panel .item").height(504)
			$("#chatVernier").css({
				marginTop: '0'
			})
		}
	},

	/**
	 * 多端登录面板UI
	 */
	YX.fn.showDevices = function() {
		this.$devices.removeClass("hide")
	}
YX.fn.hideDevices = function() {
	this.$devices.addClass("hide")
}
/**
 * 语音播放
 */
YX.fn.playAudio = function() {
	if(!!window.Audio) {
		var node = $(this),
			btn = $(this).children(".j-play")
		node.addClass("play")
		setTimeout(function() {
			node.removeClass("play");
		}, parseInt(btn.attr("data-dur")))
		new window.Audio(btn.attr("data-src") + "?audioTrans&type=mp3").play()
	}
}