123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- var Request = GetRequest()
- new Vue({
- el: '#main',
- data: {
- sessionId:null,
- sessionType:2,//1对1会话
- curDoctorCode:JSON.parse(window.localStorage.getItem('wlyyAgent')).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<vm.members.length; i++){
- if(reply.sender_id == vm.members[i].id){
- member = vm.members[i];
- break;
- }
- }
- var img = httpRequest.getImgUrl(member.avatar);
- if((img.indexOf('http') == -1) && (img.indexOf("https") == -1)){
- img = '../../../images/d-male.png';
- }
- var content = '';
- try{
- if(reply.content_type == 18 || reply.content_type == 19){//居民名片、聊天记录
- content = "暂不支持此类信息";
- }else if(reply.content_type == 3){ //语音
- content = {};
- content.path = JSON.parse(reply.content).path;
- }else if(reply.content_type == 12){ //视频文件
- var arr = reply.content.split(",");
- content = {};
- content.img = arr[0];
- content.path = arr[1];
- content.time = arr[2];
- }else{
- if(vm.isString(reply.content)){
- content = reply.content;
- }else{
- content = JSON.parse(reply.content);
- }
- }
- }catch(e){
-
- }
- var obj = {
- isSelf: isSelf,
- time: new Date(reply.timestamp).format('yyyy-MM-dd HH:mm:ss'),
- type: reply.content_type,
- name: member.name,
- img: img,
- content: content
- };
- html = template('msg_tmp', obj);
- }
- return html;
- },
- isString:function(str){
- return (typeof str=='string')&&str.constructor==String;
- },
- showWarningMsg:function(msg){
- toastr.warning(msg)
- }
- }
- })
|