sousuoyisheng.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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("setChatName", function(members) {
  138. var memberStr = "";
  139. for(var i in members){
  140. memberStr+=members[i].memberName+","
  141. }
  142. memberStr = memberStr.substring(0,memberStr.length-1);
  143. return memberStr;
  144. });
  145. template.helper("setTuanDuiName", function(members) {
  146. var memberStr = "";
  147. for(var i in members){
  148. memberStr+=members[i].name+","
  149. }
  150. memberStr = memberStr.substring(0,memberStr.length-1);
  151. return memberStr;
  152. });
  153. template.helper("setPhoto", function(members) {
  154. var images = '';
  155. var cssArr=null;
  156. if(members.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(members.length==4){
  159. cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
  160. }else if(members.length==3){
  161. cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
  162. }else if(members.length==2){
  163. cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
  164. }else if(members.length==1){
  165. cssArr = ["position: absolute;width: 50px;height: 50px;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  166. }
  167. for(var i in members){
  168. if(i>4) break;
  169. if(members[i].photo){
  170. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  171. '<img src="'+getImgUrl(members[i].photo)+'"/>'+
  172. '</div>';
  173. }else{
  174. if(members[i].sex==2){
  175. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  176. '<img src="../../../images/d-female.png"/>'+
  177. '</div>';
  178. }else{
  179. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  180. '<img src="../../../images/d-default.png"/>'+
  181. '</div>';
  182. }
  183. }
  184. }
  185. return images;
  186. });
  187. template.helper("setPhoto2", function(p) {
  188. return getImgUrl(p);
  189. });
  190. var html = template("zixun_li_tmpl", {list: list});
  191. $doingList.html(html);
  192. ellipsisText($doingList.find('.c-content-warp'),searchText);
  193. },
  194. // 搜索框搜索执行方法
  195. search = function () {
  196. var kw = $searchbarInput.val().trim();
  197. // "进行中"搜索结果列表数据
  198. promise = getReqPromises([{reqType:"GET",url:"/doctor/talkgroup/doctor_im_search",data:{filter: kw}}])
  199. // 隐藏搜索提示
  200. showSearchSuggest(false);
  201. return kw && promise.then(function(datas){
  202. var doingList = datas[0].data;
  203. if(!doingList) {
  204. $noResultWrap.show();
  205. $searchtResult.hide();
  206. } else {
  207. $noResultWrap.hide();
  208. $searchtResult.show();
  209. $doingWrapper.show();
  210. initDoingViewList(doingList);
  211. }
  212. })
  213. },
  214. // 绑定页面事件
  215. bindEvents = function () {
  216. $searchbarInput.on('input', function() {
  217. var text = $(this).val().trim();
  218. $searchtResult.hide();
  219. showSearchSuggest(text);
  220. }).on('keydown',function(e) {
  221. if (e.which === 13) {
  222. search();
  223. }
  224. });
  225. $searchSuggest.on('click',function() {
  226. $searchSuggest.hide();
  227. search();
  228. $searchtResult.show();
  229. });
  230. $searchtResult.on('click','li',function() {
  231. var $this = $(this),
  232. code = $this.attr('data-code'),
  233. name = $this.attr('data-name'),
  234. photo = $this.attr('data-photo'),
  235. sex = $this.attr('data-sex');
  236. // 转发的消息
  237. var messages = self.imMessages;
  238. mui.confirm("是否确认将消息逐条转发给"+name+"?", "提示", ["不了,谢谢", "确认转发"], function(e) {
  239. if(e.index == 0) {
  240. } else {
  241. Promise.all(_.map(messages,function(msg){
  242. return new Promise(function(resolve, reject) {
  243. im.sendPrivateMsg(loginerInfo.userAgent.uid,code,msg.content,msg.type,function() {
  244. resolve(true);
  245. },function() {
  246. resolve(true);
  247. })
  248. })
  249. })).then(function() {
  250. mui.fire(self.opener().opener(), "revertZf");
  251. self.opener().close();
  252. self.close();
  253. mui.toast("转发成功");
  254. }).catch(function(msg){
  255. console.log(msg)
  256. })
  257. }
  258. })
  259. });
  260. window.onpageshow = function() {
  261. $searchbarInput.val('').focus();
  262. $searchtResult.hide();
  263. }
  264. window.addEventListener("focusAction", function() {
  265. $searchbarInput.focus();
  266. });
  267. $searchCancelBtn.on("click",function(){
  268. plus.webview.currentWebview().close();
  269. openWebview("mingyizixunliebiao.html");
  270. })
  271. };
  272. $(function(){
  273. $searchbarInput.val('').focus();
  274. $searchtResult.hide();
  275. })
  276. // 页面业务处理流程开始
  277. new Promise(function(resolve, reject) {
  278. // TODO 临时放开
  279. // resolve(true);
  280. mui.plusReady(function() {
  281. // hrefhrefplus已经准备好,可以往下执行
  282. resolve(true);
  283. });
  284. }).then(function() {
  285. // 获取基础环境信息
  286. return getBaseEnvPromise().then(function(env) {
  287. baseEnv = env;
  288. }).then(function() {
  289. // 获取登录医生信息
  290. loginerInfo = getLoginerInfo();
  291. $searchbar.searchBar();
  292. // 绑定页面事件
  293. bindEvents();
  294. })
  295. }).catch(function(e) {
  296. plus.nativeUI.closeWaiting();
  297. console && console.error(e);
  298. });