gengduoyisheng.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // TODO 医生列表示例数据
  2. //var doctorData = {"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":"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:""},
  6. //{"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:""}],"status":200};
  7. //
  8. //// TODO 临时构造plus对象,使得能够在浏览器中进行调试
  9. //var plus = null;
  10. // 基本信息(包括userAgent、上个页面传递的数据)
  11. var baseInfo = null,
  12. // 基础环境信息(包括当前webview)
  13. baseEnv = 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. $yishengList = $('#yisheng_list');
  27. // 分页查询当前页数
  28. var curPage = 1,
  29. // iscroll 滚动条实例
  30. iscroller;
  31. // 获取基本信息(包括userAgent、上个页面传递的数据)
  32. var getBaseInfoPromise= function() {
  33. // 登录的相关信息
  34. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  35. return {
  36. userAgent: userAgent,
  37. accessData: baseEnv.webview.accessData
  38. }
  39. },
  40. // 获取基础环境信息
  41. getBaseEnvPromise = function () {
  42. var env = {
  43. webview: plus&&plus.webview.currentWebview()
  44. };
  45. return Promise.resolve().then(function(res) {
  46. return env;
  47. });
  48. },
  49. // $el: $('.c-content-warp')
  50. getRowProps = function ($el) {
  51. var $textEllipsis = $el.find('.j-text-ellipsis'),
  52. $text = $textEllipsis.eq(0),
  53. $chart = $text.text('a'),
  54. enWidth = $chart.width(),
  55. $chart = $text.text('中'),
  56. zhWidth = $chart.width(),
  57. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  58. rowHeight = $chart.height();
  59. $chart.text('');
  60. return {
  61. chartWidth: {
  62. zh: zhWidth,
  63. en: enWidth
  64. },
  65. rowHeight: Math.max(rowHeight, lineHeight),
  66. rowWidth: $el.width()
  67. };
  68. },
  69. replaceAll = function (text, arr) {
  70. var html = text;
  71. if(!arr || !arr.length) return html;
  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&&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. initYiShengViewList = function(data,keyword,isAppend) {
  129. var html = template("yisheng_li_tmpl", {list: data.list})
  130. if(isAppend === true) {
  131. $yishengList.append(html)
  132. } else {
  133. $yishengList.html(html);
  134. }
  135. ellipsisText($yishengList.find('.c-content-warp'),keyword);
  136. },
  137. // 分页查询列表
  138. searchByPaging = function () {
  139. var kw = $searchbarInput.val();
  140. // TODO 示例示例搜索参数
  141. // id: 上次搜索结果列表最后一条记录id,type:6(进行中),query:搜素关键字,pageSize:每页条数
  142. var url = "/doctor/findDoctorByDept",
  143. params = {key: kw,page:curPage,pagesize:15};
  144. getReqPromise(url,params).then(function(res){
  145. var data = res;
  146. if(!iscroller) {
  147. iscroller = initScroller($searchtResult,url,
  148. function() { // 传递分页参数
  149. return $.extend({},params,{page:curPage+1});
  150. },function(data) {
  151. if(data.list && data.list.length) {
  152. curPage++;
  153. }
  154. var kw = $searchbarInput.val();
  155. initYiShengViewList(data,kw,true);
  156. iscroller.refresh();
  157. });
  158. }
  159. // TODO 示例数据data
  160. if(!data.list || !data.list.length) {
  161. $searchtResult.hide();
  162. $noResultWrap.show();
  163. } else {
  164. $noResultWrap.hide();
  165. // TODO 示例数据data
  166. initYiShengViewList(data,kw);
  167. }
  168. iscroller.refresh();
  169. }).catch(function(e) {
  170. console && console.error(e)
  171. });
  172. },
  173. // 滚动条分页实例初始化
  174. initScroller = function($el,url,getData,pullUpAction) {
  175. var scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  176. var data = getData();
  177. getReqPromise(url,data).then(function(data) {
  178. if(pullUpAction && $.isFunction(pullUpAction)) {
  179. pullUpAction(data);
  180. updatePullUpText(scroller,data.list);
  181. }
  182. })
  183. }});
  184. return scroller;
  185. },
  186. // 更新分页上拉加载的提示文本
  187. updatePullUpText= function(scroller,list) {
  188. var $wrap = $(scroller.wrapper),
  189. $pullupLabel = $wrap.find('.pullUpLabel');
  190. if(!list || !list.length) {
  191. $pullupLabel.text('没有更多');
  192. } else {
  193. $pullupLabel.text('上拉加载更多');
  194. }
  195. scroller.on('refresh',function() {
  196. if(!list || !list.length) {
  197. $pullupLabel.text('没有更多');
  198. } else {
  199. $pullupLabel.text('上拉加载更多');
  200. }
  201. });
  202. },
  203. // 绑定页面事件
  204. bindEvents = function () {
  205. $searchbarInput.on('tap',function() {
  206. mui.fire(baseEnv.webview.opener(),"focusSearchInput");
  207. mui.back();
  208. });
  209. $yishengList.on('tap','li',function() {
  210. var code = $(this).attr('data-code');
  211. openWebview("doctor-homepage.html",{accessData: {
  212. doctor: code
  213. }});
  214. })
  215. };
  216. // 页面业务处理流程开始
  217. new Promise(function(resolve, reject) {
  218. // TODO 临时放开
  219. //resolve(true);
  220. mui.plusReady(function() {
  221. // plus已经准备好,可以往下执行
  222. resolve(true);
  223. });
  224. }).then(function() {
  225. // 获取基础环境信息
  226. return getBaseEnvPromise().then(function(env) {
  227. baseEnv = env;
  228. }).then(function() {
  229. // 获取登录医生信息
  230. baseInfo = getBaseInfoPromise();
  231. $searchbar.searchBar();
  232. var keyword = baseInfo.accessData && baseInfo.accessData.keyword;
  233. if(keyword) {
  234. $searchbarInput.val(keyword);
  235. searchByPaging();
  236. $searchtResult.show();
  237. } else {
  238. $searchtResult.hide();
  239. }
  240. // 绑定页面事件
  241. bindEvents();
  242. })
  243. }).catch(function(e) {
  244. plus.nativeUI.closeWaiting();
  245. console && console.error(e);
  246. });
  247. template.helper("setPhoto", function(p) {
  248. return getImgUrl(p);
  249. });