sousuoqunliao.js 11 KB

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