sousuodoingzixun.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. QueryParams = GetRequest(),
  8. keyword = null;
  9. activeType = null;
  10. // 搜索框
  11. var $searchbar = $('.searchbar'),
  12. // 搜索输入框
  13. $searchbarInput = $('.searchbar input'),
  14. // 搜索取消按钮
  15. $searchCancelBtn = $('.searchbar-cancel'),
  16. // 搜索框下面悬浮的搜索提示
  17. $searchSuggest = $('#search_suggest_text'),
  18. // 搜索结果展示容器
  19. $searchtResult = $('#search_result'),
  20. // 搜索无结果时显示
  21. $noResultWrap = $('#no_result_wrap'),
  22. $doingList = $('#doing_list');
  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. initDoingViewList = function(data,keyword,isAppend) {
  124. var html = template("zixun_li_tmpl", {list: data.list})
  125. if(isAppend === true) {
  126. $doingList.append(html)
  127. } else {
  128. $doingList.html(html);
  129. }
  130. ellipsisText($doingList.find('.c-content-warp'),keyword);
  131. },
  132. // 分页查询列表
  133. searchByPaging = function () {
  134. var kw = $searchbarInput.val();
  135. // TODO 示例示例搜索参数
  136. // id: 上次搜索结果列表最后一条记录id,type:6(咨询我的-进行中),type:11(我咨询的-进行中),query:搜素关键字,pageSize:每页条数
  137. var type = 6;//type:6(咨询我的-进行中)
  138. if(activeType=="2"){//type:11(我咨询的-进行中)
  139. type = 11;
  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. initDoingViewList(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. $searchtResult.show();
  166. // TODO 示例数据data
  167. initDoingViewList(data,kw);
  168. }
  169. plus.nativeUI.closeWaiting();
  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. bindEvents = function () {
  217. $searchbar.bind("click",function(){
  218. var oldback = mui.back;
  219. mui.back=function(){
  220. var wobj = plus.webview.getWebviewById("sousuozixun");//获取前一个页面ID
  221. if(wobj) {
  222. mui.fire(wobj, "focusAction");
  223. }
  224. plus.webview.currentWebview().close();
  225. oldback();
  226. }
  227. mui.back();
  228. });
  229. $doingList.on('click','li',function() {
  230. var $this = $(this),
  231. consult = $this.attr('data-consult'),
  232. code = $this.attr('data-code'),
  233. name = $this.attr('data-name'),
  234. photo = $this.attr('data-photo'),
  235. sex = $this.attr('data-sex'),
  236. patient = $this.attr('data-patient'),
  237. // (咨询状态(0进行中,1已完成,-1患者取消)
  238. status = $this.attr('data-status');
  239. if(status == "1") {
  240. // flagZx 是为了跳转至咨询详情页面控制聊天对话框的显示
  241. plus.storage.setItem("flagZx","4");
  242. } else {
  243. plus.storage.setItem("flagZx","");
  244. }
  245. openWebview("mingyizixunxiangqing.html",{consult: consult, patient: patient,name: name});
  246. });
  247. };
  248. // 页面业务处理流程开始
  249. new Promise(function(resolve, reject) {
  250. // TODO 临时放开
  251. // resolve(true);
  252. mui.plusReady(function() {
  253. // plus已经准备好,可以往下执行
  254. resolve(true);
  255. });
  256. }).then(function() {
  257. // 获取基础环境信息
  258. return getBaseEnvPromise().then(function(env) {
  259. baseEnv = env;
  260. }).then(function() {
  261. // 获取登录医生信息
  262. loginerInfo = getLoginerInfo();
  263. $searchbar.searchBar();
  264. self = plus.webview.currentWebview();
  265. keyword = self.kw;
  266. activeType = self.activeType;
  267. if(keyword) {
  268. $searchbarInput.val(keyword);
  269. searchByPaging();
  270. $searchtResult.show();
  271. } else {
  272. $searchtResult.hide();
  273. }
  274. // 绑定页面事件
  275. bindEvents();
  276. })
  277. }).catch(function(e) {
  278. plus.nativeUI.closeWaiting();
  279. console && console.error(e);
  280. });