search_jumin_more.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // 基本信息(包括userAgent)
  2. var baseInfo = null,
  3. // 基础环境信息(包括当前webview)
  4. baseEnv = null;
  5. var $searchbarInput = $('#search-input'),
  6. // 搜索取消按钮
  7. $searchCancelBtn = $('.searchbar-cancel'),
  8. // 搜索框下面悬浮的搜索提示
  9. $searchSuggest = $('#searchValWrap'),
  10. // 搜索结果展示容器
  11. $searchtResult = $('#search_result'),
  12. // 搜索无结果时显示
  13. $noResultWrap = $('#no_result_wrap'),
  14. $patiList = $('#pati_list');
  15. var lastId = 1,// 分页查询最后一页
  16. search_keyword = "",
  17. iscroller,// iscroll 滚动条实例
  18. teamId, // 从缓存取出所选团队
  19. typeId,
  20. pagesize = 15,// 每页加载数据量
  21. hasMoreResults = false,
  22. sendCount = 0; //记录发送人数
  23. var origin = '',//搜索来源位置
  24. article=null,//健康文章code
  25. articleTitle = "";//健康文章标题
  26. // 获取登录相关信息
  27. var getBaseInfo = function() {
  28. // 登录的相关信息
  29. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
  30. var self = plus.webview.currentWebview();
  31. article = self.article;
  32. articleTitle = self.articleTitle;
  33. origin = self.origin;
  34. return {
  35. userAgent: userAgent
  36. }
  37. },
  38. // 获取基础环境信息
  39. getBaseEnvPromise = function () {
  40. var env = {
  41. webview: plus&&plus.webview.currentWebview()
  42. };
  43. return Promise.resolve().then(function(res) {
  44. return env;
  45. });
  46. },
  47. // 初始化“患者”视图列表
  48. initPatientViewList = function(data,keyword,isAppend) {
  49. var html = template("pati_list_tmpl", {list: _.map(data,function(o) {
  50. o.jsonStr = JSON.stringify(o);
  51. var addr = '';
  52. if(o.address){
  53. addr = (o.address.indexOf(keyword) != -1) ? o.address : "";
  54. }
  55. var id = (o.idcard.indexOf(keyword) != -1) ? o.idcard : "";
  56. o.labels = addr +" "+ id;
  57. return o;
  58. })});
  59. if(isAppend === true) {
  60. $patiList.append(html)
  61. } else {
  62. $patiList.html(html);
  63. }
  64. ellipsisText($patiList.find('li'),keyword);
  65. },
  66. // $el: $('li')
  67. getRowProps = function ($el) {
  68. var $textEllipsis = $el,
  69. $text = $textEllipsis.eq(0),
  70. $chart = $text.text('a'),
  71. enWidth = $chart.width(),
  72. $chart = $text.text('中'),
  73. zhWidth = $chart.width(),
  74. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  75. rowHeight = $chart.height();
  76. $chart.text('');
  77. return {
  78. chartWidth: {
  79. zh: zhWidth,
  80. en: enWidth
  81. },
  82. rowHeight: Math.max(rowHeight, lineHeight),
  83. rowWidth: $el.width()
  84. };
  85. },
  86. replaceAll = function (text, arr) {
  87. var html = text;
  88. _.each(arr,function(kw) {
  89. var reg = new RegExp(kw+"(?!>)","gi");
  90. html = html.replace(reg,'<em>'+kw+'</em>');
  91. });
  92. return html;
  93. },
  94. highlineKeyword = function ($el,searchText) {
  95. var props = getRowProps($el),
  96. chartWidth = props.chartWidth,
  97. rowHeight = props.rowHeight,
  98. rowWidth = props.rowWidth,
  99. // 每行显示字符数(以中文字符为标准计算)
  100. chartNum = Math.floor(rowWidth / chartWidth.zh),
  101. // 排除指定数量字符所占宽度
  102. exceptNum = 0,
  103. // 行数
  104. rowNum = 1,
  105. // 预计显示总字符数
  106. expectedNum = chartNum * rowNum - exceptNum,
  107. $target = $el,
  108. // 目标文本
  109. text = $.trim($target.attr('data-text')),
  110. length = text.length,
  111. // 关键字数组
  112. kws = $.trim(searchText).replace(/\s+/g," ").split(" ");
  113. var fidx = 0,preFidx,diff = 0;
  114. $target.html(replaceAll(text, kws));
  115. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  116. return ;
  117. }
  118. if(text.length > expectedNum) {
  119. fidx = text.indexOf(kws[0])+kws[0].length-1;
  120. diff = fidx - expectedNum + 1;
  121. preFidx = fidx;
  122. diff = (diff<0)?0:diff;
  123. var preChar = (diff>0)?"...":"";
  124. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  125. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  126. preFidx++;
  127. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  128. }
  129. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  130. diff = diff>0?(diff - 1):0;
  131. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  132. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  133. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  134. }
  135. }
  136. },
  137. ellipsisText = function ($elements, searchText) {
  138. _.each($elements,function(el) {
  139. var $textEl = $(el).find(".j-text-ellipsis");
  140. _.each($textEl, function(t){
  141. highlineKeyword($(t),searchText);
  142. });
  143. });
  144. },
  145. // 分页查询列表
  146. searchByPaging = function () {
  147. search_keyword = $searchbarInput.val();
  148. if(!$.trim(search_keyword)) {
  149. return ;
  150. }
  151. plus.nativeUI.showWaiting();
  152. // page: 分页索引,filter:搜素关键字,pagesize:每页条数
  153. var url = "/doctor/patient_label_info/getPatientByParams",
  154. params = { page:1, filter: search_keyword, pagesize:pagesize, teamCode:teamId};
  155. getReqPromise(url,params,'get').then(function(res){
  156. if(res.status == 200) {
  157. lastId = lastId+1;
  158. if(!res.data || !res.data.length) {
  159. $searchtResult.hide();
  160. $noResultWrap.show();
  161. } else {
  162. if(res.data.length < pagesize) {
  163. hasMoreResults = false;
  164. } else {
  165. hasMoreResults = true;
  166. }
  167. $noResultWrap.hide();
  168. $searchtResult.show();
  169. initPatientViewList(res.data,search_keyword);
  170. }
  171. if(!iscroller) {
  172. iscroller = initScroller($searchtResult,url,
  173. function() { // 传递分页参数
  174. return $.extend({},params,{page:lastId,filter:search_keyword});
  175. },function(res) {
  176. lastId = lastId+1;
  177. search_keyword = $searchbarInput.val();
  178. initPatientViewList(res.data,search_keyword,true);
  179. iscroller.refresh();
  180. });
  181. }
  182. iscroller.refresh();
  183. } else {
  184. plus.nativeUI.toast("搜索失败!");
  185. }
  186. plus.nativeUI.closeWaiting();
  187. }).catch(function(e) {
  188. console && console.error(e)
  189. });
  190. },
  191. // 滚动条分页实例初始化
  192. initScroller = function($el,url,getData,pullUpAction) {
  193. var scroller;
  194. if(hasMoreResults) {
  195. scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  196. var data = getData();
  197. getReqPromise(url,data,'get').then(function(data) {
  198. if(pullUpAction && $.isFunction(pullUpAction)) {
  199. pullUpAction(data);
  200. updatePullUpText(scroller,data.list);
  201. }
  202. })
  203. }});
  204. } else {
  205. scroller = $el.initScroll({pullDown: false,pullUp: false});
  206. }
  207. return scroller;
  208. },
  209. // 更新分页上拉加载的提示文本
  210. updatePullUpText= function(scroller,list) {
  211. var $wrap = $(scroller.wrapper),
  212. $pullupLabel = $wrap.find('.pullUpLabel');
  213. if(!list || !list.length) {
  214. $pullupLabel.text('没有更多');
  215. } else {
  216. $pullupLabel.text('上拉加载更多');
  217. }
  218. scroller.on('refresh',function() {
  219. if(!list || !list.length) {
  220. $pullupLabel.text('没有更多');
  221. } else {
  222. $pullupLabel.text('上拉加载更多');
  223. }
  224. });
  225. },
  226. sendError = function (res){
  227. plus.nativeUI.closeWaiting();
  228. window.clearTimeout(timer);
  229. if(res && res.msg){
  230. mui.toast(res.msg);
  231. }else{
  232. mui.toast("发送失败!");
  233. }
  234. },
  235. closeOpener = function (cur) {
  236. if(cur.id != "jiankangjiaoyu") {
  237. var opener = cur.opener();
  238. cur.close();
  239. closeOpener(opener);
  240. }
  241. },
  242. sendSuccess = function (res){
  243. plus.nativeUI.closeWaiting();
  244. if(res.status==200){
  245. mui.toast("发送成功!");
  246. var view = plus.webview.getWebviewById("jiankangjiaoyu");
  247. if(view){
  248. mui.fire(view,"refresh");
  249. }
  250. view=plus.webview.getWebviewById("article");
  251. if(view){
  252. mui.fire(view,"refresh");
  253. openWebviewExtras("article.html",{article:article});
  254. }else{
  255. closeOpener(plus.webview.currentWebview())
  256. }
  257. }else{
  258. sendError(res);
  259. }
  260. },
  261. send = function(article,txtMsg,patient){
  262. var params = {};
  263. params.code =article;
  264. params.patient=patient;
  265. // params.teamCode=1;
  266. params.attachedContent = txtMsg;
  267. plus.nativeUI.showWaiting();
  268. sendPost("/doctor/jkEdu/article/doctorSendArticleToPatients",params,sendError,sendSuccess);
  269. },
  270. bindEvents = function(){
  271. $patiList.on('tap','li[data-patient-code]',function() {
  272. $searchbarInput.blur();
  273. var $checked = $(this).find(".n-list-checked"),
  274. isChecked = $checked.hasClass('checked');
  275. if(isChecked){
  276. $checked.removeClass('checked');
  277. sendCount--;
  278. }else{
  279. $checked.addClass('checked');
  280. sendCount++;
  281. }
  282. $("#sendCount").text(sendCount);
  283. return false;
  284. });
  285. $('#cancel').on('click', function(){
  286. mui.back();
  287. });
  288. $('.send-btn').on('click', function(){
  289. //健康教育模块
  290. if(origin == 'jiaoyu'){
  291. var patientList=[];
  292. var patientNames = [];
  293. $(".n-list-link").each(function(){
  294. var name = $(this).attr('data-patient-name');
  295. var d = $(this).find(".n-list-checked");
  296. if(d.hasClass("checked")){
  297. patientList.push($(this).attr("data-patient-code"));
  298. patientNames.push(name);
  299. }
  300. });
  301. var patient = patientList.join(",");
  302. var targets = "";
  303. if(patientList.length == 1) {
  304. targets = patientNames[0];
  305. } else if(patientList.length > 1) {
  306. targets = patientNames[0]+'等'+patientList.length+'人';
  307. }
  308. if(targets) {
  309. showDialog(article,patient,targets)
  310. }
  311. }
  312. });
  313. $searchbarInput.on('input', function(){
  314. var html = $(this).val();
  315. var reg = new RegExp("<"+"(?!>)","gi");
  316. html = html.replace(reg,'&lt');
  317. var reg = new RegExp(">"+"(?!>)","gi");
  318. html = html.replace(reg,'&gt');
  319. $('#searchVal').html(html);
  320. $(".searchbar-clear").toggle($(this).val() != "");
  321. $searchSuggest.toggle($(this).val()!="");
  322. $searchtResult.hide();
  323. $noResultWrap.hide();
  324. $("#sendCount").text(0);
  325. sendCount=0;
  326. }).on('keydown',function(e) {
  327. if (e.which === 13) {
  328. lastId = 1;
  329. $searchSuggest.hide();
  330. searchByPaging();
  331. }
  332. });
  333. $(".searchbar-clear").on('click', function(){
  334. $(this).hide();
  335. $searchbarInput.val("");
  336. $('#searchVal').html("");
  337. $searchSuggest.hide();
  338. });
  339. $searchSuggest.on('click',function() {
  340. lastId = 1;
  341. $searchSuggest.hide();
  342. $("#sendCount").text(0);
  343. sendCount=0;
  344. searchByPaging();
  345. });
  346. },
  347. showDialog = function (code,patientCode,targets) {
  348. dialog({
  349. title: '<div><div class="c-f18 c-17b3ec c-t-left">发送给</div><div class="mt5 c-f14 c-t-left c-909090">'+targets+'</div></div>',
  350. content: '<div><div class="c-f16 c-323232 c-t-left mb10">《'+articleTitle+'》</div><input id="messageInput" class="c-f14 pl10" placeholder="给居民留言..."/></div>',
  351. okValue: '立即发送',
  352. cancelValue: '我再看看',
  353. cancel: function () {
  354. return;
  355. },
  356. ok: function() {
  357. send(code,$.trim($('#messageInput').val()),patientCode);
  358. }
  359. }).showModal();
  360. };
  361. // 页面业务处理流程开始
  362. new Promise(function(resolve, reject) {
  363. // TODO 临时放开
  364. //resolve(true);
  365. mui.plusReady(function() {
  366. // hrefhrefplus已经准备好,可以往下执行
  367. resolve(true);
  368. initKeyboardAndroid();
  369. });
  370. }).then(function() {
  371. // 获取基础环境信息
  372. return getBaseEnvPromise().then(function(env) {
  373. baseEnv = env;
  374. }).then(function() {
  375. // 获取登录基本信息
  376. baseInfo = getBaseInfo();
  377. teamId = plus.storage.getItem("selectedTeamId");
  378. typeId = plus.storage.getItem("selectedTypeId");
  379. openSoftKeyboard();
  380. $searchbarInput.focus();
  381. // 绑定页面事件
  382. bindEvents();
  383. })
  384. }).catch(function(e) {
  385. plus.nativeUI.closeWaiting();
  386. console && console.error(e);
  387. });