// TODO 临时构造plus对象,使得能够在浏览器中进行调试 //var plus = null; // 登录者相关信息(包括userAgent) var loginerInfo = null, // 基础环境信息(包括当前webview) baseEnv = null, self = null, myScroll, pageType, imMessages, docInfo = null; // 搜索框 var $searchbar = $('.searchbar'), // 搜索输入框 $searchbarInput = $('.lin-search-ipt input'), // 搜索取消按钮 $searchCancelBtn = $('.searchbar-cancel'), // 搜索框下面悬浮的搜索提示 $searchSuggest = $('#search_suggest_text'), // 搜索结果展示容器 $searchtResult = $('#search_result'), // 搜索无结果时显示 $noResultWrap = $('#no_result_wrap'), $doingWrapper = $('#doing_wrapper'), $doingList = $('#doing_list'); mui.plusReady(function() { initKeyboardAndroid(); self = plus.webview.currentWebview(); docInfo = JSON.parse(plus.storage.getItem("docInfo")); myScroll = $('#search_result').lscroll(); }); document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); window.addEventListener("initSearch", function(e){ imMessages = e.detail.imMessages; openSoftKeyboard(); pageType = e.detail.pageType; setTimeout(function(){ $doingList.html(""); $("#searchPut").val(e.detail.kw); // $("#searchPut").focus(); $searchSuggest.trigger("click"); },500) }) // 获取登录相关信息 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; }); }, // $el: $('.c-content-warp') getRowProps = function ($el) { var $textEllipsis = $el.find('.j-text-ellipsis'), $text = $textEllipsis.eq(0), $chart = $text.text('a'), enWidth = $chart.width(), $chart = $text.text('中'), zhWidth = $chart.width(), lineHeight = parseFloat($chart.css("lineHeight"), 10), rowHeight = $chart.height(); $chart.text(''); return { chartWidth: { zh: zhWidth, en: enWidth }, rowHeight: Math.max(rowHeight, lineHeight), rowWidth: $el.width() }; }, replaceAll = function (text, arr) { var html = text; _.each(arr,function(kw) { var reg = new RegExp(kw+"(?!>)","gi"); html = html.replace(reg,''+kw+''); }); return html; }, highlineKeyword = function ($el,searchText) { var props = getRowProps($el), chartWidth = props.chartWidth, rowHeight = props.rowHeight, rowWidth = props.rowWidth, // 每行显示字符数(以中文字符为标准计算) chartNum = Math.floor(rowWidth / chartWidth.zh), // 排除“咨询问题:”字符及前后“...”所占宽度 exceptNum = 7, // 行数 rowNum = 1, // 预计显示总字符数 expectedNum = chartNum * rowNum - exceptNum, $target = $el.find('.j-text-ellipsis'), // 目标文本 text = $target.attr('data-text').trim(), length = text.length, // 关键字数组 kws = searchText.trim().replace(/\s+/g," ").split(" "); var fidx = 0,preFidx,diff = 0; $target.html(replaceAll(text, kws)); if(Math.ceil($target.height() / rowHeight) <= rowNum) { return ; } if(text.length > expectedNum) { fidx = text.indexOf(kws[0])+kws[0].length-1; diff = fidx - expectedNum + 1; preFidx = fidx; diff = (diff<0)?0:diff; var preChar = (diff>0)?"...":""; $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"..."); while((Math.ceil($target.height() / rowHeight) <= rowNum) && (preFidx < length)) { preFidx++; $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"..."); } if(preFidx == length && (Math.ceil($target.height() / rowHeight) <= rowNum)) { diff = diff>0?(diff - 1):0; $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)); } else if((Math.ceil($target.height() / rowHeight) > rowNum)) { $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"..."); } } }, ellipsisText = function ($elements, searchText) { //$el.ellipsis({ row: 2}); _.each($elements,function(el) { highlineKeyword($(el),searchText) }); }, // 控制搜索关键字悬浮提示的显示 showSearchSuggest = function(text) { var suggestText = '搜索“'+text+'”'; // 如果text不为空,则显示;否则隐藏 if(text&&text.trim().length) { $searchSuggest.text(suggestText); $searchSuggest.show(); } else { $searchSuggest.text(''); $searchSuggest.hide(); } }, // 初始化视图列表 initDoingViewList = function(list) { var searchText = $searchbarInput.val().trim(); template.helper("setAge", function(timestamp) { return calcAge(timestamp); }); $doingWrapper.hide(); var html = ""; if(pageType=="1"){ $(".locate-city").html("居民"); html = template("qyjm_li_tmpl", {list: list.users || []}); }else{ $(".locate-city").html("医生"); html = template("yisheng_li_tmpl", {list: list.users || []}); } $doingList.html(html); $doingWrapper.show(); myScroll.refresh(); ellipsisText($doingList.find('.c-content-warp'),searchText); plus.nativeUI.closeWaiting(); }, // 搜索框搜索执行方法 search = function () { plus.nativeUI.showWaiting(); var kw = $searchbarInput.val().trim(); // imMessages有值表示转发中的搜索 var excludeTopicEndedSessions = imMessages?false:"";//excludeTopicEndedSessions:(空值表示查询所有,false表示查询咨询中的居民,true表示查询咨询已结束的居民) if(pageType=="1"){//1 居民 2医生,,,"搜索居民"搜索结果列表数据 imClient.Search.Patient.searchPatient(docInfo.code ,kw, excludeTopicEndedSessions, 1, 100, function(res){ if(res.users.length==0) { $noResultWrap.show(); $searchtResult.hide(); plus.nativeUI.closeWaiting(); } else { $noResultWrap.hide(); $searchtResult.show(); $doingWrapper.show(); initDoingViewList(res); } }); }else{// "搜索医生"搜索结果列表数据 imClient.Search.Doctor.searchDoctors(docInfo.code ,kw, excludeTopicEndedSessions , 1, 100, function(res){ if(res.users.length==0) { $noResultWrap.show(); $searchtResult.hide(); plus.nativeUI.closeWaiting(); } else { $noResultWrap.hide(); $searchtResult.show(); $doingWrapper.show(); initDoingViewList(res); } }, function(msg){ console.error(msg); mui.toast("搜索失败!"); }) } }, calcAge = function(idcard){ var age = 0; if (!idcard) { return age; } var birth = ""; if (idcard.length == 18) { birth = idcard.substring(6, 14); } else if (idcard.length == 15) { birth = "19" + idcard.substring(6, 12); } var year = parseInt(birth.substring(0, 4)); var month = parseInt(birth.substring(4, 6)); var day = parseInt(birth.substring(6)); var cal = new Date(); age = cal.getFullYear() - year; //周岁计算 if ((cal.getMonth()+1) < (month - 1) || ((cal.getMonth()+1) == (month - 1) && cal.getDate() < day)) { age--; } return age.toString(); }, // 绑定页面事件 bindEvents = function () { $searchbarInput.on('input', function() { var text = $(this).val().trim(); $searchtResult.hide(); $('#no_result_wrap').hide(); showSearchSuggest(text); }).on('keydown',function(e) { if (e.which === 13) { search(); } }); $('.lin-search-ipt a,.div-back').on('tap', function(){ plus.webview.currentWebview().close(); }) $searchSuggest.on('click',function() { $searchSuggest.hide(); search(); $searchtResult.show(); }); $searchtResult.on('tap','li',function() { if(imMessages){//转发消息 var oCode = $(this).attr("data-code"); mui.confirm("是否确认将消息逐条转发给"+$(this).attr("data-name")+"?", "提示", ["不了,谢谢", "确认转发"], function(e) { if(e.index == 0) { } else { Promise.all(_.map(imMessages,function(msg){ return new Promise(function(resolve, reject) { imClient.Sessions.sendMessage(oCode, docInfo.code, docInfo.name, msg.content,msg.type, function(res){ resolve(true); }, function(msg){ resolve(true); }) }) })).then(function() { mui.toast("转发成功"); mui.fire(self.opener().opener().opener(), "revertZf"); self.opener().opener().close(); self.opener().close(); self.close(); }).catch(function(e){ console.log(e) }) } }) }else{ var $this = $(this), code = $this.attr('data-code'), name = $this.attr('data-name'), photo = $this.attr('data-photo'), sex = $this.attr('data-sex'), type = $this.attr('data-type'); session_type = $this.attr('data-stype'); if(session_type == '8'){ //续方咨询 openWebview('../../message/html/xufangzixun.html',{ type: type, sessionId: code, sessionName: name }); return false; } if(pageType=="1"){//1 居民 2医生 openWebview("../../message/html/p2dzixun.html",{type: type, sessionId: code, sessionName: name}); }else{ openWebview("../../message/html/p2p.html",{sessionId: code, sessionName: name}); } } }); }; $(function(){ // $searchbarInput.val('').focus(); $searchtResult.hide(); }) // 页面业务处理流程开始 new Promise(function(resolve, reject) { // TODO 临时放开 // resolve(true); mui.plusReady(function() { // hrefhrefplus已经准备好,可以往下执行 resolve(true); }); }).then(function() { // 获取基础环境信息 return getBaseEnvPromise().then(function(env) { baseEnv = env; }).then(function() { // 获取登录医生信息 loginerInfo = getLoginerInfo(); var self = self = plus.webview.currentWebview(); imMessages = self.imMessages; openSoftKeyboard(); pageType = self.pageType; $("#searchPut").val(self.kw); // 绑定页面事件 bindEvents(); $searchSuggest.trigger("click"); }) }) .catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); template.helper('getImgUrl', function(str){ return getImgUrl(str); })