Main.js 30 KB

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