Main.js 24 KB

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