searchdoctor.js 9.6 KB

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