var d = dialog({contentType:'load', skin:'bk-popup'});
var dd = dialog({contentType:'load', skin:'bk-popup', content:'发送中...'});
var dialroll;
var doctorInfo;
var content = "";
var timeStr = "";
var id = 1;
var pagesize = 500;
var Request = new Object();
Request = GetRequest();
var consult = Request["consult"];
var status = Request["status"];
var doctorType = Request["type"];
var doctor = Request['doctor'];
var toUser = Request["toUser"];
var toName = decodeURIComponent(Request["toName"]);
var openid = Request["openid"];
var represented = Request["represented"];
//var evaluate = localStorage.getItem("evaluate");//1为有评价 0为无评价
var evaluate = 0;
var docMsg = 0;
var patientcode = "";
var pagetype=24;
var networkStatus = "";
var userAgent = window.localStorage.getItem(agentName);
var userInfo = '',
patientName = "";
$(function() {
if(!userAgent) {
localStorage.setItem("toUser",toUser);
localStorage.setItem("consultCode",consult);
window.location.href = "../../home/html/login.html?openid=" + openid+"&type="+pagetype;
return false;
}
userInfo = JSON.parse(window.localStorage.getItem(agentName))
patientcode = userInfo.represented?userInfo.represented:userInfo.uid;
var userAgent1 = JSON.parse(window.localStorage.getItem(agentName1));
patientName = userAgent1.name;
//判断关系
function hasFamilyRelation(a,b){
return new Promise(function(resolve, reject) {
var data={
patient:a,
familyMember:b
}
sendPost("patient/family/is_authorize", data, "json", "get", function(res){
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求家庭成员关系失败'}).show();
}, function(res){
if(res.status == 200) {
window.localStorage.setItem('nowPatientName',res.data.name);
resolve(res)
}else if(res.status == 100) {
dialog({
content:'对不起,'+res.data.name+'未授权给您,如需查看,请切换'+res.data.name+'账号登录',
okValue:'切换账号',
ok: function() {
window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
window.location.href = "../../grzx/html/change-login.html?type=0&openid="+ Request["openid"];
},
cancelValue: '我不看了',
cancel: function () {
wx.closeWindow();
}
}).showModal();
}else{
relogin();
}
})
})
}
//重新登录操作
function relogin(){
dialog({
content:'对不起,该消息是发给'+toName+',如需查看,请切换'+toName+'账号登录',
okValue:'切换账号',
ok: function() {
window.localStorage.setItem(pageName, JSON.stringify({pageurl: location.href}))
window.location.href = "../../grzx/html/change-login.html?type=0&openid="+ Request["openid"];
},
cancelValue: '我不看了',
cancel: function () {
wx.closeWindow();
}
}).showModal();
}
//保存信息
function saveUserInfo(a,b){
Promise.all([hasFamilyRelation(a,b)]).then(function () {
newUaObj.represented = represented;
window.localStorage.setItem(agentName,JSON.stringify(newUaObj));
userInfo = JSON.parse(window.localStorage.getItem(agentName));
bindEvents();
checkStatus();
})
}
function initFamilyData () {
//从微信模板消息进入
if(represented && userAgent && toUser){
if(toUser == represented){
saveUserInfo(represented,newUaObj.uid)
}else if(toUser != represented && represented == newUaObj.uid){
//本人代理
saveUserInfo(represented,newUaObj.uid)
}else if(toUser != represented && toUser == newUaObj.uid){
//需要判定关系 200有授权 100家人 1无关系
saveUserInfo(represented,newUaObj.uid)
}else{
//重新登录
relogin();
}
}else{
userInfo = JSON.parse(window.localStorage.getItem(agentName));
bindEvents();
checkStatus();
}
}
initFamilyData();
//点击内容区域输入框失焦
$('#talkwrap').click(function(){
$("#input_content").blur();
return false;
});
//发送以后定位至最底部
$("#reply a").click(function() {
send();
});
//从后台那边获取签名等信息
var params = {};
var url1 = window.location.href;
params.pageUrl = url1;
$.ajax(server + "weixin/getSign", {
data: params,
dataType: "json",
type: "post",
success: function(res){
if (res.status == 200) {
var t = res.data.timestamp;
var noncestr = res.data.noncestr;
var signature = res.data.signature;
wx.config({
appId: appId, // 必填,公众号的唯一标识
timestamp: t, // 必填,生成签名的时间戳
nonceStr: noncestr, // 必填,生成签名的随机串
signature: signature,// 必填,签名,见附录1
jsApiList: [
'chooseImage',
'uploadImage',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'onVoicePlayEnd',
'uploadVoice',
'getNetworkType'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.ready(function(){
wx.getNetworkType({
success: function (res) {
networkStatus = res.networkType; // 返回网络类型2g,3g,4g,wifi
}
});
});
}
}
});
//获取sessionId
sendPost('/patient/consult/getTopic', {consult:consult}, 'json', 'GET', queryFailed2, function(res){
if(res.status==200){
var sessionId = JSON.parse(res.msg)[0].session_id;
//加载socket组件
jQuery.getScript(imurl+"/socket.io/socket.io.js").done(function() {
var socket = io.connect(imurl);
socket.emit('login', {userId: userInfo.represented?userInfo.represented:userInfo.uid, password: userInfo.represented?userInfo.represented:userInfo.uid,sessionId:sessionId,clientType:"patient"});
socket.on('message', function (data) {
// console.log(data)
if((data.type == 1) ||(data.type == 2) || (data.type == 6) || (data.type == 12)){
addReply(1, data.content, new Date(data.timestamp).Format('yyyy-MM-dd HH:mm:ss'), data.type, data.name,data.sender_img);
setTimeout(function(){
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
},300)
}
});
socket.on('error', function (data) {
// console.log(data);
});
socket.on('ack', function (data) {
// console.log(data);
});
function getLocalTime(nS) {
return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');
}
})
.fail(function() {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:"医生实时对话连接失败!"}).show();
});
}
});
});
//控制“发送”按钮的变化
function sendBtn() {
var tval = $(".talk-input input").val();
if (tval != "") {
$(".talk-send a").removeClass("disab");
} else {
$(".talk-send a").addClass("disab");
}
}
//控制页面高度
function winSize(h) {
var totH = $(window).height();
$("#talkwrap").height(totH - h);
}
function getDoctorWork(){
if(doctorType=="6") {
checkDoctorInWork(doctor);
} else {
sendPost('patient/sign_doctors', {patientCode:patientcode}, 'json', 'post', queryFailed, function(res){
if(res.status==200){
var doctorArray = res.familyDoctors;//家庭
if(doctorType==1){//三师
doctorArray = res.teamDoctors
}
if(doctorArray){
var doctorInfo ="";
for(var j in doctorArray){
if(doctorArray[j].level=="3"){
doctorInfo = doctorArray[j];//取出健康管理师
break;
}
}
if(doctorInfo!=""){
checkDoctorInWork(doctorInfo.code);
}
}
}else{
queryFailed(res);
}
})
}
}
function checkDoctorInWork(doctor){
sendPost('patient/consult/is_doctor_working', {doctor:doctor}, 'json', 'post', queryFailed, function(res){
if(res.status==200){
if(res.data!=1){
addReply(7, "医生不在工作时间,无法及时回复您的消息。
查看工作时间!", getNowFormatDate(),1, "系统消息");
$(".workhours").on("click",function(){
location.href='doctor-work-hours.html?doctor='+doctor;
});
setTimeout(function(){
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
},20);
}
}else{
queryFailed(res);
}
});
}
//判断记录的状态
function checkStatus(){
d.show();
if(status == 0 || status == 1){
if(status == 0){
$("#divBottom").show();
$("#finish_list").show();
winSize(102);
}else{
$("#divBottom").hide();
$("#finish_list").hide();
winSize(0);
}
//页面初始化查询
queryList();
var pulldownAction = function() {
getPage(this);
};
dialroll = iscrollAssist.newVerScrollForPull($('.pull-iscroll-wrap'), pulldownAction, null);
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
$(window).resize(function() {
if(status == 0){
winSize(102);
}
else{
winSize(0);
}
});
}else{
var data = {};
data.consult = consult;
sendPost('patient/consult/status', data, 'json', 'get', queryFailed, querySuccess);
}
}
function queryFailed(res) {
d.close();
if (res && res.msg) {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:res.msg}).show();
} else {
dialog({contentType:'tipsbox', skin:'bk-popup' , bottom:true,content:'获取咨询记录状态失败'}).show();
}
}
function querySuccess(res) {
if (res.status == 200) {
if(res.data == 0){
//未结束的咨询跳转去咨询页面
window.location.href = "../../qygl/html/sign_info.html?doctor="+doctor+"&consult="+consult;
}else{
$("#divBottom").hide();
$("#finish_list").hide();
$("#input_div").hide();
winSize(0);
//新增评价状态 1、已评价 0、未评价
var data = {};
data.consult = consult;
sendPost('patient/consult/evaluateStatus', data, 'json', 'get', queryFailed, function(res){
if (res.status == 200) {
evaluate = res.data;
//显示评价按钮
if(evaluate == 0){
$('#valFoot').show();
$('#noval').show();
$('#hasval').hide();
}else if(evaluate == 1){
$('#valFoot').show();
$('#noval').hide();
$('#hasval').show();
}
}else{
queryFailed(res);
}
});
}
status = res.data;
//聊天记录填充
queryList();
var pulldownAction = function() {
getPage(this);
};
dialroll = iscrollAssist.newVerScrollForPull($('.pull-iscroll-wrap'), pulldownAction, null);
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
$(window).resize(function() {
if(res.data == 0){
winSize(102);
}
else{
winSize(0);
}
});
d.close();
}
else{
queryFailed(res);
}
}
//页面初始化
function queryList(){
var data = {};
data.consult = consult;
data.page = id;
data.pagesize = pagesize;
sendPost('patient/consult/loglist', data, 'json', 'GET', queryFailed2, querySuccess2);
}
function queryMsg(logId,msgType){
var data={};
data.consult = consult;
data.logId = logId;
data.msgType=msgType;
sendPost('patient/consult/oneLog', data, 'json', 'GET', queryFailed2, function(res){
if(res.status==200){
var reply = res.consult;
if((reply.msgType == 1) || (reply.msgType == 2) || (reply.msgType == 6)){
addReply(reply.type, reply.content, reply.time, reply.msgType, reply.doctorName, reply.photo);
setTimeout(function(){
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
},300)
}
}
d.close();
});
}
function queryFailed2(res) {
d.close();
if (res && res.msg) {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:res.msg}).show();
} else {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:'加载失败'}).show();
}
}
function querySuccess2(res) {
if (res.status == 200) {
id++;
res = res.list;
var doctorPar = [];//储存参与咨询的医生
for(var k in res.users){
if(res.users[k].is_patient == 0 && res.users[k].id==doctor){
doctorPar.push(res.users[k])
}else if(res.users[k].is_patient == 0 && res.users[k].id!=doctor){
doctorPar.unshift(res.users[k])
}
}
doctorPar=JSON.stringify(doctorPar)
window.localStorage.setItem('doctorPar',doctorPar);
var list = res.list;
if (list && list.length > 0) {
docMsg = list[list.length - 1].content_type;
var doctor = "";
for (var j = list.length-1; j >=0; j--) {
var reply = JSON.parse(list[j]);
//type为1时,医生发的
if((reply.content_type == 3) || (reply.content_type == 1) || (reply.content_type == 2) || (reply.content_type == 6) || (reply.content_type == 12)){
var date = new Date();
date.setTime(reply.timestamp);
var time = date.format('yyyy-MM-dd hh:mm:ss');
var p;
for(var k in res.users){
if(res.users[k].id==reply.sender_id){
p = res.users[k].avatar;
}
}
addReply(reply.sender_id==patientcode? 2 : 1, reply.content, time, reply.content_type, reply.sender_name, p);
}
}
if(status==0){
getDoctorWork();
}
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
}
d.close();
}
else{
queryFailed2(res);
}
}
function getNowFormatDate() {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
if (month >= 1 && month <= 9) {
month = "0" + month;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
+ " " + date.getHours() + seperator2 + date.getMinutes()
+ seperator2 + date.getSeconds();
return currentdate;
}
//点击发送按钮
function send(){
content = $("#input_content").text().replace(/\s+/g,"");
if (content && content != null && content != "") {
var now = new Date();
timeStr = "";
if(now.getMonth()<9){
//补0
timeStr = now.getFullYear() + "-0"+ (now.getMonth()+1) + "-" +now.getDate() +' '
+ now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds();
}else{
timeStr = now.getFullYear() + "-"+ (now.getMonth()+1) + "-" +now.getDate() +' '
+ now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds();
}
$("#input_content").blur();
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
// dd.showModal();
var data = {};
data.consult = consult;
data.content = utf16toEntities(content);
data.type = 1;
sendPost('patient/consult/append', data, 'json', 'post', submitFailed, submitSuccess);
} else {
return;
}
}
function submitFailed(res) {
dd.close();
if (res && res.msg) {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:res.msg}).show();
} else {
dialog({contentType:'tipsbox', skin:'bk-popup' ,bottom:true, content:'发送失败'}).show();
}
}
function submitSuccess(res) {
if (res.status == 200) {
//置空
$("#input_content").text("");
$(".talk-send a").addClass("disab");
addReply(2, content, timeStr, 1, "");
dialroll.refresh();
dialroll.scrollTo(0, dialroll.maxScrollY);
showSendBtn(false);
dd.close();
} else {
submitFailed(res);
}
}
//回复单条内容填写
var defaultPhoto = "../../../images/p-default.png";
var doctorDefaultPhoto = "../../../images/d-default.png";
function addReply(type, content, time, msgType, doctorName,photo,prepend) {
//time = time.substr(10);
var doctorPhoto = photo ? getImgUrl(photo) : doctorDefaultPhoto;
var $div = $("
'+ doctorName + '
'+ doctorName + '