sousuosimpleliaotianjilu-ys.js 9.0 KB

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