123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587 |
- // 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+='<div class="div-image" style="'+cssArr+'">'+
- '<img src="'+getImgUrl(groupPhoto[i].avatar)+'"/>'+
- '</div>';
- 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+='<div class="div-image" style="'+cssArr[i]+'">'+
- '<img src="'+ getImgUrl(groupPhoto[i].avatar)+'"/>'+
- '</div>';
- }else{
- if(groupPhoto[i].role=="doctor"){
- images+='<div class="div-image" style="'+cssArr[i]+'">'+
- '<img src="../../../images/d-default.png"/>'+
- '</div>';
- }else{
- images+='<div class="div-image" style="'+cssArr[i]+'">'+
- '<img src="../../../images/p-default.png"/>'+
- '</div>';
- }
-
- }
- }
- 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('<img src="'+ getImgUrl(rs[k].avatar) +'">');
- 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<resultArr.length;x++){
- resultArr[x].isZD = false;
- for(var i=zdList.length-1;i>=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("取消<br/>置顶").removeClass("zdan").addClass("qxzdan");
- $zdDom = undefined;
- var newList = [];
- if(zdList){
- for(var i=0; i<zdList.length; i++){
- if(zdList[i] != code){
- newList.push(zdList[i]);
- }
- }
- newList.push(code);
- } else {
- newList = [code];
- }
- zdList = newList;
- plus.storage.setItem(zdListKey, JSON.stringify(zdList));
- }
- });
- return false;
- }
- }).on('touchmove', function(e){
- e.preventDefault();
- })
-
- };
- // 页面业务处理流程开始
- new Promise(function(resolve, reject) {
- // TODO 临时放开
- //resolve(true);
- mui.plusReady(function() {
- // plus已经准备好,可以往下执行
- resolve(true);
- });
- }).then(function() {
-
- // 获取基础环境信息
- return getBaseEnvPromise().then(function(env) {
- baseEnv = env;
- }).then(function() {
- // 获取登录医生信息
- loginerInfo = getLoginerInfo();
- loaded();
- initPage();
- fzxzData();
- bindEvents();// 绑定页面事件
- })
- }).catch(function(e) {
- plus.nativeUI.closeWaiting();
- console && console.error(e);
- });
- var xScroll = 0;
- var yOff = 0, xOff = 0, myScroll;
- var $scrollDom, $zdDom;
- /**
- * 初始化iscroll
- */
- function loaded () {
- var deceleration = mui.os.ios?0.003:0.0009;
- mui('.mui-scroll-wrapper').scroll({
- bounce: false,
- indicators: true, //是否显示滚动条
- deceleration:deceleration
- });
-
- myScroll = mui(".mui-scroll").pullToRefresh({
- down: {
- callback: function() {
- getData(false, true);
- var zkHome = plus.webview.getWebviewById("home1.html");
- if(zkHome) {
- mui.fire(zkHome,'refreshBaseInfo');
- }
- }
- },
- scrollLeft: function(){
- xScroll = true;
- }
- });
- return;
- }
- function saveMsgDelList(){
- plus.storage.setItem(msg_del_list_key, JSON.stringify(msg_del_list));
- }
- template.helper("setAge", function(timestamp) {
- return calcAge(timestamp)+"岁";
- });
- /**
- * 获取最后一个置顶元素,如无置顶元素,返回false
- */
- function getTopDom(){
- var $d;
- for(var i=0; i<zdList.length; i++){
- $d = $('#doing_list li[data-code="'+ zdList[i] +'"]');
- if($d.length > 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);
- });
|