sousuoendzixun.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null;
  7. // 搜索框
  8. var $searchbar = $('.searchbar'),
  9. // 搜索输入框
  10. $searchbarInput = $('.searchbar input'),
  11. // 搜索取消按钮
  12. $searchCancelBtn = $('.searchbar-cancel'),
  13. // 搜索框下面悬浮的搜索提示
  14. $searchSuggest = $('#search_suggest_text'),
  15. // 搜索结果展示容器
  16. $searchtResult = $('#search_result'),
  17. // 搜索无结果时显示
  18. $noResultWrap = $('#no_result_wrap'),
  19. $endList = $('#end_list'),
  20. QueryParams = GetRequest(),
  21. keyword = null;
  22. activeType = null;
  23. // 分页查询最后一条记录Id
  24. var lastId = 0,
  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. 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 = 7,
  82. // 行数
  83. rowNum = 2,
  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. initEndViewList = function(data,keyword, isAppend) {
  124. var html = template("zixun_li_tmpl", {list: data.list});
  125. if(isAppend === true) {
  126. $endList.append(html);
  127. } else {
  128. $endList.html(html);
  129. }
  130. ellipsisText($endList.find('.c-content-warp'),keyword);
  131. },
  132. // 分页查询医生列表
  133. searchByPaging = function () {
  134. var kw = $searchbarInput.val();
  135. // TODO 示例示例搜索参数
  136. // id: 上次搜索结果列表最后一条记录id,type:7(咨询我的-结束的),type:12(我咨询的-结束的),query:搜素关键字,pageSize:每页条数
  137. var type = 7;//type:7(咨询我的-结束的)
  138. if(activeType=="2"){//type:12(我咨询的-结束的)
  139. type = 12;
  140. }
  141. var url = "doctor/consult/list",
  142. params = { id:0, title: kw,pagesize:15,type:type };
  143. getReqPromise(url,params).then(function(res){
  144. // TODO 为了测试先注释
  145. var data = res;
  146. lastId = getLastId(data);
  147. if(!iscroller) {
  148. iscroller = initScroller($searchtResult,url,
  149. function() { // 传递分页参数
  150. return $.extend({},params,{id:lastId});
  151. },function(data) {
  152. // TODO 示例数据
  153. lastId = getLastId(data) || lastId;
  154. var kw = $searchbarInput.val();
  155. initEndViewList(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. initEndViewList(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,params).then(function(data) {
  178. if(pullUpAction && $.isFunction(pullUpAction)) {
  179. pullUpAction(data);
  180. updatePullUpText(scroller,data.list);
  181. }
  182. })
  183. }});
  184. return scroller;
  185. },
  186. // 获取分页搜索返回的最后一条记录的id
  187. getLastId = function(data) {
  188. var lastObj = data.list && data.list.length && data.list[data.list.length-1];
  189. // 最后一条记录
  190. if(lastObj) {
  191. return lastObj.id;
  192. } else {
  193. return null
  194. }
  195. },
  196. // 更新分页上拉加载的提示文本
  197. updatePullUpText= function(scroller,list) {
  198. var $wrap = $(scroller.wrapper),
  199. $pullupLabel = $wrap.find('.pullUpLabel');
  200. if(!list || !list.length) {
  201. $pullupLabel.text('没有更多');
  202. } else {
  203. $pullupLabel.text('上拉加载更多');
  204. }
  205. scroller.on('refresh',function() {
  206. if(!list || !list.length) {
  207. $pullupLabel.text('没有更多');
  208. } else {
  209. $pullupLabel.text('上拉加载更多');
  210. }
  211. });
  212. },
  213. // 绑定页面事件
  214. bindEvents = function () {
  215. $searchbar.bind("click",function(){
  216. var oldback = mui.back;
  217. mui.back=function(){
  218. var wobj = plus.webview.getWebviewById("sousuozixun");//获取前一个页面ID
  219. if(wobj) {
  220. mui.fire(wobj, "focusAction");
  221. }
  222. plus.webview.currentWebview().close();
  223. oldback();
  224. }
  225. mui.back();
  226. });
  227. $endList.on('click','li',function() {
  228. var $this = $(this),
  229. consult = $this.attr('data-consult'),
  230. code = $this.attr('data-code'),
  231. name = $this.attr('data-name'),
  232. photo = $this.attr('data-photo'),
  233. sex = $this.attr('data-sex'),
  234. patient = $this.attr('data-patient'),
  235. // (咨询状态(0进行中,1已完成,-1患者取消)
  236. status = $this.attr('data-status');
  237. if(status == "1") {
  238. // flagZx 是为了跳转至咨询详情页面控制聊天对话框的显示
  239. plus.storage.setItem("flagZx","4");
  240. } else {
  241. plus.storage.setItem("flagZx","");
  242. }
  243. openWebview("mingyizixunxiangqing.html",{consult: consult, patient: patient,name: name});
  244. });
  245. };
  246. // 页面业务处理流程开始
  247. new Promise(function(resolve, reject) {
  248. // TODO 临时放开
  249. // resolve(true);
  250. mui.plusReady(function() {
  251. // plus已经准备好,可以往下执行
  252. resolve(true);
  253. });
  254. }).then(function() {
  255. // 获取基础环境信息
  256. return getBaseEnvPromise().then(function(env) {
  257. baseEnv = env;
  258. }).then(function() {
  259. // 获取登录医生信息
  260. loginerInfo = getLoginerInfo();
  261. $searchbar.searchBar();
  262. self = plus.webview.currentWebview();
  263. keyword = self.kw;
  264. activeType = self.activeType;
  265. if(keyword) {
  266. $searchbarInput.val(keyword);
  267. searchByPaging();
  268. $searchtResult.show();
  269. } else {
  270. $searchtResult.hide();
  271. }
  272. // 绑定页面事件
  273. bindEvents();
  274. })
  275. }).catch(function(e) {
  276. plus.nativeUI.closeWaiting();
  277. console && console.error(e);
  278. });