var Request = GetRequest() new Vue({ el: '#main', data: { sessionId:null, sessionType:2,//1对1会话 curDoctorCode:JSON.parse(window.localStorage.getItem('wlyyAgentForDoc')).uid, curDoctorName:"", otherCode: Request["otherCode"],//对方医生code otherName: decodeURIComponent(Request["otherName"]) || "",//对方医生姓名 planDetailId:Request["planDetailId"],//项目id members:[], otherInfo:null,//对方的医生信息 firstMsg:null, page:1, pagesize:10, count:0 }, mounted: function() { this.createSession(); this.bindEvents(); }, methods:{ createSession:function(){ var vm = this; var p = {} p[vm.curDoctorCode] = 0; p[vm.otherCode] = 0; var data = { session_type: vm.sessionType, session_name: vm.otherName, participants: JSON.stringify(p) } consultingAPI.createSession(data).then(function(res){ if(res.status==200){ vm.sessionId = res.data.id; vm.connectSocket();//创建socket连接 vm.getMembers();//获取成员列表 }else{ layer.msg(res.msg, {icon: 5}) } }); }, getMembers:function(){ var vm = this; consultingAPI.getParticipants(vm.sessionId).then(function(rs){ var members = {}; var isSTF=false;//是否是专科对家医聊天,需要隐藏发送名片入口 $.each( rs , function(i, v) { if(v.level==1) isSTF=true; if(v.id!=vm.curDoctorCode){ vm.otherInfo = v; vm.otherCode = v.id; }else { vm.curDoctorName = v.name; } vm.members.push({ id:v.id, code: v.id, name: v.name, sex: v.sex, avatar: v.avatar, role: v.role, is_patient: v.is_patient,//0 医生,1 患者 last_fetch_time:v.last_fetch_time }); }); //获取消息 vm.getMessage(true); }); }, getMessage:function(isInit){ var vm = this; var params = { page: vm.page, pagesize: vm.pagesize, end_msg_id: '', start_msg_id: '', user: vm.curDoctorCode, session_id: vm.sessionId, content_type: '', isoffset: '' } consultingAPI.getMessages(params).then(function(res){ var list = res; if (list && list.length > 0) { id = list[list.length - 1].id; var html = '', length = list.length; for (var j = list.length-1; j >= 0; j--) { var reply = list[j]; html += vm.formatMsg(reply); } if(isInit){ $("#talkBox").append(html); $("#talkBox").slimScroll({ height: '100%', width: '100%', alwaysVisible: true, start : 'bottom', }).bind('slimscroll', function(e, pos) { if(pos == 'top'){ if(length == 10){ vm.page++; vm.getMessage(); } } }); }else{ $("#talkBox").prepend(html); $("#talkBox").slimScroll({ scrollTo: 'bottom' }); } $(".fancybox").fancybox({openEffect:"none",closeEffect:"none"}); plyr.setup(); } }); }, bindEvents:function(){ var vm = this; template.helper('getSourceUrl', function(str){ return httpRequest.getImgUrl(str); }); //发送图片 $("#file_head").on('change', function(){ var file = this.files[0]; //先上传图片去服务器,然后再发送消息 var fd=new FormData(); fd.append("action", "UploadVMKImagePath"); fd.append("file", file); //加入文件对象 fd.append("type", '2'); var ajaxObj = { data: fd, cache: false, processData: false, contentType: false } consultingAPI.uploadImage(ajaxObj).then(function(res){ if(res.status == 200){ vm.sendMessage(2, res.urls); var obj = { content: res.urls, content_type: '2', sender_id: vm.curDoctorCode, timestamp: new Date().getTime() } var html = vm.formatMsg(obj); $("#talkBox").append(html); $("#talkBox").slimscroll({ scrollTo: 'bottom' }); }else{ layer.msg(res.msg, {icon: 5}) } }); }); }, sendClick:function(){ var vm = this; var $this = $(this); var text = $.trim($("#input_content").text()); if(text.length == 0){ vm.showWarningMsg('发送内容不能为空'); return false; }else{ vm.sendMessage(1, text); $("#input_content").text(''); var obj = { content: text, content_type: '1', sender_id: vm.curDoctorCode, timestamp: new Date().getTime() } var html = vm.formatMsg(obj); $("#talkBox").append(html); $("#talkBox").slimscroll({ scrollTo: 'bottom' }); } }, sendMessage:function(contentType, content){ var vm = this; var params = { sender_id: vm.curDoctorCode, sender_name: vm.curDoctorName, content_type: contentType, content: content, view: 0 }; consultingAPI.sendMessage(vm.sessionId, {data: params}).then(function(res){ console.log(res); vm.count++; if(vm.count==1 && JSON.parse(window.localStorage.getItem('wlyyAgent')).doctorType==1 && vm.planDetailId && vm.planDetailId!="undefined"){//level:1 专科医生 (只记录第一条数据) var messages = res.messages[0]; var guidanceParams = { messageId:messages.id,//消息id content:messages.content,//聊天内容 planDetailId:vm.planDetailId,//服务项目id contentType:messages.content_type//消息类型 } consultingAPI.saveGuidanceMessage(guidanceParams).then(function(rs){ if(rs.status==200){ }else{ layer.msg(rs.msg, {icon: 5}) } }) } }); }, connectSocket:function(){ var vm = this; jQuery.getScript(httpRequest.socketUrl+"/socket.io/socket.io.js").done(function() { var socket = io.connect(httpRequest.socketUrl ); socket.emit('login', { userId: vm.curDoctorCode, password: vm.curDoctorCode, sessionId: vm.sessionId, clientType: "pc_doctor" }); socket.on('message', function (data) { console.log(data); if(data.read && (data.read == "all" || data.read == "one") ){ return ; } var html = vm.formatMsg(data); $("#talkBox").append(html); $("#talkBox").slimscroll({ scrollTo: 'bottom' }); }); socket.on('error', function (data) { console.log(data) }); socket.on('ack', function (data) { }); }).fail(function() { // dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:"医生实时对话连接失败!"}).show(); }); }, formatMsg:function(reply){ var vm = this; try{ reply = JSON.parse(reply); }catch(e){ } var isSelf = (reply.sender_id==vm.curDoctorCode) ? true : false; var isSystem = reply.sender_id == 'system'; var html = ''; if(reply.content_type == 7 || reply.content_type == 10 || reply.content_type == 13 || reply.content_type == 14){ var content = reply.content; if(isSystem){ content = '居民24小时内未回复,系统自动结束咨询'; } html = template('sys_msg_tmp', {content: content}); }else{ var member; for(var i=0; i