var userAgent = navigator.userAgent.toLowerCase();
var isIE = window.ActiveXObject && userAgent.indexOf('msie') != -1 && userAgent.substr(userAgent.indexOf('msie') + 5, 3);
function flashTitle() {
clearInterval(tttt);
flashtitle_step = 1;
tttt = setInterval(function() {
if (flashtitle_step == 1) {
window.parent.document.title = '【新消息】' + pagetitle;
flashtitle_step = 2
} else {
window.parent.document.title = '【 】' + pagetitle;
flashtitle_step = 1
}
}, 50);
}
function toLink(url){
window.open(url,'_blank');
}
function stopFlashTitle() {
flashtitle_step = 0;
clearInterval(tttt);
if (flashtitle_step != 0) {
flashtitle_step = 0;
clearInterval(tttt);
window.parent.document.title = pagetitle;
}
window.parent.document.title = pagetitle;
}
function getLocalTime() {
var c = new Date();
function addZeros(a, b) {
var i;
a = "" + a;
if (a.length < b) {
for (i = 0; i < (b - a.length); i++) a = "0" + a
}
return a
}
return addZeros(c.getHours(), 2) + ':' + addZeros(c.getMinutes(), 2) + ':' + addZeros(c.getSeconds(), 2)
}
/* 解析表情表 */
function getFace(){
var emojis=document.getElementsByClassName('i');
for(var i in emojis){
if(emojis[i].innerHTML==undefined) return;
if(emojis[i].innerHTML.indexOf('[]')==-1){
$(emojis[i]).parseEmotion();
}
}
}
/* 获取聊天记录 */
function getchatForm(fromId){
var chatHTML="";
$.ajax({
type: "POST",
url : "/home/service/messageLog",
data: {to_id:fromId,page:1},
success: function(msg){
var myData=msg;
myData.data.map((item,index)=>{
if(item.who_is!==1){
chatHTML+= '
';
}else{
chatHTML+= '';
}
})
$('.history').find(".overview").html(chatHTML);
if(parseInt($('.history').find(".viewport").css('height')){
if($(index).attr('id').split("g")[1]==fromId){
/* 删除消息条数dom元素 */
$($(index).find('.messCount')).css('display','none');
$($(index).find('.messCount')).html(0);
welive.vistorImg=$(index).find('img').attr('src');
console.log($(index).attr('data-isonline'))
if($(index).attr('data-isonline')!==undefined){
$(index).attr('data-isonline')=='0'?$('#guest_status').html('离线'):$('#guest_status').html('在线');
}
$(index).attr('data-isonline')=='0'?$('#guest_status').html('离线'):$('#guest_status').html('在线');
$(index).addClass('active');
$('#from').html($(index).attr('data-address'));
$('#guest_source').html($(index).attr('data-source'));
}
})
getchatForm(fromId);
/* 同时更新访客资料 */
$('#guest_id').html(fromId);
}
function getURL(a, b) {
if (a.substr(0, 5).toLowerCase() == 'href=') return a;
if (!b) b = 60;
var c = '';
if (a.length > b) {
a = a.substr(0, 30) + ' ... ' + a.substr(a.length - 18)
}
c += a + '';
return c
}
function welive_link() {
welive.ws = new WebSocket(node.address);
welive.ws.onopen = function() {
setTimeout(welive_verify, 100)
};
welive.ws.onclose = function() {
welive_close()
};
/* websocket服务端接收到消息后做处理 */
welive.ws.onmessage = function(a) {
welive_parseOut(a.data)
}
}
function TitleSound() {
sounder.html(welive.sound1);
/* if (welive.flashTitle && x) {
flashTitle();
if (welive.sound) sounder.html(welive.sound1);
welive.flashTitle = 0
} else if (welive.flashTitle) {
if (welive.sound) sounder.html(welive.sound2);
welive.flashTitle = 0
} */
}
function welive_clear() {
var a = s_history.children("div");
var b = a.length;
if (b >= 100) {
a.slice(0, b - 50).remove();
s_hwrap.welivebar_update('bottom')
}
}
function admins_update(n) {
var x = parseInt(s_admins.html());
x = x + n;
if (x < 0) x = 0;
s_admins.html(x)
}
/* 绑定客服 */
function bindAvtar(){
$.ajax({
type: "POST",
url : "/home/service/bind",
data: {client_id:welive.client_id},
success: function(msg){
/* 订阅成功 */
}
});
}
function replyForSs(){
welive.ws.send(JSON.stringify({"method":"pong", "data": "{}"}))
}
/* 获取用户的订阅信息 */
/* 改变访客状态-在线 */
function changeStatusOn(index){
var guestList=$('.visit_content');
/* 获取当前聊天的访客id */
for(var i=0;i1&&isequal){
for (var j=0;j'+
''+
''+welive.vistorName+''+
''+
'0
'+
'');
}
/* 消息展示 */
function guest_output(d, a, b) {
if (!d || !a || !b) return;
var o = $(".history");
switch (b) {
case 1:
d = '';
break;
case 2:
d = '';
break;
case 3:
d = '';
break;
case 4:
d = '';
break;
case 5:
d='';
break;
}
o.find(".overview").append(d);
window.setTimeout(function(){
$('.history').welivebar_update('bottom');
},200)
}
function welive_output(d, a, b) {
if (a) {
guest_output(d, a, b)
} else {
if (d === false) return;
s_history.append(d);
s_history.welivebar_update('bottom')
}
}
function welive_verify() {
welive.status = 1;
welive_send(JSON.stringify({"method":"login","data":{"site_code":site.code,"type":"service"}}))
$(".set_serving").hide();
$(".set_busy").show()
}
function welive_close() {
if (welive.status) {
s_online.html("");
s_admins.html(0)
}
welive.status = 0;
$("#websocket1212").remove();
welive_output('连接失败, 3秒后自动重试 ...
');
welive.ttt = setTimeout(welive_link, 3000)
}
/* 发送消息 */
function welive_send(d) {
s_send.addClass('loading2');
if (welive.status) {
welive.ws.send(d);
s_msg.val('')
} else {
welive_output('服务器连接中, 请等待 ...
')
}
s_msg.focus();
s_send.removeClass('loading2');
localStorage.clear();
}
/* 发送消息 */
function guest_send(e) {
var o = $('#sendTxt')||$('.content');
var ltly = o.val()||$('.publish').serializeArray()[0].value;//获取聊天数据
console.log();
if(ltly.length>0){
if($('#guest_id').html()==''){
/* 还未选择任何访客 提示 */
layer.msg('还未选择访客!')
}else{
$.ajax({
type: "POST",
url : "/home/service/chat",
data: {to_id:$('#guest_id').html(),type:1,content:ltly},
success: function(msg){
if(msg.status==200){
guest_output(ltly,5,2);
getFace();
$('#sendTxt').val('');
$('.content').val('');
}else{
layer.msg("发送失败!");
}
}
});
}
}
o.focus();
return false;
}
function guest_kickout() {
if ($.inArray(CurrentId, offline) < 0) welive.ws.send('x=6&a=6&g=' + CurrentId);
guest_delete(CurrentId);
CurrentId = 0;
$("#tiptip_holder").hide();
showNext()
}
/* 删除访客 */
function delGuest(index){
var guests=$('.visit_content');
var delStatus=false;
var curTab;
for(var j=0;j';
var o = $(".history");
var c = o.find(".overview>div.updating");
if (c.length) {
c.find(".i").html(b)
} else {
b = '';
o.find(".overview").append(b)
}
o.welivebar_update('bottom')
}
/* 聊天时初始化 */
function welive_init() {
guest = new Array();
offline = new Array();
g_online = $("#showusername"); /* 客户列表*/
s_chat = $("#s_chat");
s_msg = s_chat.find(".s_msg");
s_send = s_chat.find(".s_send");
s_admins = s_chat.find(".s_admins");
s_title = s_chat.find(".s_title").children(".l");
s_hwrap = $(".history");
s_owrap = s_chat.find("#s_owrap");
s_history = s_hwrap.find(".overview");
s_online = s_owrap.find(".overview");
sounder = $("#wl_sounder");
welive_link();
myWin88;
var b = s_hwrap.find(".viewport"),
s_onlineViewport = s_owrap.find(".viewport"),
xHeight = $(window).height() - 88;
g_online.height(xHeight);
s_hwrap.welivebar();
s_owrap.welivebar();
/* $(window).resize(function() {
s_hwrap.welivebar_update('bottom');
}); */
s_msg.keyup(function(e) {
/* if (e.keyCode == 13) s_send.trigger("click"); */
}).focus(function() {
welive.where = 0;
});
/* 发送消息 */
s_send.click(function(e) {
var a = $.trim(s_msg.val());
if (a) {
welive_output('抱歉, WeLive4免费版功能受限!
')
} else {
s_msg.focus()
}
e.preventDefault()
});
/* tab切换 */
$('.right-menunav').click(function(e){
var dataFor=e.target.getAttribute('for');
$('.right-menunav li').removeClass('active');
$(e.target).addClass('active');
$('.tab-content div').removeClass('show');
$('.tab-content').find('div[id^="'+dataFor+'"]').addClass("show");
})
/* 快捷回复 */
$('#reply-List').click(function(e){
var obj=$(e.target).html();
$.ajax({
type: "POST",
url : "/home/service/chat",
data: {to_id:$('#guest_id').html(),type:1,content:obj},
success: function(msg){
if(msg.status==200){
guest_output(obj,5,2);
getFace();
$('#sendTxt').val('');
$('.content').val('');
}else{
layer.msg("发送失败!");
}
}
});
})
$("#wl_ring").click(function(e) {
if (welive.sound) {
welive.sound = 0;
$(this).addClass("s_ringoff").removeClass("s_ring")
} else {
welive.sound = 1;
$(this).addClass("s_ring").removeClass("s_ringoff")
}
s_msg.focus();
e.preventDefault()
});
pagetitle = window.parent.document.title;
$(document,window.parent.document).mousedown(function(){
stopFlashTitle();
})
$(document,window.parent.document).keydown(function() {
stopFlashTitle();
});
/* welive.sound1 = '';
welive.sound2 = ''; */
window.onbeforeunload = function(a) {
welive.status = 0;
clearTimeout(welive.ttt);
return " "
};
$(window).unload(function() {
welive.status = 0;
clearTimeout(welive.ttt)
})
}
var tttt = 0,
pagetitle, flashtitle_step = 0,
sounder, towhere = 0;
var guest, offline, g_online, s_chat, s_msg, s_history, s_online, s_send, s_hwrap, s_owrap, s_admins, s_title;
var welive = {
ws: {},
index: '',
status: 0,
ttt: 0,
flashTitle: 0,
sound: 1,
sound1: '',
sound2: '',
where: 0,
client_id:'',
loginTime:'',
vistorFram_id:'',
vistorLog_id:'',
vistorName:'',
vistorImg:'',
vistorAddress:'',
vistorSource:'',
avatar:default_v_avatar, /* 客服头像*/
messageCount:0,
messContent:'',
recordPermission:false,
product:{}
};
var admin ={
id: '',
type: '',
sid: '',
fullname: '',
post: '',
agent: ''};
var myWin88, CurrentId = 0,//
zIndex = 2000;
$(function() {
var isFile=false;
var File=null;
var recorder=null;
var audio_context='';
welive_init();
$(".logout").click(function(e) {
showDialog('确定退出 WeLive 在线客服系统吗?', '', function() {
document.location = 'index.php?a=logout'
});
e.preventDefault()
})
/* 初始化商品 */
/* 加载录音模块 */
try {
// webkit shim
window.AudioContext = window.AudioContext || window.webkitAudioContext;
navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia;
window.URL = window.URL || window.webkitURL;
welive.audio_context = new AudioContext;
console.log('Audio context set up.');
console.log('navigator.getUserMedia ' + (navigator.getUserMedia ? 'available.' : 'not present!'));
} catch (e) {
console.log('No web audio support in this browser!');
}
navigator.getUserMedia({audio: true}, startUserMedia, function(e) {
console.log('No live audio input: ' + e);
});
});
$(function(){
$("input").bind("input propertychange", function() {
var ltly = $("#ltly").val();
});
});
/* 发送图片 */
$('#chooseImg').click(function(){
$('#inp-file').click();
})
$('#inp-file').change(function(){
imgUp();
});
/* 发送视频 */
$('#chooseVideo').click(function(){
$('#inp-video').click();
})
$('#inp-video').change(function(){
utils.fileChange(this,"video","视频",/video\/\w+/);
})
/* 选择录音文件 */
$('#inp-record').change(function(){
utils.fileChange(this,"audio","音频",/audio\/\w+/);
})
/* 发送其他文件 */
$('#chooseFile').click(function(){
$('#inp-ohter').click();
})
$('#inp-ohter').change(function(){
utils.fileChange(this,"file","","");
})
function imgUp(){
var formData = new FormData();
formData.append('image', $('#inp-file')[0].files[0]); //添加图片信息的参数
formData.append('type','image'); //添加其他参数
//获取文件
var file = $('#inp-file')[0].files[0];
var imageType = /^image\//;
var imgHTML="";
//是否是图片
if(!imageType.test(file.type)) {
alert("请选择图片!");
return;
}else{
$.ajax({
url: '/home/upload/upFile',
type: 'POST',
cache: false, //上传文件不需要缓存
data: formData,
processData: false, // 告诉jQuery不要去处理发送的数据
contentType: false, // 告诉jQuery不要去设置Content-Type请求头
success: function (data) {
if(data.status==200){
imgHTML+="
";
$.ajax({
type: "POST",
url : "/home/service/chat",
data: {to_id:$('#guest_id').html(),type:1,content:imgHTML},
success: function(msg){
if(msg.status==200){
welive_output(imgHTML,5,2);
getFace();
$('#sendTxt').val('');
$('.content').val('');
}else{
layer.msg('上传失败');
}
}
});
}
},
error: function (data) {
alert("上传失败");
}
})
}
}
/* 创建文件,并且写入数据内容 */
function CreateFile(fileObj)
{
var fso, tf;
fso = new ActiveXObject("Scripting.FileSystemObject");
tf = fso.CreateTextFile("../imgFile/testfile.json", true);
// 写一行,并且带有新行字符。
tf.WriteLine(fileObj) ;
tf.Close();
}
/* 录音模块 */
function startUserMedia(stream) {
if(stream!==undefined){
welive.recordPermission=true;
}
var input = audio_context.createMediaStreamSource(stream);
console.log('Media stream created.');
// Uncomment if you want the audio to feedback directly
//input.connect(audio_context.destination);
//__log('Input connected to audio context destination.');
recorder = new Recorder(input);
console.log('Recorder initialised.');
}
function startRecording(button) {
if(welive.recordPermission==false){
/* 自动触发选择录音文件 */
/* 当录音功能被禁止时 */
layer.alert("网页录音功能已被禁止,请选择音频文件!",function(){
$('#inp-record').click();
});
}
else{
welive.recorder && welive.recorder.record();
layer.open({
type: 1,
content: '正在录音……,点击完成,发送录音,取消取消发送',
btn: ['完成', '取消'],
yes:function(){
stopRecording();
},
btn1:function(){
console.log('取消发送')
}
});
console.log('Recording...');
}
}
function stopRecording(button) {
recorder && recorder.stop();
//button.disabled = true;
//button.previousElementSibling.disabled = false;
console.log('Stopped recording.');
// create WAV download link using audio data blob
createDownloadLink();
recorder.clear();
}
function createDownloadLink() {
recorder && recorder.exportWAV(function(blob) {
var url = URL.createObjectURL(blob);
var li = document.createElement('li');
var au = document.createElement('audio');
var hf = document.createElement('a');
au.controls = true;
au.src = url;
hf.href = url;
hf.download = new Date().toISOString() + '.wav';
hf.innerHTML = hf.download;
li.appendChild(au);
li.appendChild(hf);
document.getElementById('recordingslist').appendChild(li);
});
}