sousuosimpleliaotianjilu.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. self = null,
  8. docInfo = null,
  9. kw = null,
  10. myScroll,
  11. pageType,
  12. chatCode = null,
  13. type = null;
  14. // 搜索框
  15. var $searchbar = $('.searchbar'),
  16. // 搜索输入框
  17. $searchbarInput = $('.searchbar input'),
  18. // 搜索取消按钮
  19. $searchCancelBtn = $('.searchbar-cancel'),
  20. // 搜索框下面悬浮的搜索提示
  21. $searchSuggest = $('#search_suggest_text'),
  22. // 搜索结果展示容器
  23. $searchtResult = $('#search_result'),
  24. // 搜索无结果时显示
  25. $noResultWrap = $('#no_result_wrap'),
  26. $doingWrapper = $('#doing_wrapper'),
  27. $doingList = $('#doing_list'),
  28. $moreResult = $('.more-result');
  29. mui.plusReady(function() {
  30. self = plus.webview.currentWebview();
  31. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  32. myScroll = $('#search_result').lscroll();
  33. pageType = self.pageType;
  34. kw = self.kw;//搜索关键字
  35. type = self.type;//记录详情
  36. chatCode = self.chatCode;
  37. search();
  38. });
  39. // 获取登录相关信息
  40. var getLoginerInfo = function() {
  41. // 登录的相关信息
  42. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  43. return {
  44. userAgent: userAgent
  45. }
  46. },
  47. // 获取基础环境信息
  48. getBaseEnvPromise = function () {
  49. var env = {
  50. webview: plus&&plus.webview.currentWebview()
  51. };
  52. return Promise.resolve().then(function(res) {
  53. return env;
  54. });
  55. },
  56. // $el: $('.c-content-warp')
  57. getRowProps = function ($el) {
  58. var $textEllipsis = $el.find('.j-text-ellipsis'),
  59. $text = $textEllipsis.eq(0),
  60. $chart = $text.text('a'),
  61. enWidth = $chart.width(),
  62. $chart = $text.text('中'),
  63. zhWidth = $chart.width(),
  64. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  65. rowHeight = $chart.height();
  66. $chart.text('');
  67. return {
  68. chartWidth: {
  69. zh: zhWidth,
  70. en: enWidth
  71. },
  72. rowHeight: Math.max(rowHeight, lineHeight),
  73. rowWidth: $el.width()
  74. };
  75. },
  76. replaceAll = function (text, arr) {
  77. var html = text;
  78. _.each(arr,function(kw) {
  79. var reg = new RegExp(kw+"(?!>)","gi");
  80. html = html.replace(reg,'<em>'+kw+'</em>');
  81. });
  82. return html;
  83. },
  84. highlineKeyword = function ($el,searchText) {
  85. var props = getRowProps($el),
  86. chartWidth = props.chartWidth,
  87. rowHeight = props.rowHeight,
  88. rowWidth = props.rowWidth,
  89. // 每行显示字符数(以中文字符为标准计算)
  90. chartNum = Math.floor(rowWidth / chartWidth.zh),
  91. // 排除“咨询问题:”字符及前后“...”所占宽度
  92. exceptNum = 7,
  93. // 行数
  94. rowNum = 1,
  95. // 预计显示总字符数
  96. expectedNum = chartNum * rowNum - exceptNum,
  97. $target = $el.find('.j-text-ellipsis'),
  98. // 目标文本
  99. text = $target.attr('data-text').trim(),
  100. length = text.length,
  101. // 关键字数组
  102. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  103. var fidx = 0,preFidx,diff = 0;
  104. $target.html(replaceAll(text, kws));
  105. if(Math.ceil($target.height() / rowHeight) <= rowNum) {
  106. return ;
  107. }
  108. if(text.length > expectedNum) {
  109. fidx = text.indexOf(kws[0])+kws[0].length-1;
  110. diff = fidx - expectedNum + 1;
  111. preFidx = fidx;
  112. diff = (diff<0)?0:diff;
  113. var preChar = (diff>0)?"...":"";
  114. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  115. while((Math.ceil($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  116. preFidx++;
  117. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  118. }
  119. if(preFidx == length && (Math.ceil($target.height() / rowHeight) <= rowNum)) {
  120. diff = diff>0?(diff - 1):0;
  121. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  122. } else if((Math.ceil($target.height() / rowHeight) > rowNum)) {
  123. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  124. }
  125. }
  126. },
  127. ellipsisText = function ($elements, searchText) {
  128. //$el.ellipsis({ row: 2});
  129. _.each($elements,function(el) {
  130. highlineKeyword($(el),searchText)
  131. });
  132. },
  133. // 控制搜索关键字悬浮提示的显示
  134. showSearchSuggest = function(text) {
  135. var suggestText = '搜索“'+text+'”';
  136. // 如果text不为空,则显示;否则隐藏
  137. if(text&&text.trim().length) {
  138. $searchSuggest.text(suggestText);
  139. $searchSuggest.show();
  140. } else {
  141. $searchSuggest.text('');
  142. $searchSuggest.hide();
  143. }
  144. },
  145. calcAge = function(timestamp){
  146. var date = new Date(timestamp);
  147. Y = date.getFullYear() + '-';
  148. M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  149. D = date.getDate() < 10 ?'0'+date.getDate():date.getDate();
  150. var birthday = Y+M+D;
  151. var r = birthday.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  152. if(r==null)return false;
  153. var d= new Date(r[1], r[3]-1, r[4]);
  154. if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
  155. var Y = new Date().getFullYear();
  156. return (Y-r[1]);
  157. }
  158. },
  159. setPhoto = function(groupPhoto){
  160. var images = '';
  161. var cssArr=null;
  162. if(groupPhoto.length>=5){
  163. cssArr = ["top: 0;left: 50%;margin-left: -8px;","top: 12px;left: 2px;","top: 12px;left: 28px;","top: 27px;left: 5px;","top: 27px;left: 24px;"];
  164. }else if(groupPhoto.length==4){
  165. cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
  166. }else if(groupPhoto.length==3){
  167. cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
  168. }else if(groupPhoto.length==2){
  169. cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
  170. }else if(groupPhoto.length==1 || groupPhoto.length==0){
  171. cssArr = ["position: absolute;width: 50px;height: 50px;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  172. }
  173. for(var i in groupPhoto){
  174. if(i>4) break;
  175. if(groupPhoto[i].avatar){
  176. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  177. '<img src="'+ getImgUrl(groupPhoto[i].avatar) +'"/>'+
  178. '</div>';
  179. }else{
  180. if(groupPhoto[i].role=="doctor"){
  181. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  182. '<img src="../../../images/d-default.png"/>'+
  183. '</div>';
  184. }else{
  185. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  186. '<img src="../../../images/p-default.png"/>'+
  187. '</div>';
  188. }
  189. }
  190. }
  191. return images;
  192. },
  193. // 初始化“进行中”视图列表
  194. initDoingViewList = function(list) {
  195. $(".locate-city").html(list.length+'条与"'+kw+'"相关记录');
  196. if(type=="1"){//医生和居民聊天记录
  197. template.helper("setAge", function(timestamp) {
  198. return calcAge(timestamp);
  199. });
  200. var html = template("qyjm_li_tmpl", {list: list});
  201. $doingList.html(html);
  202. ellipsisText($doingList.find('.c-content-warp'),kw);
  203. myScroll.refresh();
  204. }else{//群聊
  205. var chatCodeStr = "";
  206. for(var x in list){
  207. chatCodeStr+=list[x].chat+",";
  208. }
  209. var html = template("ltjl_li_tmpl", {list: list});
  210. $doingList.html(html);
  211. ellipsisText($doingList.find('.c-content-warp'),kw);
  212. myScroll.refresh();
  213. }
  214. plus.nativeUI.closeWaiting();
  215. },
  216. // 搜索框搜索执行方法
  217. search = function () {
  218. plus.nativeUI.showWaiting();
  219. imClient.Search.searchMessages(kw, 1, 100, function(res){
  220. console.log(JSON.stringify(res))
  221. if(!res) {
  222. $noResultWrap.show();
  223. $searchtResult.hide();
  224. plus.nativeUI.closeWaiting();
  225. } else {
  226. $noResultWrap.hide();
  227. $searchtResult.show();
  228. $doingWrapper.show();
  229. initDoingViewList(res);
  230. }
  231. }, function(){
  232. mui.toast("搜索失败!");
  233. })
  234. },
  235. // 绑定页面事件
  236. bindEvents = function () {
  237. $searchtResult.on('tap','li',function() {
  238. var $this = $(this),
  239. code = $this.attr('data-code'),
  240. name = $this.attr('data-name'),
  241. photo = $this.attr('data-photo'),
  242. sex = $this.attr('data-sex'),
  243. msgId = $this.attr('data-msgId'),
  244. type = $this.attr('data-type');
  245. if(type=="jumingOryisheng"){//医生和居民间会话或医生和医生会话
  246. openWebview("../../message/html/p2p.html",{otherCode: code,otherName: name,otherPhoto: photo,otherSex: sex,msgId:msgId});
  247. }else{//群聊
  248. openWebview("../../message/html/qiuzhuqunliao.html",{groupName: name, groupCode: code,msgId:msgId});
  249. }
  250. });
  251. };
  252. $(function(){
  253. $searchbarInput.val('').focus();
  254. $searchtResult.hide();
  255. })
  256. // 页面业务处理流程开始
  257. new Promise(function(resolve, reject) {
  258. // TODO 临时放开
  259. // resolve(true);
  260. mui.plusReady(function() {
  261. // hrefhrefplus已经准备好,可以往下执行
  262. resolve(true);
  263. });
  264. }).then(function() {
  265. // 获取基础环境信息
  266. return getBaseEnvPromise().then(function(env) {
  267. baseEnv = env;
  268. }).then(function() {
  269. // 获取登录医生信息
  270. loginerInfo = getLoginerInfo();
  271. // 绑定页面事件
  272. bindEvents();
  273. })
  274. }).catch(function(e) {
  275. plus.nativeUI.closeWaiting();
  276. console && console.error(e);
  277. });
  278. window.addEventListener("initSearch", function(e){
  279. pageType = e.detail.pageType;
  280. kw = e.detail.kw;//搜索关键字
  281. type = e.detail.type;//记录详情
  282. chatCode = e.detail.chatCode;
  283. $doingList.html("");
  284. search();
  285. })
  286. template.helper('getImgUrl', function(str){
  287. return getImgUrl(str);
  288. })