Main.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.options.multitask = true;
  5. MWF.xApplication.IMV2.Main = new Class({
  6. Extends: MWF.xApplication.Common.Main,
  7. Implements: [Options, Events],
  8. options: {
  9. "style": "default",
  10. "name": "IMV2",
  11. "mvcStyle": "style.css",
  12. "icon": "icon.png",
  13. "width": "1024",
  14. "height": "768",
  15. "isResize": true,
  16. "isMax": true,
  17. "title": MWF.xApplication.IMV2.LP.title
  18. },
  19. onQueryLoad: function () {
  20. this.lp = MWF.xApplication.IMV2.LP;
  21. this.app = this;
  22. this.conversationList = [];
  23. this.conversationNodeItemList = [];
  24. this.conversationId = "";
  25. },
  26. loadApplication: function (callback) {
  27. var url = this.path + this.options.style + "/im.html";
  28. this.content.loadHtml(url, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function () {
  29. this.app.content = this.o2ImMainNode;
  30. //获取会话列表
  31. this.conversationNodeItemList = [];
  32. o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
  33. if (json.data && json.data instanceof Array) {
  34. this.conversationList = json.data;
  35. this.loadConversationList(json.data);
  36. }
  37. }.bind(this));
  38. }.bind(this));
  39. },
  40. //加载会话列表
  41. loadConversationList: function (list) {
  42. for (var i = 0; i < list.length; i++) {
  43. var chat = list[i];
  44. var itemNode = this._createConvItemNode(chat);
  45. this.conversationNodeItemList.push(itemNode);
  46. }
  47. if (list.length > 0) {
  48. this.tapConv(list[0]);
  49. }
  50. console.log("结束");
  51. },
  52. //分页获取会话的消息列表数据
  53. loadMsgListByConvId: function (page, size, convId) {
  54. var data = { "conversationId": convId };
  55. o2.Actions.load("x_message_assemble_communicate").ImAction.msgListByPaging(page, size, data, function (json) {
  56. var list = json.data;
  57. for (var i = 0; i < list.length; i++) {
  58. this._buildMsgNode(list[i]);
  59. }
  60. console.log("聊天信息添加结束!");
  61. }.bind(this), function (error) {
  62. console.log(error);
  63. }.bind(this), false);
  64. },
  65. //点击
  66. tapConv: function (conv) {
  67. console.log("clickConversationvvvvvv");
  68. this._setCheckNode(conv);
  69. var url = this.path + this.options.style + "/chat.html";
  70. var data = { "convName": conv.title };
  71. this.conversationId = conv.id;
  72. this.chatNode.empty();
  73. this.chatNode.loadHtml(url, { "bind": data, "module": this }, function () {
  74. //获取聊天信息
  75. this.loadMsgListByConvId(1, 20, conv.id);
  76. console.log("开始滚动!!!");
  77. var scrollFx = new Fx.Scroll(this.chatContentNode);
  78. scrollFx.toBottom();
  79. }.bind(this));
  80. },
  81. //点击发送消息
  82. sendMsg: function () {
  83. console.log("click send Msg btn................");
  84. var text = this.chatBottomAreaTextareaNode.value;
  85. console.log(text);
  86. if (text) {
  87. this.chatBottomAreaTextareaNode.value = "";
  88. this._newAndSendTextMsg(text);
  89. var scrollFx = new Fx.Scroll(this.chatContentNode);
  90. scrollFx.toBottom();
  91. } else {
  92. console.log("没有消息内容!");
  93. }
  94. },
  95. tapCreateSingleConv: function () {
  96. console.log("click tapCreateSingleConv................");
  97. var form = new MWF.xApplication.IMV2.SingleForm(this, {}, {}, { app: this.app });
  98. form.create()
  99. },
  100. /**
  101. * 创建会话
  102. * @param {*} persons 人员列表
  103. * @param {*} cType 会话类型 "single" "group"
  104. */
  105. newConversation: function (persons, cType) {
  106. var conv = {
  107. type: cType,
  108. personList: persons,
  109. };
  110. var _self = this;
  111. o2.Actions.load("x_message_assemble_communicate").ImAction.create(conv, function (json) {
  112. var newConv = json.data;
  113. console.log(newConv);
  114. var isOld = false;
  115. for (var i = 0; i < _self.conversationNodeItemList.length; i++) {
  116. var c = _self.conversationNodeItemList[i];
  117. if (newConv.id == c.id) {
  118. isOld = true;
  119. _self.tapConv(c);
  120. }
  121. }
  122. if(!isOld) {
  123. var itemNode = _self._createConvItemNode(newConv);
  124. _self.conversationNodeItemList.push(itemNode);
  125. _self.tapConv(newConv);
  126. }
  127. console.log("创建会话 结束。。。。。");
  128. }.bind(this), function (error) {
  129. console.log(error);
  130. }.bind(this))
  131. },
  132. _createConvItemNode: function (conv) {
  133. return new MWF.xApplication.IMV2.ConversationItem(conv, this);
  134. },
  135. _setCheckNode: function (conv) {
  136. for (var i = 0; i < this.conversationNodeItemList.length; i++) {
  137. var item = this.conversationNodeItemList[i];
  138. if (item.data.id == conv.id) {
  139. item.addCheckClass();
  140. } else {
  141. item.removeCheckClass();
  142. }
  143. }
  144. },
  145. //创建文本消息 并发送
  146. _newAndSendTextMsg: function (text) {
  147. var distinguishedName = layout.session.user.distinguishedName;
  148. var time = this._currentTime();
  149. var body = { "body": text };
  150. var bodyJson = JSON.stringify(body);
  151. var uuid = (new MWF.widget.UUID).toString();
  152. var textMessage = {
  153. "id": uuid,
  154. "conversationId": this.conversationId,
  155. "body": bodyJson,
  156. "createPerson": distinguishedName,
  157. "createTime": time,
  158. "sendStatus": 1
  159. };
  160. o2.Actions.load("x_message_assemble_communicate").ImAction.msgCreate(textMessage,
  161. function (json) {
  162. //data = json.data;
  163. console.log("消息发送成功!");
  164. }.bind(this),
  165. function (error) {
  166. console.log(error);
  167. }.bind(this));
  168. this._buildSender(body, distinguishedName, false);
  169. for (var i = 0; i < this.conversationNodeItemList.length; i++) {
  170. var node = this.conversationNodeItemList[i];
  171. if (node.data.id == this.conversationId) {
  172. node.refreshLastMsg(textMessage);
  173. }
  174. }
  175. },
  176. //创建消息html节点
  177. _buildMsgNode: function (msg) {
  178. var createPerson = msg.createPerson;
  179. var jsonbody = msg.body;
  180. var body = JSON.parse(jsonbody);//todo 目前只有一种text类型
  181. var distinguishedName = layout.session.user.distinguishedName;
  182. if (createPerson != distinguishedName) {
  183. this._buildReceiver(body, createPerson, true);
  184. } else {
  185. this._buildSender(body, createPerson, true);
  186. }
  187. },
  188. /**
  189. * 消息发送体
  190. * @param msgBody 消息体
  191. * @param createPerson 消息人员
  192. * @param isTop 是否放在顶部
  193. */
  194. _buildSender: function (msgBody, createPerson, isTop) {
  195. var receiverBodyNode = new Element("div", { "class": "chat-sender" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
  196. var avatarNode = new Element("div").inject(receiverBodyNode);
  197. var avatarUrl = this._getIcon(createPerson);
  198. var name = createPerson;
  199. if (createPerson.indexOf("@") != -1) {
  200. name = name.substring(0, createPerson.indexOf("@"));
  201. }
  202. var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
  203. var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
  204. var lastNode = new Element("div").inject(receiverBodyNode);
  205. var lastFirstNode = new Element("div", { "class": "chat-left_triangle" }).inject(lastNode);
  206. //text
  207. var lastSecNode = new Element("span", { "text": msgBody.body }).inject(lastNode);
  208. },
  209. /**
  210. * 消息接收体
  211. * @param msgBody
  212. * @param createPerson 消息人员
  213. * @param isTop 是否放在顶部
  214. */
  215. _buildReceiver: function (msgBody, createPerson, isTop) {
  216. var receiverBodyNode = new Element("div", { "class": "chat-receiver" }).inject(this.chatContentNode, isTop ? "top" : "bottom");
  217. var avatarNode = new Element("div").inject(receiverBodyNode);
  218. var avatarUrl = this._getIcon(createPerson);
  219. var name = createPerson;
  220. if (createPerson.indexOf("@") != -1) {
  221. name = name.substring(0, createPerson.indexOf("@"));
  222. }
  223. var avatarImg = new Element("img", { "src": avatarUrl }).inject(avatarNode);
  224. var nameNode = new Element("div", { "text": name }).inject(receiverBodyNode);
  225. var lastNode = new Element("div").inject(receiverBodyNode);
  226. var lastFirstNode = new Element("div", { "class": "chat-right_triangle" }).inject(lastNode);
  227. //text
  228. var lastSecNode = new Element("span", { "text": msgBody.body }).inject(lastNode);
  229. },
  230. //用户头像
  231. _getIcon: function (id) {
  232. var orgAction = MWF.Actions.get("x_organization_assemble_control")
  233. var url = (id) ? orgAction.getPersonIcon(id) : "/x_component_IMV2/$Main/default/icons/group.png";
  234. return url + "?" + (new Date().getTime());
  235. },
  236. //输出特殊的时间格式
  237. _friendlyTime: function (date) {
  238. var day = date.getDate();
  239. var monthIndex = date.getMonth();
  240. var year = date.getFullYear();
  241. var time = date.getTime();
  242. var today = new Date();
  243. var todayDay = today.getDate();
  244. var todayMonthIndex = today.getMonth();
  245. var todayYear = today.getFullYear();
  246. var todayTime = today.getTime();
  247. var retTime = "";
  248. //同一天
  249. if (day === todayDay && monthIndex === todayMonthIndex && year === todayYear) {
  250. var hour = 0;
  251. if (todayTime > time) {
  252. hour = parseInt((todayTime - time) / 3600000);
  253. if (hour == 0) {
  254. retTime = Math.max(parseInt((todayTime - time) / 60000), 1) + "分钟前"
  255. } else {
  256. retTime = hour + "小时前"
  257. }
  258. }
  259. return retTime;
  260. }
  261. var dates = parseInt(time / 86400000);
  262. var todaydates = parseInt(todayTime / 86400000);
  263. if (todaydates > dates) {
  264. var days = (todaydates - dates);
  265. if (days == 1) {
  266. retTime = "昨天";
  267. } else if (days == 2) {
  268. retTime = "前天 ";
  269. } else if (days > 2 && days < 31) {
  270. retTime = days + "天前";
  271. } else if (days >= 31 && days <= 2 * 31) {
  272. retTime = "一个月前";
  273. } else if (days > 2 * 31 && days <= 3 * 31) {
  274. retTime = "2个月前";
  275. } else if (days > 3 * 31 && days <= 4 * 31) {
  276. retTime = "3个月前";
  277. } else {
  278. retTime = this._formatDate(date);
  279. }
  280. }
  281. return retTime;
  282. },
  283. //yyyy-MM-dd
  284. _formatDate: function (date) {
  285. var month = date.getMonth() + 1;
  286. var day = date.getDate();
  287. month = (month.toString().length == 1) ? ("0" + month) : month;
  288. day = (day.toString().length == 1) ? ("0" + day) : day;
  289. return date.getFullYear() + '-' + month + '-' + day;
  290. },
  291. _currentTime: function () {
  292. var today = new Date();
  293. var year = today.getFullYear(); //得到年份
  294. var month = today.getMonth();//得到月份
  295. var date = today.getDate();//得到日期
  296. var hour = today.getHours();//得到小时
  297. var minu = today.getMinutes();//得到分钟
  298. var sec = today.getSeconds();//得到秒
  299. month = month + 1;
  300. if (month < 10) month = "0" + month;
  301. if (date < 10) date = "0" + date;
  302. if (hour < 10) hour = "0" + hour;
  303. if (minu < 10) minu = "0" + minu;
  304. if (sec < 10) sec = "0" + sec;
  305. return year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
  306. }
  307. });
  308. //会话对象
  309. MWF.xApplication.IMV2.ConversationItem = new Class({
  310. initialize: function (data, main) {
  311. this.data = data;
  312. this.main = main;
  313. this.container = this.main.chatItemListNode;
  314. this.load();
  315. },
  316. load: function () {
  317. var avatarDefault = this.main._getIcon();
  318. var convData = {
  319. "id": this.data.id,
  320. "avatarUrl": avatarDefault,
  321. "title": this.data.title,
  322. "time": "",
  323. "lastMessage": ""
  324. };
  325. var distinguishedName = layout.session.user.distinguishedName;
  326. if (this.data.type && this.data.type === "single") {
  327. var chatPerson = "";
  328. if (this.data.personList && this.data.personList instanceof Array) {
  329. for (var j = 0; j < this.data.personList.length; j++) {
  330. var person = this.data.personList[j];
  331. if (person !== distinguishedName) {
  332. chatPerson = person;
  333. }
  334. }
  335. }
  336. convData.avatarUrl = this.main._getIcon(chatPerson);
  337. var name = chatPerson;
  338. if (chatPerson.indexOf("@") != -1) {
  339. name = name.substring(0, chatPerson.indexOf("@"));
  340. }
  341. convData.title = name;
  342. }
  343. if (this.data.lastMessage) {
  344. //todo 其它消息类型
  345. var mBody = JSON.parse(this.data.lastMessage.body);
  346. convData.lastMessage = mBody.body;
  347. if (this.data.lastMessage.createTime) {
  348. var time = this.main._friendlyTime(o2.common.toDate(this.data.lastMessage.createTime));
  349. convData.time = time;
  350. }
  351. }
  352. this.node = new Element("div", { "class": "item" }).inject(this.container);
  353. this.nodeBaseItem = new Element("div", { "class": "base" }).inject(this.node);
  354. var avatarNode = new Element("div", { "class": "avatar" }).inject(this.nodeBaseItem);
  355. new Element("img", { "src": convData.avatarUrl, "class": "img" }).inject(avatarNode);
  356. var bodyNode = new Element("div", { "class": "body" }).inject(this.nodeBaseItem);
  357. var bodyUpNode = new Element("div", { "class": "body_up" }).inject(bodyNode);
  358. new Element("div", { "class": "body_title", "text": convData.title }).inject(bodyUpNode);
  359. this.messageTimeNode = new Element("div", { "class": "body_time", "text": convData.time }).inject(bodyUpNode);
  360. this.lastMessageNode = new Element("div", { "class": "body_down", "text": convData.lastMessage }).inject(bodyNode);
  361. var _self = this;
  362. this.node.addEvents({
  363. "click": function () {
  364. _self.main.tapConv(_self.data);
  365. }
  366. });
  367. },
  368. /**
  369. * {
  370. "id": uuid,
  371. "conversationId": this.conversationId,
  372. "body": bodyJson,
  373. "createPerson": distinguishedName,
  374. "createTime": time,
  375. "sendStatus": 1
  376. };
  377. * 刷新会话列表的最后消息内容
  378. * @param {*} lastMessage
  379. */
  380. refreshLastMsg: function(lastMessage) {
  381. //目前是text 类型的消息
  382. var jsonbody = lastMessage.body;
  383. var body = JSON.parse(jsonbody);//todo 目前只有一种text类型
  384. if(this.lastMessageNode) {
  385. this.lastMessageNode.set('text', body.body);
  386. }
  387. var time = this.main._friendlyTime(o2.common.toDate(lastMessage.createTime));
  388. if(this.messageTimeNode) {
  389. this.messageTimeNode.set("text", time);
  390. }
  391. },
  392. addCheckClass: function () {
  393. if (this.nodeBaseItem) {
  394. if (!this.nodeBaseItem.hasClass("check")) {
  395. this.nodeBaseItem.addClass("check");
  396. }
  397. }
  398. },
  399. removeCheckClass: function () {
  400. if (this.nodeBaseItem) {
  401. if (this.nodeBaseItem.hasClass("check")) {
  402. this.nodeBaseItem.removeClass("check");
  403. }
  404. }
  405. }
  406. });
  407. //弹出窗 表单 单聊创建的form
  408. MWF.xApplication.IMV2.SingleForm = new Class({
  409. Extends: MPopupForm,
  410. Implements: [Options, Events],
  411. options: {
  412. "style": "minder",
  413. "width": 700,
  414. //"height": 300,
  415. "height": "200",
  416. "hasTop": true,
  417. "hasIcon": false,
  418. "draggable": true,
  419. "title": "创建单聊"
  420. },
  421. _createTableContent: function () {
  422. var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
  423. "<tr><td styles='formTableTitle' lable='person' width='25%'></td>" +
  424. " <td styles='formTableValue14' item='person' colspan='3'></td></tr>" +
  425. "</table>";
  426. this.formTableArea.set("html", html);
  427. var me = layout.session.user.distinguishedName;
  428. var exclude = [];
  429. if (me) {
  430. exclude = [me];
  431. }
  432. this.form = new MForm(this.formTableArea, this.data || {}, {
  433. isEdited: true,
  434. style: "minder",
  435. hasColon: true,
  436. itemTemplate: {
  437. person: { text: "选择人员", type: "org", orgType: "person", notEmpty: true, exclude: exclude },
  438. }
  439. }, this.app);
  440. this.form.load();
  441. },
  442. _createBottomContent: function () {
  443. if (this.isNew || this.isEdited) {
  444. this.okActionNode = new Element("button.inputOkButton", {
  445. "styles": this.css.inputOkButton,
  446. "text": "确定"
  447. }).inject(this.formBottomNode);
  448. this.okActionNode.addEvent("click", function (e) {
  449. this.save(e);
  450. }.bind(this));
  451. }
  452. this.cancelActionNode = new Element("button.inputCancelButton", {
  453. "styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
  454. "text": "关闭"
  455. }).inject(this.formBottomNode);
  456. this.cancelActionNode.addEvent("click", function (e) {
  457. this.close(e);
  458. }.bind(this));
  459. },
  460. save: function () {
  461. var data = this.form.getResult(true, null, true, false, true);
  462. if (data) {
  463. console.log(data);
  464. this.app.newConversation(data.person, "single");
  465. this.close();
  466. }
  467. }
  468. });