searchdoctor.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. up : {
  223. height:50,
  224. contentinit: '',
  225. contentdown: '',
  226. contentrefresh : "正在加载...",
  227. contentnomore:'没有更多数据了',
  228. callback: function() {
  229. var self = this;
  230. page++;
  231. searchByPaging(true,function(value){
  232. setTimeout(function(){
  233. self.endPullupToRefresh(value);
  234. },500)
  235. })
  236. }
  237. }
  238. }
  239. })
  240. $('#his_scroll').css({
  241. 'height':$(window).height()-158
  242. })
  243. mui('#his_scroll').scroll({
  244. bounce: true, //是否启用回弹
  245. })
  246. $searchbarInput.on('input', function() {
  247. var text = $(this).val().trim();
  248. $searchtResult.hide();
  249. $('#no_result_wrap').hide();
  250. if(text.length > 0){
  251. $("#searchAll").hide();
  252. $history.hide();
  253. }else{
  254. $("#searchAll").show();
  255. $history.show();
  256. }
  257. showSearchSuggest(text);
  258. }).on('keydown',function(e) {
  259. if (e.which === 13) {
  260. page = 0;
  261. $searchSuggest.hide();
  262. searchByPaging(false);
  263. //保存历史数据20个
  264. saveHisData();
  265. }
  266. });
  267. $('#clear_his_data').on('tap', function(){
  268. plus.storage.removeItem("doctorSearchHis"+docInfo.code);
  269. historyList();
  270. $history.hide();
  271. })
  272. $hisSearList.on('click','.his-list',function(){
  273. var $that = $(this);
  274. page = 0;
  275. $("#searchAll").hide();
  276. $searchSuggest.hide();
  277. $history.hide();
  278. $searchbarInput.val($that.text())
  279. searchByPaging(false);
  280. })
  281. $('.lin-search-ipt a').on('tap', function(){
  282. plus.webview.currentWebview().hide();
  283. mui.later(function(){
  284. $('#search_result').hide();
  285. $('#no_result_wrap').hide();
  286. $('#searchPut').val("");
  287. $("#search_suggest_text").html("").hide();
  288. $("#searchPut").blur();
  289. }, 50)
  290. })
  291. $searchSuggest.on('click',function() {
  292. page = 0;
  293. $searchSuggest.hide();
  294. searchByPaging(false);
  295. //保存历史数据20个
  296. saveHisData()
  297. });
  298. $patiList.on('tap','li[data-code]',function(e) {
  299. var code = $(this).attr("data-code");
  300. var name = $(this).attr("data-name");
  301. mui.openWindow('../../mine/html/zuyuanziliao.html', 'zuyuanziliao', {
  302. extras: {
  303. docCode: code,
  304. docName:name,
  305. teamCode: docInfo.adminTeamCode,
  306. // isTeamOwner: isTeamOwner
  307. }
  308. });
  309. return false;
  310. })
  311. function saveHisData(){
  312. //保存历史数据20个
  313. var value = $searchbarInput.val().trim();
  314. if(value){
  315. if(historyData.length>19){
  316. historyData.pop();
  317. }
  318. if(historyData.indexOf(value)>-1){
  319. historyData = _.filter(historyData, function(o){ return o != value; });
  320. }
  321. historyData.unshift(value);
  322. var str = JSON.stringify(historyData)
  323. plus.storage.setItem("doctorSearchHis"+docInfo.code,str);
  324. historyList();
  325. }
  326. }
  327. $searchCancelBtn.on('click',function() {
  328. mui.back()
  329. })
  330. //搜索全部居民的绑定事件
  331. $("#searchAll").on('click', function(){
  332. openWebview("search_all.html");
  333. });
  334. };
  335. // 页面业务处理流程开始
  336. new Promise(function(resolve, reject) {
  337. mui.plusReady(function() {
  338. resolve(true);
  339. })
  340. }).then(function() {
  341. // 获取基础环境信息
  342. return getBaseEnvPromise().then(function(env) {
  343. baseEnv = env;
  344. }).then(function() {
  345. // 获取登录基本信息
  346. baseInfo = getBaseInfo();
  347. self = plus.webview.currentWebview();
  348. $searchbar.searchBar();
  349. // openSoftKeyboard();
  350. $searchbarInput.focus();
  351. historyList();
  352. if(historyData.length){
  353. $history.show();
  354. }
  355. // 绑定页面事件
  356. bindEvents();
  357. })
  358. }).catch(function(e) {
  359. plus.nativeUI.closeWaiting();
  360. console && console.error(e);
  361. });