support.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  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. if(v_ids.indexOf(b.data.from_id)==-1){
  274. isequal=true;
  275. if(isequal){
  276. welive.vistorLog_id=b.data.log_id;
  277. welive.vistorName=b.data.visitor.name;
  278. welive.vistorFram_id=b.data.from_id;
  279. welive.vistorAddress==b.data.visitor.address;
  280. welive.vistorSource==b.data.visitor.source;
  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. welive.vistorLog_id=b.data.log_id;
  295. welive.vistorName=b.data.visitor.name;
  296. welive.vistorFram_id=b.data.from_id;
  297. welive.vistorAddress==b.data.visitor.address;
  298. welive.vistorSource==b.data.visitor.source;
  299. guest.push({
  300. log_id:b.data.log_id,
  301. name:b.data.visitor.name,
  302. form_id:b.data.from_id,
  303. source:b.data.visitor.source,
  304. address:b.data.visitor.address
  305. })
  306. v_ids.push(b.data.from_id)
  307. guest_create(welive.vistorImg);
  308. isequal=false;
  309. }else{
  310. if(guest.length>1&&isequal){
  311. for (var j=0;j<guest.length;j++) {
  312. if(guest[j].form_id!==b.data.from_id){
  313. isequal=true;
  314. if(isequal){
  315. guest.push({
  316. log_id:b.data.log_id,
  317. name:b.data.visitor.name,
  318. form_id:b.data.from_id,
  319. source:b.data.visitor.source,
  320. address:b.data.visitor.address
  321. })
  322. v_ids.push(b.data.from_id)
  323. guest_create(welive.vistorImg);
  324. isequal=false;
  325. }
  326. }
  327. }
  328. }
  329. }
  330. break;
  331. case "logout":
  332. /* 监测用户登出 */
  333. changeStatusOff(b.data.from_id)
  334. break;
  335. }
  336. }
  337. /* 创建聊天 */
  338. function guest_create(avatar) {
  339. console.log(welive)
  340. 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+'">'+
  341. '<img class="guest-headerImg" src="'+avatar+'" width="50px">'+
  342. '<span class="guest_name">'+welive.vistorName+'</span>'+
  343. '<button class="delBtn" title="删除" onclick="delGuest(\''+welive.vistorFram_id+'\')">✕</button>'+
  344. '<div id="messCount" class="messCount">0</div>'+
  345. '</div>');
  346. }
  347. /* 消息展示 */
  348. function guest_output(d, a, b) {
  349. if (!d || !a || !b) return;
  350. var o = $(".history");
  351. switch (b) {
  352. case 1:
  353. 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>';
  354. break;
  355. case 2:
  356. 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>';
  357. break;
  358. case 3:
  359. d = '<div class="msg s"><div class="b"><div class="ico"></div><div class="i">' + d + '</div></div></div>';
  360. break;
  361. case 4:
  362. d = '<div class="msg e"><div class="b"><div class="ico"></div><div class="i">' + d + '</div></div></div>';
  363. break;
  364. case 5:
  365. 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>';
  366. break;
  367. }
  368. o.find(".overview").append(d);
  369. window.setTimeout(function(){
  370. $('.history').welivebar_update('bottom');
  371. },200)
  372. }
  373. function welive_output(d, a, b) {
  374. if (a) {
  375. guest_output(d, a, b)
  376. } else {
  377. if (d === false) return;
  378. s_history.append(d);
  379. s_history.welivebar_update('bottom')
  380. }
  381. }
  382. function welive_verify() {
  383. welive.status = 1;
  384. welive_send(JSON.stringify({"method":"login","data":{"site_code":site.code,"type":"service"}}))
  385. $(".set_serving").hide();
  386. $(".set_busy").show()
  387. }
  388. function welive_close() {
  389. if (welive.status) {
  390. s_online.html("");
  391. s_admins.html(0)
  392. }
  393. welive.status = 0;
  394. $("#websocket1212").remove();
  395. welive_output('<div class="i"><b></b>连接失败, 3秒后自动重试 ...</div>');
  396. welive.ttt = setTimeout(welive_link, 3000)
  397. }
  398. /* 发送消息 */
  399. function welive_send(d) {
  400. s_send.addClass('loading2');
  401. if (welive.status) {
  402. welive.ws.send(d);
  403. s_msg.val('')
  404. } else {
  405. welive_output('<div class=i><b></b>服务器连接中, 请等待 ...</div>')
  406. }
  407. s_msg.focus();
  408. s_send.removeClass('loading2');
  409. localStorage.clear();
  410. }
  411. /* 发送消息 */
  412. function guest_send(e) {
  413. var o = $('#sendTxt')||$('.content');
  414. var ltly = o.val()||$('.publish').serializeArray()[0].value;//获取聊天数据
  415. console.log();
  416. if(ltly.length>0){
  417. if($('#guest_id').html()==''){
  418. /* 还未选择任何访客 提示 */
  419. layer.msg('还未选择访客!')
  420. }else{
  421. $.ajax({
  422. type: "POST",
  423. url : "/home/service/chat",
  424. data: {to_id:$('#guest_id').html(),type:1,content:ltly},
  425. success: function(msg){
  426. if(msg.status==200){
  427. guest_output(ltly,5,2);
  428. getFace();
  429. $('#sendTxt').val('');
  430. $('.content').val('');
  431. }else{
  432. layer.msg("发送失败!");
  433. }
  434. }
  435. });
  436. }
  437. }
  438. o.focus();
  439. return false;
  440. }
  441. function guest_kickout() {
  442. if ($.inArray(CurrentId, offline) < 0) welive.ws.send('x=6&a=6&g=' + CurrentId);
  443. guest_delete(CurrentId);
  444. CurrentId = 0;
  445. $("#tiptip_holder").hide();
  446. showNext()
  447. }
  448. /* 删除访客 */
  449. function delGuest(index){
  450. var guests=$('.visit_content');
  451. var delStatus=false;
  452. var curTab;
  453. for(var j=0;j<v_ids.length;j++){
  454. if(v_ids[j]==index){
  455. v_ids.splice(j, 1);
  456. }
  457. }
  458. guest=guests;
  459. for(var i=0;i<guests.length;i++){
  460. if($(guests[i]).attr('id').split("g")[1]==index){
  461. /* 删除guest列表中的元素 */
  462. curTab=$(guests[i]);
  463. $.ajax({
  464. type: "POST",
  465. url : "/home/service/delChatList",
  466. data: {to_id:index},
  467. success: function(msg){
  468. var myData=msg;
  469. if(myData.status==200){
  470. delStatus=true;
  471. if(delStatus){
  472. curTab.remove();
  473. guest.splice(i,1);
  474. delStatus=false;
  475. layer.msg('删除成功!');
  476. /* 并清空聊天记录 */
  477. $('.history .overview').css('visibility','hidden');
  478. }
  479. }else{
  480. layer.msg('删除失败,稍后重新操作!');
  481. }
  482. }
  483. });
  484. }
  485. }
  486. }
  487. function welive_runtime(a, b) {
  488. if (!a || !b) return;
  489. b = format_output(b) + ' <img src="' + SYSDIR + 'public/img/writting.gif">';
  490. var o = $(".history");
  491. var c = o.find(".overview>div.updating");
  492. if (c.length) {
  493. c.find(".i").html(b)
  494. } else {
  495. b = '<div class="msg updating"><b></b><div class="b"><div class="i">' + b + '</div></div></div>';
  496. o.find(".overview").append(b)
  497. }
  498. o.welivebar_update('bottom')
  499. }
  500. /* 聊天时初始化 */
  501. function welive_init() {
  502. guest = new Array();
  503. offline = new Array();
  504. g_online = $("#showusername"); /* 客户列表*/
  505. s_chat = $("#s_chat");
  506. s_msg = s_chat.find(".s_msg");
  507. s_send = s_chat.find(".s_send");
  508. s_admins = s_chat.find(".s_admins");
  509. s_title = s_chat.find(".s_title").children(".l");
  510. s_hwrap = $(".history");
  511. s_owrap = s_chat.find("#s_owrap");
  512. s_history = s_hwrap.find(".overview");
  513. s_online = s_owrap.find(".overview");
  514. sounder = $("#wl_sounder");
  515. welive_link();
  516. myWin88;
  517. var b = s_hwrap.find(".viewport"),
  518. s_onlineViewport = s_owrap.find(".viewport"),
  519. xHeight = $(window).height() - 88;
  520. g_online.height(xHeight);
  521. s_hwrap.welivebar();
  522. s_owrap.welivebar();
  523. /* $(window).resize(function() {
  524. s_hwrap.welivebar_update('bottom');
  525. }); */
  526. s_msg.keyup(function(e) {
  527. /* if (e.keyCode == 13) s_send.trigger("click"); */
  528. }).focus(function() {
  529. welive.where = 0;
  530. });
  531. /* 发送消息 */
  532. s_send.click(function(e) {
  533. var a = $.trim(s_msg.val());
  534. if (a) {
  535. welive_output('<div class=i><b></b>抱歉, WeLive4免费版功能受限!</div>')
  536. } else {
  537. s_msg.focus()
  538. }
  539. e.preventDefault()
  540. });
  541. /* tab切换 */
  542. $('.right-menunav').click(function(e){
  543. var dataFor=e.target.getAttribute('for');
  544. $('.right-menunav li').removeClass('active');
  545. $(e.target).addClass('active');
  546. $('.tab-content div').removeClass('show');
  547. $('.tab-content').find('div[id^="'+dataFor+'"]').addClass("show");
  548. })
  549. /* 快捷回复 */
  550. $('#reply-List').click(function(e){
  551. var obj=$(e.target).html();
  552. $.ajax({
  553. type: "POST",
  554. url : "/home/service/chat",
  555. data: {to_id:$('#guest_id').html(),type:1,content:obj},
  556. success: function(msg){
  557. if(msg.status==200){
  558. guest_output(obj,5,2);
  559. getFace();
  560. $('#sendTxt').val('');
  561. $('.content').val('');
  562. }else{
  563. layer.msg("发送失败!");
  564. }
  565. }
  566. });
  567. })
  568. $("#wl_ring").click(function(e) {
  569. if (welive.sound) {
  570. welive.sound = 0;
  571. $(this).addClass("s_ringoff").removeClass("s_ring")
  572. } else {
  573. welive.sound = 1;
  574. $(this).addClass("s_ring").removeClass("s_ringoff")
  575. }
  576. s_msg.focus();
  577. e.preventDefault()
  578. });
  579. pagetitle = window.parent.document.title;
  580. $(document,window.parent.document).mousedown(function(){
  581. stopFlashTitle();
  582. })
  583. $(document,window.parent.document).keydown(function() {
  584. stopFlashTitle();
  585. });
  586. /* welive.sound1 = '<audio src="'+path+'/admin/sound1.swf" autoplay>该浏览器不支持audio属性</audio>';
  587. welive.sound2 = '<audio src="../sound2.swf" autoplay>该浏览器不支持audio属性</audio>'; */
  588. window.onbeforeunload = function(a) {
  589. welive.status = 0;
  590. clearTimeout(welive.ttt);
  591. return " "
  592. };
  593. $(window).unload(function() {
  594. welive.status = 0;
  595. clearTimeout(welive.ttt)
  596. })
  597. }
  598. var tttt = 0,
  599. pagetitle, flashtitle_step = 0,
  600. sounder, towhere = 0;
  601. var guest, offline, g_online, s_chat, s_msg, s_history, s_online, s_send, s_hwrap, s_owrap, s_admins, s_title;
  602. var welive = {
  603. ws: {},
  604. index: '',
  605. status: 0,
  606. ttt: 0,
  607. flashTitle: 0,
  608. sound: 1,
  609. sound1: '',
  610. sound2: '',
  611. where: 0,
  612. client_id:'',
  613. loginTime:'',
  614. vistorFram_id:'',
  615. vistorLog_id:'',
  616. vistorName:'',
  617. vistorImg:'',
  618. vistorAddress:'',
  619. vistorSource:'',
  620. avatar:default_v_avatar, /* 客服头像*/
  621. messageCount:0,
  622. messContent:'',
  623. recordPermission:false,
  624. product:{}
  625. };
  626. var admin ={
  627. id: '',
  628. type: '',
  629. sid: '',
  630. fullname: '',
  631. post: '',
  632. agent: ''};
  633. var myWin88, CurrentId = 0,//<a class="t_picture" type="file" title="发送图片">
  634. zIndex = 2000;
  635. $(function() {
  636. var isFile=false;
  637. var File=null;
  638. var recorder=null;
  639. var audio_context='';
  640. welive_init();
  641. $(".logout").click(function(e) {
  642. showDialog('确定退出 WeLive 在线客服系统吗?', '', function() {
  643. document.location = 'index.php?a=logout'
  644. });
  645. e.preventDefault()
  646. })
  647. /* 初始化商品 */
  648. /* 加载录音模块 */
  649. try {
  650. // webkit shim
  651. window.AudioContext = window.AudioContext || window.webkitAudioContext;
  652. navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
  653. window.URL = window.URL || window.webkitURL;
  654. welive.audio_context = new AudioContext;
  655. console.log('Audio context set up.');
  656. console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
  657. } catch (e) {
  658. console.log('No web audio support in this browser!');
  659. }
  660. navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
  661. console.log('No live audio input: ' + e);
  662. });
  663. });
  664. $(function(){
  665. $("input").bind("input propertychange", function() {
  666. var ltly = $("#ltly").val();
  667. });
  668. });
  669. /* 发送图片 */
  670. $('#chooseImg').click(function(){
  671. $('#inp-file').click();
  672. })
  673. $('#inp-file').change(function(){
  674. imgUp();
  675. });
  676. /* 发送视频 */
  677. $('#chooseVideo').click(function(){
  678. $('#inp-video').click();
  679. })
  680. $('#inp-video').change(function(){
  681. utils.fileChange(this,"video","视频",/video\/\w+/);
  682. })
  683. /* 选择录音文件 */
  684. $('#inp-record').change(function(){
  685. utils.fileChange(this,"audio","音频",/audio\/\w+/);
  686. })
  687. /* 发送其他文件 */
  688. $('#chooseFile').click(function(){
  689. $('#inp-ohter').click();
  690. })
  691. $('#inp-ohter').change(function(){
  692. utils.fileChange(this,"file","","");
  693. })
  694. function imgUp(){
  695. var formData = new FormData();
  696. formData.append('image', $('#inp-file')[0].files[0]); //添加图片信息的参数
  697. formData.append('type','image'); //添加其他参数
  698. //获取文件
  699. var file = $('#inp-file')[0].files[0];
  700. var imageType = /^image\//;
  701. var imgHTML="";
  702. //是否是图片
  703. if(!imageType.test(file.type)) {
  704. alert("请选择图片!");
  705. return;
  706. }else{
  707. $.ajax({
  708. url: '/home/upload/upFile',
  709. type: 'POST',
  710. cache: false, //上传文件不需要缓存
  711. data: formData,
  712. processData: false, // 告诉jQuery不要去处理发送的数据
  713. contentType: false, // 告诉jQuery不要去设置Content-Type请求头
  714. success: function (data) {
  715. if(data.status==200){
  716. imgHTML+="<img src='"+data.data.path+"' class='trans_img' >";
  717. $.ajax({
  718. type: "POST",
  719. url : "/home/service/chat",
  720. data: {to_id:$('#guest_id').html(),type:1,content:imgHTML},
  721. success: function(msg){
  722. if(msg.status==200){
  723. welive_output(imgHTML,5,2);
  724. getFace();
  725. $('#sendTxt').val('');
  726. $('.content').val('');
  727. }else{
  728. layer.msg('上传失败');
  729. }
  730. }
  731. });
  732. }
  733. },
  734. error: function (data) {
  735. alert("上传失败");
  736. }
  737. })
  738. }
  739. }
  740. /* 创建文件,并且写入数据内容 */
  741. function CreateFile(fileObj)
  742. {
  743. var fso, tf;
  744. fso = new ActiveXObject("Scripting.FileSystemObject");
  745. tf = fso.CreateTextFile("../imgFile/testfile.json", true);
  746. // 写一行,并且带有新行字符。
  747. tf.WriteLine(fileObj) ;
  748. tf.Close();
  749. }
  750. /* 录音模块 */
  751. function startUserMedia(stream) {
  752. if(stream!==undefined){
  753. welive.recordPermission=true;
  754. }
  755. var input = audio_context.createMediaStreamSource(stream);
  756. console.log('Media stream created.');
  757. // Uncomment if you want the audio to feedback directly
  758. //input.connect(audio_context.destination);
  759. //__log('Input connected to audio context destination.');
  760. recorder = new Recorder(input);
  761. console.log('Recorder initialised.');
  762. }
  763. function startRecording(button) {
  764. if(welive.recordPermission==false){
  765. /* 自动触发选择录音文件 */
  766. /* 当录音功能被禁止时 */
  767. layer.alert("网页录音功能已被禁止,请选择音频文件!",function(){
  768. $('#inp-record').click();
  769. });
  770. }
  771. else{
  772. welive.recorder && welive.recorder.record();
  773. layer.open({
  774. type: 1,
  775. content: '正在录音……,点击完成,发送录音,取消取消发送',
  776. btn: ['完成', '取消'],
  777. yes:function(){
  778. stopRecording();
  779. },
  780. btn1:function(){
  781. console.log('取消发送')
  782. }
  783. });
  784. console.log('Recording...');
  785. }
  786. }
  787. function stopRecording(button) {
  788. recorder && recorder.stop();
  789. //button.disabled = true;
  790. //button.previousElementSibling.disabled = false;
  791. console.log('Stopped recording.');
  792. // create WAV download link using audio data blob
  793. createDownloadLink();
  794. recorder.clear();
  795. }
  796. function createDownloadLink() {
  797. recorder && recorder.exportWAV(function(blob) {
  798. var url = URL.createObjectURL(blob);
  799. var li = document.createElement('li');
  800. var au = document.createElement('audio');
  801. var hf = document.createElement('a');
  802. au.controls = true;
  803. au.src = url;
  804. hf.href = url;
  805. hf.download = new Date().toISOString() + '.wav';
  806. hf.innerHTML = hf.download;
  807. li.appendChild(au);
  808. li.appendChild(hf);
  809. document.getElementById('recordingslist').appendChild(li);
  810. });
  811. }