zhuanyijumin_search.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. var baseInfo = null, // 基本信息(包括userAgent)
  2. baseEnv = null,// 基础环境信息(包括当前webview)
  3. self;
  4. var $searchbarInput = $('#search-input'),
  5. $searchSuggest = $('#searchValWrap'),// 搜索框下面悬浮的搜索提示
  6. $searchtResult = $('#search_result'),// 搜索结果展示容器
  7. $noResultWrap = $('#no_result_wrap'),// 搜索无结果时显示
  8. $selectedTeamName = $('#selected_team_name'),// 所选团队的团队名称显示
  9. $patiList = $('#pati_list');
  10. var lastId = 1,// 分页查询最后一页
  11. search_keyword = "",
  12. iscroller,// iscroll 滚动条实例
  13. teamId, // 所选团队
  14. pagesize = 15,// 每页加载数据量
  15. hasMoreResults = false,
  16. sendCount = 0, //记录发送人数
  17. type,//转移或分配
  18. docCode,//医生code
  19. level;//2全科 3健管
  20. // 获取登录相关信息
  21. var getBaseInfo = function() {
  22. var userAgent = JSON.parse(plus.storage.getItem("userAgent")); // 登录的相关信息
  23. self = plus.webview.currentWebview();
  24. docCode = self.docCode;
  25. level = self.level;
  26. type = self.type;
  27. teamId = self.lastTeamId;//团队id
  28. return {
  29. userAgent: userAgent,
  30. teamInfo: JSON.parse(plus.storage.getItem("teamInfo"))
  31. }
  32. },
  33. // 获取基础环境信息
  34. getBaseEnvPromise = function () {
  35. var env = {
  36. webview: plus&&plus.webview.currentWebview()
  37. };
  38. return Promise.resolve().then(function(res) {
  39. return env;
  40. });
  41. },
  42. updateLastTeamId = function(id) {
  43. teamId = id;
  44. plus.storage.setItem("selectedTeamId",teamId+"");
  45. },
  46. // 初始化选择团队
  47. initTeams = function (){
  48. template.helper("setChecked", function(id, i) {
  49. if(teamId && teamId==id)
  50. return "checked";
  51. return "";
  52. })
  53. $('.lin-sel-group').html(template('teams_tmpl', baseInfo.teamInfo));
  54. var $selected = $('.lin-sel-group').find("li.checked");
  55. $selectedTeamName.text($selected.attr("data-name"));
  56. },
  57. showGroupSel = function() {
  58. var isShow = isShow || $('.lin-sel-group:hidden').length != 0;
  59. $('.lin-mask').toggle(isShow);
  60. $('.lin-sel-group').toggle(isShow);
  61. },
  62. // 初始化“患者”视图列表
  63. initPatientViewList = function(data,keyword,isAppend) {
  64. var html = template("pati_list_tmpl", {list: _.map(data,function(o) {
  65. o.jsonStr = JSON.stringify(o);
  66. var addr = '';
  67. if(o.address){
  68. addr = (o.address.indexOf(keyword) != -1) ? o.address : "";
  69. }
  70. var id = (o.idcard.indexOf(keyword) != -1) ? o.idcard : "";
  71. o.labels = addr+" "+id;
  72. return o;
  73. })});
  74. if(isAppend === true) {
  75. $patiList.append(html)
  76. } else {
  77. $patiList.html(html);
  78. }
  79. ellipsisText($patiList.find('li'),keyword);
  80. },
  81. // $el: $('li')
  82. getRowProps = function ($el) {
  83. var $textEllipsis = $el,
  84. $text = $textEllipsis.eq(0),
  85. $chart = $text.text('a'),
  86. enWidth = $chart.width(),
  87. $chart = $text.text('中'),
  88. zhWidth = $chart.width(),
  89. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  90. rowHeight = $chart.height();
  91. $chart.text('');
  92. return {
  93. chartWidth: {
  94. zh: zhWidth,
  95. en: enWidth
  96. },
  97. rowHeight: Math.max(rowHeight, lineHeight),
  98. rowWidth: $el.width()
  99. };
  100. },
  101. replaceAll = function (text, arr) {
  102. var html = text;
  103. _.each(arr,function(kw) {
  104. var reg = new RegExp(kw+"(?!>)","gi");
  105. html = html.replace(reg,'<em>'+kw+'</em>');
  106. });
  107. return html;
  108. },
  109. highlineKeyword = function ($el,searchText) {
  110. var props = getRowProps($el),
  111. chartWidth = props.chartWidth,
  112. rowHeight = props.rowHeight,
  113. rowWidth = props.rowWidth,
  114. // 每行显示字符数(以中文字符为标准计算)
  115. chartNum = Math.floor(rowWidth / chartWidth.zh),
  116. // 排除指定数量字符所占宽度
  117. exceptNum = 0,
  118. // 行数
  119. rowNum = 1,
  120. // 预计显示总字符数
  121. expectedNum = chartNum * rowNum - exceptNum,
  122. $target = $el,
  123. // 目标文本
  124. text = $.trim($target.attr('data-text')),
  125. length = text.length,
  126. // 关键字数组
  127. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  128. var fidx = 0,preFidx,diff = 0;
  129. $target.html(replaceAll(text, kws));
  130. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  131. return ;
  132. }
  133. if(text.length > expectedNum) {
  134. fidx = text.indexOf(kws[0])+kws[0].length-1;
  135. diff = fidx - expectedNum + 1;
  136. preFidx = fidx;
  137. diff = (diff<0)?0:diff;
  138. var preChar = (diff>0)?"...":"";
  139. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  140. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  141. preFidx++;
  142. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  143. }
  144. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  145. diff = diff>0?(diff - 1):0;
  146. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  147. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  148. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  149. }
  150. }
  151. },
  152. ellipsisText = function ($elements, searchText) {
  153. _.each($elements,function(el) {
  154. var $textEl = $(el).find(".j-text-ellipsis");
  155. _.each($textEl, function(t){
  156. highlineKeyword($(t),searchText);
  157. });
  158. });
  159. },
  160. // 分页查询列表
  161. searchByPaging = function () {
  162. search_keyword = $searchbarInput.val();
  163. if(!$.trim(search_keyword)) {
  164. return ;
  165. }
  166. plus.nativeUI.showWaiting();
  167. // page: 分页索引,filter:搜素关键字,pagesize:每页条数
  168. var url = type == "分配" ? '/doctor/patient_label_info/getPatientUnSign' : '/doctor/family_contract/getPatientByParams' ;
  169. params = { page:1, filter: search_keyword, pagesize:pagesize, teamCode:teamId,doctor:docCode,level:level};
  170. getReqPromise(url,params,'get').then(function(res){
  171. if(res.status == 200) {
  172. lastId = lastId+1;
  173. if(!res.data || !res.data.length) {
  174. $searchtResult.hide();
  175. $noResultWrap.show();
  176. } else {
  177. if(res.data.length < pagesize) {
  178. hasMoreResults = false;
  179. } else {
  180. hasMoreResults = true;
  181. }
  182. $noResultWrap.hide();
  183. $searchtResult.show();
  184. initPatientViewList(res.data,search_keyword);
  185. }
  186. if(!iscroller) {
  187. iscroller = initScroller($searchtResult,url,
  188. function() { // 传递分页参数
  189. return $.extend({},params,{page:lastId,filter:search_keyword});
  190. },function(res) {
  191. lastId = lastId+1;
  192. search_keyword = $searchbarInput.val();
  193. initPatientViewList(res.data,search_keyword,true);
  194. iscroller.refresh();
  195. });
  196. }
  197. iscroller.refresh();
  198. } else {
  199. plus.nativeUI.toast("搜索失败!");
  200. }
  201. plus.nativeUI.closeWaiting();
  202. }).catch(function(e) {
  203. console && console.error(e)
  204. });
  205. },
  206. // 滚动条分页实例初始化
  207. initScroller = function($el,url,getData,pullUpAction) {
  208. var scroller;
  209. if(hasMoreResults) {
  210. scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  211. var data = getData();
  212. getReqPromise(url,data,'get').then(function(data) {
  213. if(pullUpAction && $.isFunction(pullUpAction)) {
  214. pullUpAction(data);
  215. console.log(data.list)
  216. updatePullUpText(scroller,data.list);
  217. }
  218. })
  219. }});
  220. } else {
  221. scroller = $el.initScroll({pullDown: false,pullUp: false});
  222. }
  223. return scroller;
  224. },
  225. // 更新分页上拉加载的提示文本
  226. updatePullUpText= function(scroller,list) {
  227. var $wrap = $(scroller.wrapper),
  228. $pullupLabel = $wrap.find('.pullUpLabel');
  229. if(!list || !list.length) {
  230. $pullupLabel.text('没有更多');
  231. } else {
  232. $pullupLabel.text('上拉加载更多');
  233. }
  234. scroller.on('refresh',function() {
  235. if(!list || !list.length) {
  236. $pullupLabel.text('没有更多');
  237. } else {
  238. $pullupLabel.text('上拉加载更多');
  239. }
  240. });
  241. },
  242. bindEvents = function(){
  243. $patiList.on('tap','li[data-patient-code]',function() {
  244. $searchbarInput.blur();
  245. var $checked = $(this).find(".n-list-checked"),
  246. isChecked = $checked.hasClass('checked');
  247. if(isChecked){
  248. $checked.removeClass('checked');
  249. sendCount--;
  250. }else{
  251. $checked.addClass('checked');
  252. sendCount++;
  253. }
  254. $("#sendCount").text(sendCount);
  255. if(sendCount>0){
  256. $('#sendBtn').addClass('active');//变色
  257. }else{
  258. $('#sendBtn').removeClass('active');
  259. }
  260. return false;
  261. });
  262. // $('.demo-comtop h1').on('click', showGroupSel);
  263. // $('.lin-sel-group').on('click', 'li', function(){
  264. // updateLastTeamId($(this).attr("data-code"));
  265. // $selectedTeamName.text($(this).attr("data-name"));
  266. // showGroupSel(undefined, false);
  267. // if(!$(this).hasClass('checked')){
  268. // $(this).addClass('checked').siblings().removeClass('checked');
  269. // }
  270. // refreshPage();
  271. // });
  272. $("#sendBtn").on('click',function(){
  273. if(sendCount<=0){
  274. mui.toast('请至少选一个居民!');
  275. return;
  276. }
  277. plus.nativeUI.showWaiting();
  278. var patientList=[],//包含居民code和名字
  279. pCodeArr=[],
  280. pCode;
  281. $(".n-list-link").each(function(){
  282. var d = $(this).find(".n-list-checked"),
  283. obj={};
  284. if(d.hasClass("checked")){
  285. obj.code = ($(this).attr("data-patient-code"));
  286. obj.name = ($(this).data("name"));
  287. patientList.push(obj);
  288. }
  289. })
  290. $.each(patientList, function(i, v) {
  291. pCodeArr.push(v.code)
  292. });
  293. //居民code字符串
  294. pCode = pCodeArr.join(',');
  295. if(type=='分配'){
  296. to_next(pCode);
  297. return;
  298. }
  299. sendPost("/doctor/consult/getConsultByPatientAndDoctor", {patientCode: pCode, doctor: docCode}, null, function(res){
  300. var tem = {};
  301. if(res.length==0){
  302. to_next(pCode);
  303. } else if(pCodeArr.length == res.length){
  304. plus.nativeUI.closeWaiting();
  305. mui.confirm("对不起,您选择的居民均有尚未结束的健康咨询,无法继续转移。", "提示", ["我知道了"], function(e){})
  306. return;
  307. } else if(res.length <= 3 ){
  308. tem = getCanTrans(patientList, res);
  309. mui.confirm("居民"+ tem.names + "尚有未结束的健康咨询,本次只可转移其余居民,是否继续?", "提示", ["继续转移", "不了,谢谢"], function(e){
  310. if(e.index == 0){
  311. to_next(tem.canPati);
  312. cancleSel(res);
  313. }
  314. })
  315. } else if(res.length < pCodeArr.length){
  316. tem = getCanTrans(patientList, res);
  317. mui.confirm("选中居民中有"+ res.length + "人,尚有未结束的健康咨询,本次只可转移其余居民,是否继续?", "提示", ["继续转移", "不了,谢谢"], function(e){
  318. if(e.index == 0){
  319. to_next(tem.canPati);
  320. cancleSel(res);
  321. }
  322. })
  323. } else{
  324. }
  325. plus.nativeUI.closeWaiting();
  326. })
  327. })
  328. function getCanTrans(patientList, notCodes){
  329. var canPati = [];
  330. var names = [];
  331. $.each(patientList, function(i, v) {
  332. var has = false;
  333. $.each(notCodes, function(n, m) {
  334. if(v.code == m){
  335. has = true;
  336. names.push(v.name);
  337. }
  338. });
  339. if(!has){
  340. canPati.push(v.code);
  341. }
  342. });
  343. return {canPati: canPati.join(","), names: names.join(",")}
  344. }
  345. function cancleSel(notCodes){
  346. var $ul, $prev, len;
  347. $.each(notCodes, function(i, c) {
  348. var $lis = $('#listDiv .n-list-link[data-code="'+ c +'"]');
  349. $.each($lis, function(i, v) {
  350. $(v).removeClass('checked');
  351. $ul = $(v).closest('ul');
  352. $prev = $ul.prev();
  353. len = $(v).closest('ul').find('li.checked').length;
  354. if(len==0)
  355. $prev.removeClass('checked').removeClass('incomplete');
  356. else if(len==$ul.parent().attr('data-amount'))
  357. $prev.removeClass('incomplete').addClass('checked');
  358. else
  359. $prev.removeClass('checked').addClass('incomplete');
  360. });
  361. removeChecked(c);
  362. });
  363. showNum();
  364. }
  365. function to_next(pCode){
  366. mui.openWindow({
  367. id: "choose_doctor.html",
  368. url: "../../tuandui/html/choose_doctor.html",
  369. extras: {
  370. teamCode: teamId,
  371. sendCount:sendCount,
  372. patients:pCode,
  373. level: level,
  374. docCode:docCode,
  375. type:type
  376. }
  377. })
  378. }
  379. $searchbarInput.on('input', function(){
  380. var $this = $(this),
  381. html = $this.val(),
  382. reg = new RegExp("<"+"(?!>)","gi");
  383. html = html.replace(reg,'&lt');
  384. var reg = new RegExp(">"+"(?!>)","gi");
  385. html = html.replace(reg,'&gt');
  386. $('#searchVal').html(html);
  387. $('.u-icon-delete').toggle($this.val() != "");
  388. $searchSuggest.toggle($this.val()!="");
  389. $searchtResult.hide();
  390. $noResultWrap.hide();
  391. $("#sendCount").text(0);
  392. sendCount=0;
  393. $('#sendBtn').removeClass('active');
  394. }).on('keydown',function(e) {
  395. if (e.which === 13) {
  396. lastId = 1;
  397. $searchSuggest.hide();
  398. searchByPaging();
  399. }
  400. });
  401. $('.u-icon-delete').on('click', function(){
  402. $(this).hide();
  403. $searchSuggest.hide();
  404. $searchbarInput.val("");
  405. $('#searchVal').html("");
  406. })
  407. $searchSuggest.on('click',function() {
  408. lastId = 1;
  409. $searchSuggest.hide();
  410. $("#sendCount").text(0);
  411. sendCount=0;
  412. searchByPaging();
  413. $('#sendBtn').removeClass('active');
  414. });
  415. };
  416. // 页面业务处理流程开始
  417. new Promise(function(resolve, reject) {
  418. // TODO 临时放开
  419. //resolve(true);
  420. mui.plusReady(function() {
  421. // hrefhrefplus已经准备好,可以往下执行
  422. resolve(true);
  423. initKeyboardAndroid();
  424. });
  425. }).then(function() {
  426. // 获取基础环境信息
  427. return getBaseEnvPromise().then(function(env) {
  428. baseEnv = env;
  429. }).then(function() {
  430. // 获取登录基本信息
  431. baseInfo = getBaseInfo();
  432. initTeams();
  433. openSoftKeyboard();
  434. $searchbarInput.focus();
  435. // 绑定页面事件
  436. bindEvents();
  437. })
  438. }).catch(function(e) {
  439. plus.nativeUI.closeWaiting();
  440. console && console.error(e);
  441. });