im.js 12 KB

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