chat.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. var wsHost = document.domain;
  2. // 如果你修改了application-dev.yml 中的端口号,这里的ws端口也要一块修改
  3. var wsPort = 8101;
  4. // 是否来自h5的请求
  5. var fromH5 = location.href.indexOf("/h5") > 0;
  6. var chatting = false;
  7. var systemMessage = '';
  8. var chatChannelApi = "/api/robot/chat/stream?biz=ROBOT_CHAT";
  9. function chatChannel() {
  10. systemMessage = '';
  11. chatting = false;
  12. var eventSource = new EventSource(chatChannelApi);
  13. eventSource.onopen = function(e){
  14. console.log("Connection Opened");
  15. };
  16. eventSource.onmessage = function(e){
  17. chatting = true;
  18. var message = e.data;
  19. systemMessage += message;
  20. if (e.data == ' ') {
  21. systemMessage = systemMessage + ' ';
  22. }
  23. if (e.data == 'finished') {
  24. console.log("本轮对话结束!");
  25. chatting = false;
  26. saveRobotMessage(systemMessage);
  27. systemMessage = '';
  28. } else {
  29. $("#"+robotLastMessId).html(marked.parse(systemMessage));
  30. refreshChatMessContainerScrollBar2Bottom();
  31. }
  32. };
  33. eventSource.onerror = function(e){
  34. console.log("Error");
  35. };
  36. }
  37. /**
  38. * 保存机器人聊天记录
  39. * @param robotMessage
  40. */
  41. function saveRobotMessage() {
  42. // 保存
  43. $.ajax({
  44. url:"/api/chat/mess/send",
  45. type: "post",
  46. dataType: "json",
  47. contentType: "application/json;charset=utf-8",
  48. data: JSON.stringify({
  49. receiverId: me.userId,
  50. senderId: selectCardUserId,
  51. type: 1,
  52. message: $("#"+robotLastMessId).html(),
  53. }),
  54. success:function (data) {
  55. // console.log(data);
  56. },error:function () {}
  57. })
  58. }
  59. var friendUsers = [];
  60. loadFriendList('');
  61. <!-- 创建群聊、加人加群 -->
  62. $(function () {
  63. layui.use(['dropdown', 'util', 'layer', 'table'], function(){
  64. var dropdown = layui.dropdown;
  65. //用户
  66. dropdown.render({
  67. elem: '#add-btn'
  68. , data: [{
  69. title: '创建群聊'
  70. , id: 1
  71. }, {
  72. title: '加人/订阅'
  73. , id: 2
  74. }]
  75. , click: function (obj) {
  76. if(obj.id == 1) {
  77. createGroup();
  78. } else if (obj.id == 2) {
  79. searchUserOrGroup();
  80. }
  81. }
  82. });
  83. dropdown.render({
  84. elem: '#group-ctrl'
  85. , data: [{
  86. title: '查看群成员'
  87. , id: 1
  88. }]
  89. , click: function (obj) {
  90. if(obj.id == 1) {
  91. viewGroupUsers();
  92. }
  93. }
  94. });
  95. })
  96. })
  97. function viewGroupUsers() {
  98. $.ajax({
  99. url:"/api/user/listGroupUsers/"+selectCardUserId,
  100. type:"get",
  101. success:function (data) {
  102. data = eval(data);
  103. if (data.success){
  104. var list = data.data;
  105. var groupUserContainer = '<div style=\"width: 200px; height: 300px; padding: 20px 40px; background-color: white; border: 1px solid #d6d6d6\">' +
  106. '<div style="line-height: 20px;">群成员列表</div>' +
  107. '<div>';
  108. $.each(list, function (index, user) {
  109. groupUserContainer += "<div style='position: relative; width: 100%; height: 45px; text-align: left; line-height: 45px'>" +
  110. " <img src='"+user.photo+"' style='position: relative; width: 30px; border-radius: 3px; margin: 0px 10px'><span style='font-size: 15px'>" + user.userName + "</span>" +
  111. "</div>";
  112. })
  113. groupUserContainer += '</div>' +
  114. '</div>';
  115. layer.open({
  116. type: 1,
  117. title: false,
  118. closeBtn: 0,
  119. shadeClose: true,
  120. skin: 'yourclass',
  121. content: groupUserContainer
  122. });
  123. }
  124. }
  125. })
  126. }
  127. function createGroup() {
  128. var selectedUserIdArr = [];
  129. var friendList = "";
  130. for (var i = 0; i < friendUsers.length; i++) {
  131. var user = friendUsers[i];
  132. if (user.roleCode > 3) {
  133. continue;
  134. }
  135. friendList += "<div style='position: relative; width: 80%; height: 45px; text-align: left; line-height: 45px; padding-left: 10%'>" +
  136. " <label><input type='checkbox' id='"+user.userId+"' name='checkbox-user' data-photo='"+user.photo+"' value='"+user.userId+"'> <img src='"+user.photo+"' style='position: relative; width: 30px; border-radius: 3px; margin: 0px 10px'><span style='font-size: 15px'>" + user.userName + "</span></label>" +
  137. "</div>";
  138. }
  139. var selectUserTop = fromH5 ? "50px" : "0px";
  140. var createGroupContainer = '<div style=\"width: 650px; overflow: hidden; height: 450px; background-color: white; border: 1px solid #d6d6d6\">' +
  141. '<div style="position: absolute; overflow-y: scroll; left: 0px; top: 0px; width: 49%; height: 100%; background-color: #f7f7f7; border-right: 1px solid #d6d6d6">' +
  142. ' <div style="position: relative; text-indent: 20px; line-height: 50px; font-size: 14px; text-align: left; height: 50px; color: #757373">至少选择2个用户创建群聊</div>' + friendList +
  143. '</div>' +
  144. '<div style="position: absolute; right: 0px; top: 0px; width: 49%; height: 100%">' +
  145. ' <div style="position: relative; line-height: 50px; font-size: 16px; text-align: left; height: 50px; color: black">创建群聊' +
  146. ' <input placeholder="设置群聊名称" id="createGroupName" style="position: relative; margin-right: 20px; margin-top: 10px; float: right; padding: 0px 10px; border: 1px solid #b5b5b5; border-radius: 3px; width: 150px; line-height: 28px;">' +
  147. ' </div>' +
  148. ' <div style="position: relative; height: 300px; top: '+selectUserTop+'" id="selected-user-container"></div>' +
  149. ' <div style="position: absolute; left: 0px; width: 100%; height: 65px; bottom: 0px; text-align: center;">' +
  150. ' <button id = "cancelCreateGroup" style="position: relative; padding: 7px 25px; border-radius: 3px; border: none; background-color: #eaeaea; color: black;">取消</button>' +
  151. ' <button id = "createGroup" style="position: relative; padding: 7px 25px; border-radius: 3px; border: none; background-color: #16b516; color: white;">创建</button>' +
  152. ' </div>' +
  153. '</div>' +
  154. '</div>';
  155. layer.open({
  156. type: 1,
  157. title: false,
  158. closeBtn: 0,
  159. shadeClose: true,
  160. skin: 'yourclass',
  161. content: createGroupContainer
  162. });
  163. // 获取所有的checkbox元素
  164. var checkboxes = document.querySelectorAll('input[type="checkbox"]');
  165. // 为每个checkbox添加事件监听器
  166. var marginStyle = fromH5 ? " margin: 10px 10px 10px 0px" : " margin: 20px 20px 20px 0px";
  167. checkboxes.forEach(function(checkbox) {
  168. checkbox.addEventListener('change', function() {
  169. if (this.checked) {
  170. var selectedId = this.id;
  171. var selectedPhoto = this.getAttribute("data-photo");
  172. var selectedUserCard =
  173. "<div id='selected-user-"+selectedId+"'>" +
  174. "<img src='"+selectedPhoto+"' style='width: 50px; height: 50px; border-radius: 5px; float: left; "+marginStyle+"'>" +
  175. "</div>";
  176. $("#selected-user-container").append(selectedUserCard);
  177. selectedUserIdArr.push(selectedId);
  178. } else {
  179. var cancelId = this.id;
  180. $("#selected-user-"+cancelId).remove();
  181. let indexToRemove = selectedUserIdArr.findIndex(item => item === cancelId);
  182. if (indexToRemove > -1) {
  183. selectedUserIdArr.splice(indexToRemove, 1);
  184. }
  185. }
  186. });
  187. });
  188. /**
  189. * 取消创建群聊
  190. */
  191. $("#cancelCreateGroup").on('click', function () {
  192. layer.closeAll();
  193. })
  194. /**
  195. * 创建群聊
  196. */
  197. $("#createGroup").on('click', function () {
  198. if (selectedUserIdArr.length < 2) {
  199. layer.msg("创建群组至少选择两个好友");
  200. return;
  201. }
  202. var createGroupName = $("#createGroupName").val();
  203. if (createGroupName == '' || createGroupName.trim() == '') {
  204. layer.msg("未设置群聊名称");
  205. return;
  206. }
  207. if (createGroupName.length > 20) {
  208. layer.msg("群聊名称不能超过20个字");
  209. return;
  210. }
  211. $.ajax({
  212. url:"/api/user/createChatGroup",
  213. type: "post",
  214. dataType: "json",
  215. contentType: "application/json;charset=utf-8",
  216. data: JSON.stringify({
  217. userIds: selectedUserIdArr,
  218. groupName: createGroupName
  219. }),
  220. success:function (data) {
  221. data = eval(data);
  222. if (data.success){
  223. layer.closeAll();
  224. layer.msg("创建成功");
  225. // 重新拉取群聊列表
  226. loadGroupList();
  227. } else {
  228. layer.msg(data.msg);
  229. }
  230. },error:function () {
  231. layer.msg("网络异常");
  232. }
  233. })
  234. })
  235. }
  236. function searchUserOrGroup() {
  237. var padding = fromH5 != null && fromH5 ? "padding: 10px 10px;" : "padding: 10px 50px;";
  238. var width = fromH5 != null && fromH5 ? "350px" : "500px";
  239. var searchUser = "<div style='width: "+width+"; height: 300px; background-color: white; border-radius: 5px; "+padding+"'>" +
  240. "<p style='text-align: left; line-height: 50px; font-weight: 700; font-size: 16px; color: #424242'>搜索好友/机器人/公众号 " +
  241. "<span style='font-size: 13px; font-weight: 400; float: right; color: gray'>手机号检索</span></p>" +
  242. "<input id='search-mobile' style='height: 45px; margin-top: 20px; border: 1px solid #c1d5f3; border-radius: 3px; width: 100%; text-indent: 10px;' placeholder='输入要检索的账号, 回车搜索'>" +
  243. "<div style='width: 100%; margin-top: 20px; font-size: 14px; font-weight: 400' id='search-result'>" +
  244. '检索结果: '+
  245. "</div>" +
  246. "</div>";
  247. layer.open({
  248. type: 1,
  249. title: false,
  250. closeBtn: 0,
  251. shadeClose: true,
  252. skin: 'yourclass',
  253. content: searchUser
  254. });
  255. // 监听搜索的回车事件
  256. $("#search-mobile").keydown(function(event) {
  257. if (event.keyCode == 13) {
  258. var searchMobile = $("#search-mobile").val();
  259. if (searchMobile == '' || searchMobile.toString().trim() == '') {
  260. layer.msg("输入空内容");
  261. return;
  262. }
  263. $("#search-result").empty();
  264. $.ajax({
  265. url:"/api/user/search/"+searchMobile,
  266. type:"get",
  267. success:function (data) {
  268. data = eval(data);
  269. if (data.success){
  270. var user = data.data;
  271. if(user == null) {
  272. $("#search-result").append("未查询到账号(<span style='color: orangered'>"+searchMobile+"</span>)用户!")
  273. } else {
  274. var applyText = "申请加好友";
  275. if (user.roleCode == 5) {
  276. applyText = "添加机器人";
  277. } else if (user.roleCode == 6) {
  278. applyText = "订阅公众号";
  279. }
  280. $("#search-result").append("<div class=\"search-user-card\">\n" +
  281. " <img src=\""+user.photo+"?x-oss-process=image/resize,m_fill,w_60,h_60\" class=\"search-user-photo\">\n" +
  282. " <p class=\"search-username\">"+user.userName+"</p>\n" +
  283. " <p class=\"search-mobile\">"+user.mobile+"</p>\n" +
  284. " <button class=\"applyAddUser\" onclick='applyAddUserFriend(\""+user.userId+"\", \""+user.roleCode+"\")'>"+applyText+"</button>\n" +
  285. " </div>")
  286. }
  287. } else {
  288. layer.msg("服务异常");
  289. }
  290. },error:function () {
  291. layer.msg("网络异常");
  292. }
  293. })
  294. }
  295. })
  296. }
  297. function applyAddUserFriend(friendId, friendRoleCode) {
  298. $.ajax({
  299. url:"/api/friend/applyAdd/"+friendId,
  300. type:"post",
  301. success:function (data) {
  302. data = eval(data);
  303. if (data.success){
  304. var toast = "申请已发出";
  305. if (friendRoleCode == 5) {
  306. toast = "机器人已添加";
  307. } else if (friendRoleCode == 6) {
  308. toast = "公众号订阅成功";
  309. }
  310. layer.msg(toast);
  311. } else {
  312. layer.msg(data.msg);
  313. }
  314. },error:function () {
  315. layer.msg("网络异常");
  316. }
  317. })
  318. }
  319. countNewMessage();
  320. function countNewMessage() {
  321. $.ajax({
  322. url:"/api/friend/countUnHandleApply",
  323. type:"get",
  324. success:function (data) {
  325. data = eval(data);
  326. if (data.success){
  327. if (data.data > 0) {
  328. $(".new-message-icon").show();
  329. }
  330. }
  331. }
  332. })
  333. }
  334. function loadUnHandleApplyList() {
  335. $(".apply-unhandle-list").empty();
  336. $.ajax({
  337. url:"/api/friend/listUnHandleApplyUsers",
  338. type:"get",
  339. success:function (data) {
  340. data = eval(data);
  341. if (data.success){
  342. var list = data.data;
  343. if (list.length == 0) {
  344. $(".apply-unhandle-list").append("<p style='line-height: 400px; color: #666'>暂无申请</p>");
  345. return;
  346. }
  347. $.each(list, function (index, apply) {
  348. var applyItem = "<div class='apply-item' id='apply-item-"+apply.applyId+"'>" +
  349. "<img class='applyUserPhoto' src=\""+apply.applyUser.photo+"\">" +
  350. "<span class='applyUserName'>"+apply.applyUser.userName+"</span>" +
  351. "<a onclick='applyPass(\""+apply.applyId+"\")' class='apply-pass'>同意</a>" +
  352. "<a onclick='applyRefuse(\""+apply.applyId+"\")' class='apply-refuse'>拒绝</a>" +
  353. "</div>";
  354. $(".apply-unhandle-list").append(applyItem);
  355. })
  356. }
  357. }
  358. })
  359. }
  360. function loadFriendList(userName) {
  361. friendUsers = [];
  362. $(".friend-list").empty();
  363. $.ajax({
  364. url:"/api/friend/listFriendUsers?userName="+userName,
  365. type:"get",
  366. success:function (data) {
  367. data = eval(data);
  368. if (data.success){
  369. var list = data.data;
  370. if (list.length == 0) {
  371. $(".friend-list").append("<p style='line-height: 400px; color: #666'>还没有任何好友</p>");
  372. return;
  373. }
  374. $.each(list, function (index, apply) {
  375. var user = apply.applyUser;
  376. friendUsers.push(user);
  377. var url = fromH5 ? "/h5/chat?chatid="+user.userId : "/chat?chatid="+user.userId;
  378. var applyItem = "<a href='"+url+"'><div class='apply-item' id='apply-item-"+apply.applyId+"'>" +
  379. "<img class='applyUserPhoto' src=\""+user.photo+"\">" +
  380. "<span class='friendUserName'>"+user.userName+"</span>" +
  381. "</div></a>";
  382. $(".friend-list").append(applyItem);
  383. })
  384. }
  385. }
  386. })
  387. }
  388. var colors = ["#F84E4E", "#4263EB", "#1FB759", "#017D6F", "#FA6509"];
  389. loadGroupList();
  390. function loadGroupList() {
  391. $("#chat-group-list").empty()
  392. $("#chat-group-banner").hide();
  393. $.ajax({
  394. url:"/api/user/listGroups",
  395. type:"get",
  396. success:function (data) {
  397. data = eval(data);
  398. if (data.success){
  399. var list = data.data;
  400. if (list.length == 0) {
  401. return;
  402. }
  403. var i = 0;
  404. $("#chat-group-banner").show();
  405. $.each(list, function (index, group) {
  406. var bgColor = colors[group.id % 5];
  407. var groupItem = "<a onclick=\"selectChatUser('"+group.groupId+"', '"+group.groupName+"', '群组', true)\"><button class='group-card line2ppp' style='background-color: "+bgColor+"'>"+group.groupName +"</button></a>";
  408. $("#chat-group-list").append(groupItem);
  409. })
  410. }
  411. }
  412. })
  413. }
  414. function applyPass(id) {
  415. $.ajax({
  416. url:"/api/friend/applyPass/"+id,
  417. type:"post",
  418. success:function (data) {
  419. data = eval(data);
  420. if (data.success){
  421. layer.msg("通过");
  422. $(".new-message-icon").hide();
  423. $("#apply-item-"+id).fadeOut(500);
  424. } else {
  425. layer.msg("服务异常");
  426. }
  427. }
  428. })
  429. }
  430. function applyRefuse(id) {
  431. $.ajax({
  432. url:"/api/friend/applyRefuse/"+id,
  433. type:"post",
  434. success:function (data) {
  435. data = eval(data);
  436. if (data.success){
  437. layer.msg("已拒绝");
  438. $(".new-message-icon").hide();
  439. $("#apply-item-"+id).fadeOut(500);
  440. } else {
  441. layer.msg("服务异常");
  442. }
  443. }
  444. })
  445. }
  446. var hideChatMainListContainer = function () {
  447. $(".chat-main-list-container").hide();
  448. }
  449. var initCharMenuIconStatus = function () {
  450. $(".chat-menu-icon").removeClass("selected-icon");
  451. }
  452. $(".message-icon").on('click', function () {
  453. hideChatMainListContainer();
  454. initCharMenuIconStatus();
  455. $(".apply-list-container").show();
  456. $(".message-icon").addClass("selected-icon");
  457. loadUnHandleApplyList();
  458. })
  459. $(".friend-list-icon").on('click', function () {
  460. hideChatMainListContainer();
  461. initCharMenuIconStatus();
  462. $(".friend-list-container").show();
  463. $(".friend-list-icon").addClass("selected-icon");
  464. loadFriendList('');
  465. })
  466. $(".chat-icon").on('click', function () {
  467. hideChatMainListContainer();
  468. initCharMenuIconStatus();
  469. $(".chat-user-list-container").show();
  470. $(".chat-icon").addClass("selected-icon");
  471. loadFriendList('');
  472. })
  473. $("#search-username").keydown(function(event) {
  474. if (event.keyCode == 13) {
  475. loadFriendList($("#search-username").val());
  476. }
  477. })
  478. function formatMsgTime(timespan){
  479. var dateTime = new Date(timespan);
  480. var year = dateTime.getFullYear();
  481. var month = dateTime.getMonth() + 1;
  482. var day = dateTime.getDate();
  483. var hour = dateTime.getHours();
  484. var minute = dateTime.getMinutes();
  485. var nowDate = new Date();
  486. var now_new = new Date().getTime()
  487. var milliseconds = 0;
  488. var timeSpanStr;
  489. milliseconds = now_new - timespan;
  490. if (milliseconds <= 1000 * 60 * 1) {
  491. timeSpanStr = '刚刚';
  492. }
  493. else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
  494. timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
  495. }
  496. else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
  497. timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
  498. }
  499. else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
  500. timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
  501. }
  502. else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == nowDate.getFullYear()) {
  503. timeSpanStr = day + ' / ' + month + '月';
  504. } else {
  505. timeSpanStr = year + ' / ' + month + ' / ' + day;
  506. }
  507. return timeSpanStr;
  508. };
  509. //查看结果
  510. function replace_em(str){
  511. str = str.replace(/\</g,'<');
  512. str = str.replace(/\>/g,'>');
  513. str = str.replace(/\n/g,'');
  514. str = str.replace(/\[em(\d+)\]/g, '<img src="/ref/jquery/face/$1.gif" border="0" />');
  515. return str;
  516. }
  517. function codeContentEnCode(str) {
  518. if (str == null || str == '') {return str;}
  519. str = str.replaceAll(/\r\n/g,"<br>")
  520. str = str.replaceAll(/\n/g,"<br>");
  521. str = str.replaceAll(/\'/g,"&#39;");
  522. str = str.replaceAll(/\"/g,"&quot;");
  523. str = str.replace(/&/g,"&amp;");
  524. str = str.replace(/</g,"&lt;");
  525. str = str.replace(/>/g,"&gt;");
  526. return str;
  527. }
  528. function messContentEnCode(str) {
  529. if (str == null || str == '') {return str;}
  530. str = str.replaceAll(/\r\n/g,"<br>")
  531. str = str.replaceAll(/\n/g,"<br>");
  532. str = str.replace(/&/g,"&amp;");
  533. str = str.replace(/</g,"&lt;");
  534. str = str.replace(/>/g,"&gt;");
  535. str = str.replaceAll("&lt;br&gt;","<br>")
  536. str = replace_em(str);
  537. return str;
  538. }
  539. // 获取上传按钮和文件输入框元素
  540. const uploadButton = document.getElementById('mess-file-menu');
  541. const fileInput = document.getElementById('fileInput');
  542. // 为上传按钮添加点击事件监听器
  543. uploadButton.addEventListener('click', function() {
  544. // 触发文件输入框的点击事件,以打开文件选择对话框
  545. fileInput.click();
  546. });
  547. // 为文件输入框添加变化事件监听器,以便在文件选择后上传
  548. fileInput.addEventListener('change', function() {
  549. // 检查是否选择了文件
  550. if (this.files.length > 0) {
  551. const file = this.files[0]; // 获取选择的文件
  552. uploadFile(file); // 调用上传文件的函数
  553. }
  554. });
  555. // 上传文件的函数
  556. function uploadFile(file) {
  557. const formData = new FormData();
  558. formData.append('file', file); // 将文件添加到FormData对象中
  559. fetch('/api/file/chat/upload', { // 发送POST请求到服务器的/upload端点
  560. method: 'POST',
  561. body: formData,
  562. })
  563. .then(response => response.json()) // 处理响应文本
  564. .then(data => {
  565. if (data.success) {
  566. var file = data.data;
  567. // 使用JSON.stringify将对象转换为JSON字符串,并确保转义
  568. var content = JSON.stringify({type: file.type, name: file.name, url: file.url});
  569. var data = JSON.stringify({
  570. "receiverId": selectCardUserId,
  571. "senderId": me.userId,
  572. "message": content,
  573. "type": 2
  574. });
  575. $("#chat-message-container").append(buildFileMess(JSON.parse(content), me.photo, "right"));
  576. refreshChatMessContainerScrollBar2Bottom();
  577. sendChatWebSocket(data);
  578. $.ajax({
  579. url:"/api/chat/mess/send",
  580. type: "post",
  581. dataType: "json",
  582. contentType: "application/json;charset=utf-8",
  583. data: data,
  584. success:function (data) {
  585. data = eval(data);
  586. if (!data.success){
  587. layer.msg(data.msg);
  588. } else {
  589. if (selectCardUser.roleCode != 5) {
  590. loadChat(false);
  591. }
  592. }
  593. },error:function () {}
  594. })
  595. } else {
  596. layer.msg(data.msg);
  597. }
  598. })
  599. .catch(error => {
  600. console.error('上传失败:', error); // 处理上传错误
  601. });
  602. }