sousuomingyi.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. keyword = null;
  8. // 搜索框
  9. var $searchbar = $('.searchbar'),
  10. // 搜索输入框
  11. $searchbarInput = $('.searchbar input'),
  12. // 搜索取消按钮
  13. $searchCancelBtn = $('.searchbar-cancel'),
  14. // 搜索框下面悬浮的搜索提示
  15. $searchSuggest = $('#search_suggest_text'),
  16. // 搜索结果展示容器
  17. $searchtResult = $('#search_result'),
  18. // 搜索无结果时显示
  19. $noResultWrap = $('#no_result_wrap'),
  20. $mingyiWrapper = $('#mingyi_wrapper'),
  21. $mingyiList = $('#mingyi_list'),
  22. $selectedNum = $('#selected_num');
  23. // 分页查询最后一页
  24. var lastId = 1,
  25. // iscroll 滚动条实例
  26. iscroller;
  27. // 获取登录相关信息
  28. var getLoginerInfo = function() {
  29. // 登录的相关信息
  30. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  31. return {
  32. userAgent: userAgent
  33. }
  34. },
  35. // 获取基础环境信息
  36. getBaseEnvPromise = function () {
  37. var env = {
  38. webview: plus&&plus.webview.currentWebview()
  39. };
  40. return Promise.resolve().then(function(res) {
  41. return env;
  42. });
  43. },
  44. // $el: $('.c-content-warp')
  45. getRowProps = function ($el) {
  46. var $textEllipsis = $el.find('.j-text-ellipsis'),
  47. $text = $textEllipsis.eq(0),
  48. $chart = $text.text('a'),
  49. enWidth = $chart.width(),
  50. $chart = $text.text('中'),
  51. zhWidth = $chart.width(),
  52. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  53. rowHeight = $chart.height();
  54. $chart.text('');
  55. return {
  56. chartWidth: {
  57. zh: zhWidth,
  58. en: enWidth
  59. },
  60. rowHeight: Math.max(rowHeight, lineHeight),
  61. rowWidth: $el.width()
  62. };
  63. },
  64. replaceAll = function (text, arr) {
  65. var html = text;
  66. _.each(arr,function(kw) {
  67. var reg = new RegExp(kw+"(?!>)","gi");
  68. html = html.replace(reg,'<em>'+kw+'</em>');
  69. });
  70. return html;
  71. },
  72. highlineKeyword = function ($el,searchText) {
  73. var props = getRowProps($el),
  74. chartWidth = props.chartWidth,
  75. rowHeight = props.rowHeight,
  76. rowWidth = props.rowWidth,
  77. // 每行显示字符数(以中文字符为标准计算)
  78. chartNum = Math.floor(rowWidth / chartWidth.zh),
  79. // 排除指定数量字符所占宽度
  80. exceptNum = 0,
  81. // 行数
  82. rowNum = 1,
  83. // 预计显示总字符数
  84. expectedNum = chartNum * rowNum - exceptNum,
  85. $target = $el.find('.j-text-ellipsis'),
  86. // 目标文本
  87. text = $target.attr('data-text').trim(),
  88. length = text.length,
  89. // 关键字数组
  90. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  91. var fidx = 0,preFidx,diff = 0;
  92. $target.html(replaceAll(text, kws));
  93. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  94. return ;
  95. }
  96. if(text.length > expectedNum) {
  97. fidx = text.indexOf(kws[0])+kws[0].length-1;
  98. diff = fidx - expectedNum + 1;
  99. preFidx = fidx;
  100. diff = (diff<0)?0:diff;
  101. var preChar = (diff>0)?"...":"";
  102. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  103. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  104. preFidx++;
  105. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  106. }
  107. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  108. diff = diff>0?(diff - 1):0;
  109. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  110. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  111. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  112. }
  113. }
  114. },
  115. ellipsisText = function ($elements, searchText) {
  116. //$el.ellipsis({ row: 2});
  117. _.each($elements,function(el) {
  118. highlineKeyword($(el),searchText)
  119. });
  120. },
  121. // 初始化“名医”视图列表
  122. initMingYiViewList = function(data,keyword,isAppend) {
  123. var html = template("yisheng_li_tmpl", {list: data.list}),
  124. searchText = $searchbarInput.val().trim();
  125. if(isAppend === true) {
  126. $mingyiList.append(html)
  127. } else {
  128. $mingyiList.html(html);
  129. }
  130. ellipsisText($mingyiList.find('.c-list-info'),searchText);
  131. },
  132. // 分页查询列表
  133. searchByPaging = function () {
  134. var kw = $searchbarInput.val();
  135. // TODO 示例示例搜索参数
  136. // id: 上次搜索结果列表最后一条记录id,type:6(进行中),query:搜素关键字,pageSize:每页条数
  137. var url = "doctor/findFamousDoctor",
  138. params = { page:1, key: kw,pagesize:15,type:1,level: 1};
  139. getReqPromise(url,params).then(function(res){
  140. // TODO 为了测试先注释
  141. var data = res;
  142. lastId = lastId+1;
  143. if(!iscroller) {
  144. iscroller = initScroller($searchtResult,url,
  145. function() { // 传递分页参数
  146. return $.extend({},params,{page:lastId});
  147. },function(data) {
  148. // TODO 示例数据
  149. lastId = lastId+1;
  150. var kw = $searchbarInput.val();
  151. initMingYiViewList(data,kw,true);
  152. iscroller.refresh();
  153. });
  154. }
  155. // TODO 示例数据data
  156. if(!data.list || !data.list.length) {
  157. $searchtResult.hide();
  158. $noResultWrap.show();
  159. } else {
  160. $noResultWrap.hide();
  161. // TODO 示例数据data
  162. initMingYiViewList(data,kw);
  163. }
  164. iscroller.refresh();
  165. }).catch(function(e) {
  166. console && console.error(e)
  167. });
  168. },
  169. // 滚动条分页实例初始化
  170. initScroller = function($el,url,getData,pullUpAction) {
  171. var scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  172. var data = getData();
  173. getReqPromise(url,data).then(function(data) {
  174. if(pullUpAction && $.isFunction(pullUpAction)) {
  175. pullUpAction(data);
  176. updatePullUpText(scroller,data.list);
  177. }
  178. })
  179. }});
  180. return scroller;
  181. },
  182. // 获取分页搜索返回的最后一条记录的id
  183. getLastId = function(data) {
  184. var lastObj = data.list && data.list.length && data.list[data.list.length-1];
  185. // 最后一条记录
  186. if(lastObj) {
  187. return lastObj.id;
  188. } else {
  189. return null
  190. }
  191. },
  192. // 更新分页上拉加载的提示文本
  193. updatePullUpText= function(scroller,list) {
  194. var $wrap = $(scroller.wrapper),
  195. $pullupLabel = $wrap.find('.pullUpLabel');
  196. if(!list || !list.length) {
  197. $pullupLabel.text('没有更多');
  198. } else {
  199. $pullupLabel.text('上拉加载更多');
  200. }
  201. scroller.on('refresh',function() {
  202. if(!list || !list.length) {
  203. $pullupLabel.text('没有更多');
  204. } else {
  205. $pullupLabel.text('上拉加载更多');
  206. }
  207. });
  208. },
  209. // 显示选中的人数
  210. showSelectedNum = function() {
  211. var num = $searchtResult.find('input[type="checkbox"]:checked').length;
  212. if(num) {
  213. $selectedNum.text('('+num+'人)')
  214. } else {
  215. $selectedNum.text('');
  216. }
  217. },
  218. // 点击“确定”按钮处理方法
  219. submit = function() {
  220. // TODO 这里需要补充处理逻辑
  221. var checklength = $searchtResult.find('input[type="checkbox"]:checked');
  222. if(checklength.length<=0){
  223. mui.toast("请至少选择一名医生!");
  224. return false;
  225. }
  226. var consultCode = localStorage.getItem("consult");
  227. var doctors = new Array();
  228. $searchtResult.find('input[type="checkbox"]:checked').each(function(){
  229. doctors.push($(this).val());
  230. })
  231. sendPost("doctor/consult/transfer", {
  232. consult: consultCode,
  233. doctor: doctors.join(","),
  234. type: 1 //无用
  235. }, null,
  236. function(res) {
  237. if(res.status == 200) {
  238. mui.toast("邀请成功");
  239. setTimeout(function(){
  240. plus.webview.currentWebview().close();
  241. openWebview("../../zdzx/html/jiankangzixunxiangqing.html");
  242. }, 1000);
  243. }
  244. });
  245. },
  246. // 绑定页面事件
  247. bindEvents = function () {
  248. $searchtResult.on('click','li',function(e) {
  249. var $el = $(e.target);
  250. setTimeout(function() {
  251. showSelectedNum();
  252. },0);
  253. if($el.attr('type')=="checkbox") {
  254. return ;
  255. }
  256. var checkbox = $(this).find('input[type="checkbox"]')[0];
  257. if(checkbox.checked) {
  258. checkbox.checked = false;
  259. } else {
  260. checkbox.checked = true;
  261. }
  262. });
  263. $searchbar.bind("click",function(){
  264. var oldback = mui.back;
  265. mui.back=function(){
  266. var wobj = plus.webview.getWebviewById("sousuoyisheng");//获取前一个页面ID
  267. if(wobj) {
  268. mui.fire(wobj, "focusAction");
  269. }
  270. plus.webview.currentWebview().close();
  271. oldback();
  272. }
  273. mui.back();
  274. })
  275. };
  276. // 页面业务处理流程开始
  277. new Promise(function(resolve, reject) {
  278. // TODO 临时放开
  279. // resolve(true);
  280. mui.plusReady(function() {
  281. // plus已经准备好,可以往下执行
  282. resolve(true);
  283. });
  284. }).then(function() {
  285. // 获取基础环境信息
  286. return getBaseEnvPromise().then(function(env) {
  287. baseEnv = env;
  288. }).then(function() {
  289. // 获取登录医生信息
  290. loginerInfo = getLoginerInfo();
  291. $searchbar.searchBar();
  292. self = plus.webview.currentWebview();
  293. keyword = self.kw;
  294. console.log(keyword);
  295. if(keyword) {
  296. $searchbarInput.val(keyword);
  297. searchByPaging();
  298. $searchtResult.show();
  299. } else {
  300. $searchtResult.hide();
  301. }
  302. // 绑定页面事件
  303. bindEvents();
  304. })
  305. }).catch(function(e) {
  306. plus.nativeUI.closeWaiting();
  307. console && console.error(e);
  308. });