Main.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. MWF.require("MWF.widget.UUID", null, false);
  2. MWF.xDesktop.requireApp("Template", "MForm", null, false);
  3. MWF.xDesktop.requireApp("Template", "MPopupForm", null, false);
  4. MWF.xApplication.IMV2.Main = new Class({
  5. Extends: MWF.xApplication.Common.Main,
  6. Implements: [Options, Events],
  7. options: {
  8. "style": "default",
  9. "name": "IMV2",
  10. "mvcStyle": "style.css",
  11. "icon": "icon.png",
  12. "width": "1024",
  13. "height": "768",
  14. "isResize": true,
  15. "isMax": true,
  16. "title": MWF.xApplication.IMV2.LP.title,
  17. "conversationId": ""
  18. },
  19. onQueryLoad: function () {
  20. this.lp = MWF.xApplication.IMV2.LP;
  21. this.app = this;
  22. this.conversationNodeItemList = [];
  23. this.conversationId = this.options.conversationId || "";
  24. this.messageList = [];
  25. this.emojiList = [];
  26. //添加87个表情
  27. for (var i = 1; i < 88; i++) {
  28. var emoji = {
  29. "key": i > 9 ? "[" + i + "]" : "[0" + i + "]",
  30. "path": i > 9 ? "/x_component_IMV2/$Main/emotions/im_emotion_" + i + ".png" : "/x_component_IMV2/$Main/emotions/im_emotion_0" + i + ".png",
  31. };
  32. this.emojiList.push(emoji);
  33. }
  34. },
  35. onQueryClose: function () {
  36. this.closeListening()
  37. },
  38. loadApplication: function (callback) {
  39. var url = this.path + this.options.style + "/im.html";
  40. this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
  41. //设置content
  42. this.app.content = this.o2ImMainNode;
  43. //启动监听
  44. this.startListening();
  45. //获取会话列表
  46. this.conversationNodeItemList = [];
  47. o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
  48. if (json.data && json.data instanceof Array) {
  49. this.loadConversationList(json.data);
  50. }
  51. }.bind(this));
  52. }.bind(this));
  53. },
  54. startListening: function () {
  55. this.messageNumber = layout.desktop.message.items.length;
  56. //查询ws消息 如果增加
  57. if (this.listener) {
  58. clearInterval(this.listener);
  59. }
  60. this.listener = setInterval(function () {
  61. var newNumber = layout.desktop.message.items.length;
  62. //判断是否有新的ws消息
  63. if (newNumber > this.messageNumber) {
  64. //查询会话数据
  65. this._checkConversationMessage();
  66. //查询聊天数据
  67. this._checkNewMessage();
  68. this.messageNumber = newNumber;
  69. }
  70. }.bind(this), 1000);
  71. },
  72. closeListening: function () {
  73. if (this.listener) {
  74. clearInterval(this.listener);
  75. }
  76. },
  77. //加载会话列表
  78. loadConversationList: function (list) {
  79. for (var i = 0; i < list.length; i++) {
  80. var chat = list[i];
  81. var itemNode = this._createConvItemNode(chat);
  82. this.conversationNodeItemList.push(itemNode);
  83. if (this.conversationId && this.conversationId == chat.id) {
  84. this.tapConv(chat);
  85. }
  86. }
  87. },
  88. //分页获取会话的消息列表数据
  89. loadMsgListByConvId: function (page, size, convId) {
  90. var data = { "conversationId": convId };
  91. o2.Actions.load("x_message_assemble_communicate").ImAction.msgListByPaging(page, size, data, function (json) {
  92. var list = json.data;
  93. for (var i = 0; i < list.length; i++) {
  94. this.messageList.push(list[i]);
  95. this._buildMsgNode(list[i], true);
  96. }
  97. }.bind(this), function (error) {
  98. console.log(error);
  99. }.bind(this), false);
  100. },
  101. //点击会话
  102. tapConv: function (conv) {
  103. this._setCheckNode(conv);
  104. var url = this.path + this.options.style + "/chat.html";
  105. var data = { "convName": conv.title };
  106. this.conversationId = conv.id;
  107. this.chatNode.empty();
  108. this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
  109. //获取聊天信息
  110. this.messageList = [];
  111. this.loadMsgListByConvId(1, 20, conv.id);
  112. var scrollFx = new Fx.Scroll(this.chatContentNode);
  113. scrollFx.toBottom();
  114. }.bind(this));
  115. },
  116. //点击发送消息
  117. sendMsg: function () {
  118. var text = this.chatBottomAreaTextareaNode.value;
  119. if (text) {
  120. this.chatBottomAreaTextareaNode.value = "";
  121. this._newAndSendTextMsg(text, "text");
  122. } else {
  123. console.log("没有消息内容!");
  124. }
  125. },
  126. //点击表情按钮
  127. showEmojiBox: function () {
  128. if (!this.emojiBoxNode) {
  129. this.emojiBoxNode = new Element("div", { "class": "chat-emoji-box" }).inject(this.chatNode);
  130. var _self = this;
  131. for (var i = 0; i < this.emojiList.length; i++) {
  132. var emoji = this.emojiList[i];
  133. var emojiNode = new Element("img", { "src": emoji.path, "class": "chat-emoji-img" }).inject(this.emojiBoxNode);
  134. emojiNode.addEvents({
  135. "mousedown": function (ev) {
  136. _self.sendEmojiMsg(this.emoji);
  137. _self.hideEmojiBox();
  138. }.bind({ emoji: emoji })
  139. });
  140. }
  141. }
  142. this.emojiBoxNode.setStyle("display", "block");
  143. this.hideFun = this.hideEmojiBox.bind(this);
  144. document.body.addEvent("mousedown", this.hideFun);
  145. },
  146. hideEmojiBox: function () {
  147. //关闭emojiBoxNode
  148. this.emojiBoxNode.setStyle("display", "none");
  149. document.body.removeEvent("mousedown", this.hideFun);
  150. },
  151. //发送表情消息
  152. sendEmojiMsg: function (emoji) {
  153. this._newAndSendTextMsg(emoji.key, "emoji");
  154. },
  155. //点击创建单聊按钮
  156. tapCreateSingleConv: function () {
  157. var form = new MWF.xApplication.IMV2.SingleForm(this, {}, {}, { app: this.app });
  158. form.create()
  159. },
  160. /**
  161. * 创建会话
  162. * @param {*} persons 人员列表
  163. * @param {*} cType 会话类型 "single" "group"
  164. */
  165. newConversation: function (persons, cType) {
  166. var conv = {
  167. type: cType,
  168. personList: persons,
  169. };
  170. var _self = this;
  171. o2.Actions.load("x_message_assemble_communicate").ImAction.create(conv, function (json) {
  172. var newConv = json.data;
  173. var isOld = false;
  174. for (var i = 0; i < _self.conversationNodeItemList.length; i++) {
  175. var c = _self.conversationNodeItemList[i];
  176. if (newConv.id == c.data.id) {
  177. isOld = true;
  178. _self.tapConv(c);
  179. }
  180. }
  181. if (!isOld) {
  182. var itemNode = _self._createConvItemNode(newConv);
  183. _self.conversationNodeItemList.push(itemNode);
  184. _self.tapConv(newConv);
  185. }
  186. }.bind(this), function (error) {
  187. console.log(error);
  188. }.bind(this))
  189. },
  190. //创建会话ItemNode
  191. _createConvItemNode: function (conv) {
  192. return new MWF.xApplication.IMV2.ConversationItem(conv, this);
  193. },
  194. //会话ItemNode 点击背景色
  195. _setCheckNode: function (conv) {
  196. for (var i = 0; i < this.conversationNodeItemList.length; i++) {
  197. var item = this.conversationNodeItemList[i];
  198. if (item.data.id == conv.id) {
  199. item.addCheckClass();
  200. } else {
  201. item.removeCheckClass();
  202. }
  203. }
  204. },
  205. //创建文本消息 并发送
  206. _newAndSendTextMsg: function (text, type) {
  207. var distinguishedName = layout.session.user.distinguishedName;
  208. var time = this._currentTime();
  209. var body = { "body": text, "type": type };
  210. var bodyJson = JSON.stringify(body);
  211. var uuid = (new MWF.widget.UUID).toString();
  212. var textMessage = {
  213. "id": uuid,
  214. "conversationId": this.conversationId,
  215. "body": bodyJson,
  216. "createPerson": distinguishedName,
  217. "createTime": time,
  218. "sendStatus": 1
  219. };
  220. o2.Actions.load("x_message_assemble_communicate").ImAction.msgCreate(textMessage,
  221. function (json) {
  222. //data = json.data;
  223. console.log("消息发送成功!");
  224. }.bind(this),
  225. function (error) {
  226. console.log(error);
  227. }.bind(this));
  228. this.messageList.push(textMessage);
  229. this._buildSender(body, distinguishedName, false);
  230. this._refreshConvMessage(textMessage);
  231. },
  232. //刷新会话Item里面的最后消息内容
  233. _refreshConvMessage: function (msg) {
  234. for (var i = 0; i < this.conversationNodeItemList.length; i++) {
  235. var node = this.conversationNodeItemList[i];
  236. if (node.data.id == this.conversationId) {
  237. node.refreshLastMsg(msg);
  238. }
  239. }
  240. },
  241. //检查会话列表是否有更新
  242. _checkConversationMessage: function () {
  243. o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
  244. if (json.data && json.data instanceof Array) {
  245. var newConList = json.data;
  246. for (var j = 0; j < newConList.length; j++) {
  247. var nCv = newConList[j];
  248. var isNew = true;
  249. for (var i = 0; i < this.conversationNodeItemList.length; i++) {
  250. var cv = this.conversationNodeItemList[i];
  251. if (cv.data.id == nCv.id) {
  252. isNew = false;
  253. //刷新
  254. cv.refreshLastMsg(nCv.lastMessage);
  255. }
  256. }
  257. //新会话 创建
  258. if (isNew) {
  259. var itemNode = this._createConvItemNode(nCv);
  260. this.conversationNodeItemList.push(itemNode);
  261. }
  262. }
  263. //this.loadConversationList(json.data);
  264. }
  265. }.bind(this));
  266. },
  267. //检查是否有新消息
  268. _checkNewMessage: function () {
  269. if (this.conversationId && this.conversationId != "") {//是否有会话窗口
  270. var data = { "conversationId": this.conversationId };
  271. o2.Actions.load("x_message_assemble_communicate").ImAction.msgListByPaging(1, 10, data, function (json) {
  272. var list = json.data;
  273. if (list && list.length > 0) {
  274. var msg = list[0];
  275. //检查聊天框是否有变化
  276. if (this.conversationId == msg.conversationId) {
  277. for (var i = 0; i < list.length; i++) {
  278. var isnew = true;
  279. var m = list[i];
  280. for (var j = 0; j < this.messageList.length; j++) {
  281. if (this.messageList[j].id == m.id) {
  282. isnew = false;
  283. }
  284. }
  285. if (isnew) {
  286. this.messageList.push(m);
  287. this._buildMsgNode(m, false);
  288. // this._refreshConvMessage(m);
  289. }
  290. }
  291. }
  292. }
  293. }.bind(this), function (error) {
  294. console.log(error);
  295. }.bind(this), false);
  296. }
  297. },
  298. //创建消息html节点
  299. _buildMsgNode: function (msg, isTop) {
  300. var createPerson = msg.createPerson;
  301. var jsonbody = msg.body;
  302. var body = JSON.parse(jsonbody);//todo 目前只有一种text类型
  303. var distinguishedName = layout.session.user.distinguishedName;
  304. if (createPerson != distinguishedName) {
  305. this._buildReceiver(body, createPerson, isTop);
  306. } else {
  307. this._buildSender(body, createPerson, isTop);
  308. }
  309. },
  310. /**
  311. * 消息发送体
  312. * @param msgBody 消息体
  313. * @param createPerson 消息人员
  314. * @param isTop 是否放在顶部
  315. */
  316. _buildSender: function (msgBody, createPerson, isTop) {
  317. var receiverBodyNode = new Element("div", { "class": "chat-sender" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
  318. var avatarNode = new Element("div").inject(receiverBodyNode);
  319. var avatarUrl = this._getIcon(createPerson);
  320. var name = createPerson;
  321. if (createPerson.indexOf("@") != -1) {
  322. name = name.substring(0, createPerson.indexOf("@"));
  323. }
  324. var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
  325. var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
  326. var lastNode = new Element("div").inject(receiverBodyNode);
  327. var lastFirstNode = new Element("div", { "class": "chat-left_triangle" }).inject(lastNode);
  328. //text
  329. if (msgBody.type == "emoji") { // 表情
  330. var img = "";
  331. for (var i = 0; i < this.emojiList.length; i++) {
  332. if (msgBody.body == this.emojiList[i].key) {
  333. img = this.emojiList[i].path;
  334. }
  335. }
  336. new Element("img", { "src": img, "class": "chat-content-emoji" }).inject(lastNode);
  337. } else if (msgBody.type == "image") {//image
  338. var imgBox = new Element("div", { "class": "img-chat" }).inject(lastNode);
  339. var url = this._getFileUrlWithWH(msgBody.fileId, 144, 192);
  340. new Element("img", { "src": url }).inject(imgBox);
  341. imgBox.addEvents({
  342. "click": function(e){
  343. var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
  344. window.open(downloadUrl);
  345. }.bind(this)
  346. });
  347. } else if (msgBody.type == "audio") {
  348. var url = this._getFileDownloadUrl(msgBody.fileId);
  349. new Element("audio", { "src": url, "controls":"controls", "preload":"preload" }).inject(lastNode);
  350. } else if (msgBody.type == "location") {
  351. var mapBox = new Element("span").inject(lastNode);
  352. new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width":24, "height":24 }).inject(mapBox);
  353. var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
  354. new Element("a", {"href":url, "target":"_blank", "text": msgBody.address}).inject(mapBox);
  355. } else {//text
  356. new Element("span", { "text": msgBody.body }).inject(lastNode);
  357. }
  358. if (!isTop) {
  359. var scrollFx = new Fx.Scroll(this.chatContentNode);
  360. scrollFx.toBottom();
  361. }
  362. },
  363. /**
  364. * 消息接收体
  365. * @param msgBody
  366. * @param createPerson 消息人员
  367. * @param isTop 是否放在顶部
  368. */
  369. _buildReceiver: function (msgBody, createPerson, isTop) {
  370. var receiverBodyNode = new Element("div", { "class": "chat-receiver" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
  371. var avatarNode = new Element("div").inject(receiverBodyNode);
  372. var avatarUrl = this._getIcon(createPerson);
  373. var name = createPerson;
  374. if (createPerson.indexOf("@") != -1) {
  375. name = name.substring(0, createPerson.indexOf("@"));
  376. }
  377. var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
  378. var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
  379. var lastNode = new Element("div").inject(receiverBodyNode);
  380. var lastFirstNode = new Element("div", { "class": "chat-right_triangle" }).inject(lastNode);
  381. if (msgBody.type == "emoji") { // 表情
  382. var img = "";
  383. for (var i = 0; i < this.emojiList.length; i++) {
  384. if (msgBody.body == this.emojiList[i].key) {
  385. img = this.emojiList[i].path;
  386. }
  387. }
  388. new Element("img", { "src": img, "class": "chat-content-emoji" }).inject(lastNode);
  389. } else if (msgBody.type == "image") {//image
  390. var imgBox = new Element("div", { "class": "img-chat" }).inject(lastNode);
  391. var url = this._getFileUrlWithWH(msgBody.fileId, 144, 192);
  392. new Element("img", { "src": url }).inject(imgBox);
  393. imgBox.addEvents({
  394. "click": function(e){
  395. var downloadUrl = this._getFileDownloadUrl(msgBody.fileId);
  396. window.open(downloadUrl);
  397. }.bind(this)
  398. });
  399. } else if (msgBody.type == "audio") {
  400. var url = this._getFileDownloadUrl(msgBody.fileId);
  401. new Element("audio", { "src": url, "controls":"controls", "preload":"preload" }).inject(lastNode);
  402. } else if (msgBody.type == "location") {
  403. var mapBox = new Element("span").inject(lastNode);
  404. new Element("img", { "src": "../x_component_IMV2/$Main/default/icons/location.png", "width":24, "height":24 }).inject(mapBox);
  405. var url = this._getBaiduMapUrl(msgBody.latitude, msgBody.longitude, msgBody.address, msgBody.addressDetail);
  406. new Element("a", {"href":url, "target":"_blank", "text": msgBody.address}).inject(mapBox);
  407. } else {//text
  408. new Element("span", { "text": msgBody.body }).inject(lastNode);
  409. }
  410. if (!isTop) {
  411. var scrollFx = new Fx.Scroll(this.chatContentNode);
  412. scrollFx.toBottom();
  413. }
  414. },
  415. //图片 根据大小 url
  416. _getFileUrlWithWH: function(id, width, height) {
  417. var action = MWF.Actions.get("x_message_assemble_communicate").action;
  418. var url = action.address + action.actions.imgFileDownloadWithWH.uri;
  419. url = url.replace("{id}", encodeURIComponent(id));
  420. url = url.replace("{width}", encodeURIComponent(width));
  421. url = url.replace("{height}", encodeURIComponent(height));
  422. return url;
  423. },
  424. //file 下载的url
  425. _getFileDownloadUrl: function(id) {
  426. var action = MWF.Actions.get("x_message_assemble_communicate").action;
  427. var url = action.address + action.actions.imgFileDownload.uri;
  428. url = url.replace("{id}", encodeURIComponent(id));
  429. return url;
  430. },
  431. //百度地图打开地址
  432. _getBaiduMapUrl: function(lat, longt, address, content) {
  433. var url = "https://api.map.baidu.com/marker?location="+lat+","+longt+"&title="+address+"&content="+content+"&output=html&src=net.o2oa.map";
  434. return url;
  435. },
  436. //用户头像
  437. _getIcon: function (id) {
  438. var orgAction = MWF.Actions.get("x_organization_assemble_control")
  439. var url = (id) ? orgAction.getPersonIcon(id) : "../x_component_IMV2/$Main/default/icons/group.png";
  440. return url + "?" + (new Date().getTime());
  441. },
  442. //输出特殊的时间格式
  443. _friendlyTime: function (date) {
  444. var day = date.getDate();
  445. var monthIndex = date.getMonth();
  446. var year = date.getFullYear();
  447. var time = date.getTime();
  448. var today = new Date();
  449. var todayDay = today.getDate();
  450. var todayMonthIndex = today.getMonth();
  451. var todayYear = today.getFullYear();
  452. var todayTime = today.getTime();
  453. var retTime = "";
  454. //同一天
  455. if (day === todayDay && monthIndex === todayMonthIndex && year === todayYear) {
  456. var hour = 0;
  457. if (todayTime > time) {
  458. hour = parseInt((todayTime - time) / 3600000);
  459. if (hour == 0) {
  460. retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + "分钟前"
  461. } else {
  462. retTime = hour + "小时前"
  463. }
  464. }
  465. return retTime;
  466. }
  467. var dates = parseInt(time / 86400000);
  468. var todaydates = parseInt(todayTime / 86400000);
  469. if (todaydates > dates) {
  470. var days = (todaydates - dates);
  471. if (days == 1) {
  472. retTime = "昨天";
  473. } else if (days == 2) {
  474. retTime = "前天 ";
  475. } else if (days > 2 && days < 31) {
  476. retTime = days + "天前";
  477. } else if (days >= 31 && days <= 2 * 31) {
  478. retTime = "一个月前";
  479. } else if (days > 2 * 31 && days <= 3 * 31) {
  480. retTime = "2个月前";
  481. } else if (days > 3 * 31 && days <= 4 * 31) {
  482. retTime = "3个月前";
  483. } else {
  484. retTime = this._formatDate(date);
  485. }
  486. }
  487. return retTime;
  488. },
  489. //yyyy-MM-dd
  490. _formatDate: function (date) {
  491. var month = date.getMonth() + 1;
  492. var day = date.getDate();
  493. month = (month.toString().length == 1) ? ("0" + month) : month;
  494. day = (day.toString().length == 1) ? ("0" + day) : day;
  495. return date.getFullYear() + '-' + month + '-' + day;
  496. },
  497. //当前时间 yyyy-MM-dd HH:mm:ss
  498. _currentTime: function () {
  499. var today = new Date();
  500. var year = today.getFullYear(); //得到年份
  501. var month = today.getMonth();//得到月份
  502. var date = today.getDate();//得到日期
  503. var hour = today.getHours();//得到小时
  504. var minu = today.getMinutes();//得到分钟
  505. var sec = today.getSeconds();//得到秒
  506. month = month + 1;
  507. if (month < 10) month = "0" + month;
  508. if (date < 10) date = "0" + date;
  509. if (hour < 10) hour = "0" + hour;
  510. if (minu < 10) minu = "0" + minu;
  511. if (sec < 10) sec = "0" + sec;
  512. return year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
  513. }
  514. });
  515. //会话对象
  516. MWF.xApplication.IMV2.ConversationItem = new Class({
  517. initialize: function (data, main) {
  518. this.data = data;
  519. this.main = main;
  520. this.container = this.main.chatItemListNode;
  521. this.load();
  522. },
  523. load: function () {
  524. var avatarDefault = this.main._getIcon();
  525. var convData = {
  526. "id": this.data.id,
  527. "avatarUrl": avatarDefault,
  528. "title": this.data.title,
  529. "time": "",
  530. "lastMessage": "",
  531. "lastMessageType": "text"
  532. };
  533. var distinguishedName = layout.session.user.distinguishedName;
  534. if (this.data.type && this.data.type === "single") {
  535. var chatPerson = "";
  536. if (this.data.personList && this.data.personList instanceof Array) {
  537. for (var j = 0; j < this.data.personList.length; j++) {
  538. var person = this.data.personList[j];
  539. if (person !== distinguishedName) {
  540. chatPerson = person;
  541. }
  542. }
  543. }
  544. convData.avatarUrl = this.main._getIcon(chatPerson);
  545. var name = chatPerson;
  546. if (chatPerson.indexOf("@") != -1) {
  547. name = name.substring(0, chatPerson.indexOf("@"));
  548. }
  549. convData.title = name;
  550. }
  551. if (this.data.lastMessage) {
  552. //todo 其它消息类型
  553. var mBody = JSON.parse(this.data.lastMessage.body);
  554. convData.lastMessage = mBody.body;
  555. if (this.data.lastMessage.createTime) {
  556. var time = this.main._friendlyTime(o2.common.toDate(this.data.lastMessage.createTime));
  557. convData.time = time;
  558. }
  559. if (mBody.type) {
  560. convData.lastMessageType = mBody.type;
  561. }
  562. }
  563. this.node = new Element("div", { "class": "item" }).inject(this.container);
  564. this.nodeBaseItem = new Element("div", { "class": "base" }).inject(this.node);
  565. var avatarNode = new Element("div", { "class": "avatar" }).inject(this.nodeBaseItem);
  566. new Element("img", { "src": convData.avatarUrl, "class": "img" }).inject(avatarNode);
  567. var bodyNode = new Element("div", { "class": "body" }).inject(this.nodeBaseItem);
  568. var bodyUpNode = new Element("div", { "class": "body_up" }).inject(bodyNode);
  569. new Element("div", { "class": "body_title", "text": convData.title }).inject(bodyUpNode);
  570. this.messageTimeNode = new Element("div", { "class": "body_time", "text": convData.time }).inject(bodyUpNode);
  571. if (convData.lastMessageType == "emoji") {
  572. this.lastMessageNode = new Element("div", { "class": "body_down" }).inject(bodyNode);
  573. var imgPath = "";
  574. for (var i = 0; i < this.main.emojiList.length; i++) {
  575. var emoji = this.main.emojiList[i];
  576. if (emoji.key == convData.lastMessage) {
  577. imgPath = emoji.path;
  578. }
  579. }
  580. new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
  581. } else {
  582. this.lastMessageNode = new Element("div", { "class": "body_down", "text": convData.lastMessage }).inject(bodyNode);
  583. }
  584. var _self = this;
  585. this.node.addEvents({
  586. "click": function () {
  587. _self.main.tapConv(_self.data);
  588. }
  589. });
  590. },
  591. /**
  592. *
  593. * 刷新会话列表的最后消息内容
  594. * @param {*} lastMessage
  595. */
  596. refreshLastMsg: function (lastMessage) {
  597. //目前是text 类型的消息
  598. var jsonbody = lastMessage.body;
  599. var body = JSON.parse(jsonbody);
  600. if (this.lastMessageNode) {
  601. if (body.type == "emoji") { //表情 消息
  602. var imgPath = "";
  603. for (var i = 0; i < this.main.emojiList.length; i++) {
  604. var emoji = this.main.emojiList[i];
  605. if (emoji.key == body.body) {
  606. imgPath = emoji.path;
  607. }
  608. }
  609. this.lastMessageNode.empty();
  610. new Element("img", { "src": imgPath, "style": "width: 16px;height: 16px;" }).inject(this.lastMessageNode);
  611. } else { //文本消息
  612. this.lastMessageNode.empty();
  613. this.lastMessageNode.set('text', body.body);
  614. }
  615. }
  616. var time = this.main._friendlyTime(o2.common.toDate(lastMessage.createTime));
  617. if (this.messageTimeNode) {
  618. this.messageTimeNode.set("text", time);
  619. }
  620. },
  621. addCheckClass: function () {
  622. if (this.nodeBaseItem) {
  623. if (!this.nodeBaseItem.hasClass("check")) {
  624. this.nodeBaseItem.addClass("check");
  625. }
  626. }
  627. },
  628. removeCheckClass: function () {
  629. if (this.nodeBaseItem) {
  630. if (this.nodeBaseItem.hasClass("check")) {
  631. this.nodeBaseItem.removeClass("check");
  632. }
  633. }
  634. }
  635. });
  636. //弹出窗 表单 单聊创建的form
  637. MWF.xApplication.IMV2.SingleForm = new Class({
  638. Extends: MPopupForm,
  639. Implements: [Options, Events],
  640. options: {
  641. "style": "minder",
  642. "width": 700,
  643. //"height": 300,
  644. "height": "200",
  645. "hasTop": true,
  646. "hasIcon": false,
  647. "draggable": true,
  648. "title": "创建单聊"
  649. },
  650. _createTableContent: function () {
  651. var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
  652. "<tr><td styles='formTableTitle' lable='person' width='25%'></td>" +
  653. " <td styles='formTableValue14' item='person' colspan='3'></td></tr>" +
  654. "</table>";
  655. this.formTableArea.set("html", html);
  656. var me = layout.session.user.distinguishedName;
  657. var exclude = [];
  658. if (me) {
  659. exclude = [me];
  660. }
  661. this.form = new MForm(this.formTableArea, this.data || {}, {
  662. isEdited: true,
  663. style: "minder",
  664. hasColon: true,
  665. itemTemplate: {
  666. person: { text: "选择人员", type: "org", orgType: "person", notEmpty: true, exclude: exclude },
  667. }
  668. }, this.app);
  669. this.form.load();
  670. },
  671. _createBottomContent: function () {
  672. if (this.isNew || this.isEdited) {
  673. this.okActionNode = new Element("button.inputOkButton", {
  674. "styles": this.css.inputOkButton,
  675. "text": "确定"
  676. }).inject(this.formBottomNode);
  677. this.okActionNode.addEvent("click", function (e) {
  678. this.save(e);
  679. }.bind(this));
  680. }
  681. this.cancelActionNode = new Element("button.inputCancelButton", {
  682. "styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
  683. "text": "关闭"
  684. }).inject(this.formBottomNode);
  685. this.cancelActionNode.addEvent("click", function (e) {
  686. this.close(e);
  687. }.bind(this));
  688. },
  689. save: function () {
  690. var data = this.form.getResult(true, null, true, false, true);
  691. if (data) {
  692. this.app.newConversation(data.person, "single");
  693. this.close();
  694. }
  695. }
  696. });