search_jumin_one.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview)
  4. baseEnv = null;
  5. var $searchbarInput = $('#search-input'),
  6. // 搜索取消按钮
  7. $searchCancelBtn = $('.searchbar-cancel'),
  8. // 搜索框下面悬浮的搜索提示
  9. $searchSuggest = $('#searchValWrap'),
  10. // 搜索结果展示容器
  11. $searchtResult = $('#search_result'),
  12. // 搜索无结果时显示
  13. $noResultWrap = $('#no_result_wrap'),
  14. $patiList = $('#pati_list');
  15. var lastId = 1,// 分页查询最后一页
  16. search_keyword = "",
  17. iscroller,// iscroll 滚动条实例
  18. teamId,// 从缓存取出所选团队
  19. typeId,
  20. pagesize = 15,// 每页加载数据量
  21. hasMoreResults = false,
  22. sendCount = 0;//记录发送人数
  23. var origin,//搜索来源位置
  24. chooseDate,
  25. follow_type = 0; //记录随访方式 1-新增随访,2-临时随访
  26. // 获取登录相关信息
  27. var getBaseInfo = function() {
  28. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));// 登录的相关信息
  29. follow_type = baseEnv.webview.follow_type;
  30. var self = plus.webview.currentWebview();
  31. origin = self.origin;
  32. chooseDate = self.chooseDate;
  33. return {
  34. userAgent: userAgent,
  35. teamInfo: JSON.parse(plus.storage.getItem("teamInfo"))
  36. }
  37. },
  38. // 获取基础环境信息
  39. getBaseEnvPromise = function () {
  40. var env = {
  41. webview: plus&&plus.webview.currentWebview()
  42. };
  43. return Promise.resolve().then(function(res) {
  44. return env;
  45. });
  46. },
  47. // 初始化“患者”视图列表
  48. initPatientViewList = function(data,keyword,isAppend) {
  49. var html = template("pati_list_tmpl", {list: _.map(data,function(o) {
  50. o.jsonStr = JSON.stringify(o);
  51. var addr = '';
  52. var id = (o.idcard.indexOf(keyword) != -1) ? o.idcard : "";
  53. if(o.address){
  54. addr = (o.address.indexOf(keyword) != -1) ? o.address : "";
  55. o.labels = addr +" "+ id;
  56. }else{
  57. o.labels = id;
  58. }
  59. return o;
  60. })});
  61. if(isAppend === true) {
  62. $patiList.append(html)
  63. } else {
  64. $patiList.html(html);
  65. }
  66. ellipsisText($patiList.find('li'),keyword);
  67. },
  68. // $el: $('li')
  69. getRowProps = function ($el) {
  70. var $textEllipsis = $el,
  71. $text = $textEllipsis.eq(0),
  72. $chart = $text.text('a'),
  73. enWidth = $chart.width(),
  74. $chart = $text.text('中'),
  75. zhWidth = $chart.width(),
  76. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  77. rowHeight = $chart.height();
  78. $chart.text('');
  79. return {
  80. chartWidth: {
  81. zh: zhWidth,
  82. en: enWidth
  83. },
  84. rowHeight: Math.max(rowHeight, lineHeight),
  85. rowWidth: $el.width()
  86. };
  87. },
  88. replaceAll = function (text, arr) {
  89. var html = text;
  90. _.each(arr,function(kw) {
  91. var reg = new RegExp(kw+"(?!>)","gi");
  92. html = html.replace(reg,'<em>'+kw+'</em>');
  93. });
  94. return html;
  95. },
  96. highlineKeyword = function ($el,searchText) {
  97. var props = getRowProps($el),
  98. chartWidth = props.chartWidth,
  99. rowHeight = props.rowHeight,
  100. rowWidth = props.rowWidth,
  101. // 每行显示字符数(以中文字符为标准计算)
  102. chartNum = Math.floor(rowWidth / chartWidth.zh),
  103. // 排除指定数量字符所占宽度
  104. exceptNum = 0,
  105. // 行数
  106. rowNum = 1,
  107. // 预计显示总字符数
  108. expectedNum = chartNum * rowNum - exceptNum,
  109. $target = $el,
  110. // 目标文本
  111. text = $.trim($target.attr('data-text')),
  112. length = text.length,
  113. // 关键字数组
  114. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  115. var fidx = 0,preFidx,diff = 0;
  116. $target.html(replaceAll(text, kws));
  117. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  118. return ;
  119. }
  120. if(text.length > expectedNum) {
  121. fidx = text.indexOf(kws[0])+kws[0].length-1;
  122. diff = fidx - expectedNum + 1;
  123. preFidx = fidx;
  124. diff = (diff<0)?0:diff;
  125. var preChar = (diff>0)?"...":"";
  126. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  127. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  128. preFidx++;
  129. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  130. }
  131. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  132. diff = diff>0?(diff - 1):0;
  133. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  134. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  135. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  136. }
  137. }
  138. },
  139. ellipsisText = function ($elements, searchText) {
  140. _.each($elements,function(el) {
  141. var $textEl = $(el).find(".j-text-ellipsis");
  142. _.each($textEl, function(t){
  143. highlineKeyword($(t),searchText);
  144. });
  145. });
  146. },
  147. // 分页查询列表
  148. searchByPaging = function () {
  149. search_keyword = $searchbarInput.val();
  150. if(!$.trim(search_keyword)) {
  151. return ;
  152. }
  153. plus.nativeUI.showWaiting();
  154. // page: 分页索引,filter:搜素关键字,pagesize:每页条数
  155. var url = "/doctor/patient_label_info/getPatientByParams",
  156. params = { page:1, filter: search_keyword, pagesize:pagesize, teamCode:teamId};
  157. getReqPromise(url,params,'get').then(function(res){
  158. if(res.status == 200) {
  159. lastId = lastId+1;
  160. if(!res.data || !res.data.length) {
  161. $searchtResult.hide();
  162. $noResultWrap.show();
  163. } else {
  164. if(res.data.length < pagesize) {
  165. hasMoreResults = false;
  166. } else {
  167. hasMoreResults = true;
  168. }
  169. $noResultWrap.hide();
  170. $searchtResult.show();
  171. initPatientViewList(res.data,search_keyword);
  172. }
  173. if(!iscroller) {
  174. iscroller = initScroller($searchtResult,url,
  175. function() { // 传递分页参数
  176. return $.extend({},params,{page:lastId,filter:search_keyword});
  177. },function(res) {
  178. lastId = lastId+1;
  179. search_keyword = $searchbarInput.val();
  180. initPatientViewList(res.data,search_keyword,true);
  181. iscroller.refresh();
  182. });
  183. }
  184. iscroller.refresh();
  185. } else {
  186. plus.nativeUI.toast("搜索失败!");
  187. }
  188. plus.nativeUI.closeWaiting();
  189. }).catch(function(e) {
  190. console && console.error(e)
  191. });
  192. },
  193. // 滚动条分页实例初始化
  194. initScroller = function($el,url,getData,pullUpAction) {
  195. var scroller;
  196. if(hasMoreResults) {
  197. scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  198. var data = getData();
  199. getReqPromise(url,data,'get').then(function(data) {
  200. if(pullUpAction && $.isFunction(pullUpAction)) {
  201. pullUpAction(data);
  202. updatePullUpText(scroller,data.list);
  203. }
  204. })
  205. }});
  206. } else {
  207. scroller = $el.initScroll({pullDown: false,pullUp: false});
  208. }
  209. return scroller;
  210. },
  211. // 更新分页上拉加载的提示文本
  212. updatePullUpText= function(scroller,list) {
  213. var $wrap = $(scroller.wrapper),
  214. $pullupLabel = $wrap.find('.pullUpLabel');
  215. if(!list || !list.length) {
  216. $pullupLabel.text('没有更多');
  217. } else {
  218. $pullupLabel.text('上拉加载更多');
  219. }
  220. scroller.on('refresh',function() {
  221. if(!list || !list.length) {
  222. $pullupLabel.text('没有更多');
  223. } else {
  224. $pullupLabel.text('上拉加载更多');
  225. }
  226. });
  227. },
  228. bindEvents = function(){
  229. $patiList.on('tap','li[data-patient-code]',function() {
  230. var patiInfo = $(this).attr("data-json");
  231. patiInfo = JSON.parse(patiInfo);
  232. if(origin == 'wdyy'){
  233. openWebview("../../wdyy/html/appointment-register.html",{patient:patiInfo.code});
  234. return false;
  235. }
  236. if(origin == 'suifang'){
  237. if(follow_type == 1){
  238. openWebview("../../suifang/html/add_plan.html",{patientInfo: patiInfo,chooseDate:chooseDate});
  239. return false;
  240. }
  241. if(follow_type == 2){
  242. openWebview("../../suifang/html/follow_way.html",{patientInfo: patiInfo});
  243. return false;
  244. }
  245. }
  246. })
  247. $('#cancel').on('click', function(){
  248. mui.back();
  249. })
  250. $searchbarInput.on('input', function(){
  251. var html = $(this).val();
  252. var reg = new RegExp("<"+"(?!>)","gi");
  253. html = html.replace(reg,'&lt');
  254. var reg = new RegExp(">"+"(?!>)","gi");
  255. html = html.replace(reg,'&gt');
  256. $('#searchVal').html(html);
  257. $(".searchbar-clear").toggle($(this).val() != "");
  258. $searchSuggest.toggle($(this).val()!="");
  259. $searchtResult.hide();
  260. $noResultWrap.hide();
  261. $("#sendCount").text(0);
  262. sendCount=0;
  263. }).on('keydown',function(e) {
  264. if (e.which === 13) {
  265. lastId = 1;
  266. $searchSuggest.hide();
  267. searchByPaging();
  268. }
  269. });
  270. $(".searchbar-clear").on('click', function(){
  271. $(this).hide();
  272. $searchbarInput.val("");
  273. $('#searchVal').html("");
  274. $searchSuggest.hide();
  275. });
  276. $searchSuggest.on('click',function() {
  277. lastId = 1;
  278. $searchSuggest.hide();
  279. $("#sendCount").text(0);
  280. sendCount=0;
  281. searchByPaging();
  282. });
  283. };
  284. // 页面业务处理流程开始
  285. new Promise(function(resolve, reject) {
  286. // TODO 临时放开
  287. //resolve(true);
  288. mui.plusReady(function() {
  289. // hrefhrefplus已经准备好,可以往下执行
  290. resolve(true);
  291. initKeyboardAndroid();
  292. });
  293. }).then(function() {
  294. // 获取基础环境信息
  295. return getBaseEnvPromise().then(function(env) {
  296. baseEnv = env;
  297. }).then(function() {
  298. // 获取登录基本信息
  299. baseInfo = getBaseInfo();
  300. teamId = plus.storage.getItem("selectedTeamId");
  301. typeId = plus.storage.getItem("selectedTypeId");
  302. openSoftKeyboard();
  303. $searchbarInput.focus();
  304. // 绑定页面事件
  305. bindEvents();
  306. })
  307. }).catch(function(e) {
  308. plus.nativeUI.closeWaiting();
  309. console && console.error(e);
  310. })