// TODO 临时构造plus对象,使得能够在浏览器中进行调试
//var plus = null;
// 登录者相关信息(包括userAgent)
var loginerInfo = null,
// 基础环境信息(包括当前webview)
baseEnv = null,
docInfo = null,
self = null,
activeItem = null,
isInit;
var $queryTab = $('#query_tab'),
$doingView = $('#search_result'),
$doingViewList = $doingView.find('.c-list'),
// 搜索无结果时显示
$noResultWrap = $('#no_result_wrap'),
searchPage,
$searchbar = $(".searchbar-input");
var orginalData = [];
var zdList, zdListKey = "",
msg_del_list, msg_del_list_key = "",
msg_cache_key = "";
var first = true;
mui.plusReady(function() {
self = plus.webview.currentWebview();
docInfo = JSON.parse(plus.storage.getItem("docInfo"));
zdListKey = "jm_zdList_"+ docInfo.code;
msg_del_list_key = "jm_msg_del_list_"+ docInfo.code;
msg_cache_key = "jm_msg_cache_key"+ docInfo.code;
zdList = plus.storage.getItem(zdListKey);
zdList = zdList? JSON.parse(zdList) : [];
msg_del_list = plus.storage.getItem(msg_del_list_key);
msg_del_list = msg_del_list? JSON.parse(msg_del_list) : {};
/**
* 预加载查询页面
*/
searchPage = plus.webview.getWebviewById('sousuojuming');
if(searchPage){
searchPage.hide();
}else{
searchPage = mui.preload({
id:"sousuojuming",
url: 'sousuojuming.html'
});
}
});
// ”医生im”列表分页查询最后一条记录Id
var doingLastId = 0,
// ”医生im”列表iscroll 滚动条实例
doingIscroller;
// 获取登录相关信息
var getLoginerInfo = function() {
// 登录的相关信息
var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
return {
userAgent: userAgent
}
},
// 获取基础环境信息
getBaseEnvPromise = function () {
var env = {
webview: plus&&plus.webview.currentWebview()
};
return Promise.resolve().then(function(res) {
return env;
});
},
formatDate = function(timestamp){
var date = new Date(timestamp);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() < 10 ?'0'+date.getDate():date.getDate();
return Y+M+D;
},
setPhoto = function(groupPhoto){
var images = '';
var cssArr=null;
for(var i in groupPhoto){
if(groupPhoto[i].ispatient=="1"){//患者信息
cssArr = ["position: absolute;width: 50px !important;height: 50px !important;border-radius: 50px;background-size: 50px;overflow: hidden;"];
images+='
'+
'
'+
'
';
break;
}
}
return images;
return false;
var images = '';
var cssArr=null;
if(groupPhoto.length>=5){
cssArr = ["top: 0;left: 50%;margin-left: -8px;","top: 12px;left: 2px;","top: 12px;left: 28px;","top: 27px;left: 5px;","top: 27px;left: 24px;"];
}else if(groupPhoto.length==4){
cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
}else if(groupPhoto.length==3){
cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
}else if(groupPhoto.length==2){
cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
}else if(groupPhoto.length==1 || groupPhoto.length==0){
cssArr = ["position: absolute;width: 50px !important;height: 50px !important;border-radius: 50px;background-size: 50px;overflow: hidden;"];
}
for(var i in groupPhoto){
if(i>4) break;
if(groupPhoto[i].avatar){
images+=''+
'
'+
'
';
}else{
if(groupPhoto[i].role=="doctor"){
images+=''+
'
'+
'
';
}else{
images+=''+
'
'+
'
';
}
}
}
return images;
},
initPage = function(){
// var msg = plus.storage.getItem(msg_cache_key);
// if(msg){
// msg = JSON.parse(msg);
// render(msg, true, false);
// getData(false, false);
// } else {
getData(false, false);
// }
},
render= function(data, isInit, isPullDown){
// plus.storage.setItem(msg_cache_key, JSON.stringify(data));
if(data.length==0){
myScroll.refresh();
myScroll.endPullDownToRefresh();
mui.later(function(){
$(".l-pullDown").hide();
}, 200)
} else {
if(isInit){
//初始化
console.log(data)
$doingViewList.html( template("jm_li_tmpl", {list: data}) );
} else {
//更新
var v;
var $lastDom = $('#doing_list li.zd');
$lastDom = $lastDom.length? $($lastDom[$lastDom.length-1]) : undefined;
for(var i=data.length-1; i>=0; i--){
v = data[i];
var $dom = $('#doing_list li[data-code="'+ v.code +'"]');
if($dom.length){
if($dom.attr('data-timestamp')==v.timestamp){
$dom.find('.last-content').text(v.content);
$dom.find('.red-dot').text(v.count).toggle(v.count>0&&v.my_role==0);
} else {
$dom.remove();
$lastDom ? $lastDom.after( template('jm_li_tmpl', {list: [v]}) )
: $doingViewList.prepend( template('jm_li_tmpl', {list: [v]}) );
}
} else {
$lastDom ? $lastDom.after( template('jm_li_tmpl', {list: [v]}) )
: $doingViewList.prepend( template('jm_li_tmpl', {list: [v]}) );
}
}
if($('#doing_list:hidden').length){
$doingViewList.show();
$(".lin-search").show();
$(".l-pullDown").show();
}
}
if(isPullDown){
loadPhoto();
myScroll.endPullDownToRefresh();
}else{
myScroll.refresh();
loadPhoto();
}
}
},
//获取非咨询中居民未读消息
fzxzData = function(){
sendPost("/doctor/message/messages",{}, null, function(res){
if(res.status == 200){
var data = res.data;
var xxWD = JSON.parse(data.imMsgCount);
var _xxWD = xxWD.patientEnd;
if(_xxWD>0&&_xxWD<99){
$('.weXX').html(_xxWD);
}else if(_xxWD>99){
$(".weXX").html('99+');
}else{
$('.Xweizhi').hide();
}
}else{
}
},'POST','',true);
}
getData = function(isInit, isPullDown) {
var count = 0;
var resultArr = [];
imClient.Sessions.getSessionsWithPatient(docInfo.code, 1, 2000, 0, function(rs){
$('#doing_list').find('li').not('li[data-type="link"]').remove();
var d, isNoRs = true;
for(var k in rs){
d = rs[k];
if(d.business_type==2 && d.type!=0 && d.type!=3 && d.last_content_type!="7"){
isNoRs = false;
var dateStr = asessTime(d.create_date);
var p = {
count: d.unread_count || 0,
time: d.last_content==""? "" : dateStr,
code: d.id,
name: d.name,
last_content:formatMsg( d.last_content, d.last_content_type,d.type),
timestamp: d.create_date,
photo:"",
type: d.type,
my_role: d.my_role,
sex:d.sender_sex,
birthday:d.sender_birthday,
senderName:d.sender_name,
userLevel:docInfo.level,
is_invite:d.is_invite,
last_content_type:d.last_content_type=="5"?JSON.stringify(d.last_content_type):d.last_content_type
}
if(p.my_role != "1"){ //旁听的消息未读数不记录count
count += p.count;
}
resultArr.push(p);
}
}
if(isNoRs){
render([], isInit, isPullDown);
} else {
render(soreByTimeStamp(resultArr), isInit, isPullDown);
//刷新未读消息数
sendPost("/doctor/message/messages",{}, null, function(res){
if(res.status == 200){
var data = res.data;
var xxWD = data.imMsgCount?JSON.parse(data.imMsgCount):'';
mui.fire(plus.webview.getWebviewById("xiaoxi.html"), "imMsgCount", {patient: xxWD.patient,patientEnd:xxWD.patientEnd,doctor:xxWD.doctor});
}
})
}
$('#doing_list').find('li[data-type="link"]').remove();
$('#doing_list').prepend($(".div-fzxzjm").html());
}, function(msg){
console.error("e:"+JSON.stringify(msg))
errHandle(isPullDown);
})
},
loadPhoto = function(){
$.each($('#doing_list li[data-load!="1"]'), function(i, v) {
imClient.Sessions.getParticipantsAvatars($(v).attr('data-code'), function(rs){
$(v).attr('data-load', 1);
if(rs.length>0){
var $photo = $(v).find('.c-avatar-m');
if($(v).attr('data-type')==2){
for(var k in rs){
if(docInfo.code != rs[k].id){
$photo.html('');
break;
}
}
} else {
$photo.html(setPhoto(rs));
}
}
}, function(){
})
});
},
errHandle = function(isPullDown){
if(isPullDown) myScroll.endPullDownToRefresh();
mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
},
soreByTimeStamp = function(resultArr){
orginalData = resultArr;
var resArr = [];
if(!isEmpty(msg_del_list)){
_.each(resultArr,function(v){
var isAdd = false;
_.each(msg_del_list,function(val){
if(msg_del_list[v.code]!=v.timestamp.toString()){
if(!isAdd){
resArr.push(v);
isAdd = true;
}
}
})
})
}else{
resArr = resultArr;
}
var aArr = [];
var bArr = [];
for(var x=0;x=0;i--){
if(zdList[i]==resultArr[x].code){
resultArr[x].isZD = true;
aArr.push(resultArr[x]);
break;
}
}
}
_.chain(resArr)
// 根据date日期字段排序
.sortBy("timestamp").reverse()
.each(function(v){
if(!(_.contains(zdList,v.code))){
bArr.push(v);
}
});
var formatResult = aArr.concat(bArr);
return formatResult;
},
isEmpty = function(value){
return (Array.isArray(value) && value.length === 0)
|| (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
},
// 绑定页面事件
bindEvents = function () {
/**
* 搜索按钮点击
*/
$('.lin-search').on('tap', function() {
mui.fire(searchPage, "initSearch", null);
searchPage.show();
})
$('#doing_list').on('tap','li',function() {
var $this = $(this),
code = $this.attr('data-code'),
name = $this.find('.c-name').html(),
type = $this.attr('data-type');
if(type=="link"){
openWebview($this.attr('data-html'))
} else if(type == '8'){
//续方咨询
openWebview('../../message/html/xufangzixun.html',{
type: type,
sessionId: code,
sessionName: name
})
}
else{
openWebview("../../message/html/p2dzixun.html",{type: type, sessionId: code, sessionName: name});
}
})
/**
* 取消左移块的事件
*/
$('body').on('touchstart', function(e){
if($scrollDom){
$scrollDom.animate({left: '0px'}, "fast", undefined, function(){
if($zdDom){
var code = $zdDom.closest('li').prependTo("#doing_list").addClass("zd").attr('data-code');
$zdDom.html("取消
置顶").removeClass("zdan").addClass("qxzdan");
$zdDom = undefined;
var newList = [];
if(zdList){
for(var i=0; i 0)
return $d;
}
return false;
}
function formatMsg(msg, last_content_type,type){
switch(parseInt(last_content_type)){
case 0:
case 1: return msg;
case 2: return "[图片]";
case 3: return "[语音]";
case 4:
msg = msg.replace(/\n/g, "\\n");
msg = msg.replace(/\r/g, "\\r");
msg = msg.replace(/\t/g, "\\t");
msg = JSON.parse(msg);
var msgType = msg.type;
if(msgType==0){
return "[健康教育]";
}else if(msgType==1){
return "[健康记录]";
}else{
return "[健康指导]";
}
case 5:
msg = JSON.parse(msg);
return "已向"+msg.doctor_name+"求助";
case 6:
if(type==5){
msg = msg.replace(/\n/g, "\\n");
msg = msg.replace(/\r/g, "\\r");
msg = msg.replace(/\t/g, "\\t");
msg = JSON.parse(msg);
return msg.title;
}
return msg;
case 7: return msg;
case 11: return msg;
case 12: return "[视频]";
case 13: return msg;
case 14: return msg;
case 15: return msg?JSON.parse(msg).result:'';
case 16: return function(){
if(msg){
var obj = JSON.parse(msg);
return obj.needUpload == 'true'?obj.tzAlert:obj.tzMsg
}else{
return ''
}
}();
case 17: return msg?JSON.parse(msg).doctorremind:'';
default: return "";
}
}
/**
* 往指定元素后面添加
*/
function appendAfterZd($lastZd, appendDom){
if($lastZd != false){
$lastZd.after(appendDom);
} else {
$('#doing_list').prepend(appendDom);
}
}
function asessTime(timestamp){
var today = new Date().format('yyyy-MM-dd');
var date = new Date();
date.setTime(timestamp);
var dateStr = date.format('yyyy-MM-dd');
if(!today.substring(0,4) == dateStr.substring(0,4)){
return dateStr;
}
if(today == dateStr){
return date.format('hh:mm');
}
return date.format('MM-dd');
}
function calcAge(timestamp){
var date = new Date(timestamp);
Y = date.getFullYear() + '-';
M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
D = date.getDate() < 10 ?'0'+date.getDate():date.getDate();
var birthday = Y+M+D;
var r = birthday.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
var Y = new Date().getFullYear();
if((Y-r[1])<=0){
return "<1";
}
return (Y-r[1]);
}
}
window.addEventListener("update", function(){
getData(false, false);
fzxzData();
});
window.addEventListener("xiaoxiUpdate", function(){
getData(false, false);
});