guidance_searchhuanzhe.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. content, //要发送的模板参数
  28. modelCode,
  29. images;
  30. var self;
  31. mui.plusReady(function() {
  32. // initKeyboardAndroid();
  33. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  34. historyStr = plus.storage.getItem("juminSearchHis"+docInfo.code);
  35. if(historyStr){
  36. historyStr = JSON.parse(historyStr).join(',');
  37. historyData = historyStr.split(',');
  38. }else{
  39. historyData = [];
  40. }
  41. })
  42. window.addEventListener("initSearch", function(e){
  43. // openSoftKeyboard();
  44. baseInfo = getBaseInfo();
  45. $("#searchAll").show();
  46. setTimeout(function(){
  47. $("#searchPut").focus();
  48. },100)
  49. if(historyData.length){
  50. $history.show();
  51. }
  52. lastTeamId = plus.storage.getItem("selectedTeamId");
  53. })
  54. // 获取登录相关信息
  55. var getBaseInfo = function() {
  56. // 登录的相关信息
  57. var userAgent = JSON.parse(plus.storage.getItem("userAgent"))
  58. return {
  59. userAgent: userAgent
  60. }
  61. },
  62. // 获取基础环境信息
  63. getBaseEnvPromise = function () {
  64. var env = {
  65. webview: plus.webview.currentWebview()
  66. }
  67. return Promise.resolve().then(function(res) {
  68. return env;
  69. })
  70. },
  71. historyList = function(){
  72. historyStr = plus.storage.getItem("juminSearchHis"+docInfo.code);
  73. if(historyStr){
  74. historyStr = JSON.parse(historyStr).join(',');
  75. historyData = historyStr.split(',');
  76. }else{
  77. historyData = [];
  78. }
  79. var html = template('his_list_tmpl',{list:historyData});
  80. $hisSearList.html('');
  81. $hisSearList.append(html);
  82. },
  83. // 初始化“患者”视图列表
  84. initPatientViewList = function(data,keyword,isAppend) {
  85. data = _.map(data,function(item){
  86. item.photo = getImgUrl(item.photo);
  87. return item;
  88. })
  89. var html = template("pati_list_tmpl", {list: data});
  90. searchText = $searchbarInput.val().trim();
  91. if(isAppend == true){
  92. $patiList.append(html)
  93. }else{
  94. mui('#jumin_wrapper').pullRefresh().scrollTo(0,0)
  95. $patiList.empty().append(html)
  96. mui('#jumin_wrapper').pullRefresh().refresh(true);//重置
  97. }
  98. ellipsisText($patiList.find('li'),searchText);
  99. },
  100. // $el: $('.c-content-warp')
  101. getRowProps = function ($el) {
  102. var $textEllipsis = $el,
  103. $text = $textEllipsis.eq(0),
  104. $chart = $text.text('a'),
  105. enWidth = $chart.width(),
  106. $chart = $text.text('中'),
  107. zhWidth = $chart.width(),
  108. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  109. rowHeight = $chart.height();
  110. $chart.text('');
  111. return {
  112. chartWidth: {
  113. zh: zhWidth,
  114. en: enWidth
  115. },
  116. rowHeight: Math.max(rowHeight, lineHeight),
  117. rowWidth: $el.width()
  118. };
  119. },
  120. replaceAll = function (text, arr) {
  121. var html = text;
  122. _.each(arr,function(kw) {
  123. var reg = new RegExp(kw+"(?!>)","gi");
  124. html = html.replace(reg,'<em>'+kw+'</em>');
  125. });
  126. return html;
  127. },
  128. highlineKeyword = function ($el,searchText) {
  129. var props = getRowProps($el),
  130. chartWidth = props.chartWidth,
  131. rowHeight = props.rowHeight,
  132. rowWidth = props.rowWidth,
  133. // 每行显示字符数(以中文字符为标准计算)
  134. chartNum = Math.floor(rowWidth / chartWidth.zh),
  135. // 排除指定数量字符所占宽度
  136. exceptNum = 0,
  137. // 行数
  138. rowNum = 1,
  139. // 预计显示总字符数
  140. expectedNum = chartNum * rowNum - exceptNum,
  141. $target = $el,
  142. // 目标文本
  143. text = $.trim($target.attr('data-text')),
  144. length = text.length,
  145. // 关键字数组
  146. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  147. var fidx = 0,preFidx,diff = 0;
  148. $target.html(replaceAll(text, kws));
  149. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  150. return ;
  151. }
  152. if(text.length > expectedNum) {
  153. fidx = text.indexOf(kws[0])+kws[0].length-1;
  154. diff = fidx - expectedNum + 1;
  155. preFidx = fidx;
  156. diff = (diff<0)?0:diff;
  157. var preChar = (diff>0)?"...":"";
  158. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  159. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  160. preFidx++;
  161. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  162. }
  163. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  164. diff = diff>0?(diff - 1):0;
  165. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  166. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  167. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  168. }
  169. }
  170. },
  171. ellipsisText = function ($elements, searchText) {
  172. _.each($elements,function(el) {
  173. var $textEl = $(el).find(".j-text-ellipsis");
  174. _.each($textEl, function(t){
  175. highlineKeyword($(t),searchText)
  176. })
  177. });
  178. },
  179. // 控制搜索关键字悬浮提示的显示
  180. showSearchSuggest = function(text) {
  181. var suggestText = '搜索“'+text+'”';
  182. // 如果text不为空,则显示;否则隐藏
  183. if(text&&text.trim().length) {
  184. $searchSuggest.text(suggestText);
  185. $searchSuggest.show();
  186. } else {
  187. $searchSuggest.text('');
  188. $searchSuggest.hide();
  189. }
  190. },
  191. // 分页查询列表
  192. searchByPaging = function (isAppend,fun) {
  193. $history.hide();
  194. $("#searchPut").blur();
  195. search_keyword = $searchbarInput.val();
  196. if(!$.trim(search_keyword)) {
  197. return ;
  198. }
  199. plus.nativeUI.showWaiting();
  200. var url = "/doctor/concern/getConcernPatients",
  201. params = { page:page,pageSize:pagesize,patientName:search_keyword};
  202. getReqPromise(url,params, "GET").then(function(res){
  203. if(res.status == 200) {
  204. if((page==0 && !res.data) || (page==0 && !res.data.length)) {
  205. $searchtResult.hide();
  206. $noResultWrap.show();
  207. } else {
  208. $noResultWrap.hide();
  209. $searchtResult.show();
  210. initPatientViewList(res.data,search_keyword,isAppend);
  211. if(res.data.length < pagesize){
  212. fun&&fun.call(this,true)
  213. }else{
  214. fun&&fun.call(this,false)
  215. }
  216. }
  217. }else{
  218. mui.toast("搜索失败");
  219. }
  220. plus.nativeUI.closeWaiting();
  221. }).catch(function(e) {
  222. plus.nativeUI.closeWaiting();
  223. console && console.error(e)
  224. })
  225. },
  226. // 绑定页面事件
  227. bindEvents = function (){
  228. mui.init({
  229. pullRefresh : {
  230. container:'#jumin_wrapper',
  231. up : {
  232. height:50,
  233. contentinit: '',
  234. contentdown: '',
  235. contentrefresh : "正在加载...",
  236. contentnomore:'没有更多数据了',
  237. callback: function() {
  238. var self = this;
  239. page++;
  240. searchByPaging(true,function(value){
  241. setTimeout(function(){
  242. self.endPullupToRefresh(value);
  243. },500)
  244. })
  245. }
  246. }
  247. }
  248. })
  249. $('#his_scroll').css({
  250. 'height':$(window).height()-158
  251. })
  252. mui('#his_scroll').scroll({
  253. bounce: true, //是否启用回弹
  254. })
  255. $searchbarInput.on('input', function() {
  256. var text = $(this).val().trim();
  257. $searchtResult.hide();
  258. $('#no_result_wrap').hide();
  259. if(text.length > 0){
  260. $("#searchAll").hide();
  261. $history.hide();
  262. }else{
  263. $("#searchAll").show();
  264. $history.show();
  265. }
  266. showSearchSuggest(text);
  267. }).on('keydown',function(e) {
  268. if (e.which === 13) {
  269. page = 0;
  270. $searchSuggest.hide();
  271. searchByPaging(false);
  272. //保存历史数据20个
  273. saveHisData();
  274. }
  275. });
  276. $('#clear_his_data').on('tap', function(){
  277. plus.storage.removeItem("juminSearchHis"+docInfo.code);
  278. historyList();
  279. $history.hide();
  280. })
  281. $hisSearList.on('click','.his-list',function(){
  282. var $that = $(this);
  283. page = 0;
  284. $("#searchAll").hide();
  285. $searchSuggest.hide();
  286. $history.hide();
  287. $searchbarInput.val($that.text())
  288. searchByPaging(false);
  289. })
  290. $('.lin-search-ipt a').on('tap', function(){
  291. plus.webview.currentWebview().hide();
  292. mui.later(function(){
  293. $('#search_result').hide();
  294. $('#no_result_wrap').hide();
  295. $('#searchPut').val("");
  296. $("#search_suggest_text").html("").hide();
  297. $("#searchPut").blur();
  298. }, 50)
  299. })
  300. $searchSuggest.on('click',function() {
  301. page = 0;
  302. $searchSuggest.hide();
  303. searchByPaging(false);
  304. //保存历史数据20个
  305. saveHisData()
  306. });
  307. var acTiveId='activeId';//选中的ID
  308. $patiList.on('tap','li[data-patient-code]',function(e) {
  309. var code = $(this).attr("data-patient-code");
  310. var mobile = $(this).attr("data-patient-phone");
  311. var address = $(this).attr("data-patient-address");
  312. var isCheck = $(this).attr("check");
  313. var id = $(this).attr('id');
  314. if(id != acTiveId ){ //如果点击的不是之前选中的
  315. $('#'+acTiveId).find('.checboxImg').find('img').attr('src','../images/checbox-false.png');
  316. $('#'+acTiveId).removeAttr('id');
  317. $(this).find('.checboxImg').find('img').attr('src','../images/checbox-true.png');
  318. $(this).attr('check','yes');
  319. $(this).attr('id',acTiveId);
  320. }
  321. else { //如果点击的是自己的话
  322. if(isCheck == 'no'){
  323. $(this).find('.checboxImg').find('img').attr('src','../images/checbox-true.png');
  324. $(this).attr('check','yes');
  325. }
  326. else {
  327. $(this).find('.checboxImg').find('img').attr('src','../images/checbox-false.png');
  328. $(this).attr('check','no');
  329. $(this).removeAttr('id',acTiveId);
  330. }
  331. }
  332. // if(baseEnv.webview.origin=="suifang") {//“随访”功能
  333. // if(baseEnv.webview.follow_type == 1){
  334. // openWebview("../../suifang/html/add_plan.html",{patientInfo: {code:code,mobile:mobile,address:address},chooseDate:baseEnv.webview.chooseDate});
  335. // return false;
  336. // }
  337. // if(baseEnv.webview.follow_type == 2){
  338. // openWebview("../../suifang/html/follow_way.html",{patientInfo: {code:code,mobile:mobile,address:address}});
  339. // return false;
  340. // }
  341. // }else{
  342. // openWebview("../../huanzhe/html/huanzhexinxi.html",{
  343. // patiCode: code
  344. // });
  345. // }
  346. return false;
  347. })
  348. $(".send_btn").on('tap', function(){
  349. var patiCode =$('#'+acTiveId).attr('data-patient-code');
  350. if(!patiCode) {mui.toast("请选择居民再进行发送");return};
  351. dialog({
  352. content: "发出后无法变更,是否确认发送给居民?",
  353. okValue: "继续发送",
  354. ok: function(){
  355. var url = "doctor/health/guidance/add",
  356. params = {
  357. patient: patiCode,
  358. content: content,
  359. modelCode: modelCode,
  360. images: images
  361. };
  362. console.log(params)
  363. plus.nativeUI.showWaiting();
  364. sendPost(url, params, null, function(res){
  365. if (res.status == 200) {
  366. mui.toast("发送成功!");
  367. var main =plus.webview.getWebviewById('guidance_list');
  368. mui.fire(main,'refresh');
  369. var self = plus.webview.currentWebview(),
  370. opener = self.opener(),
  371. pre_opener = self.opener().opener();
  372. if(opener.id == "jkzd"){ //自己的模板详情
  373. if(pre_opener.id == "guidance_info"){
  374. mui.fire(pre_opener.opener(), "update");
  375. pre_opener.opener().show();
  376. mui.later(function(){
  377. pre_opener.close('none');
  378. opener.close('none');
  379. self.close('none');
  380. }, 300);
  381. }else{
  382. mui.fire(pre_opener, "update");
  383. pre_opener.show();
  384. mui.later(function(){
  385. opener.close('none');
  386. self.close('none');
  387. }, 300);
  388. }
  389. }else{ //系统模板详情
  390. var pre_pre_opener = pre_opener.opener();
  391. if(pre_pre_opener.id == "guidance_info"){
  392. mui.fire(pre_pre_opener.opener(), "update");
  393. pre_pre_opener.opener().show();
  394. mui.later(function(){
  395. pre_pre_opener.close('none');
  396. opener.close('none');
  397. pre_opener.close('none');
  398. self.close('none');
  399. }, 300);
  400. }else{
  401. mui.fire(pre_pre_opener, "update");
  402. pre_pre_opener.show();
  403. mui.later(function(){
  404. opener.close('none');
  405. pre_opener.close('none');
  406. self.close('none');
  407. }, 300);
  408. }
  409. }
  410. }else{
  411. mui.toast(res.msg)
  412. }
  413. plus.nativeUI.closeWaiting();
  414. }, 'POST', '', true);
  415. },
  416. cancelValue: "不了,谢谢",
  417. cancel: function(){}
  418. }).showModal();
  419. });
  420. function saveHisData(){
  421. //保存历史数据20个
  422. var value = $searchbarInput.val().trim();
  423. if(value){
  424. if(historyData.length>19){
  425. historyData.pop();
  426. }
  427. if(historyData.indexOf(value)>-1){
  428. historyData = _.filter(historyData, function(o){ return o != value; });
  429. }
  430. historyData.unshift(value);
  431. var str = JSON.stringify(historyData)
  432. plus.storage.setItem("juminSearchHis"+docInfo.code,str);
  433. historyList();
  434. }
  435. }
  436. $searchCancelBtn.on('click',function() {
  437. mui.back()
  438. })
  439. //搜索全部居民的绑定事件
  440. $("#searchAll").on('click', function(){
  441. openWebview("search_all.html");
  442. });
  443. };
  444. // 页面业务处理流程开始
  445. new Promise(function(resolve, reject) {
  446. mui.plusReady(function() {
  447. resolve(true);
  448. })
  449. }).then(function() {
  450. // 获取基础环境信息
  451. return getBaseEnvPromise().then(function(env) {
  452. baseEnv = env;
  453. }).then(function() {
  454. // 获取登录基本信息
  455. baseInfo = getBaseInfo();
  456. lastTeamId = plus.storage.getItem("selectedTeamId");
  457. self = plus.webview.currentWebview();
  458. content =self.content;
  459. modelCode =self.modelCode;
  460. images =self.images;
  461. $searchbar.searchBar();
  462. // openSoftKeyboard();
  463. $searchbarInput.focus();
  464. historyList();
  465. if(historyData.length){
  466. $history.show();
  467. }
  468. // 绑定页面事件
  469. bindEvents();
  470. })
  471. }).catch(function(e) {
  472. plus.nativeUI.closeWaiting();
  473. console && console.error(e);
  474. });