sousuojuminoryisheng.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. self = null,
  8. myScroll,
  9. pageType,
  10. imMessages,
  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. imMessages = e.detail.imMessages;
  35. openSoftKeyboard();
  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. // 初始化视图列表
  151. initDoingViewList = function(list) {
  152. var searchText = $searchbarInput.val().trim();
  153. template.helper("setAge", function(timestamp) {
  154. return calcAge(timestamp);
  155. });
  156. $doingWrapper.hide();
  157. var html = "";
  158. if(pageType=="1"){
  159. $(".locate-city").html("居民");
  160. html = template("qyjm_li_tmpl", {list: list.users || []});
  161. }else{
  162. $(".locate-city").html("医生");
  163. html = template("yisheng_li_tmpl", {list: list.users || []});
  164. }
  165. $doingList.html(html);
  166. $doingWrapper.show();
  167. myScroll.refresh();
  168. ellipsisText($doingList.find('.c-content-warp'),searchText);
  169. plus.nativeUI.closeWaiting();
  170. },
  171. // 搜索框搜索执行方法
  172. search = function () {
  173. plus.nativeUI.showWaiting();
  174. var kw = $searchbarInput.val().trim();
  175. // imMessages有值表示转发中的搜索
  176. var excludeTopicEndedSessions = imMessages?false:"";//excludeTopicEndedSessions:(空值表示查询所有,false表示查询咨询中的居民,true表示查询咨询已结束的居民)
  177. if(pageType=="1"){//1 居民 2医生,,,"搜索居民"搜索结果列表数据
  178. imClient.Search.Patient.searchPatient(docInfo.code ,kw, excludeTopicEndedSessions, 1, 100, function(res){
  179. if(res.users.length==0) {
  180. $noResultWrap.show();
  181. $searchtResult.hide();
  182. plus.nativeUI.closeWaiting();
  183. } else {
  184. $noResultWrap.hide();
  185. $searchtResult.show();
  186. $doingWrapper.show();
  187. initDoingViewList(res);
  188. }
  189. });
  190. }else{// "搜索医生"搜索结果列表数据
  191. imClient.Search.Doctor.searchDoctors(docInfo.code ,kw, excludeTopicEndedSessions , 1, 100, function(res){
  192. if(res.users.length==0) {
  193. $noResultWrap.show();
  194. $searchtResult.hide();
  195. plus.nativeUI.closeWaiting();
  196. } else {
  197. $noResultWrap.hide();
  198. $searchtResult.show();
  199. $doingWrapper.show();
  200. initDoingViewList(res);
  201. }
  202. }, function(msg){
  203. console.error(msg);
  204. mui.toast("搜索失败!");
  205. })
  206. }
  207. },
  208. calcAge = function(idcard){
  209. var age = 0;
  210. if (!idcard) {
  211. return age;
  212. }
  213. var birth = "";
  214. if (idcard.length == 18) {
  215. birth = idcard.substring(6, 14);
  216. } else if (idcard.length == 15) {
  217. birth = "19" + idcard.substring(6, 12);
  218. }
  219. var year = parseInt(birth.substring(0, 4));
  220. var month = parseInt(birth.substring(4, 6));
  221. var day = parseInt(birth.substring(6));
  222. var cal = new Date();
  223. age = cal.getFullYear() - year;
  224. //周岁计算
  225. if ((cal.getMonth()+1) < (month - 1) || ((cal.getMonth()+1) == (month - 1) && cal.getDate() < day)) {
  226. age--;
  227. }
  228. return age.toString();
  229. },
  230. // 绑定页面事件
  231. bindEvents = function () {
  232. $searchbarInput.on('input', function() {
  233. var text = $(this).val().trim();
  234. $searchtResult.hide();
  235. $('#no_result_wrap').hide();
  236. showSearchSuggest(text);
  237. }).on('keydown',function(e) {
  238. if (e.which === 13) {
  239. search();
  240. }
  241. });
  242. $('.lin-search-ipt a,.div-back').on('tap', function(){
  243. plus.webview.currentWebview().close();
  244. })
  245. $searchSuggest.on('click',function() {
  246. $searchSuggest.hide();
  247. search();
  248. $searchtResult.show();
  249. });
  250. $searchtResult.on('tap','li',function() {
  251. if(imMessages){//转发消息
  252. var oCode = $(this).attr("data-code");
  253. mui.confirm("是否确认将消息逐条转发给"+$(this).attr("data-name")+"?", "提示", ["不了,谢谢", "确认转发"], function(e) {
  254. if(e.index == 0) {
  255. } else {
  256. Promise.all(_.map(imMessages,function(msg){
  257. return new Promise(function(resolve, reject) {
  258. imClient.Sessions.sendMessage(oCode, docInfo.code, docInfo.name, msg.content,msg.type, function(res){
  259. resolve(true);
  260. }, function(msg){
  261. resolve(true);
  262. })
  263. })
  264. })).then(function() {
  265. mui.toast("转发成功");
  266. mui.fire(self.opener().opener().opener(), "revertZf");
  267. self.opener().opener().close();
  268. self.opener().close();
  269. self.close();
  270. }).catch(function(e){
  271. console.log(e)
  272. })
  273. }
  274. })
  275. }else{
  276. var $this = $(this),
  277. code = $this.attr('data-code'),
  278. name = $this.attr('data-name'),
  279. photo = $this.attr('data-photo'),
  280. sex = $this.attr('data-sex'),
  281. type = $this.attr('data-type');
  282. session_type = $this.attr('data-stype');
  283. if(session_type == '8'){
  284. //续方咨询
  285. openWebview('../../message/html/xufangzixun.html',{
  286. type: type,
  287. sessionId: code,
  288. sessionName: name
  289. });
  290. return false;
  291. }
  292. if(pageType=="1"){//1 居民 2医生
  293. openWebview("../../message/html/p2dzixun.html",{type: type, sessionId: code, sessionName: name});
  294. }else{
  295. openWebview("../../message/html/p2p.html",{sessionId: code, sessionName: name});
  296. }
  297. }
  298. });
  299. };
  300. $(function(){
  301. // $searchbarInput.val('').focus();
  302. $searchtResult.hide();
  303. })
  304. // 页面业务处理流程开始
  305. new Promise(function(resolve, reject) {
  306. // TODO 临时放开
  307. // resolve(true);
  308. mui.plusReady(function() {
  309. // hrefhrefplus已经准备好,可以往下执行
  310. resolve(true);
  311. });
  312. }).then(function() {
  313. // 获取基础环境信息
  314. return getBaseEnvPromise().then(function(env) {
  315. baseEnv = env;
  316. }).then(function() {
  317. // 获取登录医生信息
  318. loginerInfo = getLoginerInfo();
  319. var self = self = plus.webview.currentWebview();
  320. imMessages = self.imMessages;
  321. openSoftKeyboard();
  322. pageType = self.pageType;
  323. $("#searchPut").val(self.kw);
  324. // 绑定页面事件
  325. bindEvents();
  326. $searchSuggest.trigger("click");
  327. })
  328. })
  329. .catch(function(e) {
  330. plus.nativeUI.closeWaiting();
  331. console && console.error(e);
  332. });
  333. template.helper('getImgUrl', function(str){
  334. return getImgUrl(str);
  335. })