sousuojuming.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. self = null,
  8. imMessages = null;
  9. docInfo = null;
  10. // 搜索框
  11. var $searchbar = $('.searchbar'),
  12. // 搜索输入框
  13. $searchbarInput = $('.lin-search-ipt input'),
  14. // 搜索取消按钮
  15. $searchCancelBtn = $('.searchbar-cancel'),
  16. // 搜索框下面悬浮的搜索提示
  17. $searchSuggest = $('#search_suggest_text'),
  18. // 搜索结果展示容器
  19. $searchtResult = $('#search_result'),
  20. // 搜索无结果时显示
  21. $noResultWrap = $('#no_result_wrap'),
  22. $doingWrapper = $('#doing_wrapper'),
  23. $endWrapper = $('#end_wrapper'),
  24. $qlWrapper = $('#ql_wrapper'),
  25. $doingList = $('#doing_list'),
  26. $endList = $('#end_list'),
  27. $qlList = $('#ql_list'),
  28. searchPage1,
  29. searchPage2,
  30. searchPage3,
  31. searchPage4,
  32. $moreResult1 = $('#doing_wrapper .more-result'),
  33. $moreResult2 = $('#ql_wrapper .more-result');
  34. $moreResult3 = $('#end_wrapper .more-result');
  35. mui.plusReady(function() {
  36. initKeyboardAndroid();
  37. self = plus.webview.currentWebview();
  38. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  39. /**
  40. * 预加载查询页面
  41. */
  42. /*searchPage1 = mui.preload({
  43. id:"sousuojuminoryisheng"+new Date().getTime(),
  44. url: 'sousuojuminoryisheng.html'
  45. });
  46. searchPage2 = mui.preload({
  47. id:"sousuoliaotianjilu"+new Date().getTime(),
  48. url: 'sousuoliaotianjilu.html'
  49. });
  50. searchPage3 = mui.preload({
  51. id:"sousuosimpleliaotianjilu"+new Date().getTime(),
  52. url: 'sousuosimpleliaotianjilu.html'
  53. });
  54. searchPage4 = mui.preload({
  55. id:"sousuoqunliao"+new Date().getTime(),
  56. url: 'sousuoqunliao.html'
  57. });*/
  58. });
  59. document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
  60. window.addEventListener("initSearch", function(e){
  61. imMessages = e.detail.imMessages;
  62. openSoftKeyboard();
  63. setTimeout(function(){
  64. $("#searchPut").focus();
  65. },500)
  66. })
  67. // 获取登录相关信息
  68. var getLoginerInfo = function() {
  69. // 登录的相关信息
  70. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  71. return {
  72. userAgent: userAgent
  73. }
  74. },
  75. // 获取基础环境信息
  76. getBaseEnvPromise = function () {
  77. var env = {
  78. webview: plus&&plus.webview.currentWebview()
  79. };
  80. return Promise.resolve().then(function(res) {
  81. return env;
  82. });
  83. },
  84. // $el: $('.c-content-warp')
  85. getRowProps = function ($el) {
  86. var $textEllipsis = $el.find('.j-text-ellipsis'),
  87. $text = $textEllipsis.eq(0),
  88. $chart = $text.text('a'),
  89. enWidth = $chart.width(),
  90. $chart = $text.text('中'),
  91. zhWidth = $chart.width(),
  92. lineHeight = parseFloat($chart.css("lineHeight"), 10),
  93. rowHeight = $chart.height();
  94. $chart.text('');
  95. return {
  96. chartWidth: {
  97. zh: zhWidth,
  98. en: enWidth
  99. },
  100. rowHeight: Math.max(rowHeight, lineHeight),
  101. rowWidth: $el.width()
  102. };
  103. },
  104. replaceAll = function (text, arr) {
  105. var html = text;
  106. _.each(arr,function(kw) {
  107. var reg = new RegExp(kw+"(?!>)","gi");
  108. html = html.replace(reg,'<em>'+kw+'</em>');
  109. });
  110. return html;
  111. },
  112. highlineKeyword = function ($el,searchText) {
  113. var props = getRowProps($el),
  114. chartWidth = props.chartWidth,
  115. rowHeight = props.rowHeight,
  116. rowWidth = props.rowWidth,
  117. // 每行显示字符数(以中文字符为标准计算)
  118. chartNum = Math.floor(rowWidth / chartWidth.zh),
  119. // 排除“咨询问题:”字符及前后“...”所占宽度
  120. exceptNum = 0,
  121. // 行数
  122. rowNum = 1,
  123. // 预计显示总字符数
  124. expectedNum = chartNum * rowNum - exceptNum,
  125. $target = $el.find('.j-text-ellipsis'),
  126. // 目标文本
  127. text = $target.attr('data-text').trim(),
  128. length = text.length,
  129. // 关键字数组
  130. kws = searchText.trim().replace(/\s+/g," ").split(" ");
  131. var fidx = 0,preFidx,diff = 0;
  132. $target.html(replaceAll(text, kws));
  133. if(Math.ceil($target.height() / rowHeight) <= rowNum) {
  134. return ;
  135. }
  136. if(text.length > expectedNum) {
  137. fidx = text.indexOf(kws[0])+kws[0].length-1;
  138. diff = fidx - expectedNum + 1;
  139. preFidx = fidx;
  140. diff = (diff<0)?0:diff;
  141. var preChar = (diff>0)?"...":"";
  142. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  143. while((Math.ceil($target.height() / rowHeight) <= rowNum) && (preFidx < length)) {
  144. preFidx++;
  145. $target.html(preChar+replaceAll(text.slice(diff,preFidx+1), kws)+"...");
  146. }
  147. if(preFidx == length && (Math.ceil($target.height() / rowHeight) <= rowNum)) {
  148. diff = diff>0?(diff - 1):0;
  149. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws));
  150. } else if((Math.ceil($target.height() / rowHeight) > rowNum)) {
  151. $target.html(preChar+replaceAll(text.slice(diff,preFidx), kws)+"...");
  152. }
  153. }
  154. },
  155. ellipsisText = function ($elements, searchText) {
  156. //$el.ellipsis({ row: 2});
  157. _.each($elements,function(el) {
  158. highlineKeyword($(el),searchText)
  159. });
  160. },
  161. // 控制搜索关键字悬浮提示的显示
  162. showSearchSuggest = function(text) {
  163. var suggestText = '搜索“'+text+'”';
  164. // 如果text不为空,则显示;否则隐藏
  165. if(text&&text.trim().length) {
  166. $searchSuggest.text(suggestText);
  167. $searchSuggest.show();
  168. } else {
  169. $searchSuggest.text('');
  170. $searchSuggest.hide();
  171. }
  172. },
  173. // 初始化视图列表
  174. initDoingViewList = function(list) {
  175. var searchText = $searchbarInput.val().trim();
  176. template.helper("setAge", function(timestamp) {
  177. return calcAge(timestamp);
  178. });
  179. $doingWrapper.hide();
  180. $endWrapper.hide();
  181. $qlWrapper.hide();
  182. if(list.users.length>0){//签约居民
  183. var html = template("qyjm_li_tmpl", {list: list.users.slice(0,3) || []});
  184. $doingList.html(html);
  185. $doingWrapper.show();
  186. if(list.users.length>3){
  187. $doingWrapper.find('.more-result').css("display","block");
  188. }else{
  189. $doingWrapper.find('.more-result').css("display","none");
  190. }
  191. ellipsisText($doingList.find('.c-content-warp'),searchText);
  192. }
  193. // if(list.sessions.length>0){//群聊
  194. // var groupList = _.map(list.sessions, function(o){
  195. // if(o.members.length > 0){
  196. // var member = [];
  197. // for(i=0; i<o.members.length; i++){
  198. // member.push(o.members[i].name);
  199. // }
  200. // o.members = member.join(",");
  201. // }
  202. // return o;
  203. // });
  204. // var html = template("ql_li_tmpl", {list: groupList.slice(0,3) || []});
  205. // $qlList.html(html);
  206. // $qlWrapper.show();
  207. // if(list.sessions.length>3){
  208. // $qlWrapper.find('.more-result').css("display","block");
  209. // }else{
  210. // $qlWrapper.find('.more-result').css("display","none");
  211. // }
  212. // ellipsisText($qlList.find('.c-content-warp'),searchText);
  213. // }
  214. // if(list.messages.length>0){//聊天记录
  215. // var chatList = _.map(list.messages, function(o){
  216. // o.amount = o.messages.length;
  217. // o.content = o.messages[0].content;
  218. // o.msgId = o.messages[0].id;
  219. // return o;
  220. // });
  221. // var html = template("ltjl_li_tmpl", {list: chatList});
  222. // $endList.html(html);
  223. // $endWrapper.show();
  224. // if(list.messages.length>3){
  225. // $endWrapper.find('.more-result').css("display","block");
  226. // }else{
  227. // $endWrapper.find('.more-result').css("display","none");
  228. // }
  229. // ellipsisText($endList.find('.c-content-warp'),searchText);
  230. // }
  231. plus.nativeUI.closeWaiting();
  232. loadPhoto();
  233. },
  234. loadPhoto = function(){
  235. $.each($('#search_result li[data-load=1]'), function(i, v) {
  236. imClient.Sessions.getParticipantsAvatars($(v).attr('data-code'), function(rs){
  237. if(rs.length>0){
  238. var $photo = $(v).find('.c-avatar-m');
  239. if($(v).attr('data-type')==2){
  240. for(var k in rs){
  241. if(docInfo.code != rs[k].id){
  242. $photo.html('<img src="'+ getImgUrl(rs[k].avatar) +'">');
  243. break;
  244. }
  245. }
  246. } else {
  247. $photo.html(setPhoto(rs));
  248. }
  249. }
  250. }, function(){
  251. })
  252. });
  253. },
  254. setPhoto = function(groupPhoto){
  255. var images = '';
  256. var cssArr=null;
  257. var length = groupPhoto.length;
  258. if(length>=5){
  259. cssArr = ["top: 0;left: 50%;margin-left: -8px;","top: 12px;left: 2px;","top: 12px;left: 28px;","top: 27px;left: 5px;","top: 27px;left: 24px;"];
  260. }else if(length==4){
  261. cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
  262. }else if(length==3){
  263. cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
  264. }else if(length==2){
  265. cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
  266. }else if(length==1 || length==0){
  267. cssArr = ["position: absolute;width: 50px;height: 50px;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  268. }
  269. for(i=0; i<length; i++){
  270. images += '<div class="div-image" style="'+cssArr[i]+'">'+
  271. '<img src="'+getImgUrl(groupPhoto[i].avatar)+'"/>'+
  272. '</div>';
  273. }
  274. return images;
  275. },
  276. // 搜索框搜索执行方法
  277. search = function () {
  278. plus.nativeUI.showWaiting();
  279. var kw = $searchbarInput.val().trim();
  280. // imMessages有值表示转发中的居民搜索
  281. var excludeTopicEndedSessions = imMessages?false:"";//excludeTopicEndedSessions:(空值表示查询所有,false表示查询咨询中的居民,true表示查询咨询已结束的居民)
  282. imClient.Search.Patient.searchAll(docInfo.code, kw, excludeTopicEndedSessions, function(res){
  283. if((res && res.users.length==0) || res=="") {
  284. $noResultWrap.show();
  285. $searchtResult.hide();
  286. plus.nativeUI.closeWaiting();
  287. } else {
  288. $noResultWrap.hide();
  289. $searchtResult.show();
  290. $doingWrapper.show();
  291. initDoingViewList(res);
  292. }
  293. plus.nativeUI.closeWaiting();
  294. }, null);
  295. },
  296. calcAge = function(idcard){
  297. var age = 0;
  298. if (!idcard) {
  299. return age;
  300. }
  301. var birth = "";
  302. if (idcard.length == 18) {
  303. birth = idcard.substring(6, 14);
  304. } else if (idcard.length == 15) {
  305. birth = "19" + idcard.substring(6, 12);
  306. }
  307. var year = parseInt(birth.substring(0, 4));
  308. var month = parseInt(birth.substring(4, 6));
  309. var day = parseInt(birth.substring(6));
  310. var cal = new Date();
  311. age = cal.getFullYear() - year;
  312. //周岁计算
  313. if ((cal.getMonth()+1) < (month - 1) || ((cal.getMonth()+1) == (month - 1) && cal.getDate() < day)) {
  314. age--;
  315. }
  316. return age.toString();
  317. },
  318. // 绑定页面事件
  319. bindEvents = function () {
  320. $searchbarInput.on('input', function() {
  321. var text = $(this).val().trim();
  322. $searchtResult.hide();
  323. $('#no_result_wrap').hide();
  324. showSearchSuggest(text);
  325. }).on('keydown',function(e) {
  326. if (e.which === 13) {
  327. $searchSuggest.hide();
  328. search();
  329. }
  330. });
  331. $('.lin-search-ipt a').on('tap', function(){
  332. plus.webview.currentWebview().hide();
  333. mui.later(function(){
  334. $('#search_result').hide();
  335. $('#no_result_wrap').hide();
  336. $('#searchPut').val("");
  337. $("#search_suggest_text").html("").hide();
  338. $("#searchPut").blur();
  339. }, 50)
  340. })
  341. $searchSuggest.on('click',function() {
  342. $searchSuggest.hide();
  343. search();
  344. $searchtResult.show();
  345. });
  346. $moreResult1.on('click',function() {
  347. var kw = $searchbarInput.val().trim();
  348. mui.openWindow({
  349. id:"sousuojuminoryisheng"+new Date().getTime(),
  350. url: 'sousuojuminoryisheng.html',
  351. extras: {
  352. kw:kw,pageType:"1"
  353. }
  354. })
  355. });
  356. $moreResult2.on('click',function() {
  357. var kw = $searchbarInput.val().trim();
  358. mui.openWindow({
  359. id:"sousuoqunliao"+new Date().getTime(),
  360. url: 'sousuoqunliao.html',
  361. extras:{
  362. kw:kw,pageType:"1"
  363. }
  364. });
  365. /*mui.fire(searchPage4, "initSearch", {kw:kw,pageType:"1"});
  366. searchPage4.show();*/
  367. });
  368. $moreResult3.on('click',function() {
  369. var kw = $searchbarInput.val().trim();
  370. mui.openWindow({
  371. id:"sousuoliaotianjilu"+new Date().getTime(),
  372. url: 'sousuoliaotianjilu.html',
  373. extras: {
  374. kw:kw,pageType:"1"
  375. }
  376. });
  377. /*mui.fire(searchPage2, "initSearch", {kw:kw,pageType:"1"});
  378. searchPage2.show();*/
  379. });
  380. $searchtResult.on('tap','li',function() {
  381. var $this = $(this),
  382. sessionId = $this.attr('data-code'),
  383. name = $this.attr('data-name'),
  384. photo = $this.attr('data-photo'),
  385. sex = $this.attr('data-sex'),
  386. msgId = $this.attr('data-msgId'),
  387. type = $this.attr('data-type'),
  388. session_type = $this.attr('data-stype');
  389. if(imMessages){//转发消息
  390. mui.confirm("是否确认将消息逐条转发给"+name+"?", "提示", ["不了,谢谢", "确认转发"], function(e) {
  391. if(e.index == 0) {
  392. } else {
  393. Promise.all(_.map(imMessages,function(msg){
  394. return new Promise(function(resolve, reject) {
  395. imClient.Sessions.sendMessage(sessionId, docInfo.code, docInfo.name, msg.content,msg.type, function(res){
  396. resolve(true);
  397. }, function(msg){
  398. resolve(true);
  399. })
  400. })
  401. })).then(function() {
  402. mui.toast('发送成功');
  403. var self = plus.webview.currentWebview();
  404. mui.fire(self.opener(), "revertZf");
  405. self.close();
  406. })
  407. }
  408. })
  409. }else{
  410. if(session_type == '8'){
  411. //续方咨询
  412. openWebview('../../message/html/xufangzixun.html',{
  413. type: type,
  414. sessionId: sessionId,
  415. sessionName: name
  416. });
  417. return false;
  418. }
  419. openWebview("../../message/html/p2dzixun.html",{type: type, sessionId: sessionId, sessionName: name});
  420. }
  421. });
  422. };
  423. $(function(){
  424. // $searchbarInput.val('').focus();
  425. $searchtResult.hide();
  426. })
  427. // 页面业务处理流程开始
  428. new Promise(function(resolve, reject) {
  429. // TODO 临时放开
  430. // resolve(true);
  431. mui.plusReady(function() {
  432. // hrefhrefplus已经准备好,可以往下执行
  433. resolve(true);
  434. });
  435. }).then(function() {
  436. // 获取基础环境信息
  437. return getBaseEnvPromise().then(function(env) {
  438. baseEnv = env;
  439. }).then(function() {
  440. // 获取登录医生信息
  441. loginerInfo = getLoginerInfo();
  442. // $searchbar.searchBar();
  443. // 绑定页面事件
  444. bindEvents();
  445. })
  446. }).catch(function(e) {
  447. plus.nativeUI.closeWaiting();
  448. console && console.error(e);
  449. });
  450. template.helper('getImgUrl', function(str){
  451. return getImgUrl(str);
  452. })