define(['zepto', 'medtap'], function($, medtap) { var chat = { userId: medtap.getRequest('userId'), relationId: '', doctorId: '', doctorName: '', doctorProfile: '', helperName: '', helperId: '', helperProfile: '', userName: '', userProfile: '', hasMore: false, limit: 50, offset: 1, minId: 0, maxId: 0, // 默认消息类型 num:0, init: function() { chat.bindEve(); chat.getPmdOrderInfo(); chat.talkRelation(); chat.getImgConsultNumber(); chat.getConsultNumber(); chat.getPmdNumber(); /* $(window).scroll(function() { var scrollTop = $(this).scrollTop(); //滚动条距离顶部的高度 var scrollHeight = $(document).height(); //当前页面的总高度 var clientHeight = $(this).height(); //当前可视的页面高度 if (scrollTop + clientHeight >= scrollHeight) { //距离顶部+当前高度 >=文档总高度 即代表滑动到底部 //滚动条到达底部 chat.offset = 1; chat.refreshRecord(0); } else if (scrollTop <= 0) { //滚动条到达顶部 chat.offset = 1; chat.refreshRecord(1); } }); */ var getTalk = setInterval(function() { chat.getTalkRecord(); if (document.documentElement.scrollHeight > document.documentElement.clientHeight) { setTimeout(function() { document.documentElement.scrollTop = document.documentElement.scrollHeight; }) } }, 5000) $('input').on('focus', function() { clearInterval(getTalk); }); $('input').on('blur', function() { var getTalk = setInterval(function() { chat.getTalkRecord(); if (document.documentElement.scrollHeight > document.documentElement.clientHeight) { setTimeout(function() { document.documentElement.scrollTop = document.documentElement.scrollHeight; }) } }, 5000) }); }, bindEve: function() { $('.close_info_btn').unbind().bind('click', function() { $('.chat_info').hide(); $(this).hide(); $('.open_info_btn').show(); }); $('.open_info_btn').unbind().bind('click', function() { $('.chat_info').show(); $(this).hide(); $('.close_info_btn').show(); }); $('.send').unbind().bind('click', function() { if (!$('#sendMsg').val()) { medtap.winPop('消息不能为空') } else { chat.sendMsg(chat.num); } }) }, // 图片选择 choseImage: function(){ $('.footer_addPic').on('click', function() { $('.footer').toggleClass("main"); $('.footer_choseImg').toggle() $('.footer_gallery').unbind().bind('click',function(){ //alert(selectImgObj) var selectImgObj = { crop:false,//是否裁剪 compress:true,//是否压缩 multiple:1 //图片数量 }; try { if (!WebAPI) { } try { WebAPI.selectImages(selectImgObj,function(data){ // alert(JSON.stringify(data.keys[0])) chat.sendMsg(1,data.keys[0]) }); } catch (e) { } } catch (e) { } // 隐藏图片选择模块 $('.footer').toggleClass("main"); $('.footer_choseImg').toggle() }); $('.footer_Photograph').unbind().bind('click',function(){ var cameraObj = { crop:false,//是否裁剪 compress:true//是否压缩 } try { if (!WebAPI) { } try { WebAPI.openCamera(cameraObj,function(data){ chat.sendMsg(1,data.keys[0]) }); } catch (e) { } } catch (e) { } // 隐藏图片选择模块 $('.footer').toggleClass("main"); $('.footer_choseImg').toggle() }) }) }, // 图片预览 previewImg: function(){ $('.chat_list').on('click', '.chat_nopadding img',function() { var imgObject = {}; // var imgSelectsrc = $(this).index(); var imgIndex = 0; var urls = [], picUrl = ""; // [].forEach.call($('.chat_list .chat_nopadding img'),function(item,index){ // if($(item).index() == imgSelect){ // imgIndex = index; // } urls.push($(this).attr('src')); // }) imgObject.imgList = urls; imgObject.imgIndex = imgIndex; try { if (!WebAPI) { } try { WebAPI.showImages(imgObject); } catch (e) { } } catch (e) { } }); }, //获取私人医生订单相关信息 getPmdOrderInfo: function() { medtap.jzz(1); $.ajax({ url: 'https://gateway.medtap.cn/trade/pmdConsultOrder/listValidOrder', type: 'post', contentType: "application/json", async: false, headers: medtap.getHeaders(), data: JSON.stringify({ clientId: parseInt(chat.userId), clientType: 'USER' }), success: function(res) { medtap.jzz(0); if (res.success == true) { var pmdOrderList = res.content.list; if (pmdOrderList.length == 0) { //console.log(1) $('.service_btn').addClass('service_none'); } else { //console.log(2) } } } }) }, //绑定,获取聊天关系 talkRelation: function() { medtap.jzz(1); $.ajax({ url: 'https://gateway.medtap.cn/operation/talk/talkRelation', type: 'post', async: false, contentType: 'application/json', headers: medtap.getHeaders(), data: JSON.stringify({ userId: parseInt(chat.userId) }), success: function(res) { medtap.jzz(0); if (res.success == true) { chat.relationId = res.content.talkRealtion.id; chat.doctorId = res.content.talkRealtion.doctorId; chat.doctorName = res.content.talkRealtion.doctorName; chat.doctorProfile = res.content.talkRealtion.doctorProfile; chat.userName = res.content.talkRealtion.username; chat.userProfile = res.content.talkRealtion.userProfile; chat.helperId = res.content.talkRealtion.helperId; chat.helperName = res.content.talkRealtion.helperName; chat.helperProfile = res.content.talkRealtion.helperProfile; $('.user_profile').attr('src', chat.userProfile); $('.user_name').html(chat.userName); $('.assistant_name').html(chat.helperName); document.title = chat.userName; $('.assistant_profile').attr('src', chat.helperProfile); chat.getTalkRecord(); } } }) }, sendMsg: function(num,content) { medtap.jzz(1); $.ajax({ url: 'https://gateway.medtap.cn/operation/talk/talk', type: 'post', async: false, headers: medtap.getHeaders(), contentType: 'application/json', data: JSON.stringify({ contentType: num, content: content? content:$('#sendMsg').val(), orderSn: '', relationId: chat.relationId }), success: function(res) { medtap.jzz(0); if (res.success == true) { //window.location.reload(); chat.getTalkRecord(); //window.scrollTo(0, document.documentElement.clientHeight); $('#sendMsg').val('') } else { medtap.winPop(res.resultDesc) } } }) }, //获取消息列表 getTalkRecord: function() { medtap.jzz(1); $.ajax({ url: 'https://gateway.medtap.cn/operation/talk/talkRecord', type: 'get', async: false, headers: medtap.getHeaders(), data: { offset: chat.offset, limit: 50, relationId: chat.relationId, orderSn: '', }, success: function(res) { medtap.jzz(0); var record = res.content.list; var html = ''; if (record.length == 0) { chat.hasMore = false; //medtap.winPop('没有更多了') } else { chat.hasMore = true; chat.minId = record[0].id; chat.maxId = record[record.length - 1].id; //console.log(chat.minId + ':' + chat.maxId) for (var i = 0; i < record.length; i++) { var type = record[i].contentType; if (type == 0) { if (record[i].senderId == chat.doctorId) { html += '<div class="chat_item chat_item_right">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' + '<div class="chat_message_warp">' + '<div class="chat_message chat_message_send">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } else if (record[i].senderId == chat.userId) { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.userProfile + '" class="chat_profile from_profile">' + '<div class="chat_message_warp">' + /* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */ '<div class="chat_message chat_message_from">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } else { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.helperProfile + '" class="chat_profile from_profile" style="margin-top:0.2rem">' + '<div class="chat_message_warp">' + '<p class="chat_from_name">' + chat.helperName + '</p>' + '<div class="chat_message chat_message_from">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } } else if (record[i].contentType == 10) { var content = JSON.parse(record[i].content); html += '<div class="service_message img_consult clearfix" style="margin-bottom:0.2rem;" data-id="' + content.orderSn + '">' + '<div class="service_left_box sevice_img_consult">' + '<img src="images/consult_order_twzx_png@2x.png" >' + '<p class="service_title">图文咨询</p>' + '<p class="service_price">' + content.payFee + '</p>' + '</div>' + '<div class="service_right_box service_message_detail">' + '<p class="detail_patient_name">' + content.texts[0] + '</p>' + '</div>' + '</div>'; } else if (record[i].contentType == 11) { var content = JSON.parse(record[i].content); html += '<div class="service_message consult_box clearfix" data-id="' + content.orderSn + '">' + '<div class="service_left_box sevice_consult">' + '<img src="images/consult_order_twzx_png@2x.png" >' + '<p class="service_title">私人医生</p>' + '<p class="service_price">' + content.payFee + '</p>' + '</div>' + '<div class="service_right_box service_message_detail">' + /* '<p class="detail_patient_name">患者:张天问</p>'+ */ '<p class="detail_question">' + content.texts[1] + '</p>' + '</div>' + '</div>'; } else if (type == 3) { var content = record[i].content; content.replace(/\n/g, "<br>"); html += '<p class="system_msg">' + content.replace(/\n/g, "<br>") + '</p>'; } else if (type == 1) { if (record[i].senderId == chat.userId) { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.userProfile + '" class="chat_profile send_profile">' + '<div class="chat_message_warp">' + '<div class="chat_message chat_message_send chat_nopadding">' + // 图片消息 '<img src="'+record[i].contentPic[0]+'" />'+ '</div>' + '</div>' + '</div>' + '</div>'; } else if (record[i].senderId == chat.doctorId) { html += '<div class="chat_item chat_item_right">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.doctorProfile + '" class="chat_profile from_profile">' + '<div class="chat_message_warp">' + // '<p class="chat_from_name">' + // chat.doctorName + // '医生</p>' + '<div class="chat_message chat_message_from chat_nopadding">' + // 图片消息 '<img src="'+record[i].contentPic[0]+'" />'+ '</div>' + '</div>' + '</div>' + '</div>'; }else if (record[i].senderId == chat.helperId) { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.helperProfile + '" class="chat_profile from_profile">' + '<div class="chat_message_warp">' + '<p class="chat_from_name">' + chat.helperName + '</p>' + '<div class="chat_message chat_message_from chat_nopadding">' + // 图片消息 '<img src="'+record[i].contentPic[0]+'" />'+ '</div>' + '</div>' + '</div>' + '</div>'; } } } } if (chat.offset == 1) { $('.chat_list').html(html); if (document.documentElement.scrollHeight > document.documentElement.clientHeight) { setTimeout(function() { document.documentElement.scrollTop = document.documentElement.scrollHeight; }) } } else { $('.chat_list').prepend(html); if (document.documentElement.scrollHeight > document.documentElement.clientHeight) { setTimeout(function() { document.documentElement.scrollTop = document.documentElement.scrollHeight; }) } } $('.consult_box').unbind().bind('click', function() { var id = $(this).attr('data-id'); //medtap.pushWindow('../../wechat_lung_prvivateDoctor/pages/pmdDetail.html?id=' + id + '&wechatId=' + medtap.getRequest('wechatId')); window.location.href = 'medtapdoc://medtap.cn/order/detail?orderSn=' + id; }); $('.img_consult').unbind().bind('click', function() { var id = $(this).attr('data-id'); //medtap.pushWindow('../../wechat_lung_imgConsult/pages/imgConsultDetail.html?id=' + id + '&wechatId=' + medtap.getRequest('wechatId')); window.location.href = 'medtapdoc://medtap.cn/order/detail?orderSn=' + id; }) } }) }, //刷新消息列表 refreshRecord: function(operateType) { var id = 0; if (operateType == 0) { id = chat.maxId; } else { id = chat.minId; } medtap.jzz(1); $.ajax({ url: 'https://gateway.medtap.cn/operation/talk/talkRecord/refresh', type: 'get', async: false, headers: medtap.getHeaders(), data: { offset: chat.offset, limit: 10, relationId: chat.relationId, orderSn: '', operateType: operateType, startId: id }, success: function(res) { medtap.jzz(0); chat.offset += 1; var record = res.content.list; var html = ''; if (record.length == 0) { chat.hasMore = false; //medtap.winPop('没有更多了'); } else { chat.hasMore = true; if (operateType == 0) { chat.maxId = record[record.length - 1].id; for (var i = 0; i < record.length; i++) { var type = record[i].contentType; if (type == 0) { if (record[i].senderId == chat.doctorId) { html += '<div class="chat_item chat_item_right">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' + '<div class="chat_message_warp">' + '<div class="chat_message chat_message_send">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } else if (record[i].senderId == chat.userId) { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.userProfile + '" class="chat_profile from_profile">' + '<div class="chat_message_warp">' + /* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */ '<div class="chat_message chat_message_from">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } } $('.chat_list').append(html); } } else { chat.minId = record[0].id; for (var i = 0; i < record.length; i++) { var type = record[i].contentType; if (type == 0) { if (record[i].senderId == chat.doctorId) { html += '<div class="chat_item chat_item_right">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.doctorProfile + '" class="chat_profile send_profile">' + '<div class="chat_message_warp">' + '<div class="chat_message chat_message_send">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } else if (record[i].senderId == chat.userId) { html += '<div class="chat_item chat_item_left">' + '<p class="message_time">' + record[i].createTime.substring(5, 16) + '</p>' + '<div class="chat_info_warp clearfix">' + '<img src="' + chat.userProfile + '" class="chat_profile from_profile">' + '<div class="chat_message_warp">' + /* '<p class="chat_from_name">' + chat.doctorName + '医生</p>' + */ '<div class="chat_message chat_message_from">' + record[i].content + '</div>' + '</div>' + '</div>' + '</div>'; } } } $('.chat_list').prepend(html); } } } }) }, getImgConsultNumber:function(){ medtap.jzz(1); $.ajax({ url:'https://gateway.medtap.cn/trade/imageConsultOrder/countOrder', type:'post', async: false, contentType: 'application/json', headers:medtap.getHeaders(), data:JSON.stringify({ condition:{ serviceStatusList:['3','10'], clientId:parseInt(chat.userId), clientType:"USER" } }), success:function(res){ medtap.jzz(0); if(res.success == true){ var count = res.content.totalCount; if(count == 0){ $('.to_img_consult').addClass('service_none'); }else{ $('.to_img_consult').removeClass('service_none'); $('.to_img_consult').unbind().bind('click',function(){ //window.location.href = 'pages/myOrder.html?orderType=TWZX&userId=' + chat.userId; medtap.pushNewWindow('pages/myOrder.html?orderType=TWZX&userId=' + chat.userId); }) } } } }) }, getConsultNumber:function(){ medtap.jzz(1); $.ajax({ url:'https://gateway.medtap.cn/trade/consult/countOrder', type:'post', async: false, contentType: 'application/json', headers:medtap.getHeaders(), data:JSON.stringify({ condition:{ serviceStatusList:['3','10'], userId:parseInt(chat.userId) } }), success:function(res){ medtap.jzz(0); if(res.success == true){ var count = res.content.totalCount; if(count == 0){ $('.to_consult').addClass('service_none'); }else{ $('.to_consult').removeClass('service_none'); $('.to_consult').unbind().bind('click',function(){ //window.location.href = 'pages/myOrder.html?orderType=DHZX&userId=' + chat.userId; medtap.pushNewWindow('pages/myOrder.html?orderType=DHZX&userId=' + chat.userId); }) } } } }) }, getPmdNumber:function(){ medtap.jzz(1); $.ajax({ url:'https://gateway.medtap.cn/trade/pmdConsultOrder/countOrder', type:'post', async: false, contentType: 'application/json', headers:medtap.getHeaders(), data:JSON.stringify({ condition:{ serviceStatusList:['3','10'], clientId:parseInt(chat.userId), clientType:"USER" } }), success:function(res){ medtap.jzz(0); if(res.success == true){ var count = res.content.totalCount; if(count == 0){ $('.to_pmd').addClass('service_none'); }else{ $('.to_pmd').removeClass('service_none'); $('.to_pmd').unbind().bind('click',function(){ //window.location.href = 'pages/myOrder.html?orderType=PMD&userId=' + chat.userId; medtap.pushNewWindow('pages/myOrder.html?orderType=PMD&userId=' + chat.userId) }) } } } }) } } chat.init(); chat.choseImage(); chat.previewImg(); })