Starter.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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 = MWF.xApplication.IMV2 || {};
  5. MWF.require("MWF.widget.Mask", null, false);
  6. MWF.xDesktop.requireApp("IMV2", "lp."+o2.language, null, false);
  7. MWF.xApplication.IMV2.Starter = new Class({
  8. Extends: MWF.widget.Common,
  9. Implements: [Options, Events],
  10. options: {
  11. "style": "default",
  12. "businessType" : "none", // 业务类型 :none, process, cms 。启动聊天 是否带业务类型
  13. "businessId": null, // 业务ID 新创建为空
  14. "includePersonList": [], // 在规定范围内的人员进行选择, 如果为空,则不限制
  15. "conversationId": null, // 传入的会话id, 新创建为空
  16. "mode": "default" // 展现模式:default onlyChat 。 onlyChat的模式需要传入conversationId 会打开这个会话的聊天窗口并隐藏左边的会话列表
  17. },
  18. initialize: function(data, app, options){
  19. console.log("init IMV2.Starter " );
  20. this.setOptions(options);
  21. this.path = "../x_component_IMV2/$Main/";
  22. MWF.xDesktop.requireApp("IMV2", "lp."+o2.language, null, false);
  23. this.lp = MWF.xApplication.IMV2.LP;
  24. this.data = data;
  25. this.app = app;
  26. },
  27. load: function(){
  28. console.log("init IMV2.load " );
  29. debugger;
  30. // 打开聊天会话
  31. if (this.options.conversationId && this.options.conversationId !== "") {
  32. this.openConversationWindow(this.options.conversationId);
  33. } else {
  34. // 创建聊天
  35. var me = layout.session.user.distinguishedName;
  36. var exclude = [];
  37. if (me) {
  38. exclude = [me];
  39. }
  40. // 默认创建聊天
  41. if ( !(this.options.businessType) || this.options.businessType === "none" || !(this.options.businessId) || this.options.businessId === "" ) {
  42. var form = new MWF.xApplication.IMV2.Starter.CreateConversationForm(this);
  43. form.create()
  44. } else if (this.options.businessType === "process") {
  45. if (this.options.businessId) {
  46. //TODO 如果已经存在是否考虑可创建多个,那就需要提示创建新的还是打开老的
  47. console.log("根据流程的job id查询,会话是否存在,如果存在,则打开,如果不存在,则创建。jobId: " + this.options.businessId);
  48. this.findConversationByBusinessId();
  49. } else {
  50. this.app.notice(this.lp.msgNoBusinessId, "error");
  51. }
  52. //打开工作 o2.env.form.openJob
  53. } else {
  54. this.app.notice(this.lp.msgNotSupport, "error");
  55. }
  56. }
  57. },
  58. // 打开会话聊天窗口
  59. openConversationWindow: function(conversationId) {
  60. var options = {
  61. conversationId: conversationId,
  62. mode: this.options.mode || "default"
  63. }
  64. layout.openApplication(null, "IMV2", options);
  65. },
  66. // 创建会话
  67. newConversation: function(personList) {
  68. console.log("newConversation", personList);
  69. var cType = "single"; // cType 会话类型 "single" "group"
  70. if (personList.length > 1) {
  71. cType = "group"
  72. }
  73. var conv = {
  74. type: cType,
  75. personList: personList,
  76. };
  77. if (this.options.businessId) {
  78. conv.businessId = this.options.businessId;
  79. conv.businessType = this.options.businessType;
  80. }
  81. o2.Actions.load("x_message_assemble_communicate").ImAction.create(conv, function (json) {
  82. var newConv = json.data;
  83. if (this.app.refreshAll) this.app.refreshAll();
  84. this.openConversationWindow(newConv.id);
  85. }.bind(this), function (error) {
  86. console.log(error);
  87. this.app.notice(error, "error");
  88. if (this.app.refreshAll) this.app.refreshAll();
  89. }.bind(this))
  90. },
  91. // 根据流程的job id查询,会话是否存在
  92. findConversationByBusinessId: function() {
  93. o2.Actions.load("x_message_assemble_communicate").ImAction.conversationFindByBusinessId(this.options.businessId, function(json){
  94. if (json.data && json.data.length > 0) {
  95. this.showChooseConversationDialog(json.data);
  96. } else {
  97. if (this.options.businessType === "process") {
  98. this.getProcessReviewByJobId();
  99. }
  100. }
  101. }.bind(this), function (error) {
  102. console.log(error);
  103. if (this.options.businessType === "process") {
  104. this.getProcessReviewByJobId();
  105. }
  106. }.bind(this));
  107. },
  108. // 有存在的会话 展现可以点击打开会话聊天
  109. showChooseConversationDialog: function(conversationList) {
  110. if (conversationList && conversationList.length > 0) {
  111. var cssPath = this.path + "default/style.css";
  112. var chooseConversationHtmlPath = this.path + "default/chooseConversation.html";
  113. this.app.content.loadAll({
  114. "css": [cssPath],
  115. "html": chooseConversationHtmlPath,
  116. }, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function(){
  117. // 会话列表
  118. for (let index = 0; index < conversationList.length; index++) {
  119. const conv = conversationList[index];
  120. // <div class="conversation-item">群聊1111 | <span style="color: #4A90E2;">打开</span></div>
  121. var conversationItem = new Element("div", {"class": "conversation-item"}).inject(this.conversationChooseListNode);
  122. new Element("span", {"text": "【"+conv.title+"】"}).inject(conversationItem);
  123. new Element("span", {"text": "打开", "style": "color: #4A90E2;"}).inject(conversationItem);
  124. // conversationItem.set("text", conv.title + " | <span style=\"color: #4A90E2;\">打开</span>");
  125. conversationItem.store("conversation", conv);
  126. conversationItem.addEvents({
  127. "click": function(e){
  128. debugger;
  129. var myConv = null;
  130. if (e.target.get("tag") === "span") {
  131. myConv = e.target.parentNode.retrieve("conversation");
  132. } else {
  133. myConv = e.target.retrieve("conversation");
  134. }
  135. if (myConv) {
  136. this.openConversationWindow(myConv.id);
  137. this.closeChooseConversationDialog();
  138. }
  139. }.bind(this)
  140. });
  141. }
  142. // 关闭按钮
  143. this.conversationChooseCloseNode.addEvents({
  144. "click": function(e){
  145. this.closeChooseConversationDialog();
  146. }.bind(this)
  147. });
  148. // 新建聊天按钮
  149. this.conversationCreateNewNode.addEvents({
  150. "click": function(e){
  151. this.closeChooseConversationDialog();
  152. if (this.options.businessType === "process") {
  153. this.getProcessReviewByJobId();
  154. }
  155. }.bind(this)
  156. })
  157. }.bind(this));
  158. }
  159. },
  160. // 关闭会话选择窗口
  161. closeChooseConversationDialog: function() {
  162. if (this.conversationChooseDialogNode) {
  163. this.conversationChooseDialogNode.destroy();
  164. this.conversationChooseDialogNode = null;
  165. }
  166. if (this.app.refreshAll) this.app.refreshAll();
  167. },
  168. // 显示创建会话的表单,上面列示出人员列表,可以选择人员
  169. showCreateConversationWithPersonsDialog: function(personList) {
  170. if (personList && personList.length > 0) {
  171. var cssPath = this.path + "default/style.css";
  172. var choosePersonHtmlPath = this.path + "default/choosePerson.html";
  173. this.app.content.loadAll({
  174. "css": [cssPath],
  175. "html": choosePersonHtmlPath,
  176. }, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function(){
  177. //载入完成后的回调
  178. // 人员列表
  179. for (let index = 0; index < personList.length; index++) {
  180. const person = personList[index];
  181. // <div class="person-tag person-tag-active">了国栋</div>
  182. var personTag = new Element("div", {"class": "person-tag person-tag-active"}).inject(this.personListNode);
  183. var name = person;
  184. if (person.indexOf("@") != -1) {
  185. name = name.substring(0, person.indexOf("@"));
  186. }
  187. personTag.set("text", name);
  188. personTag.store("person", person);
  189. personTag.addEvents({
  190. "click": function(e){
  191. if (e.target.get("class") == "person-tag person-tag-active") {
  192. e.target.set("class", "person-tag");
  193. } else {
  194. e.target.set("class", "person-tag person-tag-active");
  195. }
  196. }
  197. });
  198. }
  199. // 创建会话按钮
  200. this.personChooseCreateNode.addEvents({
  201. "click": function(e){
  202. var personList = []
  203. this.personListNode.getChildren().each(function(tag){
  204. if (tag.get("class") == "person-tag person-tag-active") {
  205. personList.push(tag.retrieve("person"));
  206. }
  207. });
  208. if (personList.length > 0) {
  209. this.newConversation(personList);
  210. this.closeChoosePersonDialog();
  211. } else {
  212. this.app.notice(this.lp.msgNeedChoosePerson, "error");
  213. }
  214. }.bind(this)
  215. });
  216. // 关闭按钮
  217. this.personChooseCloseBtnNode.addEvents({
  218. "click": function(e){
  219. this.closeChoosePersonDialog();
  220. }.bind(this)
  221. })
  222. }.bind(this));
  223. } else {
  224. this.app.notice(this.lp.msgNoBusinessPerson, "error");
  225. }
  226. },
  227. // 关闭人员选择
  228. closeChoosePersonDialog: function(){
  229. if (this.personChooseDialogNode) {
  230. this.personChooseDialogNode.destroy();
  231. this.personChooseDialogNode = null;
  232. }
  233. if (this.app.refreshAll) this.app.refreshAll();
  234. },
  235. // 根据jobid查询review,获取所有相关的人员
  236. getProcessReviewByJobId: function() {
  237. o2.Actions.load("x_processplatform_assemble_surface").ReviewAction.listWithJob(this.options.businessId, function(json){
  238. if (json.data && json.data.length > 0) {
  239. var personList = [];
  240. json.data.each(function(review){
  241. if (review.person && review.person !== layout.session.user.distinguishedName) {
  242. personList.push(review.person);
  243. }
  244. });
  245. this.showCreateConversationWithPersonsDialog(personList);
  246. } else {
  247. this.showCreateConversationWithPersonsDialog([]);
  248. }
  249. }.bind(this), function(error){
  250. console.log(error);
  251. this.showCreateConversationWithPersonsDialog([]);
  252. }.bind(this));
  253. }
  254. });
  255. //创建聊天 弹出窗表单
  256. MWF.xApplication.IMV2.Starter.CreateConversationForm = new Class({
  257. Extends: MPopupForm,
  258. Implements: [Options, Events],
  259. options: {
  260. "style": "minder",
  261. "width": 700,
  262. "height": "200",
  263. "hasTop": true,
  264. "hasIcon": false,
  265. "draggable": true,
  266. "title": MWF.xApplication.IMV2.LP.createConversation,
  267. "includePersonList": [], // 在规定范围内的人员进行选择, 如果为空,则不限制
  268. "personSelected": []
  269. },
  270. _createTableContent: function () {
  271. var html = "<table width='100%' bordr='0' cellpadding='7' cellspacing='0' styles='formTable' style='margin-top: 20px; '>" +
  272. "<tr><td styles='formTableTitle' lable='person' width='25%'></td>" +
  273. " <td styles='formTableValue14' item='person' colspan='3'></td></tr>" +
  274. "</table>";
  275. this.formTableArea.set("html", html);
  276. var me = layout.session.user.distinguishedName;
  277. var exclude = [];
  278. if (me) {
  279. exclude = [me];
  280. }
  281. this.form = new MForm(this.formTableArea, this.data || {}, {
  282. isEdited: true,
  283. style: "minder",
  284. hasColon: true,
  285. itemTemplate: {
  286. person: {
  287. text: MWF.xApplication.IMV2.LP.selectPerson,
  288. type: "org",
  289. orgType: "person",
  290. notEmpty: true,
  291. exclude: exclude,
  292. count: 0, // 可选人数 0是不限制人数
  293. value: this.options["personSelected"] },
  294. }
  295. }, this.app);
  296. this.form.load();
  297. },
  298. _createBottomContent: function () {
  299. if (this.isNew || this.isEdited) {
  300. this.okActionNode = new Element("button.inputOkButton", {
  301. "styles": this.css.inputOkButton,
  302. "text": MWF.xApplication.IMV2.LP.ok
  303. }).inject(this.formBottomNode);
  304. this.okActionNode.addEvent("click", function (e) {
  305. this.save(e);
  306. }.bind(this));
  307. }
  308. this.cancelActionNode = new Element("button.inputCancelButton", {
  309. "styles": (this.isEdited || this.isNew || this.getEditPermission()) ? this.css.inputCancelButton : this.css.inputCancelButton_long,
  310. "text": MWF.xApplication.IMV2.LP.close
  311. }).inject(this.formBottomNode);
  312. this.cancelActionNode.addEvent("click", function (e) {
  313. this.close(e);
  314. }.bind(this));
  315. },
  316. save: function () {
  317. var data = this.form.getResult(true, null, true, false, true);
  318. if (data) {
  319. this.explorer.newConversation(data.person);
  320. this.close();
  321. }
  322. }
  323. });
  324. /**
  325. * 分享消息、转发消息
  326. */
  327. MWF.xApplication.IMV2.ShareToConversation = new Class({
  328. Extends: MWF.widget.Common,
  329. Implements: [Options, Events],
  330. options: {
  331. "style": "default",
  332. "businessType" : "none", // 业务类型 :none, process, cms 。启动聊天 是否带业务类型
  333. "businessId": null, // 业务ID 新创建为空
  334. "includePersonList": [], // 在规定范围内的人员进行选择, 如果为空,则不限制
  335. "conversationId": null, // 传入的会话id, 新创建为空
  336. "mode": "default" // 展现模式:default onlyChat 。 onlyChat的模式需要传入conversationId 会打开这个会话的聊天窗口并隐藏左边的会话列表
  337. },
  338. initialize: function(data, app, options){
  339. console.log("init IMV2.Starter " );
  340. this.setOptions(options);
  341. this.path = "../x_component_IMV2/$Main/";
  342. MWF.xDesktop.requireApp("IMV2", "lp."+o2.language, null, false);
  343. this.lp = MWF.xApplication.IMV2.LP;
  344. this.data = data; // msgBody 消息体
  345. this.app = app;
  346. },
  347. load: function(){
  348. console.log("init IMV2.ShareToConversation " );
  349. debugger;
  350. if (this.data && this.data.msgBody) {
  351. console.log(this.data.msgBody );
  352. this.openConversationListDialog();
  353. } else {
  354. this.app.notice(this.lp.msgShareNoBody, "error");
  355. }
  356. },
  357. // 打开窗口
  358. openConversationListDialog: function() {
  359. var cssPath = this.path + "default/style.css";
  360. var choosePersonHtmlPath = this.path + "default/shareToConversation.html";
  361. this.app.content.loadAll({
  362. "css": [cssPath],
  363. "html": choosePersonHtmlPath,
  364. }, { "bind": { "lp": this.lp, "data": {} }, "module": this }, function() {
  365. this.conversationChooseCloseBtnNode.addEvents({
  366. "click": function(){this.closeConversationListDialog()}.bind(this)
  367. });
  368. this.loadConversationList();
  369. }.bind(this));
  370. },
  371. // 关闭窗口
  372. closeConversationListDialog: function() {
  373. if (this.conversationChooseDialogNode) {
  374. this.conversationChooseDialogNode.destroy();
  375. this.conversationChooseDialogNode = null;
  376. }
  377. if (this.app.refreshAll) this.app.refreshAll();
  378. },
  379. // 加载会话列表
  380. loadConversationList: function() {
  381. o2.Actions.load("x_message_assemble_communicate").ImAction.myConversationList(function (json) {
  382. this.conversationNodeItemList = [];
  383. if (json.data && json.data instanceof Array) {
  384. for (var i = 0; i < json.data.length; i++) {
  385. var conversation = json.data[i];
  386. var itemNode = this._createConvItemNode(conversation);
  387. this.conversationNodeItemList.push(itemNode);
  388. }
  389. }
  390. }.bind(this));
  391. },
  392. //用户头像
  393. _getIcon: function (id) {
  394. var orgAction = MWF.Actions.get("x_organization_assemble_control")
  395. var url = (id) ? orgAction.getPersonIcon(id) : "../x_component_IMV2/$Main/default/icons/group.png";
  396. return url + "?" + (new Date().getTime());
  397. },
  398. // 创建会话Node
  399. _createConvItemNode: function(conversation) {
  400. var avatarDefault = this._getIcon();
  401. var convData = {
  402. "id": conversation.id,
  403. "avatarUrl": avatarDefault,
  404. "title": conversation.title,
  405. };
  406. var distinguishedName = layout.session.user.distinguishedName;
  407. if (conversation.type && conversation.type === "single") {
  408. var chatPerson = "";
  409. if (conversation.personList && conversation.personList instanceof Array) {
  410. for (var j = 0; j < conversation.personList.length; j++) {
  411. var person = conversation.personList[j];
  412. if (person !== distinguishedName) {
  413. chatPerson = person;
  414. }
  415. }
  416. }
  417. convData.avatarUrl = this._getIcon(chatPerson);
  418. var name = chatPerson;
  419. if (chatPerson.indexOf("@") != -1) {
  420. name = name.substring(0, chatPerson.indexOf("@"));
  421. }
  422. convData.title = name;
  423. }
  424. var itemNode = new Element("div", { "class": "item" }).inject(this.conversationListNode);
  425. var nodeBaseItem = new Element("div", { "class": "base" }).inject(itemNode);
  426. var avatarNode = new Element("div", { "class": "avatar" }).inject(nodeBaseItem);
  427. new Element("img", { "src": convData.avatarUrl, "class": "img" }).inject(avatarNode);
  428. new Element("div", { "class": "body" , "text": convData.title }).inject(nodeBaseItem);
  429. itemNode.store("conversation", conversation);
  430. itemNode.addEvents({
  431. "click": function() {
  432. this.clickConversationItem(conversation)
  433. }.bind(this)
  434. })
  435. return itemNode;
  436. },
  437. // 点击会话
  438. clickConversationItem: function(conversation) {
  439. console.log(conversation)
  440. var distinguishedName = layout.session.user.distinguishedName;
  441. var time = this._currentTime();
  442. var bodyJson = JSON.stringify(this.data.msgBody);
  443. var uuid = new MWF.widget.UUID().createTrueUUID();
  444. var textMessage = {
  445. "id": uuid,
  446. "conversationId": conversation.id,
  447. "body": bodyJson,
  448. "createPerson": distinguishedName,
  449. "createTime": time,
  450. "sendStatus": 1
  451. };
  452. o2.Actions.load("x_message_assemble_communicate").ImAction.msgCreate(textMessage,
  453. function (json) {
  454. var options = {
  455. conversationId: conversation.id,
  456. mode: this.options.mode || "default"
  457. }
  458. layout.openApplication(null, "IMV2", options);
  459. }.bind(this),
  460. function (error) {
  461. console.log(error);
  462. this.app.notice(this.lp.msgShareError, "error");
  463. }.bind(this));
  464. this.closeConversationListDialog();
  465. },
  466. _currentTime: function () {
  467. var today = new Date();
  468. var year = today.getFullYear(); //得到年份
  469. var month = today.getMonth();//得到月份
  470. var date = today.getDate();//得到日期
  471. var hour = today.getHours();//得到小时
  472. var minu = today.getMinutes();//得到分钟
  473. var sec = today.getSeconds();//得到秒
  474. month = month + 1;
  475. if (month < 10) month = "0" + month;
  476. if (date < 10) date = "0" + date;
  477. if (hour < 10) hour = "0" + hour;
  478. if (minu < 10) minu = "0" + minu;
  479. if (sec < 10) sec = "0" + sec;
  480. return year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
  481. }
  482. });