Main.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. MWF.xApplication.IMV2.options.multitask = true;
  2. MWF.xApplication.IMV2.Main = new Class({
  3. Extends: MWF.xApplication.Common.Main,
  4. Implements: [Options, Events],
  5. options: {
  6. "style": "default",
  7. "name": "IMV2",
  8. "mvcStyle": "style.css",
  9. "icon": "icon.png",
  10. "width": "1200",
  11. "height": "700",
  12. "isResize": true,
  13. "isMax": true,
  14. "title": MWF.xApplication.IMV2.LP.title
  15. },
  16. onQueryLoad: function () {
  17. this.lp = MWF.xApplication.IMV2.LP;
  18. this.conversationList = [];
  19. },
  20. loadApplication: function (callback) {
  21. var url = this.path + this.options.style + "/im.html";
  22. this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
  23. //获取会话列表
  24. o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
  25. if (json.data && json.data instanceof Array) {
  26. this.conversationList = json.data;
  27. this.loadConversationList(json.data);
  28. }
  29. }.bind(this));
  30. }.bind(this));
  31. },
  32. //加载会话列表
  33. loadConversationList: function (list) {
  34. for (var i = 0; i < list.length; i++) {
  35. var chat = list[i];
  36. var url = this.path + this.options.style + "/conversationItem.html";
  37. var avatarDefault = this._getIcon();
  38. var data = {
  39. "id": chat.id,
  40. "avatarUrl": avatarDefault,
  41. "title": chat.title,
  42. "time": "",
  43. "lastMessage": ""
  44. };
  45. var distinguishedName = layout.session.user.distinguishedName;
  46. if (chat.type && chat.type === "single") {
  47. var chatPerson = "";
  48. if (chat.personList && chat.personList instanceof Array) {
  49. for (var j = 0; j < chat.personList.length; j++) {
  50. var person = chat.personList[j];
  51. if (person !== distinguishedName) {
  52. chatPerson = person;
  53. }
  54. }
  55. }
  56. data.avatarUrl = this._getIcon(chatPerson);
  57. var name = chatPerson;
  58. if (chatPerson.indexOf("@") != -1) {
  59. name = name.substring(0, chatPerson.indexOf("@"));
  60. }
  61. data.title = name;
  62. }
  63. if (chat.lastMessage) {
  64. //todo 其它消息类型
  65. var mBody = JSON.parse(chat.lastMessage.body);
  66. data.lastMessage = mBody.body;
  67. if (chat.lastMessage.createTime) {
  68. var time = this._friendlyTime(o2.common.toDate(chat.lastMessage.createTime));
  69. data.time = time;
  70. }
  71. }
  72. var chatItemNode = new Element("div", {"class": "item"}).inject(this.chatItemListNode);
  73. var baseClass = "base";
  74. if (i == 0) {
  75. baseClass = "base check";
  76. }
  77. var chatItemBaseNode = new Element("div", {"class": baseClass}).inject(chatItemNode);
  78. var avatarNode = new Element("div", {"class": "avatar"}).inject(chatItemBaseNode);
  79. var avatarImg = new Element("img", {"src": data.avatarUrl, "class": "img"}).inject(avatarNode);
  80. var bodyNode = new Element("div", {"class": "body"}).inject(chatItemBaseNode);
  81. var bodyUpNode = new Element("div", {"class": "body_up"}).inject(bodyNode);
  82. var bodyTitleNode = new Element("div", {"class": "body_title", "text": data.title}).inject(bodyUpNode);
  83. var bodyTimeNode = new Element("div", {"class": "body_time", "text": data.time}).inject(bodyUpNode);
  84. var bodyDownNode = new Element("div", {"class": "body_down", "text": data.lastMessage}).inject(bodyNode);
  85. var _self = this;
  86. chatItemNode.addEvents({
  87. "click": function(){
  88. _self.tapConv(chat);
  89. }
  90. });
  91. }
  92. console.log("结束");
  93. },
  94. //点击
  95. tapConv: function (conv) {
  96. console.log("clickConversationvvvvvv");
  97. var url = this.path + this.options.style + "/chat.html";
  98. var data = {"convName": conv.title};
  99. this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
  100. }.bind(this));
  101. },
  102. //发送消息
  103. sendMsg: function() {
  104. console.log("click send Msg btn................");
  105. var text = this.chatBottomAreaTextareaNode.value;
  106. console.log(text);
  107. },
  108. _getIcon: function (id) {
  109. var orgAction = MWF.Actions.get("x_organization_assemble_control")
  110. var url = (id) ? orgAction.getPersonIcon(id) : "/x_component_IMV2/$Main/default/icons/group.png";
  111. return url + "?" + (new Date().getTime());
  112. },
  113. //输出特殊的时间格式
  114. _friendlyTime: function (date) {
  115. var day = date.getDate();
  116. var monthIndex = date.getMonth();
  117. var year = date.getFullYear();
  118. var time = date.getTime();
  119. var today = new Date();
  120. var todayDay = today.getDate();
  121. var todayMonthIndex = today.getMonth();
  122. var todayYear = today.getFullYear();
  123. var todayTime = today.getTime();
  124. var retTime = "";
  125. //同一天
  126. if (day === todayDay && monthIndex === todayMonthIndex && year === todayYear) {
  127. var hour = 0;
  128. if (todayTime > time) {
  129. hour = parseInt((todayTime - time) / 3600000);
  130. if (hour == 0) {
  131. retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + "分钟前"
  132. } else {
  133. retTime = hour + "小时前"
  134. }
  135. }
  136. return retTime;
  137. }
  138. var dates = parseInt(time / 86400000);
  139. var todaydates = parseInt(todayTime / 86400000);
  140. if (todaydates > dates) {
  141. var days = (todaydates - dates);
  142. if (days == 1) {
  143. retTime = "昨天";
  144. } else if (days == 2) {
  145. retTime = "前天 ";
  146. } else if (days > 2 && days < 31) {
  147. retTime = days + "天前";
  148. } else if (days >= 31 && days <= 2 * 31) {
  149. retTime = "一个月前";
  150. } else if (days > 2 * 31 && days <= 3 * 31) {
  151. retTime = "2个月前";
  152. } else if (days > 3 * 31 && days <= 4 * 31) {
  153. retTime = "3个月前";
  154. } else {
  155. retTime = this._formatDate(date);
  156. }
  157. }
  158. return retTime;
  159. },
  160. //yyyy-MM-dd
  161. _formatDate: function (date) {
  162. var month = date.getMonth() + 1;
  163. var day = date.getDate();
  164. month = (month.toString().length == 1) ? ("0" + month) : month;
  165. day = (day.toString().length == 1) ? ("0" + day) : day;
  166. return date.getFullYear() + '-' + month + '-' + day;
  167. }
  168. });