sousuoyisheng.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // TODO 列表示例数据
  2. //var data = {"msg":"查询成功","list":[
  3. //{"id":"3502050100","consult":"",patient:"P123",photo:"",title:"",name:"张敏敏",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-12 16:49:17",status:"",sex:0,doctorRead:""},
  4. //{"id":"3502050101","consult":"",patient:"P124",photo:"",title:"",name:"李佳佳",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-13 16:49:17",status:"",sex:0,doctorRead:""},
  5. //{"id":"3502050102","consult":"",patient:"P124",photo:"",title:"",name:"李佳敏",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-13 16:49:17",status:"",sex:0,doctorRead:""},
  6. //{"id":"3502050103","consult":"",patient:"P124",photo:"",title:"",name:"赵六",age:23,comment:"激光治疗红血丝治疗部位保持清洁,避免感染和摩擦;部位有痂皮的会自行脱落,不要手动进行抠除。",star:3,type:5,time:"2016-09-13 16:49:17",status:"",sex:0,doctorRead:""}],"status":200};
  7. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  8. //var plus = null;
  9. // 登录者相关信息(包括userAgent)
  10. var loginerInfo = null,
  11. // 基础环境信息(包括当前webview)
  12. baseEnv = null;
  13. // 搜索框
  14. var $searchbar = $('.searchbar'),
  15. // 搜索输入框
  16. $searchbarInput = $('.searchbar input'),
  17. // 搜索取消按钮
  18. $searchCancelBtn = $('.searchbar-cancel'),
  19. // 搜索框下面悬浮的搜索提示
  20. $searchSuggest = $('#search_suggest_text'),
  21. // 搜索结果展示容器
  22. $searchtResult = $('#search_result'),
  23. // 搜索无结果时显示
  24. $noResultWrap = $('#no_result_wrap'),
  25. $mingyiWrapper = $('#mingyi_wrapper'),
  26. $qitaWrapper = $('#qita_wrapper'),
  27. $mingyiList = $('#mingyi_list'),
  28. $qitaList = $('#qita_list'),
  29. $selectedNum = $('#selected_num'),
  30. $moreResult = $('.more-result');
  31. // 获取登录相关信息
  32. var getLoginerInfo = function() {
  33. // 登录的相关信息
  34. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  35. return {
  36. userAgent: userAgent
  37. }
  38. },
  39. // 获取基础环境信息
  40. getBaseEnvPromise = function () {
  41. var env = {
  42. webview: plus&&plus.webview.currentWebview()
  43. };
  44. return Promise.resolve().then(function(res) {
  45. return env;
  46. });
  47. },
  48. // $el: $('.c-content-warp')
  49. getRowProps = function ($el) {
  50. var $textEllipsis = $el.find('.j-text-ellipsis'),
  51. $text = $textEllipsis.eq(0),
  52. $chart = $text.text('a'),
  53. enWidth = $chart.width(),
  54. $chart = $text.text('中'),
  55. zhWidth = $chart.width(),
  56. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  57. rowHeight = $chart.height();
  58. $chart.text('');
  59. return {
  60. chartWidth: {
  61. zh: zhWidth,
  62. en: enWidth
  63. },
  64. rowHeight: Math.max(rowHeight, lineHeight),
  65. rowWidth: $el.width()
  66. };
  67. },
  68. replaceAll = function (text, arr) {
  69. var html = text;
  70. _.each(arr,function(kw) {
  71. var reg = new RegExp(kw+"(?!>)","gi");
  72. html = html.replace(reg,'<em>'+kw+'</em>');
  73. });
  74. return html;
  75. },
  76. highlineKeyword = function ($el,searchText) {
  77. var props = getRowProps($el),
  78. chartWidth = props.chartWidth,
  79. rowHeight = props.rowHeight,
  80. rowWidth = props.rowWidth,
  81. // 每行显示字符数(以中文字符为标准计算)
  82. chartNum = Math.floor(rowWidth / chartWidth.zh),
  83. // 排除指定数量字符所占宽度
  84. exceptNum = 0,
  85. // 行数
  86. rowNum = 1,
  87. // 预计显示总字符数
  88. expectedNum = chartNum * rowNum - exceptNum,
  89. $target = $el.find('.j-text-ellipsis'),
  90. // 目标文本
  91. text = $target.attr('data-text').trim(),
  92. length = text.length,
  93. // 关键字数组
  94. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  95. var fidx = 0,preFidx,diff = 0;
  96. $target.html(replaceAll(text, kws));
  97. if(Math.floor($target.height() / rowHeight) <= rowNum) {
  98. return ;
  99. }
  100. if(text.length > expectedNum) {
  101. fidx = text.indexOf(kws[0])+kws[0].length-1;
  102. diff = fidx - expectedNum + 1;
  103. preFidx = fidx;
  104. diff = (diff<0)?0:diff;
  105. var preChar = (diff>0)?"...":"";
  106. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  107. while((Math.floor($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  108. preFidx++;
  109. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  110. }
  111. if(preFidx == length && (Math.floor($target.height() / rowHeight) <= rowNum)) {
  112. diff = diff>0?(diff - 1):0;
  113. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  114. } else if((Math.floor($target.height() / rowHeight) > rowNum)) {
  115. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  116. }
  117. }
  118. },
  119. ellipsisText = function ($elements, searchText) {
  120. //$el.ellipsis({ row: 2});
  121. _.each($elements,function(el) {
  122. highlineKeyword($(el),searchText)
  123. });
  124. },
  125. // 控制搜索关键字悬浮提示的显示
  126. showSearchSuggest = function(text) {
  127. var suggestText = '搜索“'+text+'”';
  128. // 如果text不为空,则显示;否则隐藏
  129. if(text&&text.trim().length) {
  130. $searchSuggest.text(suggestText);
  131. $searchSuggest.show();
  132. } else {
  133. $searchSuggest.text('');
  134. $searchSuggest.hide();
  135. }
  136. },
  137. // 初始化“名医”视图列表
  138. initMingYiViewList = function(list) {
  139. var html = template("yisheng_li_tmpl", {list: list.slice(0,3)}),
  140. searchText = $searchbarInput.val().trim();
  141. // 搜索结果小于等于3个时,隐藏“查看更多”
  142. if(list.length<=3) {
  143. $mingyiWrapper.find('.more-result').hide();
  144. } else {
  145. $mingyiWrapper.find('.more-result').show();
  146. }
  147. $mingyiList.html(html);
  148. ellipsisText($mingyiList.find('.c-list-info'),searchText);
  149. },
  150. // 初始化“其它”视图列表
  151. initQiTaViewList = function(list) {
  152. var html = template("yisheng_qt_li_tmpl", {list: list.slice(0,3)}),
  153. searchText = $searchbarInput.val().trim();
  154. // 搜索结果小于等于3个时,隐藏“查看更多”
  155. if(list.length<=3) {
  156. $qitaWrapper.find('.more-result').hide();
  157. } else {
  158. $qitaWrapper.find('.more-result').show();
  159. }
  160. $qitaList.html(html);
  161. ellipsisText($qitaList.find('.c-list-info'),searchText);
  162. },
  163. // 搜索框搜索执行方法
  164. search = function () {
  165. var kw = $searchbarInput.val().trim(),
  166. // "名医"搜索结果列表数据
  167. mingyiList = [],
  168. // "其它"搜索结果列表数据
  169. qitaList = [],
  170. // TODO promise请求测试示例
  171. promise = Promise.resolve();
  172. promise = getReqPromises([{url:"doctor/findFamousDoctor",data:{page:1,key: kw,type:1,pagesize:4,level:1}},{url:"doctor/findFamousDoctor",data:{page:1,key: kw,type:0,pagesize:4,level:1}}])
  173. // 隐藏搜索提示
  174. showSearchSuggest(false);
  175. return kw && promise.then(function(datas){
  176. // TODO 测试数据示例
  177. // var mingyiList = data.list,
  178. // qitaList = data.list;
  179. var mingyiList = datas[0].list,
  180. qitaList = datas[1].list;
  181. if(!mingyiList.length && !qitaList.length) {
  182. $noResultWrap.show();
  183. } else {
  184. $noResultWrap.hide();
  185. $searchtResult.show();
  186. initMingYiViewList(mingyiList);
  187. initQiTaViewList(qitaList);
  188. }
  189. })
  190. },
  191. // 显示选中的人数
  192. showSelectedNum = function() {
  193. var num = $searchtResult.find('input[type="checkbox"]:checked').length;
  194. if(num) {
  195. $selectedNum.text('('+num+'人)')
  196. } else {
  197. $selectedNum.text('');
  198. }
  199. },
  200. // 点击“确定”按钮处理方法
  201. submit = function() {
  202. // TODO 这里需要补充处理逻辑
  203. var checklength = $searchtResult.find('input[type="checkbox"]:checked');
  204. if(checklength.length<=0){
  205. mui.toast("请至少选择一名医生!");
  206. return false;
  207. }
  208. var consultCode = localStorage.getItem("consult");
  209. var doctors = new Array();
  210. $searchtResult.find('input[type="checkbox"]:checked').each(function(){
  211. doctors.push($(this).val());
  212. })
  213. sendPost("doctor/consult/transfer", {
  214. consult: consultCode,
  215. doctor: doctors.join(","),
  216. type: 1 //无用
  217. }, null,
  218. function(res) {
  219. if(res.status == 200) {
  220. mui.toast("邀请成功");
  221. setTimeout(function(){
  222. plus.webview.currentWebview().close();
  223. openWebview("../../zdzx/html/jiankangzixunxiangqing.html");
  224. }, 1000);
  225. }
  226. });
  227. },
  228. // 绑定页面事件
  229. bindEvents = function () {
  230. $searchbarInput.on('input', function() {
  231. var text = $(this).val().trim();
  232. $searchtResult.hide();
  233. showSearchSuggest(text);
  234. }).on('keydown',function(e) {
  235. if (e.which === 13) {
  236. search();
  237. }
  238. });
  239. $searchSuggest.on('click',function() {
  240. $searchSuggest.hide();
  241. search();
  242. });
  243. $searchtResult.on('click','li',function(e) {
  244. var $el = $(e.target);
  245. setTimeout(function() {
  246. showSelectedNum();
  247. },0);
  248. if($el.attr('type')=="checkbox") {
  249. return ;
  250. }
  251. var checkbox = $(this).find('input[type="checkbox"]')[0];
  252. if(checkbox.checked) {
  253. checkbox.checked = false;
  254. } else {
  255. checkbox.checked = true;
  256. }
  257. return false;
  258. });
  259. $moreResult.on('click',function() {
  260. var kw = $searchbarInput.val().trim(),
  261. href = $(this).attr('data-href');
  262. var params = {};
  263. params.kw = kw;
  264. openWebviewExtras(href,params);
  265. });
  266. $searchCancelBtn.on("click",function(){
  267. plus.webview.currentWebview().close();
  268. openWebview("yaoqingzkys");
  269. })
  270. window.addEventListener("focusAction", function() {
  271. $searchbarInput.focus();
  272. });
  273. };
  274. $(function(){
  275. $searchbarInput.val('').focus();
  276. $searchtResult.hide();
  277. })
  278. // 页面业务处理流程开始
  279. new Promise(function(resolve, reject) {
  280. // TODO 临时放开
  281. // resolve(true);
  282. mui.plusReady(function() {
  283. // plus已经准备好,可以往下执行
  284. resolve(true);
  285. });
  286. }).then(function() {
  287. // 获取基础环境信息
  288. return getBaseEnvPromise().then(function(env) {
  289. baseEnv = env;
  290. }).then(function() {
  291. // 获取登录医生信息
  292. loginerInfo = getLoginerInfo();
  293. $searchbar.searchBar();
  294. // 绑定页面事件
  295. bindEvents();
  296. })
  297. }).catch(function(e) {
  298. plus.nativeUI.closeWaiting();
  299. console && console.error(e);
  300. });