layIM-init.jsp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!-- 本页面是layIM聊天插件
  2. 使用方法 :在需要支持聊天的页面直接引入该页面,<-%@include file=".../common/layIM-init.jsp->
  3. -->
  4. <%@ page contentType="text/html;charset=UTF-8" %>
  5. <%@ include file="/webpage/include/taglib.jsp"%>
  6. <meta name="decorator" content="ani"/>
  7. <link rel="stylesheet" href="${ctxStatic}/plugin/layui/dist/css/layui.css">
  8. <script src="${ctxStatic}/plugin/layui/dist/layui.js"></script>
  9. <script src="${ctxStatic}/plugin/layui/sockjs.js"></script><!-- 如果浏览器不支持socktjs,添加支持 -->
  10. <script type="text/javascript">
  11. (function() {
  12. //初始化
  13. layui.use('layim', function(layim){
  14. //---即使聊天服务端地址配置,端口和web服务器端口一样,80可以省略
  15. var host = window.document.location.host;
  16. var pathName = window.document.location.pathname;
  17. var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
  18. var wsServer = "ws://" + host + projectName;
  19. var currentLoginName = '${fns:getUser().loginName}';
  20. var currentName = '${fns:getUser().name}';
  21. var currentFace ='${fns:getUser().photo}';
  22. var webSocket = null;
  23. if ('WebSocket' in window || 'MozWebSocket' in window) {
  24. webSocket = new WebSocket(wsServer+"/layIMSocketServer");
  25. } else {
  26. webSocket = new SockJS( wsServer+"/sockjs/layIMSocketServer");
  27. }
  28. webSocket.onerror = function(event) {
  29. jp.alert("websockt连接发生错误,请刷新页面重试!")
  30. };
  31. // 连接成功建立的回调方法
  32. webSocket.onopen = function(event) {
  33. // webSocket.send("_online_user_"+currentLoginName);
  34. };
  35. // 接收到消息的回调方法
  36. webSocket.onmessage = function(event) {
  37. var res=event.data;
  38. if(res.indexOf("_online_all_status_")>=0){
  39. var dd = res.substring("_online_all_status_".length);
  40. setTimeout(function(){updateOnlineStatus(eval('('+dd+')').body.data);},500);
  41. }
  42. if(res.indexOf('_sys_')>=0){//系统通知
  43. var arra = res.split("_sys_");
  44. var sender=arra[0];
  45. var contents=arra[1];
  46. var obj = {
  47. username: "系统通知"
  48. ,avatar: layui.cache.dir+'images/face/notify.png'
  49. ,id:sender
  50. ,type: 'friend'
  51. ,content: contents
  52. }
  53. layim.getMessage(obj);
  54. return;
  55. }
  56. if(res.indexOf("_msg_")>=0){
  57. var arra = res.split("_msg_");
  58. var sender=arra[0];
  59. var receiver=arra[1];//发送者登录名
  60. var content=arra[2];
  61. var avatar=arra[3];
  62. var type=arra[4];
  63. var senderName=arra[5];//发送者姓名
  64. var sendtime = arra[6];
  65. if(sendtime != 'NAN'){//使用历史记录时间
  66. layim.getMessage({
  67. username: senderName
  68. ,avatar: avatar
  69. ,id: sender
  70. ,type: type
  71. ,content: content
  72. ,timestamp:parseFloat(sendtime)
  73. });
  74. }else{
  75. layim.getMessage({
  76. username: senderName
  77. ,avatar: avatar
  78. ,id: sender
  79. ,type: type
  80. ,content: content
  81. });
  82. }
  83. }
  84. };
  85. //发送消息的方法
  86. function send(mine,To) {
  87. webSocket.send(currentLoginName+"_msg_"+To.id+"_msg_"+mine.content+"_msg_"+mine.avatar+"_msg_"+To.type+"_msg_"+currentName+"_msg_NAN");
  88. };
  89. //切换在线状态的方法
  90. function setonline() {
  91. webSocket.send("_online_user_"+currentLoginName);
  92. };
  93. //切换离线状态的方法
  94. function sethide() {
  95. webSocket.send("_leave_user_"+currentLoginName);
  96. };
  97. //更新在线用户信息
  98. function updateOnlineStatus(arra)//更新在线用户信息
  99. {
  100. $("div.layui-layim-main ul.layim-list-friend li ul.layui-layim-list li").each(function(){//状态还原
  101. if(this.className != 'layim-null'){
  102. var span = $(this).find("span:first");
  103. var name = span.html();
  104. var loginName = this.className.replace("layim-friend","").trim();
  105. if((','+arra+",").indexOf(','+loginName+',')>=0){
  106. if(name.indexOf('(<font color="red">离线</font>)')>=0){
  107. span.replace('(<font color="red">离线</font>)', '(<font color="green">在线</font>)')
  108. } else if (name.indexOf('(<font color="green">在线</font>)')>=0){
  109. }else{
  110. span.html(name +'(<font color="green">在线</font>)');
  111. }
  112. }else{
  113. if(name.indexOf('(<font color="red">离线</font>)')>=0){
  114. } else if (name.indexOf('(<font color="green">在线</font>)')>=0){
  115. span.replace( '(<font color="green">在线</font>)', '(<font color="red">离线</font>)')
  116. }else{
  117. span.html(name+'(<font color="red">离线</font>)');
  118. }
  119. }
  120. }
  121. });
  122. }
  123. //基础配置
  124. layim.config({
  125. //初始化接口
  126. init: {
  127. url: '${ctx}/iim/contact/friend'
  128. ,data: {}
  129. }
  130. //简约模式(不显示主面板)
  131. //,brief: true
  132. //查看群员接口
  133. ,members: {
  134. url: '${ctx}/iim/contact/getMembers'
  135. ,data: {}
  136. }
  137. ,uploadImage: {
  138. url: '${ctx}/iim/contact/uploadImage'//(返回的数据格式见下文)
  139. ,type: 'post' //默认post
  140. }
  141. ,uploadFile: {
  142. url: '${ctx}/iim/contact/uploadFile' //(返回的数据格式见下文)
  143. ,type: 'post' //默认post
  144. }
  145. //扩展工具栏,下文会做进一步介绍(如果无需扩展,剔除该项即可)
  146. ,tool: [{
  147. alias: 'code' //工具别名
  148. ,title: '代码' //工具名称
  149. ,icon: '&#xe64e;' //工具图标,参考图标文档
  150. }]
  151. // ,msgbox: layui.cache.dir + 'css/modules/layim/html/msgbox.html' //消息盒子页面地址,若不开启,剔除该项即可
  152. ,chatLog:'${ctx}/iim/chatHistory' //聊天记录地址
  153. ,find:'${ctx}/iim/contact/layimManager' //发现页面地址,若不开启,剔除该项即可
  154. });
  155. //监听自定义工具栏点击,以添加代码为例
  156. layim.on('tool(code)', function(insert, send, obj){ //事件中的tool为固定字符,而code则为过滤器,对应的是工具别名(alias)
  157. layer.prompt({
  158. title: '插入代码'
  159. ,formType: 2
  160. ,shade: 0
  161. }, function(text, index){
  162. layer.close(index);
  163. insert('[pre class=layui-code]' + text + '[/pre]'); //将内容插入到编辑器,主要由insert完成
  164. //send(); //自动发送
  165. });
  166. });
  167. //监听发送消息
  168. layim.on('sendMessage', function(data){
  169. var mine = data.mine; //包含我发送的消息及我的信息
  170. var To = data.to; //对方的信息
  171. //发送消息到WebSocket服务
  172. send(mine,To);
  173. });
  174. //监听在线状态的切换事件
  175. layim.on('online', function(data){
  176. if(data=="online"){
  177. setonline(); //用户上线
  178. }else{
  179. sethide();//用户离线
  180. }
  181. });
  182. //layim建立就绪
  183. layim.on('ready', function(res){
  184. //更改签名
  185. var signObj = $('.layui-layim-remark');
  186. signObj.focus(function () {
  187. signObj.removeClass('layim-sign-hide');
  188. console.log('准备更改签名');
  189. });
  190. signObj.blur(function () {
  191. console.log('更改签名完毕');
  192. signObj.addClass('layim-sign-hide');
  193. });
  194. //enter提交
  195. signObj.keydown(function (event) {
  196. if (event.which == 13) {
  197. console.log('按下了Enter提交签名');
  198. signObj.addClass('layim-sign-hide');
  199. signObj.blur();
  200. var value= signObj.val();
  201. $.post("${ctx}/sys/user/saveSign",{'sign':value},function(data){
  202. top.layer.alert(data.msg, {icon: 1});
  203. })
  204. }
  205. });
  206. });
  207. //监听查看群员
  208. layim.on('members', function(data){
  209. console.log(data);
  210. });
  211. //监听聊天窗口的切换
  212. layim.on('chatChange', function(data){
  213. console.log(data);
  214. });
  215. });
  216. })(jQuery);
  217. </script>