123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573 |
- // 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 = "ys_zdList_"+ docInfo.code;
- msg_del_list_key = "ys_msg_del_list_"+ docInfo.code;
- msg_cache_key = "ys_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('sousuoyisheng');
- if(searchPage){
- searchPage.hide();
- }else{
- searchPage = mui.preload({
- id:"sousuoyisheng",
- url: 'sousuoyisheng.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;
- 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;height: 50px;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{
- images+='<div class="div-image" style="'+cssArr[i]+'">'+
- '<img src="../../../images/d-default.png"/>'+
- '</div>';
- }
- }
- if(groupPhoto.length==0){
- images+='<div class="div-image" style="'+cssArr[0]+'">'+
- '<img src="../../../images/d-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(true, false);
- }
- },
- render= function(data, isInit, isPullDown){
- plus.storage.setItem(msg_cache_key, JSON.stringify(data));
- if(data.length==0){
- $doingViewList.hide();
- $noResultWrap.show();
- $(".lin-search").hide();
- myScroll.refresh();
- myScroll.endPullDownToRefresh();
- mui.later(function(){
- $(".l-pullDown").hide();
- }, 200)
- } else {
- if(isInit){
- //初始化
- $doingViewList.html( template("ys_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('ys_li_tmpl', {list: [v]}) )
- : $doingViewList.prepend( template('ys_li_tmpl', {list: [v]}) );
- }
- } else {
- $lastDom ? $lastDom.after( template('ys_li_tmpl', {list: [v]}) )
- : $doingViewList.prepend( template('ys_li_tmpl', {list: [v]}) );
- }
- }
-
- if($('#doing_list:hidden').length){
- $doingViewList.show();
- $noResultWrap.hide();
- $(".lin-search").show();
- $(".l-pullDown").show();
- }
-
- if(isPullDown)
- myScroll.endPullDownToRefresh();
- else
- myScroll.refresh();
- }
- loadPhoto();
- }
- },
- getData = function(isInit, isPullDown) {
- var count = 0;
- var resultArr = [];
- imClient.Sessions.getSessionsWithDoctor(docInfo.code, 1, 2000, function(rs){
- var d, isNoRs = true;
- for(var k in rs){
- d = rs[k];
- if(d.business_type==1 && d.type!=0 && d.type!=1 && d.name!="系统消息"){
- 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),
- timestamp: d.create_date,
- photo:"",
- type: d.type, //2:p2p, 3:group
- my_role: d.my_role
- }
- 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});
- }
- })
- }
- }, function(msg){
- errHandle(isPullDown);
- console.error("e:"+JSON.stringify(msg))
- })
- },
- 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('touchstart', 'div.zdan', function(){ // 置顶按钮
- $zdDom = $(this);
- })
- .on('touchstart', 'div.qxzdan', function(){ // 取消置顶按钮
- var $li = $(this).closest('li');
- var code = $li.attr('data-code');
- zdList = _.without(zdList,code);
- plus.storage.setItem(zdListKey, JSON.stringify(zdList));
- var data = soreByTimeStamp(orginalData);
- var v;
- for(var i=0; i<data.length; i++){
- v =data[i];
- if(v.code == code){
- $li.remove();
- var $aLi = $doingViewList.find("li:eq("+ i +")");
- if($aLi.length>0){
- $aLi.before( template('ys_li_tmpl', {list: [v]}) );
- } else {
- $doingViewList.append( template('ys_li_tmpl', {list: [v]}) );
- }
- break;
- }
- }
- })
- .on('touchstart', 'div.scan', function(){ // 删除按钮
- var $li = $(this).closest('li');
- var code = $li.attr('data-code');
- msg_del_list[code] = $li.attr('data-timestamp');
- var newList = [];
- if(zdList){
- for(var i=0; i<zdList.length; i++){
- if(zdList[i] != code){
- newList.push(zdList[i]);
- }
- }
- }
- zdList = newList;
- plus.storage.setItem(zdListKey, JSON.stringify(zdList));
- saveMsgDelList();
- $li.remove();
- myScroll.refresh();
- return false;
- })
- .on('touchstart', 'li', function(e){ //左滑动作开始
- e.preventDefault();
- startScrollX = e.originalEvent.targetTouches[0].screenX;
- })
- .on('touchmove', 'li', function(e){ //左滑动作移动
- if(xScroll != true) return;
- e.preventDefault();
- scrollX = e.originalEvent.targetTouches[0].screenX - startScrollX;
- var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-70);
- if(scrollX<0 && scrollX >= offLeft)
- $(e.currentTarget).css('left', scrollX + 'px');
- })
- .on('touchend', 'li', function(e){ //左滑动作结束
- e.preventDefault();
- if(scrollX<-50){
- $scrollDom = $(e.currentTarget);
- var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-70);
- $scrollDom.animate({left: offLeft}, "fast");
- } else{
- startScrollX = undefined;
- $scrollDom = undefined;
- $(e.currentTarget).animate({left: '0px'}, "fast");
- }
- scrollX = undefined;
- xScroll = false;
- myScroll.options.isLeftScroll = false;
- })
- */
- .on('tap','li',function() {
- var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
- if(userAgent.observer){
- mui.toast("观察者模式无法发送消息");
- }else{
- var $this = $(this),
- code = $this.attr('data-code'),
- name = $this.attr('data-name'),
- type = $this.attr('data-type');
- if(type==2){//医生对医生1对1
- openWebview("../../message/html/p2p.html",{sessionId: code, sessionName: name, type:"doctor-to-doctor"});
- }else{
- openWebview("../../message/html/tuanduiqunliao.html",{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();
-
- 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() {
- var self = this;
- setTimeout(function() {
- getData(false, true);
- }, 1000);
- 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));
- }
- /**
- * 获取最后一个置顶元素,如无置顶元素,返回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, type){
- switch(parseInt(type)){
- 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 = JSON.parse(msg);
- var msgType = msg.type || 0;
- if(msgType==0){
- return "[健康教育]";
- }else if(msgType==1){
- return "[健康记录]";
- }else{
- return "[健康指导]";
- }
- case 5:
- msg = JSON.parse(msg);
- return "已向"+msg.doctor_name+"求助";
- case 6: return msg;
- case 7: return msg;
- case 12: return "[视频]";
- case 13: return msg;
- }
- }
- /**
- * 往指定元素后面添加
- */
- 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');
- }
- window.addEventListener("update", function(){
- getData(false, false);
- });
- window.addEventListener("xiaoxiUpdate", function(){
- getData(false, false);
- });
|