index.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <title>社交聊天</title>
  2. <div class="layui-card layadmin-header">
  3. <div class="layui-breadcrumb" lay-filter="breadcrumb">
  4. <a lay-href="">主页</a>
  5. <a><cite>高级</cite></a>
  6. <a><cite>通讯系统</cite></a>
  7. <a><cite>社交聊天</cite></a>
  8. </div>
  9. </div>
  10. <div class="layui-fluid">
  11. <div class="layui-row layui-col-space15">
  12. <div class="layui-col-md12">
  13. <div class="layui-card">
  14. <div class="layui-card-header">面板外的操作示例</div>
  15. <div class="layui-card-body">
  16. <div class="layui-btn-container LAY-senior-im-chat-demo">
  17. <button class="layui-btn" data-type="chat">自定义会话</button>
  18. <button class="layui-btn" data-type="message">接受好友的消息</button>
  19. <button class="layui-btn" data-type="messageAudio">接受音频消息</button>
  20. <button class="layui-btn" data-type="messageVideo">接受视频消息</button>
  21. <button class="layui-btn" data-type="messageTemp">接受临时会话消息</button>
  22. <br>
  23. <button class="layui-btn" data-type="add">申请好友</button>
  24. <button class="layui-btn" data-type="addqun">申请加群</button>
  25. <button class="layui-btn" data-type="addFriend">同意好友</button>
  26. <button class="layui-btn" data-type="addGroup">增加群组到主面板</button>
  27. <button class="layui-btn" data-type="removeFriend">删除主面板好友</button>
  28. <button class="layui-btn" data-type="removeGroup">删除主面板群组</button>
  29. <br>
  30. <button class="layui-btn" data-type="setGray">置灰离线好友</button>
  31. <button class="layui-btn" data-type="unGray">取消好友置灰</button>
  32. <button class="layui-btn" data-type="kefu1">在线客服一</button>
  33. <button class="layui-btn" data-type="kefu2">在线客服二</button>
  34. <button class="layui-btn" data-type="mobile">移动端版本</button>
  35. </div>
  36. </div>
  37. </div>
  38. </div>
  39. </div>
  40. </div>
  41. <script>
  42. layui.use(['admin', 'layim'], function(){
  43. var $ = layui.$
  44. ,admin = layui.admin
  45. ,element = layui.element
  46. ,router = layui.router();
  47. var layim = layui.layim;
  48. //演示自动回复
  49. var autoReplay = [
  50. '您好,我现在有事不在,一会再和您联系。',
  51. '你没发错吧?face[微笑] ',
  52. '洗澡中,请勿打扰,偷窥请购票,个体四十,团体八折,订票电话:一般人我不告诉他!face[哈哈] ',
  53. '你好,我是主人的美女秘书,有什么事就跟我说吧,等他回来我会转告他的。face[心] face[心] face[心] ',
  54. 'face[威武] face[威武] face[威武] face[威武] ',
  55. '<(@ ̄︶ ̄@)>',
  56. '你要和我说话?你真的要和我说话?你确定自己想说吗?你一定非说不可吗?那你说吧,这是自动回复。',
  57. 'face[黑线] 你慢慢说,别急……',
  58. '(*^__^*) face[嘻嘻] ,是贤心吗?'
  59. ];
  60. //基础配置
  61. layim.config({
  62. //初始化接口
  63. init: {
  64. url: './json/layim/getList.js'
  65. ,data: {}
  66. }
  67. //查看群员接口
  68. ,members: {
  69. url: './json/layim/getMembers.js'
  70. ,data: {}
  71. }
  72. ,uploadImage: {
  73. url: '' //(返回的数据格式见下文)
  74. ,type: '' //默认post
  75. }
  76. ,uploadFile: {
  77. url: '' //(返回的数据格式见下文)
  78. ,type: '' //默认post
  79. }
  80. ,isAudio: true //开启聊天工具栏音频
  81. ,isVideo: true //开启聊天工具栏视频
  82. //扩展工具栏
  83. ,tool: [{
  84. alias: 'code'
  85. ,title: '代码'
  86. ,icon: '&#xe64e;'
  87. }]
  88. //,brief: true //是否简约模式(若开启则不显示主面板)
  89. //,title: 'WebIM' //自定义主面板最小化时的标题
  90. //,right: '100px' //主面板相对浏览器右侧距离
  91. //,minRight: '90px' //聊天面板最小化时相对浏览器右侧距离
  92. ,initSkin: '3.jpg' //1-5 设置初始背景
  93. //,skin: ['aaa.jpg'] //新增皮肤
  94. //,isfriend: false //是否开启好友
  95. //,isgroup: false //是否开启群组
  96. //,min: true //是否始终最小化主面板,默认false
  97. //,notice: true //是否开启桌面消息提醒,默认false
  98. //,voice: false //声音提醒,默认开启,声音文件为:default.mp3
  99. ,msgbox: '/layim/demo/msgbox.html' //消息盒子页面地址,若不开启,剔除该项即可
  100. ,find: '/layim/demo/find.html' //发现页面地址,若不开启,剔除该项即可
  101. ,chatLog: '/layim/demo/chatlog.html' //聊天记录页面地址,若不开启,剔除该项即可
  102. });
  103. //监听在线状态的切换事件
  104. layim.on('online', function(status){
  105. layer.msg(status);
  106. });
  107. //监听签名修改
  108. layim.on('sign', function(value){
  109. layer.msg(value);
  110. });
  111. //监听自定义工具栏点击,以添加代码为例
  112. layim.on('tool(code)', function(insert){
  113. layer.prompt({
  114. title: '插入代码 - 工具栏扩展示例'
  115. ,formType: 2
  116. ,shade: 0
  117. }, function(text, index){
  118. layer.close(index);
  119. insert('[pre class=layui-code]' + text + '[/pre]'); //将内容插入到编辑器
  120. });
  121. });
  122. //监听layim建立就绪
  123. layim.on('ready', function(res){
  124. //console.log(res.mine);
  125. layim.msgbox(5); //模拟消息盒子有新消息,实际使用时,一般是动态获得
  126. });
  127. //监听发送消息
  128. layim.on('sendMessage', function(data){
  129. var To = data.to;
  130. //console.log(data);
  131. if(To.type === 'friend'){
  132. layim.setChatStatus('<span style="color:#FF5722;">对方正在输入。。。</span>');
  133. }
  134. //演示自动回复
  135. setTimeout(function(){
  136. var obj = {};
  137. if(To.type === 'group'){
  138. obj = {
  139. username: '模拟群员'+(Math.random()*100|0)
  140. ,avatar: layui.cache.dir + 'images/face/'+ (Math.random()*72|0) + '.gif'
  141. ,id: To.id
  142. ,type: To.type
  143. ,content: autoReplay[Math.random()*9|0]
  144. }
  145. } else {
  146. obj = {
  147. username: To.name
  148. ,avatar: To.avatar
  149. ,id: To.id
  150. ,type: To.type
  151. ,content: autoReplay[Math.random()*9|0]
  152. }
  153. layim.setChatStatus('<span style="color:#FF5722;">在线</span>');
  154. }
  155. layim.getMessage(obj);
  156. }, 1000);
  157. });
  158. //监听查看群员
  159. layim.on('members', function(data){
  160. //console.log(data);
  161. });
  162. //监听聊天窗口的切换
  163. layim.on('chatChange', function(res){
  164. var type = res.data.type;
  165. console.log(res.data.id)
  166. if(type === 'friend'){
  167. //模拟标注好友状态
  168. //layim.setChatStatus('<span style="color:#FF5722;">在线</span>');
  169. } else if(type === 'group'){
  170. //模拟系统消息
  171. layim.getMessage({
  172. system: true
  173. ,id: res.data.id
  174. ,type: "group"
  175. ,content: '模拟群员'+(Math.random()*100|0) + '加入群聊'
  176. });
  177. }
  178. });
  179. //面板外的操作
  180. var $ = layui.jquery, active = {
  181. chat: function(){
  182. //自定义会话
  183. layim.chat({
  184. name: '小闲'
  185. ,type: 'friend'
  186. ,avatar: '//tva3.sinaimg.cn/crop.0.0.180.180.180/7f5f6861jw1e8qgp5bmzyj2050050aa8.jpg'
  187. ,id: 1008612
  188. });
  189. layer.msg('也就是说,此人可以不在好友面板里');
  190. }
  191. ,message: function(){
  192. //制造好友消息
  193. layim.getMessage({
  194. username: "贤心"
  195. ,avatar: "//tp1.sinaimg.cn/1571889140/180/40030060651/1"
  196. ,id: "100001"
  197. ,type: "friend"
  198. ,content: "嗨,你好!欢迎体验LayIM。演示标记:"+ new Date().getTime()
  199. ,timestamp: new Date().getTime()
  200. });
  201. }
  202. ,messageAudio: function(){
  203. //接受音频消息
  204. layim.getMessage({
  205. username: "林心如"
  206. ,avatar: "//tp3.sinaimg.cn/1223762662/180/5741707953/0"
  207. ,id: "76543"
  208. ,type: "friend"
  209. ,content: "audio[http://gddx.sc.chinaz.com/Files/DownLoad/sound1/201510/6473.mp3]"
  210. ,timestamp: new Date().getTime()
  211. });
  212. }
  213. ,messageVideo: function(){
  214. //接受视频消息
  215. layim.getMessage({
  216. username: "林心如"
  217. ,avatar: "//tp3.sinaimg.cn/1223762662/180/5741707953/0"
  218. ,id: "76543"
  219. ,type: "friend"
  220. ,content: "video[http://www.w3school.com.cn//i/movie.ogg]"
  221. ,timestamp: new Date().getTime()
  222. });
  223. }
  224. ,messageTemp: function(){
  225. //接受临时会话消息
  226. layim.getMessage({
  227. username: "小酱"
  228. ,avatar: "//tva1.sinaimg.cn/crop.7.0.736.736.50/bd986d61jw8f5x8bqtp00j20ku0kgabx.jpg"
  229. ,id: "198909151014"
  230. ,type: "friend"
  231. ,content: "临时:"+ new Date().getTime()
  232. });
  233. }
  234. ,add: function(){
  235. //实际使用时数据由动态获得
  236. layim.add({
  237. type: 'friend'
  238. ,username: '麻花疼'
  239. ,avatar: '//tva1.sinaimg.cn/crop.0.0.720.720.180/005JKVuPjw8ers4osyzhaj30k00k075e.jpg'
  240. ,submit: function(group, remark, index){
  241. layer.msg('好友申请已发送,请等待对方确认', {
  242. icon: 1
  243. ,shade: 0.5
  244. }, function(){
  245. layer.close(index);
  246. });
  247. //通知对方
  248. /*
  249. $.post('/im-applyFriend/', {
  250. uid: info.uid
  251. ,from_group: group
  252. ,remark: remark
  253. }, function(res){
  254. if(res.status != 0){
  255. return layer.msg(res.msg);
  256. }
  257. layer.msg('好友申请已发送,请等待对方确认', {
  258. icon: 1
  259. ,shade: 0.5
  260. }, function(){
  261. layer.close(index);
  262. });
  263. });
  264. */
  265. }
  266. });
  267. }
  268. ,addqun: function(){
  269. layim.add({
  270. type: 'group'
  271. ,username: 'LayIM会员群'
  272. ,avatar: '//tva2.sinaimg.cn/crop.0.0.180.180.50/6ddfa27bjw1e8qgp5bmzyj2050050aa8.jpg'
  273. ,submit: function(group, remark, index){
  274. layer.msg('申请已发送,请等待管理员确认', {
  275. icon: 1
  276. ,shade: 0.5
  277. }, function(){
  278. layer.close(index);
  279. });
  280. //通知对方
  281. /*
  282. $.post('/im-applyGroup/', {
  283. uid: info.uid
  284. ,from_group: group
  285. ,remark: remark
  286. }, function(res){
  287. });
  288. */
  289. }
  290. });
  291. }
  292. ,addFriend: function(){
  293. var user = {
  294. type: 'friend'
  295. ,id: 1234560
  296. ,username: '李彦宏' //好友昵称,若申请加群,参数为:groupname
  297. ,avatar: '//tva4.sinaimg.cn/crop.0.0.996.996.180/8b2b4e23jw8f14vkwwrmjj20ro0rpjsq.jpg' //头像
  298. ,sign: '全球最大的中文搜索引擎'
  299. }
  300. layim.setFriendGroup({
  301. type: user.type
  302. ,username: user.username
  303. ,avatar: user.avatar
  304. ,group: layim.cache().friend //获取好友列表数据
  305. ,submit: function(group, index){
  306. //一般在此执行Ajax和WS,以通知对方已经同意申请
  307. //……
  308. //同意后,将好友追加到主面板
  309. layim.addList({
  310. type: user.type
  311. ,username: user.username
  312. ,avatar: user.avatar
  313. ,groupid: group //所在的分组id
  314. ,id: user.id //好友ID
  315. ,sign: user.sign //好友签名
  316. });
  317. layer.close(index);
  318. }
  319. });
  320. }
  321. ,addGroup: function(){
  322. layer.msg('已成功把[Angular开发]添加到群组里', {
  323. icon: 1
  324. });
  325. //增加一个群组
  326. layim.addList({
  327. type: 'group'
  328. ,avatar: "//tva3.sinaimg.cn/crop.64.106.361.361.50/7181dbb3jw8evfbtem8edj20ci0dpq3a.jpg"
  329. ,groupname: 'Angular开发'
  330. ,id: "12333333"
  331. ,members: 0
  332. });
  333. }
  334. ,removeFriend: function(){
  335. layer.msg('已成功删除[凤姐]', {
  336. icon: 1
  337. });
  338. //删除一个好友
  339. layim.removeList({
  340. id: 121286
  341. ,type: 'friend'
  342. });
  343. }
  344. ,removeGroup: function(){
  345. layer.msg('已成功删除[前端群]', {
  346. icon: 1
  347. });
  348. //删除一个群组
  349. layim.removeList({
  350. id: 101
  351. ,type: 'group'
  352. });
  353. }
  354. //置灰离线好友
  355. ,setGray: function(){
  356. layim.setFriendStatus(168168, 'offline');
  357. layer.msg('已成功将好友[马小云]置灰', {
  358. icon: 1
  359. });
  360. }
  361. //取消好友置灰
  362. ,unGray: function(){
  363. layim.setFriendStatus(168168, 'online');
  364. layer.msg('成功取消好友[马小云]置灰状态', {
  365. icon: 1
  366. });
  367. }
  368. ,kefu1: function(){
  369. layim.chat({
  370. name: '在线客服一' //名称
  371. ,type: 'kefu' //聊天类型
  372. ,avatar: '//tp1.sinaimg.cn/5619439268/180/40030060651/1' //头像
  373. ,id: 1111111 //定义唯一的id方便你处理信息
  374. })
  375. }
  376. ,kefu2: function(){
  377. layim.chat({
  378. name: '在线客服二' //名称
  379. ,type: 'kefu' //聊天类型
  380. ,avatar: '//tp1.sinaimg.cn/5619439268/180/40030060651/1' //头像
  381. ,id: 2222222 //定义唯一的id方便你处理信息
  382. });
  383. }
  384. //移动端版本
  385. ,mobile: function(){
  386. var device = layui.device();
  387. var mobileHome = '/layim/demo/mobile.html';
  388. if(device.android || device.ios){
  389. return location.href = mobileHome;
  390. }
  391. var index = layer.open({
  392. type: 2
  393. ,title: '移动版演示 (或手机扫右侧二维码预览)'
  394. ,content: mobileHome
  395. ,area: ['375px', '667px']
  396. ,shadeClose: true
  397. ,shade: 0.8
  398. ,end: function(){
  399. layer.close(index + 2);
  400. }
  401. });
  402. layer.photos({
  403. photos: {
  404. "data": [{
  405. "src": "http://cdn.layui.com/upload/2016_12/168_1481056358469_50288.png",
  406. }]
  407. }
  408. ,anim: 0
  409. ,shade: false
  410. ,success: function(layero){
  411. layero.css('margin-left', '350px');
  412. }
  413. });
  414. }
  415. };
  416. $('.LAY-senior-im-chat-demo .layui-btn').on('click', function(){
  417. var type = $(this).data('type');
  418. active[type] ? active[type].call(this) : '';
  419. });
  420. });
  421. </script>