searchhuanzhe.js 10 KB

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