msgbox.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6. <title>消息盒子</title>
  7. <link rel="stylesheet" href="../../../layui.css?v=1">
  8. <style>
  9. .layim-msgbox{margin: 15px;}
  10. .layim-msgbox li{position: relative; margin-bottom: 10px; padding: 0 130px 10px 60px; padding-bottom: 10px; line-height: 22px; border-bottom: 1px dotted #e2e2e2;}
  11. .layim-msgbox .layim-msgbox-tips{margin: 0; padding: 10px 0; border: none; text-align: center; color: #999;}
  12. .layim-msgbox .layim-msgbox-system{padding: 0 10px 10px 10px;}
  13. .layim-msgbox li p span{padding-left: 5px; color: #999;}
  14. .layim-msgbox li p em{font-style: normal; color: #FF5722;}
  15. .layim-msgbox-avatar{position: absolute; left: 0; top: 0; width: 50px; height: 50px;}
  16. .layim-msgbox-user{padding-top: 5px;}
  17. .layim-msgbox-content{margin-top: 3px;}
  18. .layim-msgbox .layui-btn-small{padding: 0 15px; margin-left: 5px;}
  19. .layim-msgbox-btn{position: absolute; right: 0; top: 12px; color: #999;}
  20. </style>
  21. </head>
  22. <body>
  23. <ul class="layim-msgbox" id="LAY_view"></ul>
  24. <div style="margin: 0 15px;">
  25. <blockquote class="layui-elem-quote">注意:这些都是模拟数据,实际使用时,需将其中的模拟接口改为你的项目真实接口。
  26. <br>该模版文件所在目录(相对于layui.js):/css/modules/layim/html/msgbox.html</blockquote>
  27. </div>
  28. <textarea title="消息模版" id="LAY_tpl" style="display:none;">
  29. {{# layui.each(d.data, function(index, item){
  30. if(item.from){ }}
  31. <li data-uid="{{ item.from }}" data-fromGroup="{{ item.from_group }}">
  32. <a href="/u/{{ item.from }}/" target="_blank">
  33. <img src="{{ item.user.avatar }}" class="layui-circle layim-msgbox-avatar">
  34. </a>
  35. <p class="layim-msgbox-user">
  36. <a href="/u/{{ item.from }}/" target="_blank">{{ item.user.username||'' }}</a>
  37. <span>{{ item.time }}</span>
  38. </p>
  39. <p class="layim-msgbox-content">
  40. {{ item.content }}
  41. <span>{{ item.remark ? '附言: '+item.remark : '' }}</span>
  42. </p>
  43. <p class="layim-msgbox-btn">
  44. <button class="layui-btn layui-btn-small" data-type="agree">同意</button>
  45. <button class="layui-btn layui-btn-small layui-btn-primary" data-type="refuse">拒绝</button>
  46. </p>
  47. </li>
  48. {{# } else { }}
  49. <li class="layim-msgbox-system">
  50. <p><em>系统:</em>{{ item.content }}<span>{{ item.time }}</span></p>
  51. </li>
  52. {{# }
  53. }); }}
  54. </textarea>
  55. <!--
  56. 上述模版采用了 laytpl 语法,不了解的同学可以去看下文档:http://www.layui.com/doc/modules/laytpl.html
  57. -->
  58. <script src="../../../../layui.js?v=1"></script>
  59. <script>
  60. layui.use(['layim', 'flow'], function(){
  61. var layim = layui.layim
  62. ,layer = layui.layer
  63. ,laytpl = layui.laytpl
  64. ,$ = layui.jquery
  65. ,flow = layui.flow;
  66. var cache = {}; //用于临时记录请求到的数据
  67. //请求消息
  68. var renderMsg = function(page, callback){
  69. //实际部署时,请将下述 getmsg.json 改为你的接口地址
  70. $.get('getmsg.json', {
  71. page: page || 1
  72. }, function(res){
  73. if(res.code != 0){
  74. return layer.msg(res.msg);
  75. }
  76. //记录来源用户信息
  77. layui.each(res.data, function(index, item){
  78. cache[item.from] = item.user;
  79. });
  80. callback && callback(res.data, res.pages);
  81. });
  82. };
  83. //消息信息流
  84. flow.load({
  85. elem: '#LAY_view' //流加载容器
  86. ,isAuto: false
  87. ,end: '<li class="layim-msgbox-tips">暂无更多新消息</li>'
  88. ,done: function(page, next){ //加载下一页
  89. renderMsg(page, function(data, pages){
  90. var html = laytpl(LAY_tpl.value).render({
  91. data: data
  92. ,page: page
  93. });
  94. next(html, page < pages);
  95. });
  96. }
  97. });
  98. //打开页面即把消息标记为已读
  99. /*
  100. $.post('/message/read', {
  101. type: 1
  102. });
  103. */
  104. //操作
  105. var active = {
  106. //同意
  107. agree: function(othis){
  108. var li = othis.parents('li')
  109. ,uid = li.data('uid')
  110. ,from_group = li.data('fromGroup')
  111. ,user = cache[uid];
  112. //选择分组
  113. parent.layui.layim.setFriendGroup({
  114. type: 'friend'
  115. ,username: user.username
  116. ,avatar: user.avatar
  117. ,group: parent.layui.layim.cache().friend //获取好友分组数据
  118. ,submit: function(group, index){
  119. //将好友追加到主面板
  120. parent.layui.layim.addList({
  121. type: 'friend'
  122. ,avatar: user.avatar //好友头像
  123. ,username: user.username //好友昵称
  124. ,groupid: group //所在的分组id
  125. ,id: uid //好友ID
  126. ,sign: user.sign //好友签名
  127. });
  128. parent.layer.close(index);
  129. othis.parent().html('已同意');
  130. //实际部署时,请开启下述注释,并改成你的接口地址
  131. /*
  132. $.post('/im/agreeFriend', {
  133. uid: uid //对方用户ID
  134. ,from_group: from_group //对方设定的好友分组
  135. ,group: group //我设定的好友分组
  136. }, function(res){
  137. if(res.code != 0){
  138. return layer.msg(res.msg);
  139. }
  140. //将好友追加到主面板
  141. parent.layui.layim.addList({
  142. type: 'friend'
  143. ,avatar: user.avatar //好友头像
  144. ,username: user.username //好友昵称
  145. ,groupid: group //所在的分组id
  146. ,id: uid //好友ID
  147. ,sign: user.sign //好友签名
  148. });
  149. parent.layer.close(index);
  150. othis.parent().html('已同意');
  151. });
  152. */
  153. }
  154. });
  155. }
  156. //拒绝
  157. ,refuse: function(othis){
  158. var li = othis.parents('li')
  159. ,uid = li.data('uid');
  160. layer.confirm('确定拒绝吗?', function(index){
  161. $.post('/im/refuseFriend', {
  162. uid: uid //对方用户ID
  163. }, function(res){
  164. if(res.code != 0){
  165. return layer.msg(res.msg);
  166. }
  167. layer.close(index);
  168. othis.parent().html('<em>已拒绝</em>');
  169. });
  170. });
  171. }
  172. };
  173. $('body').on('click', '.layui-btn', function(){
  174. var othis = $(this), type = othis.data('type');
  175. active[type] ? active[type].call(this, othis) : '';
  176. });
  177. });
  178. </script>
  179. </body>
  180. </html>