searchhuanzhe.js 12 KB

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