Main.js 30.4 KB
Newer Older
F
fancy 已提交
1 2 3
MWF.require("MWF.widget.UUID", null, false);
MWF.xDesktop.requireApp("Template", "MForm", null, false);
MWF.xDesktop.requireApp("Template", "MPopupForm", null, false);
NoSubject's avatar
NoSubject 已提交
4 5 6 7 8 9 10 11 12
MWF.xApplication.IMV2.Main = new Class({
	Extends: MWF.xApplication.Common.Main,
	Implements: [Options, Events],

	options: {
		"style": "default",
		"name": "IMV2",
		"mvcStyle": "style.css",
		"icon": "icon.png",
F
fancy 已提交
13 14
		"width": "1024",
		"height": "768",
NoSubject's avatar
NoSubject 已提交
15 16
		"isResize": true,
		"isMax": true,
F
fancy 已提交
17
		"title": MWF.xApplication.IMV2.LP.title,
F
fancy 已提交
18
		"conversationId": ""
NoSubject's avatar
NoSubject 已提交
19 20 21
	},
	onQueryLoad: function () {
		this.lp = MWF.xApplication.IMV2.LP;
F
fancy 已提交
22 23
		this.app = this;
		this.conversationNodeItemList = [];
F
fancy 已提交
24 25
		this.conversationId = this.options.conversationId || "";
		this.messageList = [];
F
fancy 已提交
26 27
		this.emojiList = [];
		//添加87个表情
F
fancy 已提交
28
		for (var i = 1; i < 88; i++) {
F
fancy 已提交
29
			var emoji = {
F
fancy 已提交
30 31
				"key": i > 9 ? "[" + i + "]" : "[0" + i + "]",
				"path": i > 9 ? "/x_component_IMV2/$Main/emotions/im_emotion_" + i + ".png" : "/x_component_IMV2/$Main/emotions/im_emotion_0" + i + ".png",
F
fancy 已提交
32 33 34
			};
			this.emojiList.push(emoji);
		}
F
fancy 已提交
35
	},
F
fancy 已提交
36
	onQueryClose: function () {
F
fancy 已提交
37
		this.closeListening()
NoSubject's avatar
NoSubject 已提交
38 39 40 41
	},
	loadApplication: function (callback) {
		var url = this.path + this.options.style + "/im.html";
		this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
F
fancy 已提交
42
			//设置content
F
fancy 已提交
43
			this.app.content = this.o2ImMainNode;
F
fancy 已提交
44 45
			//启动监听
			this.startListening();
NoSubject's avatar
NoSubject 已提交
46
			//获取会话列表
F
fancy 已提交
47
			this.conversationNodeItemList = [];
NoSubject's avatar
NoSubject 已提交
48 49 50 51 52 53 54 55
			o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
				if (json.data && json.data instanceof Array) {
					this.loadConversationList(json.data);
				}
			}.bind(this));

		}.bind(this));
	},
F
fancy 已提交
56
	startListening: function () {
F
fancy 已提交
57 58 59 60 61
		this.messageNumber = layout.desktop.message.items.length;
		//查询ws消息 如果增加
		if (this.listener) {
			clearInterval(this.listener);
		}
F
fancy 已提交
62
		this.listener = setInterval(function () {
F
fancy 已提交
63 64
			var newNumber = layout.desktop.message.items.length;
			//判断是否有新的ws消息
F
fancy 已提交
65
			if (newNumber > this.messageNumber) {
F
fancy 已提交
66 67 68 69 70 71 72 73
				//查询会话数据
				this._checkConversationMessage();
				//查询聊天数据
				this._checkNewMessage();
				this.messageNumber = newNumber;
			}
		}.bind(this), 1000);
	},
F
fancy 已提交
74
	closeListening: function () {
F
fancy 已提交
75 76 77 78
		if (this.listener) {
			clearInterval(this.listener);
		}
	},
NoSubject's avatar
NoSubject 已提交
79 80 81 82
	//加载会话列表
	loadConversationList: function (list) {
		for (var i = 0; i < list.length; i++) {
			var chat = list[i];
F
fancy 已提交
83 84
			var itemNode = this._createConvItemNode(chat);
			this.conversationNodeItemList.push(itemNode);
F
fancy 已提交
85
			if (this.conversationId && this.conversationId == chat.id) {
F
fancy 已提交
86 87
				this.tapConv(chat);
			}
F
fancy 已提交
88
		}
NoSubject's avatar
NoSubject 已提交
89
	},
F
fancy 已提交
90 91 92 93 94 95
	//分页获取会话的消息列表数据
	loadMsgListByConvId: function (page, size, convId) {
		var data = { "conversationId": convId };
		o2.Actions.load("x_message_assemble_communicate").ImAction.msgListByPaging(page, size, data, function (json) {
			var list = json.data;
			for (var i = 0; i < list.length; i++) {
F
fancy 已提交
96 97
				this.messageList.push(list[i]);
				this._buildMsgNode(list[i], true);
F
fancy 已提交
98 99 100 101 102
			}
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this), false);
	},
F
fancy 已提交
103
	//点击会话
F
fancy 已提交
104
	tapConv: function (conv) {
F
fancy 已提交
105
		this._setCheckNode(conv);
NoSubject's avatar
NoSubject 已提交
106
		var url = this.path + this.options.style + "/chat.html";
F
fancy 已提交
107 108 109
		var data = { "convName": conv.title };
		this.conversationId = conv.id;
		this.chatNode.empty();
F
fancy 已提交
110
		this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
			var me = layout.session.user.distinguishedName;
			if (conv.type === "group" && me === conv.adminPerson) {
				this.chatTitleMoreBtnNode.setStyle("display", "block");
				this.chatTitleMoreBtnNode.addEvents({
					"click": function (e) {
						var display = this.chatTitleMoreMenuNode.getStyle("display");
						if (display === "none") {
							this.chatTitleMoreMenuNode.setStyle("display", "block");
						} else {
							this.chatTitleMoreMenuNode.setStyle("display", "none");
						}
					}.bind(this)
				});
			} else {
				this.chatTitleMoreBtnNode.setStyle("display", "none");
			}
F
fancy 已提交
127
			//获取聊天信息
F
fancy 已提交
128
			this.messageList = [];
F
fancy 已提交
129 130 131
			this.loadMsgListByConvId(1, 20, conv.id);
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
NoSubject's avatar
NoSubject 已提交
132 133
		}.bind(this));
	},
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
	//修改群名
	tapUpdateConvTitle: function() {
		this.chatTitleMoreMenuNode.setStyle("display", "none");
		var form = new MWF.xApplication.IMV2.UpdateConvTitleForm(this, {}, {}, { app: this.app });
		form.create();
	},
	//修改群成员
	tapUpdateConvMembers: function() {
		this.chatTitleMoreMenuNode.setStyle("display", "none");
		var members = [];
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var c = this.conversationNodeItemList[i];
			if (this.conversationId == c.data.id) {
				members = c.data.personList;
			}
		}
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": "修改成员", "personCount": 0, "personSelected": members, "isUpdateMember": true }, { app: this.app });
		form.create()
	},
F
fancy 已提交
153 154
	//点击发送消息
	sendMsg: function () {
F
fancy 已提交
155
		var text = this.chatBottomAreaTextareaNode.value;
F
fancy 已提交
156 157
		if (text) {
			this.chatBottomAreaTextareaNode.value = "";
F
fancy 已提交
158
			this._newAndSendTextMsg(text, "text");
F
fancy 已提交
159 160 161 162
		} else {
			console.log("没有消息内容!");
		}
	},
F
fancy 已提交
163
	//点击表情按钮
F
fancy 已提交
164 165
	showEmojiBox: function () {
		if (!this.emojiBoxNode) {
F
fancy 已提交
166 167
			this.emojiBoxNode = new Element("div", { "class": "chat-emoji-box" }).inject(this.chatNode);
			var _self = this;
F
fancy 已提交
168
			for (var i = 0; i < this.emojiList.length; i++) {
F
fancy 已提交
169
				var emoji = this.emojiList[i];
F
fancy 已提交
170
				var emojiNode = new Element("img", { "src": emoji.path, "class": "chat-emoji-img" }).inject(this.emojiBoxNode);
F
fancy 已提交
171 172 173 174
				emojiNode.addEvents({
					"mousedown": function (ev) {
						_self.sendEmojiMsg(this.emoji);
						_self.hideEmojiBox();
F
fancy 已提交
175
					}.bind({ emoji: emoji })
F
fancy 已提交
176 177 178 179 180 181 182
				});
			}
		}
		this.emojiBoxNode.setStyle("display", "block");
		this.hideFun = this.hideEmojiBox.bind(this);
		document.body.addEvent("mousedown", this.hideFun);
	},
F
fancy 已提交
183
	hideEmojiBox: function () {
F
fancy 已提交
184 185 186 187 188
		//关闭emojiBoxNode
		this.emojiBoxNode.setStyle("display", "none");
		document.body.removeEvent("mousedown", this.hideFun);
	},
	//发送表情消息
F
fancy 已提交
189
	sendEmojiMsg: function (emoji) {
F
fancy 已提交
190 191
		this._newAndSendTextMsg(emoji.key, "emoji");
	},
F
fancy 已提交
192
	//点击创建单聊按钮
F
fancy 已提交
193
	tapCreateSingleConv: function () {
194 195 196 197 198 199 200 201
		// var form = new MWF.xApplication.IMV2.SingleForm(this, {}, {}, { app: this.app });
		// form.create()
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": "创建单聊", "personCount": 1 }, { app: this.app });
		form.create()
	},
	//点击创建群聊按钮
	tapCreateGroupConv: function () {
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": "创建群聊", "personCount": 0, "personSelected": [] }, { app: this.app });
F
fancy 已提交
202
		form.create()
F
fancy 已提交
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
	//更新群名
	updateConversationTitle: function(title, convId) {
		var conv = {
			id: convId,
			title: title,
		};
		var _self = this;
		o2.Actions.load("x_message_assemble_communicate").ImAction.update(conv, function (json) {
			var newConv = json.data;
			//点击会话 刷新聊天界面
			_self.tapConv(newConv);
			//刷新会话列表的title
			for (var i = 0; i < this.conversationNodeItemList.length; i++) {
				var cv = this.conversationNodeItemList[i];
				if (cv.data.id == convId) {
					//刷新
					cv.refreshConvTitle(title);
				}
			}

		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
	//更新群成员
	updateConversationMembers: function(members, convId) {
		var conv = {
			id: convId,
			personList: members,
		};
		var _self = this;
		o2.Actions.load("x_message_assemble_communicate").ImAction.update(conv, function (json) {
			var newConv = json.data;
			_self.tapConv(newConv);
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
F
fancy 已提交
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	/**
	 * 	创建会话
	 * @param {*} persons 人员列表
	 * @param {*} cType 会话类型 "single" "group"
	 */
	newConversation: function (persons, cType) {
		var conv = {
			type: cType,
			personList: persons,
		};
		var _self = this;
		o2.Actions.load("x_message_assemble_communicate").ImAction.create(conv, function (json) {
			var newConv = json.data;
			var isOld = false;
			for (var i = 0; i < _self.conversationNodeItemList.length; i++) {
				var c = _self.conversationNodeItemList[i];
F
fancy 已提交
258
				if (newConv.id == c.data.id) {
F
fancy 已提交
259 260 261 262
					isOld = true;
					_self.tapConv(c);
				}
			}
F
fancy 已提交
263
			if (!isOld) {
F
fancy 已提交
264 265 266 267 268 269 270 271
				var itemNode = _self._createConvItemNode(newConv);
				_self.conversationNodeItemList.push(itemNode);
				_self.tapConv(newConv);
			}
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
F
fancy 已提交
272
	//创建会话ItemNode
F
fancy 已提交
273 274 275
	_createConvItemNode: function (conv) {
		return new MWF.xApplication.IMV2.ConversationItem(conv, this);
	},
F
fancy 已提交
276
	//会话ItemNode 点击背景色
F
fancy 已提交
277 278 279 280 281 282 283 284 285 286 287
	_setCheckNode: function (conv) {
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var item = this.conversationNodeItemList[i];
			if (item.data.id == conv.id) {
				item.addCheckClass();
			} else {
				item.removeCheckClass();
			}
		}
	},
	//创建文本消息 并发送
F
fancy 已提交
288
	_newAndSendTextMsg: function (text, type) {
F
fancy 已提交
289 290
		var distinguishedName = layout.session.user.distinguishedName;
		var time = this._currentTime();
F
fancy 已提交
291
		var body = { "body": text, "type": type };
F
fancy 已提交
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
		var bodyJson = JSON.stringify(body);
		var uuid = (new MWF.widget.UUID).toString();
		var textMessage = {
			"id": uuid,
			"conversationId": this.conversationId,
			"body": bodyJson,
			"createPerson": distinguishedName,
			"createTime": time,
			"sendStatus": 1
		};
		o2.Actions.load("x_message_assemble_communicate").ImAction.msgCreate(textMessage,
			function (json) {
				//data = json.data;
				console.log("消息发送成功!");
			}.bind(this),
			function (error) {
				console.log(error);
			}.bind(this));
F
fancy 已提交
310
		this.messageList.push(textMessage);
F
fancy 已提交
311
		this._buildSender(body, distinguishedName, false);
F
fancy 已提交
312 313 314
		this._refreshConvMessage(textMessage);
	},
	//刷新会话Item里面的最后消息内容
F
fancy 已提交
315
	_refreshConvMessage: function (msg) {
F
fancy 已提交
316 317 318
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var node = this.conversationNodeItemList[i];
			if (node.data.id == this.conversationId) {
F
fancy 已提交
319
				node.refreshLastMsg(msg);
F
fancy 已提交
320 321 322
			}
		}
	},
F
fancy 已提交
323
	//检查会话列表是否有更新
F
fancy 已提交
324
	_checkConversationMessage: function () {
F
fancy 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
		o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
			if (json.data && json.data instanceof Array) {
				var newConList = json.data;
				for (var j = 0; j < newConList.length; j++) {
					var nCv = newConList[j];
					var isNew = true;
					for (var i = 0; i < this.conversationNodeItemList.length; i++) {
						var cv = this.conversationNodeItemList[i];
						if (cv.data.id == nCv.id) {
							isNew = false;
							//刷新
							cv.refreshLastMsg(nCv.lastMessage);
						}
					}
					//新会话 创建
F
fancy 已提交
340
					if (isNew) {
F
fancy 已提交
341 342 343 344 345 346 347 348 349
						var itemNode = this._createConvItemNode(nCv);
						this.conversationNodeItemList.push(itemNode);
					}
				}
				//this.loadConversationList(json.data);
			}
		}.bind(this));
	},
	//检查是否有新消息
F
fancy 已提交
350
	_checkNewMessage: function () {
F
fancy 已提交
351 352 353 354
		if (this.conversationId && this.conversationId != "") {//是否有会话窗口
			var data = { "conversationId": this.conversationId };
			o2.Actions.load("x_message_assemble_communicate").ImAction.msgListByPaging(1, 10, data, function (json) {
				var list = json.data;
F
fancy 已提交
355
				if (list && list.length > 0) {
F
fancy 已提交
356 357 358 359 360 361 362
					var msg = list[0];
					//检查聊天框是否有变化
					if (this.conversationId == msg.conversationId) {
						for (var i = 0; i < list.length; i++) {
							var isnew = true;
							var m = list[i];
							for (var j = 0; j < this.messageList.length; j++) {
F
fancy 已提交
363
								if (this.messageList[j].id == m.id) {
F
fancy 已提交
364 365 366
									isnew = false;
								}
							}
F
fancy 已提交
367
							if (isnew) {
F
fancy 已提交
368 369 370 371 372 373 374
								this.messageList.push(m);
								this._buildMsgNode(m, false);
								// this._refreshConvMessage(m);
							}
						}
					}
				}
F
fancy 已提交
375

F
fancy 已提交
376 377 378 379 380
			}.bind(this), function (error) {
				console.log(error);
			}.bind(this), false);
		}
	},
F
fancy 已提交
381
	//创建消息html节点
F
fancy 已提交
382
	_buildMsgNode: function (msg, isTop) {
F
fancy 已提交
383 384 385 386 387
		var createPerson = msg.createPerson;
		var jsonbody = msg.body;
		var body = JSON.parse(jsonbody);//todo 目前只有一种text类型
		var distinguishedName = layout.session.user.distinguishedName;
		if (createPerson != distinguishedName) {
F
fancy 已提交
388
			this._buildReceiver(body, createPerson, isTop);
F
fancy 已提交
389
		} else {
F
fancy 已提交
390
			this._buildSender(body, createPerson, isTop);
F
fancy 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
		}
	},
	/**
	 * 消息发送体
	 * @param  msgBody 消息体
	 * @param createPerson 消息人员
	 * @param isTop 是否放在顶部
	 */
	_buildSender: function (msgBody, createPerson, isTop) {
		var receiverBodyNode = new Element("div", { "class": "chat-sender" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
		var avatarNode = new Element("div").inject(receiverBodyNode);
		var avatarUrl = this._getIcon(createPerson);
		var name = createPerson;
		if (createPerson.indexOf("@") != -1) {
			name = name.substring(0, createPerson.indexOf("@"));
		}
		var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
		var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
		var lastNode = new Element("div").inject(receiverBodyNode);
		var lastFirstNode = new Element("div", { "class": "chat-left_triangle" }).inject(lastNode);
		//text
F
fancy 已提交
412 413
		if (msgBody.type == "emoji") { // 表情
			var img = "";
F
fancy 已提交
414 415
			for (var i = 0; i < this.emojiList.length; i++) {
				if (msgBody.body == this.emojiList[i].key) {
F
fancy 已提交
416
					img = this.emojiList[i].path;
F
fancy 已提交
417
				}
F
fancy 已提交
418
			}
F
fancy 已提交
419 420 421 422 423 424
			new Element("img", { "src": img, "class": "chat-content-emoji" }).inject(lastNode);
		} else if (msgBody.type == "image") {//image
			var imgBox = new Element("div", { "class": "img-chat" }).inject(lastNode);
			var url = this._getFileUrlWithWH(msgBody.fileId, 144, 192);
			new Element("img", { "src": url }).inject(imgBox);
			imgBox.addEvents({
425
				"click": function (e) {
F
fancy 已提交
426 427 428 429 430 431
					var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
					window.open(downloadUrl);
				}.bind(this)
			});
		} else if (msgBody.type == "audio") {
			var url = this._getFileDownloadUrl(msgBody.fileId);
432
			new Element("audio", { "src": url, "controls": "controls", "preload": "preload" }).inject(lastNode);
F
fancy 已提交
433 434
		} else if (msgBody.type == "location") {
			var mapBox = new Element("span").inject(lastNode);
435
			new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width": 24, "height": 24 }).inject(mapBox);
F
fancy 已提交
436
			var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
437
			new Element("a", { "href": url, "target": "_blank", "text": msgBody.address }).inject(mapBox);
F
fancy 已提交
438
		} else {//text
F
fancy 已提交
439 440 441
			new Element("span", { "text": msgBody.body }).inject(lastNode);
		}

F
fancy 已提交
442
		if (!isTop) {
F
fancy 已提交
443 444 445
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
		}
F
fancy 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	},
	/**
	 * 消息接收体
	 * @param  msgBody 
	 * @param createPerson 消息人员
	 * @param isTop 是否放在顶部
	 */
	_buildReceiver: function (msgBody, createPerson, isTop) {
		var receiverBodyNode = new Element("div", { "class": "chat-receiver" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
		var avatarNode = new Element("div").inject(receiverBodyNode);
		var avatarUrl = this._getIcon(createPerson);
		var name = createPerson;
		if (createPerson.indexOf("@") != -1) {
			name = name.substring(0, createPerson.indexOf("@"));
		}
		var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
		var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
		var lastNode = new Element("div").inject(receiverBodyNode);
		var lastFirstNode = new Element("div", { "class": "chat-right_triangle" }).inject(lastNode);
F
fancy 已提交
465

F
fancy 已提交
466 467
		if (msgBody.type == "emoji") { // 表情
			var img = "";
F
fancy 已提交
468 469
			for (var i = 0; i < this.emojiList.length; i++) {
				if (msgBody.body == this.emojiList[i].key) {
F
fancy 已提交
470
					img = this.emojiList[i].path;
F
fancy 已提交
471
				}
F
fancy 已提交
472
			}
F
fancy 已提交
473 474 475 476 477 478
			new Element("img", { "src": img, "class": "chat-content-emoji" }).inject(lastNode);
		} else if (msgBody.type == "image") {//image
			var imgBox = new Element("div", { "class": "img-chat" }).inject(lastNode);
			var url = this._getFileUrlWithWH(msgBody.fileId, 144, 192);
			new Element("img", { "src": url }).inject(imgBox);
			imgBox.addEvents({
479
				"click": function (e) {
F
fancy 已提交
480 481 482 483 484 485
					var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
					window.open(downloadUrl);
				}.bind(this)
			});
		} else if (msgBody.type == "audio") {
			var url = this._getFileDownloadUrl(msgBody.fileId);
486
			new Element("audio", { "src": url, "controls": "controls", "preload": "preload" }).inject(lastNode);
F
fancy 已提交
487 488
		} else if (msgBody.type == "location") {
			var mapBox = new Element("span").inject(lastNode);
489
			new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width": 24, "height": 24 }).inject(mapBox);
F
fancy 已提交
490
			var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
491
			new Element("a", { "href": url, "target": "_blank", "text": msgBody.address }).inject(mapBox);
F
fancy 已提交
492
		} else {//text
F
fancy 已提交
493 494
			new Element("span", { "text": msgBody.body }).inject(lastNode);
		}
F
fancy 已提交
495 496

		if (!isTop) {
F
fancy 已提交
497 498 499
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
		}
F
fancy 已提交
500
	},
F
fancy 已提交
501
	//图片 根据大小 url
502
	_getFileUrlWithWH: function (id, width, height) {
F
fancy 已提交
503 504 505 506 507 508 509 510
		var action = MWF.Actions.get("x_message_assemble_communicate").action;
		var url = action.address + action.actions.imgFileDownloadWithWH.uri;
		url = url.replace("{id}", encodeURIComponent(id));
		url = url.replace("{width}", encodeURIComponent(width));
		url = url.replace("{height}", encodeURIComponent(height));
		return url;
	},
	//file 下载的url
511
	_getFileDownloadUrl: function (id) {
F
fancy 已提交
512 513 514 515 516 517
		var action = MWF.Actions.get("x_message_assemble_communicate").action;
		var url = action.address + action.actions.imgFileDownload.uri;
		url = url.replace("{id}", encodeURIComponent(id));
		return url;
	},
	//百度地图打开地址
518 519
	_getBaiduMapUrl: function (lat, longt, address, content) {
		var url = "https://api.map.baidu.com/marker?location=" + lat + "," + longt + "&title=" + address + "&content=" + content + "&output=html&src=net.o2oa.map";
F
fancy 已提交
520 521
		return url;
	},
F
fancy 已提交
522
	//用户头像
NoSubject's avatar
NoSubject 已提交
523 524
	_getIcon: function (id) {
		var orgAction = MWF.Actions.get("x_organization_assemble_control")
525
		var url = (id) ? orgAction.getPersonIcon(id) : "../x_component_IMV2/$Main/default/icons/group.png";
NoSubject's avatar
NoSubject 已提交
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
		return url + "?" + (new Date().getTime());
	},
	//输出特殊的时间格式
	_friendlyTime: function (date) {
		var day = date.getDate();
		var monthIndex = date.getMonth();
		var year = date.getFullYear();
		var time = date.getTime();
		var today = new Date();
		var todayDay = today.getDate();
		var todayMonthIndex = today.getMonth();
		var todayYear = today.getFullYear();
		var todayTime = today.getTime();

		var retTime = "";
		//同一天
		if (day === todayDay && monthIndex === todayMonthIndex && year === todayYear) {
			var hour = 0;
			if (todayTime > time) {
				hour = parseInt((todayTime - time) / 3600000);
				if (hour == 0) {
					retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + "分钟前"
				} else {
					retTime = hour + "小时前"
				}

			}
			return retTime;
		}
		var dates = parseInt(time / 86400000);
		var todaydates = parseInt(todayTime / 86400000);
		if (todaydates > dates) {
			var days = (todaydates - dates);
			if (days == 1) {
				retTime = "昨天";
			} else if (days == 2) {
				retTime = "前天 ";
			} else if (days > 2 && days < 31) {
				retTime = days + "天前";
			} else if (days >= 31 && days <= 2 * 31) {
				retTime = "一个月前";
			} else if (days > 2 * 31 && days <= 3 * 31) {
				retTime = "2个月前";
			} else if (days > 3 * 31 && days <= 4 * 31) {
				retTime = "3个月前";
			} else {
				retTime = this._formatDate(date);
			}
		}

		return retTime;

	},
	//yyyy-MM-dd
	_formatDate: function (date) {
		var month = date.getMonth() + 1;
		var day = date.getDate();
		month = (month.toString().length == 1) ? ("0" + month) : month;
		day = (day.toString().length == 1) ? ("0" + day) : day;
		return date.getFullYear() + '-' + month + '-' + day;
F
fancy 已提交
586
	},
F
fancy 已提交
587
	//当前时间 yyyy-MM-dd HH:mm:ss
F
fancy 已提交
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
	_currentTime: function () {
		var today = new Date();
		var year = today.getFullYear(); //得到年份
		var month = today.getMonth();//得到月份
		var date = today.getDate();//得到日期
		var hour = today.getHours();//得到小时
		var minu = today.getMinutes();//得到分钟
		var sec = today.getSeconds();//得到秒
		month = month + 1;
		if (month < 10) month = "0" + month;
		if (date < 10) date = "0" + date;
		if (hour < 10) hour = "0" + hour;
		if (minu < 10) minu = "0" + minu;
		if (sec < 10) sec = "0" + sec;
		return year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
NoSubject's avatar
NoSubject 已提交
603 604 605 606
	}


});
F
fancy 已提交
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623

//会话对象
MWF.xApplication.IMV2.ConversationItem = new Class({
	initialize: function (data, main) {
		this.data = data;
		this.main = main;
		this.container = this.main.chatItemListNode;

		this.load();
	},
	load: function () {
		var avatarDefault = this.main._getIcon();
		var convData = {
			"id": this.data.id,
			"avatarUrl": avatarDefault,
			"title": this.data.title,
			"time": "",
F
fancy 已提交
624 625
			"lastMessage": "",
			"lastMessageType": "text"
F
fancy 已提交
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		};
		var distinguishedName = layout.session.user.distinguishedName;
		if (this.data.type && this.data.type === "single") {
			var chatPerson = "";
			if (this.data.personList && this.data.personList instanceof Array) {
				for (var j = 0; j < this.data.personList.length; j++) {
					var person = this.data.personList[j];
					if (person !== distinguishedName) {
						chatPerson = person;
					}
				}
			}
			convData.avatarUrl = this.main._getIcon(chatPerson);
			var name = chatPerson;
			if (chatPerson.indexOf("@") != -1) {
				name = name.substring(0, chatPerson.indexOf("@"));
			}
			convData.title = name;
		}
		if (this.data.lastMessage) {
			//todo 其它消息类型
			var mBody = JSON.parse(this.data.lastMessage.body);
			convData.lastMessage = mBody.body;
			if (this.data.lastMessage.createTime) {
				var time = this.main._friendlyTime(o2.common.toDate(this.data.lastMessage.createTime));
				convData.time = time;
			}
F
fancy 已提交
653 654 655
			if (mBody.type) {
				convData.lastMessageType = mBody.type;
			}
F
fancy 已提交
656 657 658 659 660 661 662
		}
		this.node = new Element("div", { "class": "item" }).inject(this.container);
		this.nodeBaseItem = new Element("div", { "class": "base" }).inject(this.node);
		var avatarNode = new Element("div", { "class": "avatar" }).inject(this.nodeBaseItem);
		new Element("img", { "src": convData.avatarUrl, "class": "img" }).inject(avatarNode);
		var bodyNode = new Element("div", { "class": "body" }).inject(this.nodeBaseItem);
		var bodyUpNode = new Element("div", { "class": "body_up" }).inject(bodyNode);
663
		this.titleNode = new Element("div", { "class": "body_title", "text": convData.title }).inject(bodyUpNode);
F
fancy 已提交
664
		this.messageTimeNode = new Element("div", { "class": "body_time", "text": convData.time }).inject(bodyUpNode);
F
fancy 已提交
665
		if (convData.lastMessageType == "emoji") {
F
fancy 已提交
666
			this.lastMessageNode = new Element("div", { "class": "body_down" }).inject(bodyNode);
F
fancy 已提交
667
			var imgPath = "";
F
fancy 已提交
668
			for (var i = 0; i < this.main.emojiList.length; i++) {
F
fancy 已提交
669 670 671 672 673
				var emoji = this.main.emojiList[i];
				if (emoji.key == convData.lastMessage) {
					imgPath = emoji.path;
				}
			}
F
fancy 已提交
674 675
			new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
		} else {
F
fancy 已提交
676 677
			this.lastMessageNode = new Element("div", { "class": "body_down", "text": convData.lastMessage }).inject(bodyNode);
		}
F
fancy 已提交
678

F
fancy 已提交
679 680 681 682 683 684 685 686
		var _self = this;
		this.node.addEvents({
			"click": function () {
				_self.main.tapConv(_self.data);
			}
		});
	},
	/**
F
fancy 已提交
687
	 *
F
fancy 已提交
688 689 690
	 * 刷新会话列表的最后消息内容 
	 * @param {*} lastMessage 
	 */
F
fancy 已提交
691
	refreshLastMsg: function (lastMessage) {
F
fancy 已提交
692 693
		//目前是text 类型的消息
		var jsonbody = lastMessage.body;
F
fancy 已提交
694 695
		var body = JSON.parse(jsonbody);

F
fancy 已提交
696
		if (this.lastMessageNode) {
F
fancy 已提交
697 698
			if (body.type == "emoji") { //表情 消息
				var imgPath = "";
F
fancy 已提交
699
				for (var i = 0; i < this.main.emojiList.length; i++) {
F
fancy 已提交
700 701 702 703 704 705
					var emoji = this.main.emojiList[i];
					if (emoji.key == body.body) {
						imgPath = emoji.path;
					}
				}
				this.lastMessageNode.empty();
F
fancy 已提交
706 707
				new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
			} else { //文本消息
F
fancy 已提交
708 709 710
				this.lastMessageNode.empty();
				this.lastMessageNode.set('text', body.body);
			}
F
fancy 已提交
711 712
		}
		var time = this.main._friendlyTime(o2.common.toDate(lastMessage.createTime));
F
fancy 已提交
713
		if (this.messageTimeNode) {
F
fancy 已提交
714 715 716
			this.messageTimeNode.set("text", time);
		}
	},
717 718 719
	refreshConvTitle: function(title) {
		this.titleNode.set("text", title);
	},
F
fancy 已提交
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
	addCheckClass: function () {
		if (this.nodeBaseItem) {
			if (!this.nodeBaseItem.hasClass("check")) {
				this.nodeBaseItem.addClass("check");
			}
		}
	},
	removeCheckClass: function () {
		if (this.nodeBaseItem) {
			if (this.nodeBaseItem.hasClass("check")) {
				this.nodeBaseItem.removeClass("check");
			}
		}
	}

});

//弹出窗 表单 单聊创建的form
MWF.xApplication.IMV2.SingleForm = new Class({
	Extends: MPopupForm,
	Implements: [Options, Events],
	options: {
		"style": "minder",
		"width": 700,
		//"height": 300,
		"height": "200",
		"hasTop": true,
		"hasIcon": false,
		"draggable": true,
		"title": "创建单聊"
	},
	_createTableContent: function () {
		var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
			"<tr><td styles='formTableTitle' lable='person' width='25%'></td>" +
			"    <td styles='formTableValue14' item='person' colspan='3'></td></tr>" +
			"</table>";
		this.formTableArea.set("html", html);
		var me = layout.session.user.distinguishedName;
		var exclude = [];
		if (me) {
			exclude = [me];
		}
		this.form = new MForm(this.formTableArea, this.data || {}, {
			isEdited: true,
			style: "minder",
			hasColon: true,
			itemTemplate: {
767
				person: { text: "选择人员", type: "org", orgType: "person", count: 0, notEmpty: true, exclude: exclude },
F
fancy 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
				"text": "确定"
			}).inject(this.formBottomNode);
			this.okActionNode.addEvent("click", function (e) {
				this.save(e);
			}.bind(this));
		}
		this.cancelActionNode = new Element("button.inputCancelButton", {
			"styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
			"text": "关闭"
		}).inject(this.formBottomNode);
		this.cancelActionNode.addEvent("click", function (e) {
			this.close(e);
		}.bind(this));

	},
	save: function () {
		var data = this.form.getResult(true, null, true, false, true);
		if (data) {
			this.app.newConversation(data.person, "single");
			this.close();
		}
	}
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
});


//创建聊天 弹出窗表单
MWF.xApplication.IMV2.CreateConversationForm = new Class({
	Extends: MPopupForm,
	Implements: [Options, Events],
	options: {
		"style": "minder",
		"width": 700,
		"height": "200",
		"hasTop": true,
		"hasIcon": false,
		"draggable": true,
		"title": "创建单聊",
		"personCount": 1, //1 是单选  0 是多选,
		"personSelected": [],
		"isUpdateMember": false
	},
	_createTableContent: function () {
		var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
			"<tr><td styles='formTableTitle' lable='person' width='25%'></td>" +
			"    <td styles='formTableValue14' item='person' colspan='3'></td></tr>" +
			"</table>";
		this.formTableArea.set("html", html);
		var me = layout.session.user.distinguishedName;
		var exclude = [];
		if (me) {
			exclude = [me];
		}
		this.form = new MForm(this.formTableArea, this.data || {}, {
			isEdited: true,
			style: "minder",
			hasColon: true,
			itemTemplate: {
				person: { text: "选择人员", type: "org", orgType: "person", count: this.options["personCount"], notEmpty: true, exclude: exclude, value: this.options["personSelected"] },
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
				"text": "确定"
			}).inject(this.formBottomNode);
			this.okActionNode.addEvent("click", function (e) {
				this.save(e);
			}.bind(this));
		}
		this.cancelActionNode = new Element("button.inputCancelButton", {
			"styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
			"text": "关闭"
		}).inject(this.formBottomNode);
		this.cancelActionNode.addEvent("click", function (e) {
			this.close(e);
		}.bind(this));

	},
	save: function () {
		var data = this.form.getResult(true, null, true, false, true);
		if (data) {
			if (this.options["isUpdateMember"] === true) {
				this.app.updateConversationMembers(data.person, this.app.conversationId);
			}else {
				this.app.newConversation(data.person, this.options["personCount"] === 1 ? "single": "group");
			}
			
			this.close();
		}
	}
});



//修改群名
MWF.xApplication.IMV2.UpdateConvTitleForm = new Class({
	Extends: MPopupForm,
	Implements: [Options, Events],
	options: {
		"style": "minder",
		"width": 500,
		"height": "200",
		"hasTop": true,
		"hasIcon": false,
		"draggable": true,
		"title": "修改群名"
	},
	_createTableContent: function () {
		var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
			"<tr><td styles='formTableTitle' lable='title' width='25%'></td>" +
			"    <td styles='formTableValue14' item='title' colspan='3'></td></tr>" +
			"</table>";
		this.formTableArea.set("html", html);
		 
		this.form = new MForm(this.formTableArea, this.data || {}, {
			isEdited: true,
			style: "minder",
			hasColon: true,
			itemTemplate: {
				title: {text: "群名", type: "text", notEmpty: true },
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
				"text": "确定"
			}).inject(this.formBottomNode);
			this.okActionNode.addEvent("click", function (e) {
				this.save(e);
			}.bind(this));
		}
		this.cancelActionNode = new Element("button.inputCancelButton", {
			"styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
			"text": "关闭"
		}).inject(this.formBottomNode);
		this.cancelActionNode.addEvent("click", function (e) {
			this.close(e);
		}.bind(this));
	},
	save: function () {
		var data = this.form.getResult(true, null, true, false, true);
		if (data) {
			this.app.updateConversationTitle(data.title, this.app.conversationId);
			this.close();
		}
	}
F
fancy 已提交
931
});