gengduoyiyuan.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // TODO 医院、社区列表示例数据
  2. // var orgData = {"msg":"查询成功","list":[{"code":"3502050100","name":"海沧区嵩屿街道社区卫生服务中心",address:"升平路40号"},{"code":"3502050101","name":"海沧社区卫生服务站",address:"升平路40号"},{"code":"3502050200","name":"石塘社区卫生服务中心",address:"升平路40号"},{"code":"3502050300","name":"东孚卫生院",address:"升平路40号"},{"code":"3502050301","name":"天竺社区卫生服务站",address:"升平路40号"},{"code":"3502050302","name":"国营厦门第一农场社区卫生服务站",address:"升平路40号"},{"code":"3502050400","name":"新阳社区卫生服务中心",address:"升平路40号"},{"code":"0a11148d-5b04-11e6-8344-fa163e8aee56","name":"厦门市海沧医院","photo":"",address:"升平路40号"}],"status":200};
  3. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  4. // var plus = null;
  5. // 基本信息(包括userAgent、上个页面传递的数据)
  6. var baseInfo = null,
  7. // 基础环境信息(包括当前webview)
  8. baseEnv = null;
  9. // 搜索框
  10. var $searchbar = $('.searchbar'),
  11. // 搜索输入框
  12. $searchbarInput = $('.searchbar input'),
  13. // 搜索取消按钮
  14. $searchCancelBtn = $('.searchbar-cancel'),
  15. // 搜索框下面悬浮的搜索提示
  16. $searchSuggest = $('#search_suggest_text'),
  17. // 搜索结果展示容器
  18. $searchtResult = $('#search_result'),
  19. // 搜索无结果时显示
  20. $noResultWrap = $('#no_result_wrap'),
  21. $yiyuanList = $('#yiyuan_list');
  22. // 分页查询最后一条记录Id
  23. var lastId = 0,
  24. // iscroll 滚动条实例
  25. iscroller;
  26. // 获取基本信息(包括userAgent、上个页面传递的数据)
  27. var getBaseInfoPromise = function() {
  28. // 登录的相关信息
  29. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  30. return {
  31. userAgent: userAgent,
  32. accessData: baseEnv.webview.accessData
  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. if(!arr || !arr.length) return html;
  67. _.each(arr,function(kw) {
  68. var reg = new RegExp(kw+"(?!>)","gi");
  69. html = html.replace(reg,'<em>'+kw+'</em>');
  70. });
  71. return html;
  72. },
  73. highlineKeyword = function ($el,searchText) {
  74. var props = getRowProps($el),
  75. chartWidth = props.chartWidth,
  76. rowHeight = props.rowHeight,
  77. rowWidth = props.rowWidth,
  78. // 每行显示字符数(以中文字符为标准计算)
  79. chartNum = Math.floor(rowWidth / chartWidth.zh),
  80. // 排除指定数量字符所占宽度
  81. exceptNum = 0,
  82. // 行数
  83. rowNum = 1,
  84. // 预计显示总字符数
  85. expectedNum = chartNum * rowNum - exceptNum,
  86. $target = $el.find('.j-text-ellipsis'),
  87. // 目标文本
  88. text = $target.attr('data-text').trim(),
  89. length = text.length,
  90. // 关键字数组
  91. kws = searchText&&searchText.trim().replace(/\s+/g," ").split(" ");
  92. var fidx = 0,preFidx,diff = 0;
  93. $target.html(replaceAll(text, kws));
  94. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  95. return ;
  96. }
  97. if(text.length > expectedNum) {
  98. fidx = text.indexOf(kws[0])+kws[0].length-1;
  99. diff = fidx - expectedNum + 1;
  100. preFidx = fidx;
  101. diff = (diff<0)?0:diff;
  102. var preChar = (diff>0)?"...":"";
  103. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  104. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  105. preFidx++;
  106. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  107. }
  108. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  109. diff = diff>0?(diff - 1):0;
  110. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  111. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  112. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  113. }
  114. }
  115. },
  116. ellipsisText = function ($elements, searchText) {
  117. //$el.ellipsis({ row: 2});
  118. _.each($elements,function(el) {
  119. highlineKeyword($(el),searchText)
  120. });
  121. },
  122. // 初始化“医院”视图列表
  123. initYiYuanViewList = function(data,keyword,isAppend) {
  124. var html = template("org_li_tmpl", {list: data.list})
  125. if(isAppend === true) {
  126. $yiyuanList.append(html)
  127. } else {
  128. $yiyuanList.html(html);
  129. }
  130. ellipsisText($yiyuanList.find('.c-content-warp'),keyword);
  131. },
  132. // 分页查询列表
  133. searchByPaging = function () {
  134. var kw = $searchbarInput.val();
  135. // TODO 示例示例搜索参数
  136. // id: 上次搜索结果列表最后一条记录id,type:6(进行中),query:搜素关键字,pageSize:每页条数
  137. var url = "doctor/hospitals",
  138. params = {id:0,key: kw, type:1, pagesize:15};
  139. getReqPromise(url,params).then(function(res){
  140. var data = res;
  141. lastId = getLastId(data);
  142. if(!iscroller) {
  143. iscroller = initScroller($searchtResult,url,
  144. function() { // 传递分页参数
  145. return $.extend({},params,{id:lastId});
  146. },function(data) {
  147. lastId = getLastId(data) || lastId;
  148. var kw = $searchbarInput.val();
  149. initYiYuanViewList(data,kw,true);
  150. iscroller.refresh();
  151. });
  152. }
  153. if(!data.list || !data.list.length) {
  154. $searchtResult.hide();
  155. $noResultWrap.show();
  156. } else {
  157. $noResultWrap.hide();
  158. initYiYuanViewList(data,kw);
  159. }
  160. iscroller.refresh();
  161. }).catch(function(e) {
  162. console && console.error(e)
  163. });
  164. },
  165. // 滚动条分页实例初始化
  166. initScroller = function($el,url,getData,pullUpAction) {
  167. var scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  168. var data = getData();
  169. getReqPromise(url,data).then(function(data) {
  170. if(pullUpAction && $.isFunction(pullUpAction)) {
  171. pullUpAction(data);
  172. updatePullUpText(scroller,data.list);
  173. }
  174. })
  175. }});
  176. return scroller;
  177. },
  178. // 获取分页搜索返回的最后一条记录的id
  179. getLastId = function(data) {
  180. var lastObj = data.list && data.list.length && data.list[data.list.length-1];
  181. // 最后一条记录
  182. if(lastObj) {
  183. return lastObj.id;
  184. } else {
  185. return null
  186. }
  187. },
  188. // 更新分页上拉加载的提示文本
  189. updatePullUpText= function(scroller,list) {
  190. var $wrap = $(scroller.wrapper),
  191. $pullupLabel = $wrap.find('.pullUpLabel');
  192. if(!list || !list.length) {
  193. $pullupLabel.text('没有更多');
  194. } else {
  195. $pullupLabel.text('上拉加载更多');
  196. }
  197. scroller.on('refresh',function() {
  198. if(!list || !list.length) {
  199. $pullupLabel.text('没有更多');
  200. } else {
  201. $pullupLabel.text('上拉加载更多');
  202. }
  203. });
  204. },
  205. // 绑定页面事件
  206. bindEvents = function () {
  207. $searchbarInput.on('tap',function() {
  208. mui.fire(baseEnv.webview.opener(),"focusSearchInput");
  209. mui.back();
  210. });
  211. };
  212. // 页面业务处理流程开始
  213. new Promise(function(resolve, reject) {
  214. // TODO 临时放开
  215. // resolve(true);
  216. mui.plusReady(function() {
  217. // plus已经准备好,可以往下执行
  218. resolve(true);
  219. });
  220. }).then(function() {
  221. // 获取基础环境信息
  222. return getBaseEnvPromise().then(function(env) {
  223. baseEnv = env;
  224. }).then(function() {
  225. // 获取登录医生信息
  226. baseInfo = getBaseInfoPromise();
  227. $searchbar.searchBar();
  228. var keyword = baseInfo.accessData && baseInfo.accessData.keyword;
  229. if(keyword) {
  230. $searchbarInput.val(keyword);
  231. searchByPaging();
  232. $searchtResult.show();
  233. } else {
  234. $searchtResult.hide();
  235. }
  236. // 绑定页面事件
  237. bindEvents();
  238. })
  239. }).catch(function(e) {
  240. plus.nativeUI.closeWaiting();
  241. console && console.error(e);
  242. });
  243. template.helper('getImgUrl', function(str){
  244. var str = getImgUrl(str);
  245. if(str == ''){
  246. str = '../images/hospital_default.png';
  247. }
  248. return str;
  249. });