searchhuanzhe.js 10 KB

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