sousuojuming.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. $doingList = $('#doing_list'),
  23. $moreResult = $('.more-result');
  24. mui.plusReady(function() {
  25. self = plus.webview.currentWebview();
  26. activeType = self.type;//1为咨询我的 2为我咨询的
  27. });
  28. // 获取登录相关信息
  29. var getLoginerInfo = function() {
  30. // 登录的相关信息
  31. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  32. return {
  33. userAgent: userAgent
  34. }
  35. },
  36. // 获取基础环境信息
  37. getBaseEnvPromise = function () {
  38. var env = {
  39. webview: plus&&plus.webview.currentWebview()
  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. _.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.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. showSearchSuggest = function(text) {
  124. var suggestText = '搜索“'+text+'”';
  125. // 如果text不为空,则显示;否则隐藏
  126. if(text&&text.trim().length) {
  127. $searchSuggest.text(suggestText);
  128. $searchSuggest.show();
  129. } else {
  130. $searchSuggest.text('');
  131. $searchSuggest.hide();
  132. }
  133. },
  134. // 初始化“进行中”视图列表
  135. initDoingViewList = function(list) {
  136. var searchText = $searchbarInput.val().trim();
  137. template.helper("setLabels", function(labels) {
  138. var labelStr = "";
  139. for(var i in labels){
  140. labelStr+=labels[i].labelName+","
  141. }
  142. labelStr = labelStr.substring(0,labelStr.length-1);
  143. return labelStr;
  144. });
  145. var html = template("zixun_li_tmpl", {list: list});
  146. $doingList.html(html);
  147. ellipsisText($doingList.find('.c-content-warp'),searchText);
  148. },
  149. // 搜索框搜索执行方法
  150. search = function () {
  151. var kw = $searchbarInput.val().trim();
  152. // "进行中"搜索结果列表数据
  153. promise = getReqPromises([{reqType:"GET",url:"/doctor/patient_label_info/patient_search_all",data:{teamCode:0,filter: kw,page:-1,pagesize:2}}])
  154. // 隐藏搜索提示
  155. showSearchSuggest(false);
  156. return kw && promise.then(function(datas){
  157. var doingList = datas[0].data;
  158. if(!doingList.length) {
  159. $noResultWrap.show();
  160. $searchtResult.hide();
  161. } else {
  162. $noResultWrap.hide();
  163. $searchtResult.show();
  164. $doingWrapper.show();
  165. initDoingViewList(doingList);
  166. }
  167. })
  168. },
  169. // 绑定页面事件
  170. bindEvents = function () {
  171. $searchbarInput.on('input', function() {
  172. var text = $(this).val().trim();
  173. $searchtResult.hide();
  174. showSearchSuggest(text);
  175. }).on('keydown',function(e) {
  176. if (e.which === 13) {
  177. search();
  178. }
  179. });
  180. $searchSuggest.on('click',function() {
  181. $searchSuggest.hide();
  182. search();
  183. $searchtResult.show();
  184. });
  185. $searchtResult.on('click','li',function() {
  186. var $this = $(this),
  187. code = $this.attr('data-code'),
  188. name = $this.attr('data-name'),
  189. photo = $this.attr('data-photo'),
  190. sex = $this.attr('data-sex');
  191. // 转发的消息
  192. var messages = baseEnv.webview.imMessages;
  193. mui.confirm("是否确认将消息逐条转发给"+name+"?", "提示", ["不了,谢谢", "确认转发"], function(e) {
  194. if(e.index == 0) {
  195. } else {
  196. Promise.all(_.map(messages,function(msg){
  197. return new Promise(function(resolve, reject) {
  198. im.sendPrivateMsg(baseInfo.userAgent.uid,code,msg.content,msg.type,function() {
  199. resolve(true);
  200. },function() {
  201. resolve(true);
  202. })
  203. })
  204. })).then(function() {
  205. // openWebview("../../message/html/p2p.html",{otherCode: code,otherName: name,otherPhoto: photo,otherSex: sex});
  206. var time = new Date().getTime();
  207. mui.openWindow({
  208. url:"../../message/html/p2p.html",
  209. id: "p2p"+time+".html",
  210. extras: {
  211. otherCode: code,
  212. otherName: name,
  213. otherPhoto: photo,
  214. otherSex: sex
  215. }
  216. })
  217. });
  218. }
  219. })
  220. });
  221. window.onpageshow = function() {
  222. $searchbarInput.val('').focus();
  223. $searchtResult.hide();
  224. }
  225. window.addEventListener("focusAction", function() {
  226. $searchbarInput.focus();
  227. });
  228. $searchCancelBtn.on("click",function(){
  229. plus.webview.currentWebview().close();
  230. openWebview("mingyizixunliebiao.html");
  231. })
  232. };
  233. $(function(){
  234. $searchbarInput.val('').focus();
  235. $searchtResult.hide();
  236. })
  237. // 页面业务处理流程开始
  238. new Promise(function(resolve, reject) {
  239. // TODO 临时放开
  240. // resolve(true);
  241. mui.plusReady(function() {
  242. // hrefhrefplus已经准备好,可以往下执行
  243. resolve(true);
  244. });
  245. }).then(function() {
  246. // 获取基础环境信息
  247. return getBaseEnvPromise().then(function(env) {
  248. baseEnv = env;
  249. }).then(function() {
  250. // 获取登录医生信息
  251. loginerInfo = getLoginerInfo();
  252. $searchbar.searchBar();
  253. // 绑定页面事件
  254. bindEvents();
  255. })
  256. }).catch(function(e) {
  257. plus.nativeUI.closeWaiting();
  258. console && console.error(e);
  259. });
  260. template.helper("setPhoto", function(p) {
  261. return getImgUrl(p);
  262. });