searchPatSpecail.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview)
  4. baseEnv = null;
  5. // 搜索框
  6. var $searchbar = $('.searchbar'),
  7. // 搜索输入框
  8. $searchbarInput = $('.lin-search-ipt input'),
  9. // 搜索取消按钮
  10. $searchCancelBtn = $('.searchbar-cancel'),
  11. // 搜索框下面悬浮的搜索提示
  12. $searchSuggest = $('#search_suggest_text'),
  13. // 搜索结果展示容器
  14. $searchtResult = $('#search_result'),
  15. // 搜索无结果时显示
  16. $noResultWrap = $('#no_result_wrap'),
  17. $patiList = $('#pati_list'),
  18. // 分页查询最后一页
  19. $history = $('#history_search'),//历史搜索
  20. $hisSearList = $('#his_sear_list');
  21. var page = 1,
  22. search_keyword = "", //记录搜索框的内容
  23. lastTeamId,// 从缓存取出所选团队
  24. pagesize = 15,
  25. historyData=[],//历史数据
  26. historyStr;
  27. var self;
  28. var startScrollX,
  29. scrollXd,
  30. $scrollDom;
  31. mui.plusReady(function() {
  32. // initKeyboardAndroid();
  33. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  34. historyStr = plus.storage.getItem("juminSearchHis"+docInfo.code);
  35. if(historyStr){
  36. historyStr = JSON.parse(historyStr).join(',');
  37. historyData = historyStr.split(',');
  38. }else{
  39. historyData = [];
  40. }
  41. })
  42. window.addEventListener("initSearch", function(e){
  43. // openSoftKeyboard();
  44. baseInfo = getBaseInfo();
  45. $("#searchAll").show();
  46. setTimeout(function(){
  47. $("#searchPut").focus();
  48. },100)
  49. if(historyData.length){
  50. $history.show();
  51. }
  52. lastTeamId = plus.storage.getItem("selectedTeamId");
  53. })
  54. // 获取登录相关信息
  55. var getBaseInfo = function() {
  56. // 登录的相关信息
  57. var userAgent = JSON.parse(plus.storage.getItem("userAgent"))
  58. return {
  59. userAgent: userAgent
  60. }
  61. },
  62. // 获取基础环境信息
  63. getBaseEnvPromise = function () {
  64. var env = {
  65. webview: plus.webview.currentWebview()
  66. }
  67. return Promise.resolve().then(function(res) {
  68. return env;
  69. })
  70. },
  71. historyList = function(){
  72. historyStr = plus.storage.getItem("juminSearchHis"+docInfo.code);
  73. if(historyStr){
  74. historyStr = JSON.parse(historyStr).join(',');
  75. historyData = historyStr.split(',');
  76. }else{
  77. historyData = [];
  78. }
  79. var html = template('his_list_tmpl',{list:historyData});
  80. $hisSearList.html('');
  81. $hisSearList.append(html);
  82. },
  83. // 初始化“患者”视图列表
  84. initPatientViewList = function(data,keyword,isAppend) {
  85. var html = template("pati_list_tmpl", {list: _.map(data,function(o) {
  86. o.jsonStr = JSON.stringify(o);
  87. return o;
  88. })});
  89. searchText = $searchbarInput.val().trim();
  90. if(isAppend == true){
  91. $patiList.append(html)
  92. }else{
  93. mui('#jumin_wrapper').pullRefresh().scrollTo(0,0)
  94. $patiList.empty().append(html)
  95. mui('#jumin_wrapper').pullRefresh().refresh(true);//重置
  96. }
  97. ellipsisText($patiList.find('li'),searchText);
  98. },
  99. // $el: $('.c-content-warp')
  100. getRowProps = function ($el) {
  101. var $textEllipsis = $el,
  102. $text = $textEllipsis.eq(0),
  103. $chart = $text.text('a'),
  104. enWidth = $chart.width(),
  105. $chart = $text.text('中'),
  106. zhWidth = $chart.width(),
  107. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  108. rowHeight = $chart.height();
  109. $chart.text('');
  110. return {
  111. chartWidth: {
  112. zh: zhWidth,
  113. en: enWidth
  114. },
  115. rowHeight: Math.max(rowHeight, lineHeight),
  116. rowWidth: $el.width()
  117. };
  118. },
  119. replaceAll = function (text, arr) {
  120. var html = text;
  121. _.each(arr,function(kw) {
  122. var reg = new RegExp(kw+"(?!>)","gi");
  123. html = html.replace(reg,'<em>'+kw+'</em>');
  124. });
  125. return html;
  126. },
  127. highlineKeyword = function ($el,searchText) {
  128. var props = getRowProps($el),
  129. chartWidth = props.chartWidth,
  130. rowHeight = props.rowHeight,
  131. rowWidth = props.rowWidth,
  132. // 每行显示字符数(以中文字符为标准计算)
  133. chartNum = Math.floor(rowWidth / chartWidth.zh),
  134. // 排除指定数量字符所占宽度
  135. exceptNum = 0,
  136. // 行数
  137. rowNum = 1,
  138. // 预计显示总字符数
  139. expectedNum = chartNum * rowNum - exceptNum,
  140. $target = $el,
  141. // 目标文本
  142. text = $.trim($target.attr('data-text')),
  143. length = text.length,
  144. // 关键字数组
  145. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  146. var fidx = 0,preFidx,diff = 0;
  147. $target.html(replaceAll(text, kws));
  148. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  149. return ;
  150. }
  151. if(text.length > expectedNum) {
  152. fidx = text.indexOf(kws[0])+kws[0].length-1;
  153. diff = fidx - expectedNum + 1;
  154. preFidx = fidx;
  155. diff = (diff<0)?0:diff;
  156. var preChar = (diff>0)?"...":"";
  157. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  158. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  159. preFidx++;
  160. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  161. }
  162. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  163. diff = diff>0?(diff - 1):0;
  164. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  165. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  166. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  167. }
  168. }
  169. },
  170. ellipsisText = function ($elements, searchText) {
  171. _.each($elements,function(el) {
  172. var $textEl = $(el).find(".j-text-ellipsis");
  173. _.each($textEl, function(t){
  174. highlineKeyword($(t),searchText)
  175. })
  176. });
  177. },
  178. // 控制搜索关键字悬浮提示的显示
  179. showSearchSuggest = function(text) {
  180. var suggestText = '搜索“'+text+'”';
  181. // 如果text不为空,则显示;否则隐藏
  182. if(text&&text.trim().length) {
  183. $searchSuggest.text(suggestText);
  184. $searchSuggest.show();
  185. } else {
  186. $searchSuggest.text('');
  187. $searchSuggest.hide();
  188. }
  189. },
  190. // 分页查询列表
  191. searchByPaging = function (isAppend,fun) {
  192. $history.hide();
  193. $("#searchPut").blur();
  194. search_keyword = $searchbarInput.val();
  195. if(!$.trim(search_keyword)) {
  196. return ;
  197. }
  198. plus.nativeUI.showWaiting();
  199. var url = "/doctor/specialist/getDoctorPatientByName";
  200. var params = { page:page, doctor: docInfo.code, nameKey: search_keyword, size:pagesize};
  201. getReqPromise(url,params, "GET").then(function(res){
  202. if(res.status == 200) {
  203. if((page==1 && !res.data) || (page==1 && !res.data.length)) {
  204. $searchtResult.hide();
  205. $noResultWrap.show();
  206. } else {
  207. $noResultWrap.hide();
  208. $searchtResult.show();
  209. initPatientViewList(res.data,search_keyword,isAppend);
  210. if(res.data.length < pagesize){
  211. fun&&fun.call(this,true)
  212. }else{
  213. fun&&fun.call(this,false)
  214. }
  215. }
  216. }else{
  217. mui.toast("搜索失败");
  218. }
  219. plus.nativeUI.closeWaiting();
  220. }).catch(function(e) {
  221. plus.nativeUI.closeWaiting();
  222. console && console.error(e)
  223. })
  224. },
  225. // 绑定页面事件
  226. bindEvents = function (){
  227. mui.init({
  228. pullRefresh : {
  229. container:'#jumin_wrapper',
  230. up : {
  231. height:50,
  232. contentinit: '',
  233. contentdown: '',
  234. contentrefresh : "正在加载...",
  235. contentnomore:'没有更多数据了',
  236. callback: function() {
  237. var self = this;
  238. page++;
  239. searchByPaging(true,function(value){
  240. setTimeout(function(){
  241. self.endPullupToRefresh(value);
  242. },500)
  243. })
  244. }
  245. }
  246. }
  247. })
  248. $('#his_scroll').css({
  249. 'height':$(window).height()-132
  250. })
  251. mui('#his_scroll').scroll({
  252. bounce: true, //是否启用回弹
  253. })
  254. $patiList.on('tap','.patient-list',function() {
  255. var code = $.trim($(this).attr("data-group")),
  256. isOpen = togglePatientListShow($(this));
  257. code && !isOpen && initPatientListByGroup(code);
  258. }).on('tap','li[data-patient-code]',function() {
  259. alert($(this).attr("data-patient-code"))
  260. openWebview("../../kfgl/html/denizen.html",{
  261. patiCode: $(this).attr("data-patient-code")
  262. });
  263. });
  264. $searchbarInput.on('input', function() {
  265. var text = $(this).val().trim();
  266. $searchtResult.hide();
  267. $('#no_result_wrap').hide();
  268. if(text.length > 0){
  269. $("#searchAll").hide();
  270. $history.hide();
  271. }else{
  272. $("#searchAll").show();
  273. $history.show();
  274. }
  275. showSearchSuggest(text);
  276. }).on('keydown',function(e) {
  277. if (e.which === 13) {
  278. page = 1;
  279. $searchSuggest.hide();
  280. searchByPaging(false);
  281. //保存历史数据20个
  282. saveHisData();
  283. }
  284. });
  285. $('#clear_his_data').on('tap', function(){
  286. plus.storage.removeItem("juminSearchHis"+docInfo.code);
  287. historyList();
  288. $history.hide();
  289. })
  290. $hisSearList.on('click','.his-list',function(){
  291. var $that = $(this);
  292. page = 1;
  293. $("#searchAll").hide();
  294. $searchSuggest.hide();
  295. $history.hide();
  296. $searchbarInput.val($that.text())
  297. searchByPaging(false);
  298. })
  299. $('.lin-search-ipt a').on('tap', function(){
  300. plus.webview.currentWebview().hide();
  301. mui.later(function(){
  302. $('#search_result').hide();
  303. $('#no_result_wrap').hide();
  304. $('#searchPut').val("");
  305. $("#search_suggest_text").html("").hide();
  306. $("#searchPut").blur();
  307. }, 50)
  308. })
  309. $searchSuggest.on('click',function() {
  310. page = 1;
  311. $searchSuggest.hide();
  312. searchByPaging(false);
  313. //保存历史数据20个
  314. saveHisData()
  315. });
  316. function saveHisData(){
  317. //保存历史数据20个
  318. var value = $searchbarInput.val().trim();
  319. if(value){
  320. if(historyData.length>19){
  321. historyData.pop();
  322. }
  323. if(historyData.indexOf(value)>-1){
  324. historyData = _.filter(historyData, function(o){ return o != value; });
  325. }
  326. historyData.unshift(value);
  327. var str = JSON.stringify(historyData)
  328. plus.storage.setItem("juminSearchHis"+docInfo.code,str);
  329. historyList();
  330. }
  331. }
  332. $searchCancelBtn.on('click',function() {
  333. mui.back()
  334. })
  335. //搜索全部居民的绑定事件
  336. $("#searchAll").on('click', function(){
  337. openWebview("search_all.html");
  338. });
  339. $(".c-list").on("tap", ".operate", function(e) {
  340. e.preventDefault();
  341. e.stopPropagation();
  342. openWebview("../../zhuanbing/html/fenpeijianguanshi.html",{
  343. patiCode: $(this).attr("data-patient")
  344. })
  345. }).on('touchstart', '.n-list-link', function(e){ //左滑动作开始
  346. e.preventDefault();
  347. startScrollX = e.originalEvent.targetTouches[0].screenX;
  348. })
  349. .on('touchmove', '.n-list-link', function(e){ //左滑动作移动
  350. e.preventDefault();
  351. scrollXd = e.originalEvent.targetTouches[0].screenX - startScrollX;
  352. var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-65);
  353. if(scrollXd<0 && scrollXd >= offLeft)
  354. $(e.currentTarget).css('left', scrollXd + 'px');
  355. })
  356. .on('touchend', '.n-list-link', function(e){ //左滑动作结束
  357. e.preventDefault();
  358. if(scrollXd<-50){
  359. $scrollDom = $(e.currentTarget);
  360. var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-65);
  361. $scrollDom.animate({left: offLeft}, "fast");
  362. } else{
  363. startScrollX = undefined;
  364. $scrollDom = undefined;
  365. $(e.currentTarget).animate({left: '0px'}, "fast");
  366. }
  367. scrollXd = undefined;
  368. xScroll = false;
  369. // myScroll.options.isLeftScroll = false;
  370. })
  371. };
  372. // 页面业务处理流程开始
  373. new Promise(function(resolve, reject) {
  374. mui.plusReady(function() {
  375. resolve(true);
  376. })
  377. }).then(function() {
  378. // 获取基础环境信息
  379. return getBaseEnvPromise().then(function(env) {
  380. baseEnv = env;
  381. }).then(function() {
  382. // 获取登录基本信息
  383. baseInfo = getBaseInfo();
  384. lastTeamId = plus.storage.getItem("selectedTeamId");
  385. self = plus.webview.currentWebview();
  386. $searchbar.searchBar();
  387. // openSoftKeyboard();
  388. $searchbarInput.focus();
  389. historyList();
  390. if(historyData.length){
  391. $history.show();
  392. }
  393. // 绑定页面事件
  394. bindEvents();
  395. })
  396. }).catch(function(e) {
  397. plus.nativeUI.closeWaiting();
  398. console && console.error(e);
  399. });