support.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. var userAgent = navigator.userAgent.toLowerCase();
  2. var isIE = window.ActiveXObject && userAgent.indexOf('msie') != -1 && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
  3. function flashTitle() {
  4. clearInterval(tttt);
  5. flashtitle_step = 1;
  6. tttt = setInterval(function() {
  7. if (flashtitle_step == 1) {
  8. window.parent.document.title = '【新消息】' + pagetitle;
  9. flashtitle_step = 2
  10. } else {
  11. window.parent.document.title = '【   】' + pagetitle;
  12. flashtitle_step = 1
  13. }
  14. }, 50);
  15. }
  16. function toLink(url){
  17. window.open(url,'_blank');
  18. }
  19. function stopFlashTitle() {
  20. flashtitle_step = 0;
  21. clearInterval(tttt);
  22. if (flashtitle_step != 0) {
  23. flashtitle_step = 0;
  24. clearInterval(tttt);
  25. window.parent.document.title = pagetitle;
  26. }
  27. window.parent.document.title = pagetitle;
  28. }
  29. function getLocalTime() {
  30. var c = new Date();
  31. function addZeros(a, b) {
  32. var i;
  33. a = "" + a;
  34. if (a.length < b) {
  35. for (i = 0; i < (b - a.length); i++) a = "0" + a
  36. }
  37. return a
  38. }
  39. return addZeros(c.getHours(), 2) + ':' + addZeros(c.getMinutes(), 2) + ':' + addZeros(c.getSeconds(), 2)
  40. }
  41. /* 解析表情表 */
  42. function getFace(){
  43. var emojis=document.getElementsByClassName('i');
  44. for(var i in emojis){
  45. if(emojis[i].innerHTML==undefined) return;
  46. if(emojis[i].innerHTML.indexOf('[]')==-1){
  47. $(emojis[i]).parseEmotion();
  48. }
  49. }
  50. }
  51. /* 获取聊天记录 */
  52. function getchatForm(fromId){
  53. var chatHTML="";
  54. $.ajax({
  55. type: "POST",
  56. url : "/home/service/messageLog",
  57. data: {to_id:fromId,page:1},
  58. success: function(msg){
  59. var myData=msg;
  60. myData.data.map((item,index)=>{
  61. if(item.who_is!==1){
  62. chatHTML+= '<div class="msg l"><div class="sper-img"><img src="'+welive.vistorImg+'"></div><b></b><div class="b"><div class="i">' + item.content + '</div></div><i>' + item.create_time + '</i></div>';
  63. }else{
  64. chatHTML+= '<div class="msg r"><div class="mper-img"><img src="'+default_s_avatar+'"></div><b></b><div class="b"><div class="i">' + item.content + '</div></div><i>' +item.create_time + '</i></div>';
  65. }
  66. })
  67. $('.history').find(".overview").html(chatHTML);
  68. if(parseInt($('.history').find(".viewport").css('height'))<parseInt($('.history').find(".overview").css('height'))){
  69. $('.scb_scrollbar').css('visibility','visible');
  70. window.setTimeout(function(){
  71. $('.history').welivebar_update('bottom');
  72. },200)
  73. }else{
  74. $('.history').find(".overview").css('top','0px');
  75. $('.scb_scrollbar').css('visibility','hidden');
  76. }
  77. getFace();
  78. }
  79. });
  80. }
  81. function openWin(fromId) {
  82. /* 展示新的聊天窗口 */
  83. /* 获取当前的访客 from_id */
  84. /* 查询当前用户的聊天记录 */
  85. $('.history').find('.overview').css('visibility','visible');
  86. /* 消息条数清零并隐藏 */
  87. var guestList=$('.visit_content');
  88. $('.visit_content').removeClass('active');
  89. guestList.map((item,index)=>{
  90. if($(index).attr('id').split("g")[1]==fromId){
  91. /* 删除消息条数dom元素 */
  92. $($(index).find('.messCount')).css('display','none');
  93. $($(index).find('.messCount')).html(0);
  94. welive.vistorImg=$(index).find('img').attr('src');
  95. console.log($(index).attr('data-isonline'))
  96. if($(index).attr('data-isonline')!==undefined){
  97. $(index).attr('data-isonline')=='0'?$('#guest_status').html('离线'):$('#guest_status').html('在线');
  98. }
  99. $(index).attr('data-isonline')=='0'?$('#guest_status').html('离线'):$('#guest_status').html('在线');
  100. $(index).addClass('active');
  101. $('#from').html($(index).attr('data-address'));
  102. $('#guest_source').html($(index).attr('data-source'));
  103. }
  104. })
  105. getchatForm(fromId);
  106. /* 同时更新访客资料 */
  107. $('#guest_id').html(fromId);
  108. }
  109. function getURL(a, b) {
  110. if (a.substr(0, 5).toLowerCase() == 'href=') return a;
  111. if (!b) b = 60;
  112. var c = '<a href="' + (a.substr(0, 4).toLowerCase() == 'www.' ? 'http://' + a : a) + '" target="_blank" title="' + a + '">';
  113. if (a.length > b) {
  114. a = a.substr(0, 30) + ' ... ' + a.substr(a.length - 18)
  115. }
  116. c += a + '</a>';
  117. return c
  118. }
  119. function welive_link() {
  120. welive.ws = new WebSocket(node.address);
  121. welive.ws.onopen = function() {
  122. setTimeout(welive_verify, 100)
  123. };
  124. welive.ws.onclose = function() {
  125. welive_close()
  126. };
  127. /* websocket服务端接收到消息后做处理 */
  128. welive.ws.onmessage = function(a) {
  129. welive_parseOut(a.data)
  130. }
  131. }
  132. function TitleSound() {
  133. sounder.html(welive.sound1);
  134. /* if (welive.flashTitle && x) {
  135. flashTitle();
  136. if (welive.sound) sounder.html(welive.sound1);
  137. welive.flashTitle = 0
  138. } else if (welive.flashTitle) {
  139. if (welive.sound) sounder.html(welive.sound2);
  140. welive.flashTitle = 0
  141. } */
  142. }
  143. function welive_clear() {
  144. var a = s_history.children("div");
  145. var b = a.length;
  146. if (b >= 100) {
  147. a.slice(0, b - 50).remove();
  148. s_hwrap.welivebar_update('bottom')
  149. }
  150. }
  151. function admins_update(n) {
  152. var x = parseInt(s_admins.html());
  153. x = x + n;
  154. if (x < 0) x = 0;
  155. s_admins.html(x)
  156. }
  157. /* 绑定客服 */
  158. function bindAvtar(){
  159. $.ajax({
  160. type: "POST",
  161. url : "/home/service/bind",
  162. data: {client_id:welive.client_id},
  163. success: function(msg){
  164. /* 订阅成功 */
  165. }
  166. });
  167. }
  168. function replyForSs(){
  169. welive.ws.send(JSON.stringify({"method":"pong", "data": "{}"}))
  170. }
  171. /* 获取用户的订阅信息 */
  172. /* 改变访客状态-在线 */
  173. function changeStatusOn(index){
  174. var guestList=$('.visit_content');
  175. /* 获取当前聊天的访客id */
  176. for(var i=0;i<guestList.length;i++){
  177. if($(guestList[i]).attr('id').split("g")[1]===index){
  178. /* 跳转到该用户的聊天窗口 */
  179. /* 并改变访客的状态 */
  180. $(guestList[i]).attr('data-isonline','1');
  181. $(guestList[i]).removeClass('offline');
  182. }
  183. }
  184. }
  185. /* 显示消息条数 */
  186. function showMessCount(index){
  187. var guestList=$('.visit_content');
  188. var count;
  189. /* 获取当前聊天的访客id */
  190. for(var i=0;i<guestList.length;i++){
  191. if($(guestList[i]).attr('id').split("g")[1]===index){
  192. /* 跳转到该用户的聊天窗口 */
  193. $($(guestList[i]).find('.messCount')).css('display','block');
  194. count=$($(guestList[i]).find('.messCount')).html();
  195. count++
  196. $($(guestList[i]).find('.messCount')).html(count);
  197. }
  198. }
  199. }
  200. /* 改变访客状态-离线 */
  201. function changeStatusOff(index){
  202. var guestList=$('.visit_content');
  203. /* 获取当前聊天的访客id */
  204. for(var i=0;i<guestList.length;i++){
  205. if($(guestList[i]).attr('id').split("g")[1]===index){
  206. /* 跳转到该用户的聊天窗口 */
  207. /* 并改变访客的状态 */
  208. $(guestList[i]).attr('data-isonline','0');
  209. $(guestList[i]).addClass('offline');
  210. }
  211. }
  212. }
  213. /* 获取商品信息 */
  214. /* 接收消息 */
  215. function welive_parseOut(b) {
  216. var isOther=false;
  217. var isEqual=false;
  218. var c = 0,
  219. b = JSON.parse(b);
  220. welive.client_id=b.data.client_id;
  221. welive.loginTime=b.data.time;
  222. if(b.method=='bind'){
  223. bindAvtar();
  224. }
  225. switch(b.method){
  226. case "chat":
  227. /* 接收到来自客户端的消息后,展示消息 */
  228. $('.history').find('.overview').css('visibility',' hidden');
  229. changeStatusOn(b.data.from_id);
  230. TitleSound();
  231. flashTitle();
  232. if($('#showusername .active').length!==0){
  233. welive.index=$('#showusername .active').attr('id').split('g')[1];
  234. var guestList=$('.visit_content');
  235. /* 获取当前聊天的访客id */
  236. for(var i=0;i<guestList.length;i++){
  237. if($(guestList[i]).attr('id').split("g")[1]===welive.index){
  238. if(b.data.from_id==welive.index){
  239. isEqual=true;
  240. }
  241. /* 并改变访客的状态 */
  242. $(guestList[i]).addClass('active');
  243. $('.history').find('.overview').css('visibility',' visible');
  244. }
  245. else{
  246. /* 不是被选中的人发消息时 */
  247. isOther=true;
  248. }
  249. }
  250. }else{
  251. /*没有被选中时 显示消息条数 */
  252. showMessCount(b.data.from_id);
  253. }
  254. if(isOther){
  255. showMessCount(b.data.from_id);
  256. isOther=false;
  257. }
  258. if(isEqual){
  259. guest_output(b.data.content,5,1);
  260. getFace();
  261. isEqual=false;
  262. }
  263. break;
  264. case "ping":
  265. replyForSs();
  266. break;
  267. case "subscribe":
  268. /* 使该用户上线 */
  269. changeStatusOn(b.data.from_id);
  270. b.data.visitor.avatar!==""?welive.vistorImg=b.data.visitor.avatar:welive.vistorImg=default_v_avatar;
  271. var isequal=false;
  272. guest=$('.visit_content');
  273. welive.vistorLog_id=b.data.log_id;
  274. welive.vistorName=b.data.visitor.name;
  275. welive.vistorFram_id=b.data.from_id;
  276. welive.vistorAddress=b.data.visitor.address;
  277. welive.vistorSource=b.data.visitor.source;
  278. if(v_ids.indexOf(b.data.from_id)==-1){
  279. isequal=true;
  280. if(isequal){
  281. guest.push({
  282. log_id:b.data.log_id,
  283. name:b.data.visitor.name,
  284. form_id:b.data.from_id,
  285. source:b.data.visitor.source,
  286. address:b.data.visitor.address
  287. })
  288. v_ids.push(b.data.from_id)
  289. guest_create(welive.vistorImg);
  290. isequal=false;
  291. }
  292. }
  293. if(v_ids.length==0||guest.length==0){
  294. guest.push({
  295. log_id:b.data.log_id,
  296. name:b.data.visitor.name,
  297. form_id:b.data.from_id,
  298. source:b.data.visitor.source,
  299. address:b.data.visitor.address
  300. })
  301. v_ids.push(b.data.from_id)
  302. guest_create(welive.vistorImg);
  303. isequal=false;
  304. }else{
  305. if(guest.length>1&&isequal){
  306. for (var j=0;j<guest.length;j++) {
  307. if(guest[j].form_id!==b.data.from_id){
  308. isequal=true;
  309. if(isequal){
  310. guest.push({
  311. log_id:b.data.log_id,
  312. name:b.data.visitor.name,
  313. form_id:b.data.from_id,
  314. source:b.data.visitor.source,
  315. address:b.data.visitor.address
  316. })
  317. v_ids.push(b.data.from_id)
  318. guest_create(welive.vistorImg);
  319. isequal=false;
  320. }
  321. }
  322. }
  323. }
  324. }
  325. break;
  326. case "logout":
  327. /* 监测用户登出 */
  328. changeStatusOff(b.data.from_id)
  329. break;
  330. }
  331. }
  332. /* 创建聊天 */
  333. function guest_create(avatar) {
  334. g_online.prepend('<div class="visit_content" id="g'+welive.vistorFram_id+'" onclick="openWin(\''+welive.vistorFram_id+'\')" class="g" title="' + welive.vistorName + '" data-source="'+welive.vistorSource+'" data-address="'+welive.vistorAddress+'">'+
  335. '<img class="guest-headerImg" src="'+avatar+'" width="50px">'+
  336. '<span class="guest_name">'+welive.vistorName+'</span>'+
  337. '<button class="delBtn" title="删除" onclick="delGuest(\''+welive.vistorFram_id+'\')">✕</button>'+
  338. '<div id="messCount" class="messCount">0</div>'+
  339. '</div>');
  340. }
  341. /* 消息展示 */
  342. function guest_output(d, a, b) {
  343. if (!d || !a || !b) return;
  344. var o = $(".history");
  345. switch (b) {
  346. case 1:
  347. d = '<div class="msg l"><div class="sper-img"><img src="'+welive.vistorImg+'"></div><b></b><div class="b"><div class="i">' + d + '</div></div><i>' + getLocalTime() + '</i></div>';
  348. break;
  349. case 2:
  350. d = '<div class="msg r"><div class="mper-img"><img src="'+default_s_avatar+'"></div><b></b><div class="b"><div class="i" id="result">' + d + '</div></div><i>' + getLocalTime() + '</i></div>';
  351. break;
  352. case 3:
  353. d = '<div class="msg s"><div class="b"><div class="ico"></div><div class="i">' + d + '</div></div></div>';
  354. break;
  355. case 4:
  356. d = '<div class="msg e"><div class="b"><div class="ico"></div><div class="i">' + d + '</div></div></div>';
  357. break;
  358. case 5:
  359. d='<div class="product-form"><div class="proImg"><img src="../public/img/pro1.jpg"></div><div class="proDesc"><p class="proTit">终身学习:哈佛毕业的第六堂课<p><p class="proPri">35.00<p></div></div>';
  360. break;
  361. }
  362. o.find(".overview").append(d);
  363. window.setTimeout(function(){
  364. $('.history').welivebar_update('bottom');
  365. },200)
  366. }
  367. function welive_output(d, a, b) {
  368. if (a) {
  369. guest_output(d, a, b)
  370. } else {
  371. if (d === false) return;
  372. s_history.append(d);
  373. s_history.welivebar_update('bottom')
  374. }
  375. }
  376. function welive_verify() {
  377. welive.status = 1;
  378. welive_send(JSON.stringify({"method":"login","data":{"site_code":site.code,"type":"service"}}))
  379. $(".set_serving").hide();
  380. $(".set_busy").show()
  381. }
  382. function welive_close() {
  383. if (welive.status) {
  384. s_online.html("");
  385. s_admins.html(0)
  386. }
  387. welive.status = 0;
  388. $("#websocket1212").remove();
  389. welive_output('<div class="i"><b></b>连接失败, 3秒后自动重试 ...</div>');
  390. welive.ttt = setTimeout(welive_link, 3000)
  391. }
  392. /* 发送消息 */
  393. function welive_send(d) {
  394. s_send.addClass('loading2');
  395. if (welive.status) {
  396. welive.ws.send(d);
  397. s_msg.val('')
  398. } else {
  399. welive_output('<div class=i><b></b>服务器连接中, 请等待 ...</div>')
  400. }
  401. s_msg.focus();
  402. s_send.removeClass('loading2');
  403. localStorage.clear();
  404. }
  405. /* 发送消息 */
  406. function guest_send(e) {
  407. var o = $('#sendTxt')||$('.content');
  408. var ltly = o.val()||$('.publish').serializeArray()[0].value;//获取聊天数据
  409. console.log();
  410. if(ltly.length>0){
  411. if($('#guest_id').html()==''){
  412. /* 还未选择任何访客 提示 */
  413. layer.msg('还未选择访客!')
  414. }else{
  415. $.ajax({
  416. type: "POST",
  417. url : "/home/service/chat",
  418. data: {to_id:$('#guest_id').html(),type:1,content:ltly},
  419. success: function(msg){
  420. if(msg.status==200){
  421. guest_output(ltly,5,2);
  422. getFace();
  423. $('#sendTxt').val('');
  424. $('.content').val('');
  425. }else{
  426. layer.msg("发送失败!");
  427. }
  428. }
  429. });
  430. }
  431. }
  432. o.focus();
  433. return false;
  434. }
  435. function guest_kickout() {
  436. if ($.inArray(CurrentId, offline) < 0) welive.ws.send('x=6&a=6&g=' + CurrentId);
  437. guest_delete(CurrentId);
  438. CurrentId = 0;
  439. $("#tiptip_holder").hide();
  440. showNext()
  441. }
  442. /* 删除访客 */
  443. function delGuest(index){
  444. var guests=$('.visit_content');
  445. var delStatus=false;
  446. var curTab;
  447. for(var j=0;j<v_ids.length;j++){
  448. if(v_ids[j]==index){
  449. v_ids.splice(j, 1);
  450. }
  451. }
  452. guest=guests;
  453. for(var i=0;i<guests.length;i++){
  454. if($(guests[i]).attr('id').split("g")[1]==index){
  455. /* 删除guest列表中的元素 */
  456. curTab=$(guests[i]);
  457. $.ajax({
  458. type: "POST",
  459. url : "/home/service/delChatList",
  460. data: {to_id:index},
  461. success: function(msg){
  462. var myData=msg;
  463. if(myData.status==200){
  464. delStatus=true;
  465. if(delStatus){
  466. curTab.remove();
  467. guest.splice(i,1);
  468. delStatus=false;
  469. layer.msg('删除成功!');
  470. /* 并清空聊天记录 */
  471. $('.history .overview').css('visibility','hidden');
  472. }
  473. }else{
  474. layer.msg('删除失败,稍后重新操作!');
  475. }
  476. }
  477. });
  478. }
  479. }
  480. }
  481. function welive_runtime(a, b) {
  482. if (!a || !b) return;
  483. b = format_output(b) + ' <img src="' + SYSDIR + 'public/img/writting.gif">';
  484. var o = $(".history");
  485. var c = o.find(".overview>div.updating");
  486. if (c.length) {
  487. c.find(".i").html(b)
  488. } else {
  489. b = '<div class="msg updating"><b></b><div class="b"><div class="i">' + b + '</div></div></div>';
  490. o.find(".overview").append(b)
  491. }
  492. o.welivebar_update('bottom')
  493. }
  494. /* 聊天时初始化 */
  495. function welive_init() {
  496. guest = new Array();
  497. offline = new Array();
  498. g_online = $("#showusername"); /* 客户列表*/
  499. s_chat = $("#s_chat");
  500. s_msg = s_chat.find(".s_msg");
  501. s_send = s_chat.find(".s_send");
  502. s_admins = s_chat.find(".s_admins");
  503. s_title = s_chat.find(".s_title").children(".l");
  504. s_hwrap = $(".history");
  505. s_owrap = s_chat.find("#s_owrap");
  506. s_history = s_hwrap.find(".overview");
  507. s_online = s_owrap.find(".overview");
  508. sounder = $("#wl_sounder");
  509. welive_link();
  510. myWin88;
  511. var b = s_hwrap.find(".viewport"),
  512. s_onlineViewport = s_owrap.find(".viewport"),
  513. xHeight = $(window).height() - 88;
  514. g_online.height(xHeight);
  515. s_hwrap.welivebar();
  516. s_owrap.welivebar();
  517. /* $(window).resize(function() {
  518. s_hwrap.welivebar_update('bottom');
  519. }); */
  520. s_msg.keyup(function(e) {
  521. /* if (e.keyCode == 13) s_send.trigger("click"); */
  522. }).focus(function() {
  523. welive.where = 0;
  524. });
  525. /* 发送消息 */
  526. s_send.click(function(e) {
  527. var a = $.trim(s_msg.val());
  528. if (a) {
  529. welive_output('<div class=i><b></b>抱歉, WeLive4免费版功能受限!</div>')
  530. } else {
  531. s_msg.focus()
  532. }
  533. e.preventDefault()
  534. });
  535. /* tab切换 */
  536. $('.right-menunav').click(function(e){
  537. var dataFor=e.target.getAttribute('for');
  538. $('.right-menunav li').removeClass('active');
  539. $(e.target).addClass('active');
  540. $('.tab-content div').removeClass('show');
  541. $('.tab-content').find('div[id^="'+dataFor+'"]').addClass("show");
  542. })
  543. /* 快捷回复 */
  544. $('#reply-List').click(function(e){
  545. var obj=$(e.target).html();
  546. $.ajax({
  547. type: "POST",
  548. url : "/home/service/chat",
  549. data: {to_id:$('#guest_id').html(),type:1,content:obj},
  550. success: function(msg){
  551. if(msg.status==200){
  552. guest_output(obj,5,2);
  553. getFace();
  554. $('#sendTxt').val('');
  555. $('.content').val('');
  556. }else{
  557. layer.msg("发送失败!");
  558. }
  559. }
  560. });
  561. })
  562. $("#wl_ring").click(function(e) {
  563. if (welive.sound) {
  564. welive.sound = 0;
  565. $(this).addClass("s_ringoff").removeClass("s_ring")
  566. } else {
  567. welive.sound = 1;
  568. $(this).addClass("s_ring").removeClass("s_ringoff")
  569. }
  570. s_msg.focus();
  571. e.preventDefault()
  572. });
  573. pagetitle = window.parent.document.title;
  574. $(document,window.parent.document).mousedown(function(){
  575. stopFlashTitle();
  576. })
  577. $(document,window.parent.document).keydown(function() {
  578. stopFlashTitle();
  579. });
  580. /* welive.sound1 = '<audio src="'+path+'/admin/sound1.swf" autoplay>该浏览器不支持audio属性</audio>';
  581. welive.sound2 = '<audio src="../sound2.swf" autoplay>该浏览器不支持audio属性</audio>'; */
  582. window.onbeforeunload = function(a) {
  583. welive.status = 0;
  584. clearTimeout(welive.ttt);
  585. return " "
  586. };
  587. $(window).unload(function() {
  588. welive.status = 0;
  589. clearTimeout(welive.ttt)
  590. })
  591. }
  592. var tttt = 0,
  593. pagetitle, flashtitle_step = 0,
  594. sounder, towhere = 0;
  595. var guest, offline, g_online, s_chat, s_msg, s_history, s_online, s_send, s_hwrap, s_owrap, s_admins, s_title;
  596. var welive = {
  597. ws: {},
  598. index: '',
  599. status: 0,
  600. ttt: 0,
  601. flashTitle: 0,
  602. sound: 1,
  603. sound1: '',
  604. sound2: '',
  605. where: 0,
  606. client_id:'',
  607. loginTime:'',
  608. vistorFram_id:'',
  609. vistorLog_id:'',
  610. vistorName:'',
  611. vistorImg:'',
  612. vistorAddress:'',
  613. vistorSource:'',
  614. avatar:default_v_avatar, /* 客服头像*/
  615. messageCount:0,
  616. messContent:'',
  617. recordPermission:false,
  618. product:{}
  619. };
  620. var admin ={
  621. id: '',
  622. type: '',
  623. sid: '',
  624. fullname: '',
  625. post: '',
  626. agent: ''};
  627. var myWin88, CurrentId = 0,//<a class="t_picture" type="file" title="发送图片">
  628. zIndex = 2000;
  629. $(function() {
  630. var isFile=false;
  631. var File=null;
  632. var recorder=null;
  633. var audio_context='';
  634. welive_init();
  635. $(".logout").click(function(e) {
  636. showDialog('确定退出 WeLive 在线客服系统吗?', '', function() {
  637. document.location = 'index.php?a=logout'
  638. });
  639. e.preventDefault()
  640. })
  641. /* 初始化商品 */
  642. /* 加载录音模块 */
  643. try {
  644. // webkit shim
  645. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  646. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
  647. window.URL = window.URL || window.webkitURL;
  648. welive.audio_context = new AudioContext;
  649. console.log('Audio context set up.');
  650. console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
  651. } catch (e) {
  652. console.log('No web audio support in this browser!');
  653. }
  654. navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
  655. console.log('No live audio input: ' + e);
  656. });
  657. });
  658. $(function(){
  659. $("input").bind("input propertychange", function() {
  660. var ltly = $("#ltly").val();
  661. });
  662. });
  663. /* 发送图片 */
  664. $('#chooseImg').click(function(){
  665. $('#inp-file').click();
  666. })
  667. $('#inp-file').change(function(){
  668. imgUp();
  669. });
  670. /* 发送视频 */
  671. $('#chooseVideo').click(function(){
  672. $('#inp-video').click();
  673. })
  674. $('#inp-video').change(function(){
  675. utils.fileChange(this,"video","视频",/video\/\w+/);
  676. })
  677. /* 选择录音文件 */
  678. $('#inp-record').change(function(){
  679. utils.fileChange(this,"audio","音频",/audio\/\w+/);
  680. })
  681. /* 发送其他文件 */
  682. $('#chooseFile').click(function(){
  683. $('#inp-ohter').click();
  684. })
  685. $('#inp-ohter').change(function(){
  686. utils.fileChange(this,"file","","");
  687. })
  688. function imgUp(){
  689. var formData = new FormData();
  690. formData.append('image', $('#inp-file')[0].files[0]); //添加图片信息的参数
  691. formData.append('type','image'); //添加其他参数
  692. //获取文件
  693. var file = $('#inp-file')[0].files[0];
  694. var imageType = /^image\//;
  695. var imgHTML="";
  696. //是否是图片
  697. if(!imageType.test(file.type)) {
  698. alert("请选择图片!");
  699. return;
  700. }else{
  701. $.ajax({
  702. url: '/home/upload/upFile',
  703. type: 'POST',
  704. cache: false, //上传文件不需要缓存
  705. data: formData,
  706. processData: false, // 告诉jQuery不要去处理发送的数据
  707. contentType: false, // 告诉jQuery不要去设置Content-Type请求头
  708. success: function (data) {
  709. if(data.status==200){
  710. imgHTML+="<img src='"+data.data.path+"' class='trans_img' >";
  711. $.ajax({
  712. type: "POST",
  713. url : "/home/service/chat",
  714. data: {to_id:$('#guest_id').html(),type:1,content:imgHTML},
  715. success: function(msg){
  716. if(msg.status==200){
  717. welive_output(imgHTML,5,2);
  718. getFace();
  719. $('#sendTxt').val('');
  720. $('.content').val('');
  721. }else{
  722. layer.msg('上传失败');
  723. }
  724. }
  725. });
  726. }
  727. },
  728. error: function (data) {
  729. alert("上传失败");
  730. }
  731. })
  732. }
  733. }
  734. /* 创建文件,并且写入数据内容 */
  735. function CreateFile(fileObj)
  736. {
  737. var fso, tf;
  738. fso = new ActiveXObject("Scripting.FileSystemObject");
  739. tf = fso.CreateTextFile("../imgFile/testfile.json", true);
  740. // 写一行,并且带有新行字符。
  741. tf.WriteLine(fileObj) ;
  742. tf.Close();
  743. }
  744. /* 录音模块 */
  745. function startUserMedia(stream) {
  746. if(stream!==undefined){
  747. welive.recordPermission=true;
  748. }
  749. var input = audio_context.createMediaStreamSource(stream);
  750. console.log('Media stream created.');
  751. // Uncomment if you want the audio to feedback directly
  752. //input.connect(audio_context.destination);
  753. //__log('Input connected to audio context destination.');
  754. recorder = new Recorder(input);
  755. console.log('Recorder initialised.');
  756. }
  757. function startRecording(button) {
  758. if(welive.recordPermission==false){
  759. /* 自动触发选择录音文件 */
  760. /* 当录音功能被禁止时 */
  761. layer.alert("网页录音功能已被禁止,请选择音频文件!",function(){
  762. $('#inp-record').click();
  763. });
  764. }
  765. else{
  766. welive.recorder && welive.recorder.record();
  767. layer.open({
  768. type: 1,
  769. content: '正在录音……,点击完成,发送录音,取消取消发送',
  770. btn: ['完成', '取消'],
  771. yes:function(){
  772. stopRecording();
  773. },
  774. btn1:function(){
  775. console.log('取消发送')
  776. }
  777. });
  778. console.log('Recording...');
  779. }
  780. }
  781. function stopRecording(button) {
  782. recorder && recorder.stop();
  783. //button.disabled = true;
  784. //button.previousElementSibling.disabled = false;
  785. console.log('Stopped recording.');
  786. // create WAV download link using audio data blob
  787. createDownloadLink();
  788. recorder.clear();
  789. }
  790. function createDownloadLink() {
  791. recorder && recorder.exportWAV(function(blob) {
  792. var url = URL.createObjectURL(blob);
  793. var li = document.createElement('li');
  794. var au = document.createElement('audio');
  795. var hf = document.createElement('a');
  796. au.controls = true;
  797. au.src = url;
  798. hf.href = url;
  799. hf.download = new Date().toISOString() + '.wav';
  800. hf.innerHTML = hf.download;
  801. li.appendChild(au);
  802. li.appendChild(hf);
  803. document.getElementById('recordingslist').appendChild(li);
  804. });
  805. }