sousuozixun.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. self = null,
  8. activeType = 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. $doingWrapper = $('#doing_wrapper'),
  22. $endWrapper = $('#end_wrapper'),
  23. $doingList = $('#doing_list'),
  24. $endList = $('#end_list'),
  25. $moreResult = $('.more-result');
  26. mui.plusReady(function() {
  27. self = plus.webview.currentWebview();
  28. activeType = self.type;//1为咨询我的 2为我咨询的
  29. });
  30. // 获取登录相关信息
  31. var getLoginerInfo = function() {
  32. // 登录的相关信息
  33. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  34. return {
  35. userAgent: userAgent
  36. }
  37. },
  38. // 获取基础环境信息
  39. getBaseEnvPromise = function () {
  40. var env = {
  41. webview: plus&&plus.webview.currentWebview()
  42. };
  43. return Promise.resolve().then(function(res) {
  44. return env;
  45. });
  46. },
  47. // $el: $('.c-content-warp')
  48. getRowProps = function ($el) {
  49. var $textEllipsis = $el.find('.j-text-ellipsis'),
  50. $text = $textEllipsis.eq(0),
  51. $chart = $text.text('a'),
  52. enWidth = $chart.width(),
  53. $chart = $text.text('中'),
  54. zhWidth = $chart.width(),
  55. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  56. rowHeight = $chart.height();
  57. $chart.text('');
  58. return {
  59. chartWidth: {
  60. zh: zhWidth,
  61. en: enWidth
  62. },
  63. rowHeight: Math.max(rowHeight, lineHeight),
  64. rowWidth: $el.width()
  65. };
  66. },
  67. replaceAll = function (text, arr) {
  68. var html = text;
  69. _.each(arr,function(kw) {
  70. var reg = new RegExp(kw+"(?!>)","gi");
  71. html = html.replace(reg,'<em>'+kw+'</em>');
  72. });
  73. return html;
  74. },
  75. highlineKeyword = function ($el,searchText) {
  76. var props = getRowProps($el),
  77. chartWidth = props.chartWidth,
  78. rowHeight = props.rowHeight,
  79. rowWidth = props.rowWidth,
  80. // 每行显示字符数(以中文字符为标准计算)
  81. chartNum = Math.floor(rowWidth / chartWidth.zh),
  82. // 排除“咨询问题:”字符及前后“...”所占宽度
  83. exceptNum = 7,
  84. // 行数
  85. rowNum = 2,
  86. // 预计显示总字符数
  87. expectedNum = chartNum * rowNum - exceptNum,
  88. $target = $el.find('.j-text-ellipsis'),
  89. // 目标文本
  90. text = $target.attr('data-text').trim(),
  91. length = text.length,
  92. // 关键字数组
  93. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  94. var fidx = 0,preFidx,diff = 0;
  95. $target.html(replaceAll(text, kws));
  96. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  97. return ;
  98. }
  99. if(text.length > expectedNum) {
  100. fidx = text.indexOf(kws[0])+kws[0].length-1;
  101. diff = fidx - expectedNum + 1;
  102. preFidx = fidx;
  103. diff = (diff<0)?0:diff;
  104. var preChar = (diff>0)?"...":"";
  105. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  106. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  107. preFidx++;
  108. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  109. }
  110. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  111. diff = diff>0?(diff - 1):0;
  112. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  113. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  114. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  115. }
  116. }
  117. },
  118. ellipsisText = function ($elements, searchText) {
  119. //$el.ellipsis({ row: 2});
  120. _.each($elements,function(el) {
  121. highlineKeyword($(el),searchText)
  122. });
  123. },
  124. // 控制搜索关键字悬浮提示的显示
  125. showSearchSuggest = function(text) {
  126. var suggestText = '搜索“'+text+'”';
  127. // 如果text不为空,则显示;否则隐藏
  128. if(text&&text.trim().length) {
  129. $searchSuggest.text(suggestText);
  130. $searchSuggest.show();
  131. } else {
  132. $searchSuggest.text('');
  133. $searchSuggest.hide();
  134. }
  135. },
  136. // 初始化“进行中”视图列表
  137. initDoingViewList = function(list) {
  138. var html = "";
  139. var searchText = $searchbarInput.val().trim();
  140. if(activeType=="1"){//咨询我的
  141. html = template("zixun_li_tmpl", {list: list.slice(0,3)});
  142. }else{//我咨询的
  143. html = template("wzixun_li_tmpl", {list: list.slice(0,3)});
  144. }
  145. // 搜索结果小于等于3个时,隐藏“查看更多”
  146. if(list.length<=3) {
  147. $doingWrapper.find('.more-result').hide();
  148. } else {
  149. $doingWrapper.find('.more-result').show();
  150. }
  151. $doingList.html(html);
  152. ellipsisText($doingList.find('.c-content-warp'),searchText);
  153. },
  154. // 初始化“已结束”视图列表
  155. initEndViewList = function(list) {
  156. var searchText = $searchbarInput.val().trim();
  157. if(activeType=="1"){//咨询我的
  158. html = template("zixun_li_tmpl", {list: list.slice(0,3)});
  159. }else{//我咨询的
  160. html = template("wzixun_li_tmpl", {list: list.slice(0,3)});
  161. }
  162. // 搜索结果小于等于3个时,隐藏“查看更多”
  163. if(list.length<=3) {
  164. $endWrapper.find('.more-result').hide();
  165. } else {
  166. $endWrapper.find('.more-result').show();
  167. }
  168. $endList.html(html);
  169. ellipsisText($endList.find('.c-content-warp'),searchText);
  170. },
  171. // 搜索框搜索执行方法
  172. search = function () {
  173. var kw = $searchbarInput.val().trim(),
  174. // "进行中"搜索结果列表数据
  175. doingList = [],
  176. // "已结束"搜索结果列表数据
  177. endList = [],
  178. urls = [];
  179. // TODO promise请求测试示例
  180. // promise = Promise.resolve();
  181. if(activeType=="1"){//咨询我的
  182. urls = [{url:"doctor/consult/list",data:{id:0,title: kw,type:6,pagesize:4}},{url:"doctor/consult/list",data:{id:0,title: kw,type:7,pagesize:4}}]
  183. }else{//我咨询的
  184. urls = [{url:"doctor/consult/list",data:{id:0,title: kw,type:11,pagesize:4}},{url:"doctor/consult/list",data:{id:0,title: kw,type:12,pagesize:4}}]
  185. }
  186. promise = getReqPromises(urls)
  187. // 隐藏搜索提示
  188. showSearchSuggest(false);
  189. return kw && promise.then(function(datas){
  190. var doingList = datas[0].list,
  191. endList = datas[1].list;
  192. if(!doingList.length && !endList.length) {
  193. $noResultWrap.show();
  194. $searchtResult.hide();
  195. } else {
  196. $noResultWrap.hide();
  197. $searchtResult.show();
  198. if(doingList.length && !endList.length){
  199. $doingWrapper.show();
  200. $endWrapper.hide();
  201. initDoingViewList(doingList);
  202. }
  203. if(!doingList.length && endList.length){
  204. $doingWrapper.hide();
  205. $endWrapper.show();
  206. initEndViewList(endList);
  207. }
  208. if(doingList.length && endList.length){
  209. $doingWrapper.show();
  210. $endWrapper.show();
  211. initDoingViewList(doingList);
  212. initEndViewList(endList);
  213. }
  214. }
  215. })
  216. },
  217. // 绑定页面事件
  218. bindEvents = function () {
  219. $searchbarInput.on('input', function() {
  220. var text = $(this).val().trim();
  221. $searchtResult.hide();
  222. showSearchSuggest(text);
  223. }).on('keydown',function(e) {
  224. if (e.which === 13) {
  225. search();
  226. }
  227. });
  228. $searchSuggest.on('click',function() {
  229. $searchSuggest.hide();
  230. search();
  231. $searchtResult.show();
  232. });
  233. $moreResult.on('click',function() {
  234. var kw = $searchbarInput.val().trim(),
  235. href = $(this).attr('data-href');
  236. var params = {};
  237. params.kw = kw;
  238. params.activeType = activeType;
  239. console.log(href);
  240. openWebviewExtras(href,params);
  241. });
  242. $searchtResult.on('click','li',function() {
  243. var $this = $(this),
  244. consult = $this.attr('data-consult'),
  245. code = $this.attr('data-code'),
  246. name = $this.attr('data-name'),
  247. photo = $this.attr('data-photo'),
  248. sex = $this.attr('data-sex'),
  249. patient = $this.attr('data-patient'),
  250. // (咨询状态(0进行中,1已完成,-1患者取消)
  251. status = $this.attr('data-status');
  252. // if(status == "1") {
  253. // // flagZx 是为了跳转至咨询详情页面控制聊天对话框的显示
  254. // plus.storage.setItem("flagZx","4");
  255. // } else {
  256. // plus.storage.setItem("flagZx","");
  257. // }
  258. mui.openWindow({
  259. id: "p2p",
  260. url: "../../message/html/p2p.html",
  261. extras: {
  262. otherCode: patient,
  263. otherName: name,
  264. otherPhoto: photo,
  265. otherSex: sex
  266. }
  267. })
  268. //openWebview("mingyizixunxiangqing.html",{consult: consult, patient: patient,name: name});
  269. });
  270. window.onpageshow = function() {
  271. $searchbarInput.val('').focus();
  272. $searchtResult.hide();
  273. }
  274. window.addEventListener("focusAction", function() {
  275. $searchbarInput.focus();
  276. });
  277. $searchCancelBtn.on("click",function(){
  278. plus.webview.currentWebview().close();
  279. openWebview("mingyizixunliebiao.html");
  280. })
  281. };
  282. $(function(){
  283. $searchbarInput.val('').focus();
  284. $searchtResult.hide();
  285. })
  286. // 页面业务处理流程开始
  287. new Promise(function(resolve, reject) {
  288. // TODO 临时放开
  289. // resolve(true);
  290. mui.plusReady(function() {
  291. // hrefhrefplus已经准备好,可以往下执行
  292. resolve(true);
  293. });
  294. }).then(function() {
  295. // 获取基础环境信息
  296. return getBaseEnvPromise().then(function(env) {
  297. baseEnv = env;
  298. }).then(function() {
  299. // 获取登录医生信息
  300. loginerInfo = getLoginerInfo();
  301. $searchbar.searchBar();
  302. // 绑定页面事件
  303. bindEvents();
  304. })
  305. }).catch(function(e) {
  306. plus.nativeUI.closeWaiting();
  307. console && console.error(e);
  308. });
  309. template.helper("setPhoto", function(p) {
  310. return getImgUrl(p);
  311. });