sousuoqitayisheng.js 9.8 KB

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