gengduoshequ.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. $shequList = $('#shequ_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 webview = plus.webview.currentWebview();
  38. var env = {
  39. webview: webview
  40. };
  41. return Promise.resolve().then(function(res) {
  42. return env;
  43. });
  44. },
  45. // $el: $('.c-content-warp')
  46. getRowProps = function ($el) {
  47. var $textEllipsis = $el.find('.j-text-ellipsis'),
  48. $text = $textEllipsis.eq(0),
  49. $chart = $text.text('a'),
  50. enWidth = $chart.width(),
  51. $chart = $text.text('中'),
  52. zhWidth = $chart.width(),
  53. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  54. rowHeight = $chart.height();
  55. $chart.text('');
  56. return {
  57. chartWidth: {
  58. zh: zhWidth,
  59. en: enWidth
  60. },
  61. rowHeight: Math.max(rowHeight, lineHeight),
  62. rowWidth: $el.width()
  63. };
  64. },
  65. replaceAll = function (text, arr) {
  66. var html = text;
  67. if(!arr || !arr.length) return html;
  68. _.each(arr,function(kw) {
  69. var reg = new RegExp(kw+"(?!>)","gi");
  70. html = html.replace(reg,'<em>'+kw+'</em>');
  71. });
  72. return html;
  73. },
  74. highlineKeyword = function ($el,searchText) {
  75. var props = getRowProps($el),
  76. chartWidth = props.chartWidth,
  77. rowHeight = props.rowHeight,
  78. rowWidth = props.rowWidth,
  79. // 每行显示字符数(以中文字符为标准计算)
  80. chartNum = Math.floor(rowWidth / chartWidth.zh),
  81. // 排除指定数量字符所占宽度
  82. exceptNum = 0,
  83. // 行数
  84. rowNum = 1,
  85. // 预计显示总字符数
  86. expectedNum = chartNum * rowNum - exceptNum,
  87. $target = $el.find('.j-text-ellipsis'),
  88. // 目标文本
  89. text = $target.attr('data-text').trim(),
  90. length = text.length,
  91. // 关键字数组
  92. kws = searchText&&searchText.trim().replace(/\s+/g," ").split(" ");
  93. var fidx = 0,preFidx,diff = 0;
  94. $target.html(replaceAll(text, kws));
  95. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  96. return ;
  97. }
  98. if(text.length > expectedNum) {
  99. fidx = text.indexOf(kws[0])+kws[0].length-1;
  100. diff = fidx - expectedNum + 1;
  101. preFidx = fidx;
  102. diff = (diff<0)?0:diff;
  103. var preChar = (diff>0)?"...":"";
  104. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  105. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  106. preFidx++;
  107. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  108. }
  109. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  110. diff = diff>0?(diff - 1):0;
  111. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  112. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  113. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  114. }
  115. }
  116. },
  117. ellipsisText = function ($elements, searchText) {
  118. //$el.ellipsis({ row: 2});
  119. _.each($elements,function(el) {
  120. highlineKeyword($(el),searchText)
  121. });
  122. },
  123. // 初始化“社区”视图列表
  124. initSheQuViewList = function(data,keyword,isAppend) {
  125. var html = template("org_li_tmpl", {list: data.list})
  126. if(isAppend === true) {
  127. $shequList.append(html)
  128. } else {
  129. $shequList.html(html);
  130. }
  131. ellipsisText($shequList.find('.c-content-warp'),keyword);
  132. },
  133. // 分页查询列表
  134. searchByPaging = function () {
  135. var kw = $searchbarInput.val();
  136. // TODO 示例示例搜索参数
  137. // id: 上次搜索结果列表最后一条记录id,type:0全部,1大医院,2社区,key:搜素关键字,pagesize:每页条数
  138. var url = "/doctor/hospitals",
  139. params = { id:0, key: kw,pagesize:15,type:2 };
  140. getReqPromise(url,params).then(function(res){
  141. var data = res;
  142. lastId = getLastId(data);
  143. if(!iscroller) {
  144. iscroller = initScroller($searchtResult,url,
  145. function() { // 传递分页参数
  146. return $.extend({},params,{id:lastId});
  147. },function(data) {
  148. // TODO 示例数据
  149. //data = {"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};
  150. lastId = getLastId(data) || lastId;
  151. var kw = $searchbarInput.val();
  152. initSheQuViewList(data,kw,true);
  153. iscroller.refresh();
  154. });
  155. }
  156. // TODO 示例数据data
  157. if(!data.list || !data.list.length) {
  158. $searchtResult.hide();
  159. $noResultWrap.show();
  160. } else {
  161. $noResultWrap.hide();
  162. // TODO 示例数据data
  163. initSheQuViewList(data,kw);
  164. }
  165. iscroller.refresh();
  166. }).catch(function(e) {
  167. console && console.error(e)
  168. });
  169. },
  170. // 滚动条分页实例初始化
  171. initScroller = function($el,url,getData,pullUpAction) {
  172. var scroller = $el.initScroll({pullDown: false,pullUpAction: function() {
  173. var data = getData();
  174. getReqPromise(url,data).then(function(data) {
  175. if(pullUpAction && $.isFunction(pullUpAction)) {
  176. pullUpAction(data);
  177. updatePullUpText(scroller,data.list);
  178. }
  179. })
  180. }});
  181. return scroller;
  182. },
  183. // 获取分页搜索返回的最后一条记录的id
  184. getLastId = function(data) {
  185. var lastObj = data.list && data.list.length && data.list[data.list.length-1];
  186. // 最后一条记录
  187. if(lastObj) {
  188. return lastObj.id;
  189. } else {
  190. return null
  191. }
  192. },
  193. // 更新分页上拉加载的提示文本
  194. updatePullUpText= function(scroller,list) {
  195. var $wrap = $(scroller.wrapper),
  196. $pullupLabel = $wrap.find('.pullUpLabel');
  197. if(!list || !list.length) {
  198. $pullupLabel.text('没有更多');
  199. } else {
  200. $pullupLabel.text('上拉加载更多');
  201. }
  202. scroller.on('refresh',function() {
  203. if(!list || !list.length) {
  204. $pullupLabel.text('没有更多');
  205. } else {
  206. $pullupLabel.text('上拉加载更多');
  207. }
  208. });
  209. },
  210. // 绑定页面事件
  211. bindEvents = function () {
  212. $searchbarInput.on('tap',function() {
  213. mui.fire(baseEnv.webview.opener(),"focusSearchInput");
  214. mui.back();
  215. });
  216. };
  217. // 页面业务处理流程开始
  218. new Promise(function(resolve, reject) {
  219. // TODO 临时放开
  220. //resolve(true);
  221. mui.plusReady(function() {
  222. // plus已经准备好,可以往下执行
  223. resolve(true);
  224. });
  225. }).then(function() {
  226. // 获取基础环境信息
  227. return getBaseEnvPromise().then(function(env) {
  228. baseEnv = env;
  229. }).then(function() {
  230. // 获取登录医生信息
  231. baseInfo = getBaseInfoPromise();
  232. $searchbar.searchBar();
  233. var keyword = baseInfo.accessData && baseInfo.accessData.keyword;
  234. if(keyword) {
  235. $searchbarInput.val(keyword);
  236. searchByPaging();
  237. $searchtResult.show();
  238. } else {
  239. $searchtResult.hide();
  240. }
  241. // 绑定页面事件
  242. bindEvents();
  243. })
  244. }).catch(function(e) {
  245. plus.nativeUI.closeWaiting();
  246. console && console.error(e);
  247. });
  248. template.helper('getImgUrl', function(str){
  249. var str = getImgUrl(str);
  250. if(str == ''){
  251. str = '../images/hospital_default.png';
  252. }
  253. return str;
  254. });