sousuoliaotianjilu.js 12 KB

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