Main.js 47.0 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);
F
fancy 已提交
4
MWF.xApplication.IMV2.options.multitask = true; //多窗口
NoSubject's avatar
NoSubject 已提交
5 6 7 8 9 10 11 12 13
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 已提交
14 15
		"width": "1024",
		"height": "768",
NoSubject's avatar
NoSubject 已提交
16 17
		"isResize": true,
		"isMax": true,
F
fancy 已提交
18
		"title": MWF.xApplication.IMV2.LP.title,
F
fancy 已提交
19 20
		"conversationId": "", // 传入的当前会话id
		"mode": "default" // 展现模式:default onlyChat 。 onlyChat的模式需要传入conversationId 会打开这个会话的聊天窗口并隐藏左边的会话列表
NoSubject's avatar
NoSubject 已提交
21 22 23
	},
	onQueryLoad: function () {
		this.lp = MWF.xApplication.IMV2.LP;
F
fancy 已提交
24 25
		this.app = this;
		this.conversationNodeItemList = [];
F
fancy 已提交
26
		this.messageList = [];
F
fancy 已提交
27 28
		this.emojiList = [];
		//添加87个表情
F
fancy 已提交
29
		for (var i = 1; i < 88; i++) {
F
fancy 已提交
30
			var emoji = {
F
fancy 已提交
31 32
				"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 已提交
33 34 35
			};
			this.emojiList.push(emoji);
		}
F
fancy 已提交
36 37 38 39 40 41 42 43
		
		if (!this.status) {
			this.conversationId = this.options.conversationId || "";
			this.mode = this.options.mode || "default";
		} else {
			this.conversationId = this.status.conversationId || "";
			this.mode = this.status.mode || "default";
		}
F
fancy 已提交
44

F
fancy 已提交
45
	},
F
fancy 已提交
46 47 48 49
	// 刷新的时候缓存数据
	recordStatus: function(){
		return {"conversationId": this.conversationId, "mode": this.mode};
	},
F
fancy 已提交
50
	onQueryClose: function () {
F
fancy 已提交
51 52 53 54 55 56 57 58 59 60 61 62
		this.closeListening();
	},
	// 获取组件名称
	loadComponentName: function () {
		o2.Actions.load("x_component_assemble_control").ComponentAction.get("IMV2", function (json) {
			var imComponent = json.data;
			if (imComponent && imComponent.title) {
				this.setTitle(imComponent.title);
			}
		}.bind(this), function (err) {
			console.log(err);
		})
NoSubject's avatar
NoSubject 已提交
63
	},
F
fancy 已提交
64
	// 加载应用
NoSubject's avatar
NoSubject 已提交
65
	loadApplication: function (callback) {
66 67 68
		// 判断xadmin 打开聊天功能
		if (layout.session.user && layout.session.user.name == "xadmin") {
			console.log("xadmin can not open IMV2");
F
fancy 已提交
69
			this.app.notice(this.lp.messageXadminNotSupport, "error");
70 71
			return;
		}
F
fancy 已提交
72 73 74 75 76 77 78 79 80
		// 先加载配置文件 放入imConfig对象
		MWF.xDesktop.loadConfig(function () {
			this.imConfig = layout.config.imConfig || {}
			var url = this.path + this.options.style + "/im.html";
			this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
				//设置content
				this.app.content = this.o2ImMainNode;
				//启动监听
				this.startListening();
F
fancy 已提交
81 82 83 84 85 86 87 88 89
				// 处理窗口模式
				if (this.mode === "onlyChat" && this.conversationId != "") {
					this.o2ConversationListNode.setStyle("display", "none");
					this.chatNode.setStyle("margin-left", "2px");
				} else {
					this.o2ConversationListNode.setStyle("display", "flex");
					this.chatNode.setStyle("margin-left", "259px");
				}

F
fancy 已提交
90 91 92 93 94 95 96 97 98 99 100 101
				//获取会话列表
				this.conversationNodeItemList = [];
				o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
					if (json.data && json.data instanceof Array) {
						this.loadConversationList(json.data);
					}
				}.bind(this));
				// 管理员可见设置按钮
				if (MWF.AC.isAdministrator()) {
					this.o2ImAdminSettingNode.setStyle("display", "block");
				} else {
					this.o2ImAdminSettingNode.setStyle("display", "none");
NoSubject's avatar
NoSubject 已提交
102 103 104
				}
			}.bind(this));
		}.bind(this));
F
fancy 已提交
105
		
F
fancy 已提交
106
		this.loadComponentName();
NoSubject's avatar
NoSubject 已提交
107
	},
F
fancy 已提交
108
	// 监听ws消息
F
fancy 已提交
109
	startListening: function () {
F
fancy 已提交
110 111 112 113 114
		if (layout.desktop && layout.desktop.message) {
			this.messageNumber = layout.desktop.message.items.length;
			//查询ws消息 如果增加
			if (this.listener) {
				clearInterval(this.listener);
F
fancy 已提交
115
			}
F
fancy 已提交
116 117 118 119 120 121 122 123 124
			this.listener = setInterval(function () {
				var newNumber = layout.desktop.message.items.length;
				//判断是否有新的ws消息
				if (newNumber > this.messageNumber) {
					this.reciveNewMessage();
					this.messageNumber = newNumber;
				}
			}.bind(this), 1000);
		}
F
fancy 已提交
125
	},
F
fancy 已提交
126
	// 关闭监听
F
fancy 已提交
127
	closeListening: function () {
F
fancy 已提交
128 129 130 131
		if (this.listener) {
			clearInterval(this.listener);
		}
	},
F
fancy 已提交
132
	// 接收新的消息 会话列表更新 或者 聊天窗口更新
F
fancy 已提交
133
	reciveNewMessage: function () {
F
fancy 已提交
134 135 136 137 138
		//查询会话数据
		this._checkConversationMessage();
		//查询聊天数据
		this._checkNewMessage();
	},
NoSubject's avatar
NoSubject 已提交
139 140 141 142
	//加载会话列表
	loadConversationList: function (list) {
		for (var i = 0; i < list.length; i++) {
			var chat = list[i];
F
fancy 已提交
143 144
			var itemNode = this._createConvItemNode(chat);
			this.conversationNodeItemList.push(itemNode);
F
fancy 已提交
145
			if (this.conversationId && this.conversationId == chat.id) {
F
fancy 已提交
146 147
				this.tapConv(chat);
			}
F
fancy 已提交
148
		}
NoSubject's avatar
NoSubject 已提交
149
	},
F
fancy 已提交
150 151 152 153 154 155
	//分页获取会话的消息列表数据
	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 已提交
156 157
				this.messageList.push(list[i]);
				this._buildMsgNode(list[i], true);
F
fancy 已提交
158 159 160 161 162
			}
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this), false);
	},
F
fancy 已提交
163 164 165 166 167 168 169
	// 点击设置按钮
	tapOpenSettings: function() {
		this.openSettingsDialog();
	},
	// 打开IM配置文件
	openSettingsDialog: function () {
		var settingNode = new Element("div", {"style":"padding:10px;background-color:#fff;"});
F
fancy 已提交
170 171 172 173 174 175 176

		var lineNode = new Element("div", {"style":"height:24px;line-height: 24px;", "text": this.lp.settingsClearMsg}).inject(settingNode);
		var isClearEnableNode = new Element("input", {"type":"checkbox", "checked": this.imConfig.enableClearMsg || false, "name": "clearEnable"}).inject(lineNode);

		var line2Node = new Element("div", {"style":"height:24px;line-height: 24px;", "text": this.lp.settingsRevokeMsg}).inject(settingNode);
		var isRevokeEnableNode = new Element("input", {"type":"checkbox", "checked": this.imConfig.enableRevokeMsg || false, "name": "revokeEnable"}).inject(line2Node);

F
fancy 已提交
177 178 179
		var dlg = o2.DL.open({
				"title": this.lp.setting,
				"mask": true,
F
fancy 已提交
180
				"height": "200",
F
fancy 已提交
181 182 183 184 185 186 187 188 189 190
				"content": settingNode,
				"onQueryClose": function () {
					settingNode.destroy();
				}.bind(this),
				"buttonList": [
					{
						"type": "ok",
						"text": this.lp.ok,
						"action": function () { 
							this.imConfig.enableClearMsg = isClearEnableNode.get("checked");
F
fancy 已提交
191
							this.imConfig.enableRevokeMsg = isRevokeEnableNode.get("checked");
F
fancy 已提交
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
							this.postIMConfig(this.imConfig);
							// 保存配置文件
							dlg.close(); 
						}.bind(this)
					},
					{
							"type": "cancel",
							"text": this.lp.close,
							"action": function () { dlg.close(); }
					}
				],
				"onPostShow": function () {
						dlg.reCenter();
				}.bind(this),
				"onPostClose": function(){
					dlg = null;
				}.bind(this)
		});
	},
	// 保存IM配置文件
	postIMConfig: function (imConfig) {
		o2.Actions.load("x_message_assemble_communicate").ImAction.config(imConfig, function (json) {
			this.refresh();//重新加载整个IM应用
		}.bind(this), function (error) {
			console.log(error);
			this.app.notice(error, "error", this.app.content);
		}.bind(this));
	},
F
fancy 已提交
220
	//点击会话
F
fancy 已提交
221
	tapConv: function (conv) {
F
fancy 已提交
222
		this._setCheckNode(conv);
NoSubject's avatar
NoSubject 已提交
223
		var url = this.path + this.options.style + "/chat.html";
NoSubject's avatar
NoSubject 已提交
224
		var data = { "convName": conv.title, "lp": this.lp };
F
fancy 已提交
225 226
		this.conversationId = conv.id;
		this.chatNode.empty();
F
fancy 已提交
227 228 229 230
		if (this.emojiBoxNode) {
			this.emojiBoxNode.destroy();
			this.emojiBoxNode = null;
		}
F
fancy 已提交
231
		this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
232 233 234 235 236 237 238 239
			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");
F
fancy 已提交
240 241 242 243 244 245 246
							this.chatTitleMoreMenuItem1Node.setStyle("display", "block");
							this.chatTitleMoreMenuItem2Node.setStyle("display", "block");
							if (this.imConfig.enableClearMsg) {
								this.chatTitleMoreMenuItem3Node.setStyle("display", "block");
							} else {
								this.chatTitleMoreMenuItem3Node.setStyle("display", "none");
							}
247 248 249 250 251
						} else {
							this.chatTitleMoreMenuNode.setStyle("display", "none");
						}
					}.bind(this)
				});
F
fancy 已提交
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
			} else if (conv.type !== "group") {
				if (this.imConfig.enableClearMsg) {
					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");
								this.chatTitleMoreMenuItem1Node.setStyle("display", "none");
								this.chatTitleMoreMenuItem2Node.setStyle("display", "none");
								this.chatTitleMoreMenuItem3Node.setStyle("display", "block");
							} else {
								this.chatTitleMoreMenuNode.setStyle("display", "none");
							}
						}.bind(this)
					});
				} else {
					this.chatTitleMoreBtnNode.setStyle("display", "none");
				}
271
			}
F
fancy 已提交
272
			//获取聊天信息
F
fancy 已提交
273
			this.messageList = [];
F
fancy 已提交
274 275 276
			this.loadMsgListByConvId(1, 20, conv.id);
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
			// 绑定事件
			this.chatBottomAreaTextareaNode.addEvents({
				"keyup": function (e) {
					// debugger;
					if (e.code === 13) {
						if (e.control === true) {
							var text = this.chatBottomAreaTextareaNode.value;
							this.chatBottomAreaTextareaNode.value = text + "\n";
						} else {
							this.sendMsg();
						}
						e.stopPropagation();
					}
				}.bind(this)
			});
NoSubject's avatar
NoSubject 已提交
292 293
		}.bind(this));
	},
294
	//修改群名
F
fancy 已提交
295
	tapUpdateConvTitle: function () {
296
		this.chatTitleMoreMenuNode.setStyle("display", "none");
297 298 299 300 301 302 303 304
		var title = "";
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var c = this.conversationNodeItemList[i];
			if (this.conversationId == c.data.id) {
				title = c.data.title;
			}
		}
		var form = new MWF.xApplication.IMV2.UpdateConvTitleForm(this, {}, {"defaultValue": title}, { app: this.app });
305 306 307
		form.create();
	},
	//修改群成员
F
fancy 已提交
308
	tapUpdateConvMembers: function () {
309 310 311 312 313 314 315 316
		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;
			}
		}
NoSubject's avatar
NoSubject 已提交
317
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": this.lp.modifyMember, "personCount": 0, "personSelected": members, "isUpdateMember": true }, { app: this.app });
318 319
		form.create()
	},
F
fancy 已提交
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	// 点击菜单 清空聊天记录
	tapClearMsg: function(e) {
		var _self = this;
		MWF.xDesktop.confirm("info", this.chatTitleNode, this.lp.alert, this.lp.messageClearAllMsgAlert, 400, 150, function() {
			o2.Actions.load("x_message_assemble_communicate").ImAction.clearConversationMsg(_self.conversationId, function (json) {
				_self._reclickConv();
			}, function (error) {
				console.log(error);
				_self.app.notice(error, "error", _self.app.content);
			});
			this.close();
		}, function(){
			this.close();
		}, null, null, "o2");
	},
	_reclickConv: function() {
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var c = this.conversationNodeItemList[i];
			if (this.conversationId == c.data.id) {
				this.tapConv(c.data);
			}
		}
	},
F
fancy 已提交
343 344
	//点击发送消息
	sendMsg: function () {
F
fancy 已提交
345
		var text = this.chatBottomAreaTextareaNode.value;
F
fancy 已提交
346 347
		if (text) {
			this.chatBottomAreaTextareaNode.value = "";
F
fancy 已提交
348
			this._newAndSendTextMsg(text, "text");
F
fancy 已提交
349
		} else {
NoSubject's avatar
NoSubject 已提交
350
			console.log(this.lp.noMessage);
F
fancy 已提交
351
			this.app.notice(this.lp.noMessage, "error", this.app.content);
F
fancy 已提交
352 353
		}
	},
F
fancy 已提交
354
	//点击表情按钮
F
fancy 已提交
355 356
	showEmojiBox: function () {
		if (!this.emojiBoxNode) {
F
fancy 已提交
357 358
			this.emojiBoxNode = new Element("div", { "class": "chat-emoji-box" }).inject(this.chatNode);
			var _self = this;
F
fancy 已提交
359
			for (var i = 0; i < this.emojiList.length; i++) {
F
fancy 已提交
360
				var emoji = this.emojiList[i];
F
fancy 已提交
361
				var emojiNode = new Element("img", { "src": emoji.path, "class": "chat-emoji-img" }).inject(this.emojiBoxNode);
F
fancy 已提交
362 363 364 365
				emojiNode.addEvents({
					"mousedown": function (ev) {
						_self.sendEmojiMsg(this.emoji);
						_self.hideEmojiBox();
F
fancy 已提交
366
					}.bind({ emoji: emoji })
F
fancy 已提交
367 368 369 370 371 372 373
				});
			}
		}
		this.emojiBoxNode.setStyle("display", "block");
		this.hideFun = this.hideEmojiBox.bind(this);
		document.body.addEvent("mousedown", this.hideFun);
	},
F
fancy 已提交
374
	// 点击发送文件消息
F
fancy 已提交
375 376
	showChooseFile: function () {
		if (!this.uploadFileAreaNode) {
F
fancy 已提交
377 378 379 380 381
			this.createUploadFileNode();
		}
		this.fileUploadNode.click();
	},
	//创建文件选择框
F
fancy 已提交
382
	createUploadFileNode: function () {
F
fancy 已提交
383 384 385 386
		this.uploadFileAreaNode = new Element("div");
		var html = "<input name=\"file\" type=\"file\" multiple/>";
		this.uploadFileAreaNode.set("html", html);
		this.fileUploadNode = this.uploadFileAreaNode.getFirst();
F
fancy 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
		this.fileUploadNode.addEvent("change", function () {
			var files = this.fileUploadNode.files;
			if (files.length) {
				var file = files.item(0);
				var formData = new FormData();
				formData.append('file', file);
				formData.append('fileName', file.name);
				var fileExt = file.name.substring(file.name.lastIndexOf("."));
				// 图片消息
				var type = "file"
				if (fileExt.toLowerCase() == ".bmp" || fileExt.toLowerCase() == ".jpeg"
					|| fileExt.toLowerCase() == ".png" || fileExt.toLowerCase() == ".jpg") {
					type = "image"
				} else { // 文件消息
					type = "file"
F
fancy 已提交
402
				}
F
fancy 已提交
403 404 405 406 407 408 409 410 411 412 413 414
				//上传文件
				o2.Actions.load("x_message_assemble_communicate").ImAction.uploadFile(this.conversationId, type, formData, "{}", function (json) {
					if (json.data) {
						var fileId = json.data.id
						var fileExtension = json.data.fileExtension
						var fileName = json.data.fileName
						this._newImageOrFileMsgAndSend(type, fileId, fileName, fileExtension)
					}
				}.bind(this), function (error) {
					console.log(error);
				}.bind(this))
			}
F
fancy 已提交
415 416
		}.bind(this));
	},
F
fancy 已提交
417
	hideEmojiBox: function () {
F
fancy 已提交
418 419 420 421 422
		//关闭emojiBoxNode
		this.emojiBoxNode.setStyle("display", "none");
		document.body.removeEvent("mousedown", this.hideFun);
	},
	//发送表情消息
F
fancy 已提交
423
	sendEmojiMsg: function (emoji) {
F
fancy 已提交
424 425
		this._newAndSendTextMsg(emoji.key, "emoji");
	},
F
fancy 已提交
426
	//点击创建单聊按钮
F
fancy 已提交
427
	tapCreateSingleConv: function () {
428 429
		// var form = new MWF.xApplication.IMV2.SingleForm(this, {}, {}, { app: this.app });
		// form.create()
NoSubject's avatar
NoSubject 已提交
430
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": this.lp.createSingle, "personCount": 1 }, { app: this.app });
431 432 433 434
		form.create()
	},
	//点击创建群聊按钮
	tapCreateGroupConv: function () {
NoSubject's avatar
NoSubject 已提交
435
		var form = new MWF.xApplication.IMV2.CreateConversationForm(this, {}, { "title": this.lp.createDroup, "personCount": 0, "personSelected": [] }, { app: this.app });
F
fancy 已提交
436
		form.create()
F
fancy 已提交
437
	},
438
	//更新群名
F
fancy 已提交
439
	updateConversationTitle: function (title, convId) {
440 441 442 443 444 445 446 447
		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;
			//点击会话 刷新聊天界面
448 449 450 451 452 453 454 455 456 457 458
			// _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);
			// 	}
			// }
			// 列表上的数据也要刷新
			_self.reciveNewMessage();
459 460 461 462 463 464

		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
	//更新群成员
F
fancy 已提交
465
	updateConversationMembers: function (members, convId) {
466 467 468 469 470 471 472
		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;
473 474 475
			//_self.tapConv(newConv);
			// 列表上的数据也要刷新
			_self.reciveNewMessage();
476 477 478 479
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
F
fancy 已提交
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	/**
	 * 	创建会话
	 * @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 已提交
496
				if (newConv.id == c.data.id) {
F
fancy 已提交
497 498 499 500
					isOld = true;
					_self.tapConv(c);
				}
			}
F
fancy 已提交
501
			if (!isOld) {
F
fancy 已提交
502 503 504 505 506 507 508 509
				var itemNode = _self._createConvItemNode(newConv);
				_self.conversationNodeItemList.push(itemNode);
				_self.tapConv(newConv);
			}
		}.bind(this), function (error) {
			console.log(error);
		}.bind(this))
	},
F
fancy 已提交
510
	//创建会话ItemNode
F
fancy 已提交
511 512 513
	_createConvItemNode: function (conv) {
		return new MWF.xApplication.IMV2.ConversationItem(conv, this);
	},
F
fancy 已提交
514
	//会话ItemNode 点击背景色
F
fancy 已提交
515 516 517 518 519 520 521 522 523 524
	_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 已提交
525
	//创建图片或文件消息
F
fancy 已提交
526
	_newImageOrFileMsgAndSend: function (type, fileId, fileName, fileExt) {
F
fancy 已提交
527 528 529
		var distinguishedName = layout.session.user.distinguishedName;
		var time = this._currentTime();
		var body = {
NoSubject's avatar
NoSubject 已提交
530
			"body": this.lp.file,
F
fancy 已提交
531 532 533
			"type": type,
			"fileId": fileId,
			"fileExtension": fileExt,
F
fancy 已提交
534 535 536 537 538 539 540 541 542 543 544 545 546 547
			"fileName": fileName
		};
		var bodyJson = JSON.stringify(body);
		var uuid = (new MWF.widget.UUID).toString();
		var message = {
			"id": uuid,
			"conversationId": this.conversationId,
			"body": bodyJson,
			"createPerson": distinguishedName,
			"createTime": time,
			"sendStatus": 1
		};
		o2.Actions.load("x_message_assemble_communicate").ImAction.msgCreate(message,
			function (json) {
NoSubject's avatar
NoSubject 已提交
548
				console.log(this.lp.sendSuccess);
F
fancy 已提交
549 550 551 552 553
			}.bind(this),
			function (error) {
				console.log(error);
			}.bind(this));
		this.messageList.push(message);
F
fancy 已提交
554
		this._buildReceiver(body, distinguishedName, false, message);
F
fancy 已提交
555 556
		this._refreshConvMessage(message);
	},
F
fancy 已提交
557
	//创建文本消息 并发送
F
fancy 已提交
558
	_newAndSendTextMsg: function (text, type) {
F
fancy 已提交
559 560
		var distinguishedName = layout.session.user.distinguishedName;
		var time = this._currentTime();
F
fancy 已提交
561
		var body = { "body": text, "type": type };
F
fancy 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574
		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;
NoSubject's avatar
NoSubject 已提交
575
				console.log(this.lp.sendSuccess);
F
fancy 已提交
576 577 578 579
			}.bind(this),
			function (error) {
				console.log(error);
			}.bind(this));
F
fancy 已提交
580
		this.messageList.push(textMessage);
F
fancy 已提交
581
		this._buildReceiver(body, distinguishedName, false, textMessage);
F
fancy 已提交
582 583 584
		this._refreshConvMessage(textMessage);
	},
	//刷新会话Item里面的最后消息内容
F
fancy 已提交
585
	_refreshConvMessage: function (msg) {
F
fancy 已提交
586 587 588
		for (var i = 0; i < this.conversationNodeItemList.length; i++) {
			var node = this.conversationNodeItemList[i];
			if (node.data.id == this.conversationId) {
F
fancy 已提交
589
				node.refreshLastMsg(msg);
F
fancy 已提交
590 591 592
			}
		}
	},
F
fancy 已提交
593
	//检查会话列表是否有更新
F
fancy 已提交
594
	_checkConversationMessage: function () {
F
fancy 已提交
595 596 597 598 599 600 601 602 603 604 605 606
		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);
607 608 609 610
							cv.refreshData(nCv);
							if (this.conversationId === nCv.id) {
								this.tapConv(nCv);
							}
F
fancy 已提交
611 612 613
						}
					}
					//新会话 创建
F
fancy 已提交
614
					if (isNew) {
F
fancy 已提交
615 616 617 618 619 620 621 622 623
						var itemNode = this._createConvItemNode(nCv);
						this.conversationNodeItemList.push(itemNode);
					}
				}
				//this.loadConversationList(json.data);
			}
		}.bind(this));
	},
	//检查是否有新消息
F
fancy 已提交
624
	_checkNewMessage: function () {
F
fancy 已提交
625 626 627 628
		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 已提交
629
				if (list && list.length > 0) {
F
fancy 已提交
630 631 632 633 634 635 636
					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 已提交
637
								if (this.messageList[j].id == m.id) {
F
fancy 已提交
638 639 640
									isnew = false;
								}
							}
F
fancy 已提交
641
							if (isnew) {
F
fancy 已提交
642 643 644 645 646 647 648
								this.messageList.push(m);
								this._buildMsgNode(m, false);
								// this._refreshConvMessage(m);
							}
						}
					}
				}
F
fancy 已提交
649

F
fancy 已提交
650 651 652 653 654
			}.bind(this), function (error) {
				console.log(error);
			}.bind(this), false);
		}
	},
F
fancy 已提交
655
	//创建消息html节点
F
fancy 已提交
656
	_buildMsgNode: function (msg, isTop) {
F
fancy 已提交
657 658
		var createPerson = msg.createPerson;
		var jsonbody = msg.body;
F
fancy 已提交
659
		var body = JSON.parse(jsonbody);
F
fancy 已提交
660 661
		var distinguishedName = layout.session.user.distinguishedName;
		if (createPerson != distinguishedName) {
F
fancy 已提交
662
			this._buildSender(body, createPerson, isTop, msg);
663
		} else {
F
fancy 已提交
664
			this._buildReceiver(body, createPerson, isTop, msg);
F
fancy 已提交
665 666 667 668 669 670 671
		}
	},
	/**
	 * 消息发送体
	 * @param  msgBody 消息体
	 * @param createPerson 消息人员
	 * @param isTop 是否放在顶部
F
fancy 已提交
672
	 * @param msg 消息对象
F
fancy 已提交
673
	 */
F
fancy 已提交
674 675 676
	 _buildSender: function (msgBody, createPerson, isTop, msg) {
		var receiverBodyNode = new Element("div", { "class": "chat-sender", "id": msg.id}).inject(this.chatContentNode, isTop ? "top" : "bottom");
		this._addContextMenuEvent(receiverBodyNode, msg);
F
fancy 已提交
677 678 679 680 681 682 683 684 685 686 687
		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 已提交
688 689
		if (msgBody.type == "emoji") { // 表情
			var img = "";
F
fancy 已提交
690 691
			for (var i = 0; i < this.emojiList.length; i++) {
				if (msgBody.body == this.emojiList[i].key) {
F
fancy 已提交
692
					img = this.emojiList[i].path;
F
fancy 已提交
693
				}
F
fancy 已提交
694
			}
F
fancy 已提交
695 696 697 698 699 700
			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({
701
				"click": function (e) {
F
fancy 已提交
702 703 704 705 706 707
					var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
					window.open(downloadUrl);
				}.bind(this)
			});
		} else if (msgBody.type == "audio") {
			var url = this._getFileDownloadUrl(msgBody.fileId);
708
			new Element("audio", { "src": url, "controls": "controls", "preload": "preload" }).inject(lastNode);
F
fancy 已提交
709 710
		} else if (msgBody.type == "location") {
			var mapBox = new Element("span").inject(lastNode);
711
			new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width": 24, "height": 24 }).inject(mapBox);
F
fancy 已提交
712
			var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
713
			new Element("a", { "href": url, "target": "_blank", "text": msgBody.address }).inject(mapBox);
F
fancy 已提交
714 715 716
		} else if (msgBody.type == "file") { //文件
			var mapBox = new Element("span").inject(lastNode);
			var fileIcon = this._getFileIcon(msgBody.fileExtension);
F
fancy 已提交
717
			new Element("img", { "src": "../x_component_IMV2/$Main/file_icons/" + fileIcon, "width": 48, "height": 48 }).inject(mapBox);
F
fancy 已提交
718 719
			var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
			new Element("a", { "href": downloadUrl, "target": "_blank", "text": msgBody.fileName }).inject(mapBox);
F
fancy 已提交
720
		} else {//text
F
fancy 已提交
721 722 723
			new Element("span", { "text": msgBody.body }).inject(lastNode);
		}

F
fancy 已提交
724
		if (!isTop) {
F
fancy 已提交
725 726 727
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
		}
F
fancy 已提交
728 729 730 731 732 733
	},
	/**
	 * 消息接收体
	 * @param  msgBody 
	 * @param createPerson 消息人员
	 * @param isTop 是否放在顶部
F
fancy 已提交
734
	 * @param msg 消息对象
F
fancy 已提交
735
	 */
F
fancy 已提交
736 737 738 739
	_buildReceiver: function (msgBody, createPerson, isTop, msg) {
		var receiverBodyNode = new Element("div", { "class": "chat-receiver", "id": msg.id}).inject(this.chatContentNode, isTop ? "top" : "bottom");
		this._addContextMenuEvent(receiverBodyNode, msg);
	
F
fancy 已提交
740 741 742 743 744 745 746 747 748 749
		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 已提交
750

F
fancy 已提交
751 752
		if (msgBody.type == "emoji") { // 表情
			var img = "";
F
fancy 已提交
753 754
			for (var i = 0; i < this.emojiList.length; i++) {
				if (msgBody.body == this.emojiList[i].key) {
F
fancy 已提交
755
					img = this.emojiList[i].path;
F
fancy 已提交
756
				}
F
fancy 已提交
757
			}
F
fancy 已提交
758 759 760 761 762 763
			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({
764
				"click": function (e) {
F
fancy 已提交
765 766 767 768 769 770
					var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
					window.open(downloadUrl);
				}.bind(this)
			});
		} else if (msgBody.type == "audio") {
			var url = this._getFileDownloadUrl(msgBody.fileId);
771
			new Element("audio", { "src": url, "controls": "controls", "preload": "preload" }).inject(lastNode);
F
fancy 已提交
772 773
		} else if (msgBody.type == "location") {
			var mapBox = new Element("span").inject(lastNode);
774
			new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width": 24, "height": 24 }).inject(mapBox);
F
fancy 已提交
775
			var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
776
			new Element("a", { "href": url, "target": "_blank", "text": msgBody.address }).inject(mapBox);
F
fancy 已提交
777 778 779
		} else if (msgBody.type == "file") { //文件
			var mapBox = new Element("span").inject(lastNode);
			var fileIcon = this._getFileIcon(msgBody.fileExtension);
F
fancy 已提交
780
			new Element("img", { "src": "../x_component_IMV2/$Main/file_icons/" + fileIcon, "width": 48, "height": 48 }).inject(mapBox);
F
fancy 已提交
781 782
			var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
			new Element("a", { "href": downloadUrl, "target": "_blank", "text": msgBody.fileName }).inject(mapBox);
F
fancy 已提交
783
		} else {//text
F
fancy 已提交
784 785
			new Element("span", { "text": msgBody.body }).inject(lastNode);
		}
F
fancy 已提交
786 787

		if (!isTop) {
F
fancy 已提交
788 789 790
			var scrollFx = new Fx.Scroll(this.chatContentNode);
			scrollFx.toBottom();
		}
F
fancy 已提交
791
	},
F
fancy 已提交
792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
	// 绑定右键事件
	_addContextMenuEvent: function(receiverBodyNode, msg) {
		receiverBodyNode.store("msg", msg);
		receiverBodyNode.addEvents({
			"contextmenu": function(e) {
				//取消默认的浏览器自带右键 很重要!!
				e.preventDefault();
				var menuleft=e.client.x+'px';
    		var menutop=e.client.y+'px';
				var m = receiverBodyNode.retrieve("msg");
				this._createMsgContextMenu(m, menuleft, menutop);
			}.bind(this)
		});
	},
	// 打开 消息体上 右键菜单
	_createMsgContextMenu: function(msg, menuleft, menutop) {
		var createPerson = msg.createPerson;
		var distinguishedName = layout.session.user.distinguishedName;
		var list = []; // 菜单列表
F
fancy 已提交
811 812 813 814 815 816 817 818 819 820 821
		
		if (this.imConfig.enableRevokeMsg) { // 是否启用撤回消息
			if (createPerson != distinguishedName) {
				// 判断是否群主
				var isGroupAdmin = false;
				for (var i = 0; i < this.conversationNodeItemList.length; i++) {
					var c = this.conversationNodeItemList[i];
					if (this.conversationId == c.data.id) {
						if (c.data.type === "group" && distinguishedName === c.data.adminPerson) {
							isGroupAdmin = true;
						}
F
fancy 已提交
822 823
					}
				}
F
fancy 已提交
824 825 826 827 828
				if (isGroupAdmin) {
					list.push({"id":"revokeMemberMsg", "text": this.lp.msgMenuItemRevokeMemberMsg});
				}
			} else {
				list.push({"id":"revokeMsg", "text": this.lp.msgMenuItemRevokeMsg});
F
fancy 已提交
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
			}
		}
		if (this.menuNode) {
			this.menuNode.destroy();
			this.menuNode = null;
		}
		if (list.length > 0) {
			// 生成菜单
			this.menuNode = new Element("ul", {"class": "chat-menulist", "styles": { "position": "fixed", "z-index": "9999", "top": menutop, "left": menuleft } }).inject(this.chatNode);
			for (let index = 0; index < list.length; index++) {
				const element = list[index];
				var menuItemNode = new Element("li", {"text": element.text}).inject(this.menuNode);
				menuItemNode.store('menuItemData', element);
				menuItemNode.store('menuItemMsgData', msg);
				menuItemNode.addEvents({
					"click": function(e) {
						var menuItemData = menuItemNode.retrieve('menuItemData'); // 菜单项数据
						var menuItemMsgData = menuItemNode.retrieve('menuItemMsgData'); // 消息数据
						this._clickMsgContextMenuItem(menuItemData, menuItemMsgData);
						e.preventDefault();
					}.bind(this)
				});
			}
			// 添加关闭菜单事件
			this.closeMsgContextMenuFun = function(e) {
				if (this.menuNode) {
					this.menuNode.destroy();
					this.menuNode = null;
				}
				e.preventDefault();
				if( this.closeMsgContextMenuFun )this.app.content.removeEvent( "click", this.closeMsgContextMenuFun );
			}.bind(this);
	
			this.app.content.addEvents({
				"click": this.closeMsgContextMenuFun
			});
		}
	},
	// 点击 右键菜单项
	_clickMsgContextMenuItem: function(menuItemData, menuItemMsgData) {
		// 关闭菜单
		if (this.menuNode) {
			this.menuNode.destroy();
			this.menuNode = null;
		}
		// 根据菜单不同处理不同内容
		// 撤回
		if (menuItemData.id === "revokeMemberMsg" || menuItemData.id === "revokeMsg") {
			this._revokeMsg(menuItemMsgData);
		}
	},
	// 撤回消息
	_revokeMsg: function(msg) {
		o2.Actions.load("x_message_assemble_communicate").ImAction.msgRevoke(msg.id, function(json) {
			console.log("撤回消息:", json);
			// 删除消息
			$(msg.id).destroy();
		}.bind(this));
	},
F
fancy 已提交
888
	//图片 根据大小 url
889
	_getFileUrlWithWH: function (id, width, height) {
F
fancy 已提交
890
		var action = MWF.Actions.get("x_message_assemble_communicate").action;
F
fancy 已提交
891
		var url = action.getAddress() + action.actions.imgFileDownloadWithWH.uri;
F
fancy 已提交
892 893 894 895 896 897
		url = url.replace("{id}", encodeURIComponent(id));
		url = url.replace("{width}", encodeURIComponent(width));
		url = url.replace("{height}", encodeURIComponent(height));
		return url;
	},
	//file 下载的url
898
	_getFileDownloadUrl: function (id) {
F
fancy 已提交
899
		var action = MWF.Actions.get("x_message_assemble_communicate").action;
F
fancy 已提交
900
		var url = action.getAddress() + action.actions.imgFileDownload.uri;
F
fancy 已提交
901 902 903 904
		url = url.replace("{id}", encodeURIComponent(id));
		return url;
	},
	//百度地图打开地址
905 906
	_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 已提交
907 908
		return url;
	},
F
fancy 已提交
909
	//用户头像
NoSubject's avatar
NoSubject 已提交
910 911
	_getIcon: function (id) {
		var orgAction = MWF.Actions.get("x_organization_assemble_control")
912
		var url = (id) ? orgAction.getPersonIcon(id) : "../x_component_IMV2/$Main/default/icons/group.png";
NoSubject's avatar
NoSubject 已提交
913 914
		return url + "?" + (new Date().getTime());
	},
F
fancy 已提交
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
	// 文件类型icon图
	_getFileIcon: function (ext) {
		if (ext) {
			if (ext === "jpg" || ext === "jpeg") {
				return "icon_file_jpeg.png";
			} else if (ext === "gif") {
				return "icon_file_gif.png";
			} else if (ext === "png") {
				return "icon_file_png.png";
			} else if (ext === "tiff") {
				return "icon_file_tiff.png";
			} else if (ext === "bmp" || ext === "webp") {
				return "icon_file_img.png";
			} else if (ext === "ogg" || ext === "mp3" || ext === "wav" || ext === "wma") {
				return "icon_file_mp3.png";
			} else if (ext === "mp4") {
				return "icon_file_mp4.png";
			} else if (ext === "avi") {
				return "icon_file_avi.png";
			} else if (ext === "mov" || ext === "rm" || ext === "mkv") {
				return "icon_file_rm.png";
			} else if (ext === "doc" || ext === "docx") {
				return "icon_file_word.png";
			} else if (ext === "xls" || ext === "xlsx") {
				return "icon_file_excel.png";
			} else if (ext === "ppt" || ext === "pptx") {
				return "icon_file_ppt.png";
			} else if (ext === "html") {
				return "icon_file_html.png";
			} else if (ext === "pdf") {
				return "icon_file_pdf.png";
			} else if (ext === "txt" || ext === "json") {
				return "icon_file_txt.png";
			} else if (ext === "zip") {
				return "icon_file_zip.png";
			} else if (ext === "rar") {
				return "icon_file_rar.png";
			} else if (ext === "7z") {
				return "icon_file_arch.png";
			} else if (ext === "ai") {
				return "icon_file_ai.png";
			} else if (ext === "att") {
				return "icon_file_att.png";
			} else if (ext === "au") {
				return "icon_file_au.png";
			} else if (ext === "cad") {
				return "icon_file_cad.png";
			} else if (ext === "cdr") {
				return "icon_file_cdr.png";
			} else if (ext === "eps") {
				return "icon_file_eps.png";
			} else if (ext === "exe") {
				return "icon_file_exe.png";
			} else if (ext === "iso") {
				return "icon_file_iso.png";
			} else if (ext === "link") {
				return "icon_file_link.png";
			} else if (ext === "swf") {
				return "icon_file_flash.png";
			} else if (ext === "psd") {
				return "icon_file_psd.png";
			} else if (ext === "tmp") {
				return "icon_file_tmp.png";
F
fancy 已提交
978
			} else {
F
fancy 已提交
979 980
				return "icon_file_unkown.png";
			}
F
fancy 已提交
981
		} else {
F
fancy 已提交
982 983 984
			return "icon_file_unkown.png";
		}
	},
NoSubject's avatar
NoSubject 已提交
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
	//输出特殊的时间格式
	_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) {
NoSubject's avatar
NoSubject 已提交
1004
					retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + this.lp.minutesBefore
NoSubject's avatar
NoSubject 已提交
1005
				} else {
NoSubject's avatar
NoSubject 已提交
1006
					retTime = hour + this.lp.hoursBefore
NoSubject's avatar
NoSubject 已提交
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
				}

			}
			return retTime;
		}
		var dates = parseInt(time / 86400000);
		var todaydates = parseInt(todayTime / 86400000);
		if (todaydates > dates) {
			var days = (todaydates - dates);
			if (days == 1) {
NoSubject's avatar
NoSubject 已提交
1017
				retTime = this.lp.yesterday;
NoSubject's avatar
NoSubject 已提交
1018
			} else if (days == 2) {
NoSubject's avatar
NoSubject 已提交
1019
				retTime = this.lp.beforeYesterday;
NoSubject's avatar
NoSubject 已提交
1020
			} else if (days > 2 && days < 31) {
NoSubject's avatar
NoSubject 已提交
1021
				retTime = days + this.lp.daysBefore;
NoSubject's avatar
NoSubject 已提交
1022
			} else if (days >= 31 && days <= 2 * 31) {
NoSubject's avatar
NoSubject 已提交
1023
				retTime = this.lp.monthAgo;
NoSubject's avatar
NoSubject 已提交
1024
			} else if (days > 2 * 31 && days <= 3 * 31) {
NoSubject's avatar
NoSubject 已提交
1025
				retTime = this.lp.towMonthAgo;
NoSubject's avatar
NoSubject 已提交
1026
			} else if (days > 3 * 31 && days <= 4 * 31) {
NoSubject's avatar
NoSubject 已提交
1027
				retTime = this.lp.threeMonthAgo;
NoSubject's avatar
NoSubject 已提交
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
			} 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 已提交
1043
	},
F
fancy 已提交
1044
	//当前时间 yyyy-MM-dd HH:mm:ss
F
fancy 已提交
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	_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 已提交
1060 1061 1062 1063
	}


});
F
fancy 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

//会话对象
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 已提交
1081 1082
			"lastMessage": "",
			"lastMessageType": "text"
F
fancy 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
		};
		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 已提交
1110 1111 1112
			if (mBody.type) {
				convData.lastMessageType = mBody.type;
			}
F
fancy 已提交
1113 1114 1115 1116 1117 1118 1119
		}
		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);
1120
		this.titleNode = new Element("div", { "class": "body_title", "text": convData.title }).inject(bodyUpNode);
F
fancy 已提交
1121
		this.messageTimeNode = new Element("div", { "class": "body_time", "text": convData.time }).inject(bodyUpNode);
F
fancy 已提交
1122
		if (convData.lastMessageType == "emoji") {
F
fancy 已提交
1123
			this.lastMessageNode = new Element("div", { "class": "body_down" }).inject(bodyNode);
F
fancy 已提交
1124
			var imgPath = "";
F
fancy 已提交
1125
			for (var i = 0; i < this.main.emojiList.length; i++) {
F
fancy 已提交
1126 1127 1128 1129 1130
				var emoji = this.main.emojiList[i];
				if (emoji.key == convData.lastMessage) {
					imgPath = emoji.path;
				}
			}
F
fancy 已提交
1131 1132
			new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
		} else {
F
fancy 已提交
1133 1134
			this.lastMessageNode = new Element("div", { "class": "body_down", "text": convData.lastMessage }).inject(bodyNode);
		}
F
fancy 已提交
1135

F
fancy 已提交
1136 1137 1138 1139 1140 1141 1142 1143
		var _self = this;
		this.node.addEvents({
			"click": function () {
				_self.main.tapConv(_self.data);
			}
		});
	},
	/**
F
fancy 已提交
1144
	 *
F
fancy 已提交
1145 1146 1147
	 * 刷新会话列表的最后消息内容 
	 * @param {*} lastMessage 
	 */
F
fancy 已提交
1148
	refreshLastMsg: function (lastMessage) {
F
fancy 已提交
1149 1150 1151 1152
		if (lastMessage) {
			//目前是text 类型的消息
			var jsonbody = lastMessage.body;
			var body = JSON.parse(jsonbody);
F
fancy 已提交
1153

F
fancy 已提交
1154 1155 1156 1157 1158 1159 1160 1161
			if (this.lastMessageNode) {
				if (body.type == "emoji") { //表情 消息
					var imgPath = "";
					for (var i = 0; i < this.main.emojiList.length; i++) {
						var emoji = this.main.emojiList[i];
						if (emoji.key == body.body) {
							imgPath = emoji.path;
						}
F
fancy 已提交
1162
					}
F
fancy 已提交
1163 1164 1165 1166 1167
					this.lastMessageNode.empty();
					new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
				} else { //文本消息
					this.lastMessageNode.empty();
					this.lastMessageNode.set('text', body.body);
F
fancy 已提交
1168 1169
				}
			}
F
fancy 已提交
1170 1171 1172 1173
			var time = this.main._friendlyTime(o2.common.toDate(lastMessage.createTime));
			if (this.messageTimeNode) {
				this.messageTimeNode.set("text", time);
			}
F
fancy 已提交
1174 1175
		}
	},
1176
	// 更新聊天窗口上的标题 修改标题的时候使用 @Disuse 使用refreshData
F
fancy 已提交
1177
	refreshConvTitle: function (title) {
1178 1179
		this.titleNode.set("text", title);
	},
1180 1181 1182 1183 1184 1185
	// 更新会话数据
	refreshData: function (data) {
		this.data = data;
		// 更新聊天窗口上的标题 修改标题的时候使用
		this.titleNode.set("text", data.title);
	},
F
fancy 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214
	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,
NoSubject's avatar
NoSubject 已提交
1215
		"title": MWF.xApplication.IMV2.LP.createSingle
F
fancy 已提交
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
	},
	_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: {
NoSubject's avatar
NoSubject 已提交
1233
				person: { text: MWF.xApplication.IMV2.LP.selectPerson, type: "org", orgType: "person", count: 0, notEmpty: true, exclude: exclude },
F
fancy 已提交
1234 1235 1236 1237 1238 1239 1240 1241 1242
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
NoSubject's avatar
NoSubject 已提交
1243
				"text": MWF.xApplication.IMV2.LP.ok
F
fancy 已提交
1244 1245 1246 1247 1248 1249 1250
			}).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,
NoSubject's avatar
NoSubject 已提交
1251
			"text": MWF.xApplication.IMV2.LP.close
F
fancy 已提交
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
		}).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();
		}
	}
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278
});


//创建聊天 弹出窗表单
MWF.xApplication.IMV2.CreateConversationForm = new Class({
	Extends: MPopupForm,
	Implements: [Options, Events],
	options: {
		"style": "minder",
		"width": 700,
		"height": "200",
		"hasTop": true,
		"hasIcon": false,
		"draggable": true,
NoSubject's avatar
NoSubject 已提交
1279
		"title": MWF.xApplication.IMV2.LP.createSingle,
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
		"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: {
NoSubject's avatar
NoSubject 已提交
1300
				person: { text: MWF.xApplication.IMV2.LP.selectPerson, type: "org", orgType: "person", count: this.options["personCount"], notEmpty: true, exclude: exclude, value: this.options["personSelected"] },
1301 1302 1303 1304 1305 1306 1307 1308 1309
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
NoSubject's avatar
NoSubject 已提交
1310
				"text": MWF.xApplication.IMV2.LP.ok
1311 1312 1313 1314 1315 1316 1317
			}).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,
NoSubject's avatar
NoSubject 已提交
1318
			"text": MWF.xApplication.IMV2.LP.close
1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		}).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);
F
fancy 已提交
1330 1331
			} else {
				this.app.newConversation(data.person, this.options["personCount"] === 1 ? "single" : "group");
1332
			}
F
fancy 已提交
1333

1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351
			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,
1352
		"defaultValue": "", // 默认值
NoSubject's avatar
NoSubject 已提交
1353
		"title": MWF.xApplication.IMV2.LP.modifyGroupName
1354 1355 1356 1357 1358 1359 1360
	},
	_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);
F
fancy 已提交
1361

1362 1363 1364 1365 1366
		this.form = new MForm(this.formTableArea, this.data || {}, {
			isEdited: true,
			style: "minder",
			hasColon: true,
			itemTemplate: {
1367
				title: { text: MWF.xApplication.IMV2.LP.groupName, type: "text", notEmpty: true, value:  this.options["defaultValue"] },
1368 1369 1370 1371 1372 1373 1374 1375 1376
			}
		}, this.app);
		this.form.load();

	},
	_createBottomContent: function () {
		if (this.isNew || this.isEdited) {
			this.okActionNode = new Element("button.inputOkButton", {
				"styles": this.css.inputOkButton,
NoSubject's avatar
NoSubject 已提交
1377
				"text": MWF.xApplication.IMV2.LP.ok
1378 1379 1380 1381 1382 1383 1384
			}).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,
NoSubject's avatar
NoSubject 已提交
1385
			"text": MWF.xApplication.IMV2.LP.close
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
		}).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();
		}
	}
NoSubject's avatar
NoSubject 已提交
1398
});