jumingim.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. // TODO 临时构造plus对象,使得能够在浏览器中进行调试
  2. //var plus = null;
  3. // 登录者相关信息(包括userAgent)
  4. var loginerInfo = null,
  5. // 基础环境信息(包括当前webview)
  6. baseEnv = null,
  7. docInfo = null,
  8. self = null,
  9. activeItem = null,
  10. isInit;
  11. var $queryTab = $('#query_tab'),
  12. $doingView = $('#search_result'),
  13. $doingViewList = $doingView.find('.c-list'),
  14. // 搜索无结果时显示
  15. $noResultWrap = $('#no_result_wrap'),
  16. searchPage,
  17. $searchbar = $(".searchbar-input");
  18. var orginalData = [];
  19. var zdList, zdListKey = "",
  20. msg_del_list, msg_del_list_key = "",
  21. msg_cache_key = "";
  22. var first = true;
  23. mui.plusReady(function() {
  24. self = plus.webview.currentWebview();
  25. docInfo = JSON.parse(plus.storage.getItem("docInfo"));
  26. zdListKey = "jm_zdList_"+ docInfo.code;
  27. msg_del_list_key = "jm_msg_del_list_"+ docInfo.code;
  28. msg_cache_key = "jm_msg_cache_key"+ docInfo.code;
  29. zdList = plus.storage.getItem(zdListKey);
  30. zdList = zdList? JSON.parse(zdList) : [];
  31. msg_del_list = plus.storage.getItem(msg_del_list_key);
  32. msg_del_list = msg_del_list? JSON.parse(msg_del_list) : {};
  33. /**
  34. * 预加载查询页面
  35. */
  36. searchPage = plus.webview.getWebviewById('sousuojuming');
  37. if(searchPage){
  38. searchPage.hide();
  39. }else{
  40. searchPage = mui.preload({
  41. id:"sousuojuming",
  42. url: 'sousuojuming.html'
  43. });
  44. }
  45. });
  46. // ”医生im”列表分页查询最后一条记录Id
  47. var doingLastId = 0,
  48. // ”医生im”列表iscroll 滚动条实例
  49. doingIscroller;
  50. // 获取登录相关信息
  51. var getLoginerInfo = function() {
  52. // 登录的相关信息
  53. var userAgent = plus && JSON.parse(plus.storage.getItem("userAgent"))
  54. return {
  55. userAgent: userAgent
  56. }
  57. },
  58. // 获取基础环境信息
  59. getBaseEnvPromise = function () {
  60. var env = {
  61. webview: plus&&plus.webview.currentWebview()
  62. };
  63. return Promise.resolve().then(function(res) {
  64. return env;
  65. });
  66. },
  67. formatDate = function(timestamp){
  68. var date = new Date(timestamp);
  69. Y = date.getFullYear() + '-';
  70. M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  71. D = date.getDate() < 10 ?'0'+date.getDate():date.getDate();
  72. return Y+M+D;
  73. },
  74. setPhoto = function(groupPhoto){
  75. var images = '';
  76. var cssArr=null;
  77. for(var i in groupPhoto){
  78. if(groupPhoto[i].ispatient=="1"){//患者信息
  79. cssArr = ["position: absolute;width: 50px !important;height: 50px !important;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  80. images+='<div class="div-image" style="'+cssArr+'">'+
  81. '<img src="'+getImgUrl(groupPhoto[i].avatar)+'"/>'+
  82. '</div>';
  83. break;
  84. }
  85. }
  86. return images;
  87. return false;
  88. var images = '';
  89. var cssArr=null;
  90. if(groupPhoto.length>=5){
  91. cssArr = ["top: 0;left: 50%;margin-left: -8px;","top: 12px;left: 2px;","top: 12px;left: 28px;","top: 27px;left: 5px;","top: 27px;left: 24px;"];
  92. }else if(groupPhoto.length==4){
  93. cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
  94. }else if(groupPhoto.length==3){
  95. cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
  96. }else if(groupPhoto.length==2){
  97. cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
  98. }else if(groupPhoto.length==1 || groupPhoto.length==0){
  99. cssArr = ["position: absolute;width: 50px !important;height: 50px !important;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  100. }
  101. for(var i in groupPhoto){
  102. if(i>4) break;
  103. if(groupPhoto[i].avatar){
  104. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  105. '<img src="'+ getImgUrl(groupPhoto[i].avatar)+'"/>'+
  106. '</div>';
  107. }else{
  108. if(groupPhoto[i].role=="doctor"){
  109. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  110. '<img src="../../../images/d-default.png"/>'+
  111. '</div>';
  112. }else{
  113. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  114. '<img src="../../../images/p-default.png"/>'+
  115. '</div>';
  116. }
  117. }
  118. }
  119. return images;
  120. },
  121. initPage = function(){
  122. // var msg = plus.storage.getItem(msg_cache_key);
  123. // if(msg){
  124. // msg = JSON.parse(msg);
  125. // render(msg, true, false);
  126. // getData(false, false);
  127. // } else {
  128. getData(false, false);
  129. // }
  130. },
  131. render= function(data, isInit, isPullDown){
  132. // plus.storage.setItem(msg_cache_key, JSON.stringify(data));
  133. if(data.length==0){
  134. myScroll.refresh();
  135. myScroll.endPullDownToRefresh();
  136. mui.later(function(){
  137. $(".l-pullDown").hide();
  138. }, 200)
  139. } else {
  140. if(isInit){
  141. //初始化
  142. console.log(data)
  143. $doingViewList.html( template("jm_li_tmpl", {list: data}) );
  144. } else {
  145. //更新
  146. var v;
  147. var $lastDom = $('#doing_list li.zd');
  148. $lastDom = $lastDom.length? $($lastDom[$lastDom.length-1]) : undefined;
  149. for(var i=data.length-1; i>=0; i--){
  150. v = data[i];
  151. var $dom = $('#doing_list li[data-code="'+ v.code +'"]');
  152. if($dom.length){
  153. if($dom.attr('data-timestamp')==v.timestamp){
  154. $dom.find('.last-content').text(v.content);
  155. $dom.find('.red-dot').text(v.count).toggle(v.count>0&&v.my_role==0);
  156. } else {
  157. $dom.remove();
  158. $lastDom ? $lastDom.after( template('jm_li_tmpl', {list: [v]}) )
  159. : $doingViewList.prepend( template('jm_li_tmpl', {list: [v]}) );
  160. }
  161. } else {
  162. $lastDom ? $lastDom.after( template('jm_li_tmpl', {list: [v]}) )
  163. : $doingViewList.prepend( template('jm_li_tmpl', {list: [v]}) );
  164. }
  165. }
  166. if($('#doing_list:hidden').length){
  167. $doingViewList.show();
  168. $(".lin-search").show();
  169. $(".l-pullDown").show();
  170. }
  171. }
  172. if(isPullDown){
  173. loadPhoto();
  174. myScroll.endPullDownToRefresh();
  175. }else{
  176. myScroll.refresh();
  177. loadPhoto();
  178. }
  179. }
  180. },
  181. //获取非咨询中居民未读消息
  182. fzxzData = function(){
  183. sendPost("/doctor/message/messages",{}, null, function(res){
  184. if(res.status == 200){
  185. var data = res.data;
  186. var xxWD = JSON.parse(data.imMsgCount);
  187. var _xxWD = xxWD.patientEnd;
  188. if(_xxWD>0&&_xxWD<99){
  189. $('.weXX').html(_xxWD);
  190. }else if(_xxWD>99){
  191. $(".weXX").html('99+');
  192. }else{
  193. $('.Xweizhi').hide();
  194. }
  195. }else{
  196. }
  197. },'POST','',true);
  198. }
  199. getData = function(isInit, isPullDown) {
  200. var count = 0;
  201. var resultArr = [];
  202. imClient.Sessions.getSessionsWithPatient(docInfo.code, 1, 2000, 0, function(rs){
  203. $('#doing_list').find('li').not('li[data-type="link"]').remove();
  204. var d, isNoRs = true;
  205. for(var k in rs){
  206. d = rs[k];
  207. if(d.business_type==2 && d.type!=0 && d.type!=3 && d.last_content_type!="7"){
  208. isNoRs = false;
  209. var dateStr = asessTime(d.create_date);
  210. var p = {
  211. count: d.unread_count || 0,
  212. time: d.last_content==""? "" : dateStr,
  213. code: d.id,
  214. name: d.name,
  215. last_content:formatMsg( d.last_content, d.last_content_type,d.type),
  216. timestamp: d.create_date,
  217. photo:"",
  218. type: d.type,
  219. my_role: d.my_role,
  220. sex:d.sender_sex,
  221. birthday:d.sender_birthday,
  222. senderName:d.sender_name,
  223. userLevel:docInfo.level,
  224. is_invite:d.is_invite,
  225. last_content_type:d.last_content_type=="5"?JSON.stringify(d.last_content_type):d.last_content_type
  226. }
  227. if(p.my_role != "1"){ //旁听的消息未读数不记录count
  228. count += p.count;
  229. }
  230. resultArr.push(p);
  231. }
  232. }
  233. if(isNoRs){
  234. render([], isInit, isPullDown);
  235. } else {
  236. render(soreByTimeStamp(resultArr), isInit, isPullDown);
  237. //刷新未读消息数
  238. sendPost("/doctor/message/messages",{}, null, function(res){
  239. if(res.status == 200){
  240. var data = res.data;
  241. var xxWD = data.imMsgCount?JSON.parse(data.imMsgCount):'';
  242. mui.fire(plus.webview.getWebviewById("xiaoxi.html"), "imMsgCount", {patient: xxWD.patient,patientEnd:xxWD.patientEnd,doctor:xxWD.doctor});
  243. }
  244. })
  245. }
  246. $('#doing_list').find('li[data-type="link"]').remove();
  247. $('#doing_list').prepend($(".div-fzxzjm").html());
  248. }, function(msg){
  249. console.error("e:"+JSON.stringify(msg))
  250. errHandle(isPullDown);
  251. })
  252. },
  253. loadPhoto = function(){
  254. $.each($('#doing_list li[data-load!="1"]'), function(i, v) {
  255. imClient.Sessions.getParticipantsAvatars($(v).attr('data-code'), function(rs){
  256. $(v).attr('data-load', 1);
  257. if(rs.length>0){
  258. var $photo = $(v).find('.c-avatar-m');
  259. if($(v).attr('data-type')==2){
  260. for(var k in rs){
  261. if(docInfo.code != rs[k].id){
  262. $photo.html('<img src="'+ getImgUrl(rs[k].avatar) +'">');
  263. break;
  264. }
  265. }
  266. } else {
  267. $photo.html(setPhoto(rs));
  268. }
  269. }
  270. }, function(){
  271. })
  272. });
  273. },
  274. errHandle = function(isPullDown){
  275. if(isPullDown) myScroll.endPullDownToRefresh();
  276. mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
  277. },
  278. soreByTimeStamp = function(resultArr){
  279. orginalData = resultArr;
  280. var resArr = [];
  281. if(!isEmpty(msg_del_list)){
  282. _.each(resultArr,function(v){
  283. var isAdd = false;
  284. _.each(msg_del_list,function(val){
  285. if(msg_del_list[v.code]!=v.timestamp.toString()){
  286. if(!isAdd){
  287. resArr.push(v);
  288. isAdd = true;
  289. }
  290. }
  291. })
  292. })
  293. }else{
  294. resArr = resultArr;
  295. }
  296. var aArr = [];
  297. var bArr = [];
  298. for(var x=0;x<resultArr.length;x++){
  299. resultArr[x].isZD = false;
  300. for(var i=zdList.length-1;i>=0;i--){
  301. if(zdList[i]==resultArr[x].code){
  302. resultArr[x].isZD = true;
  303. aArr.push(resultArr[x]);
  304. break;
  305. }
  306. }
  307. }
  308. _.chain(resArr)
  309. // 根据date日期字段排序
  310. .sortBy("timestamp").reverse()
  311. .each(function(v){
  312. if(!(_.contains(zdList,v.code))){
  313. bArr.push(v);
  314. }
  315. });
  316. var formatResult = aArr.concat(bArr);
  317. return formatResult;
  318. },
  319. isEmpty = function(value){
  320. return (Array.isArray(value) && value.length === 0)
  321. || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
  322. },
  323. // 绑定页面事件
  324. bindEvents = function () {
  325. /**
  326. * 搜索按钮点击
  327. */
  328. $('.lin-search').on('tap', function() {
  329. mui.fire(searchPage, "initSearch", null);
  330. searchPage.show();
  331. })
  332. $('#doing_list').on('tap','li',function() {
  333. var $this = $(this),
  334. code = $this.attr('data-code'),
  335. name = $this.find('.c-name').html(),
  336. type = $this.attr('data-type');
  337. if(type=="link"){
  338. openWebview($this.attr('data-html'))
  339. } else if(type == '8'){
  340. //续方咨询
  341. openWebview('../../message/html/xufangzixun.html',{
  342. type: type,
  343. sessionId: code,
  344. sessionName: name
  345. })
  346. }
  347. else{
  348. openWebview("../../message/html/p2dzixun.html",{type: type, sessionId: code, sessionName: name});
  349. }
  350. })
  351. /**
  352. * 取消左移块的事件
  353. */
  354. $('body').on('touchstart', function(e){
  355. if($scrollDom){
  356. $scrollDom.animate({left: '0px'}, "fast", undefined, function(){
  357. if($zdDom){
  358. var code = $zdDom.closest('li').prependTo("#doing_list").addClass("zd").attr('data-code');
  359. $zdDom.html("取消<br/>置顶").removeClass("zdan").addClass("qxzdan");
  360. $zdDom = undefined;
  361. var newList = [];
  362. if(zdList){
  363. for(var i=0; i<zdList.length; i++){
  364. if(zdList[i] != code){
  365. newList.push(zdList[i]);
  366. }
  367. }
  368. newList.push(code);
  369. } else {
  370. newList = [code];
  371. }
  372. zdList = newList;
  373. plus.storage.setItem(zdListKey, JSON.stringify(zdList));
  374. }
  375. });
  376. return false;
  377. }
  378. }).on('touchmove', function(e){
  379. e.preventDefault();
  380. })
  381. };
  382. // 页面业务处理流程开始
  383. new Promise(function(resolve, reject) {
  384. // TODO 临时放开
  385. //resolve(true);
  386. mui.plusReady(function() {
  387. // plus已经准备好,可以往下执行
  388. resolve(true);
  389. });
  390. }).then(function() {
  391. // 获取基础环境信息
  392. return getBaseEnvPromise().then(function(env) {
  393. baseEnv = env;
  394. }).then(function() {
  395. // 获取登录医生信息
  396. loginerInfo = getLoginerInfo();
  397. loaded();
  398. initPage();
  399. fzxzData();
  400. bindEvents();// 绑定页面事件
  401. })
  402. }).catch(function(e) {
  403. plus.nativeUI.closeWaiting();
  404. console && console.error(e);
  405. });
  406. var xScroll = 0;
  407. var yOff = 0, xOff = 0, myScroll;
  408. var $scrollDom, $zdDom;
  409. /**
  410. * 初始化iscroll
  411. */
  412. function loaded () {
  413. var deceleration = mui.os.ios?0.003:0.0009;
  414. mui('.mui-scroll-wrapper').scroll({
  415. bounce: false,
  416. indicators: true, //是否显示滚动条
  417. deceleration:deceleration
  418. });
  419. myScroll = mui(".mui-scroll").pullToRefresh({
  420. down: {
  421. callback: function() {
  422. getData(false, true);
  423. var zkHome = plus.webview.getWebviewById("home1.html");
  424. if(zkHome) {
  425. mui.fire(zkHome,'refreshBaseInfo');
  426. }
  427. }
  428. },
  429. scrollLeft: function(){
  430. xScroll = true;
  431. }
  432. });
  433. return;
  434. }
  435. function saveMsgDelList(){
  436. plus.storage.setItem(msg_del_list_key, JSON.stringify(msg_del_list));
  437. }
  438. template.helper("setAge", function(timestamp) {
  439. return calcAge(timestamp)+"岁";
  440. });
  441. /**
  442. * 获取最后一个置顶元素,如无置顶元素,返回false
  443. */
  444. function getTopDom(){
  445. var $d;
  446. for(var i=0; i<zdList.length; i++){
  447. $d = $('#doing_list li[data-code="'+ zdList[i] +'"]');
  448. if($d.length > 0)
  449. return $d;
  450. }
  451. return false;
  452. }
  453. function formatMsg(msg, last_content_type,type){
  454. switch(parseInt(last_content_type)){
  455. case 0:
  456. case 1: return msg;
  457. case 2: return "[图片]";
  458. case 3: return "[语音]";
  459. case 4:
  460. msg = msg.replace(/\n/g, "\\n");
  461. msg = msg.replace(/\r/g, "\\r");
  462. msg = msg.replace(/\t/g, "\\t");
  463. msg = JSON.parse(msg);
  464. var msgType = msg.type;
  465. if(msgType==0){
  466. return "[健康教育]";
  467. }else if(msgType==1){
  468. return "[健康记录]";
  469. }else{
  470. return "[健康指导]";
  471. }
  472. case 5:
  473. msg = JSON.parse(msg);
  474. return "已向"+msg.doctor_name+"求助";
  475. case 6:
  476. if(type==5){
  477. msg = msg.replace(/\n/g, "\\n");
  478. msg = msg.replace(/\r/g, "\\r");
  479. msg = msg.replace(/\t/g, "\\t");
  480. msg = JSON.parse(msg);
  481. return msg.title;
  482. }
  483. return msg;
  484. case 7: return msg;
  485. case 11: return msg;
  486. case 12: return "[视频]";
  487. case 13: return msg;
  488. case 14: return msg;
  489. case 15: return msg?JSON.parse(msg).result:'';
  490. case 16: return function(){
  491. if(msg){
  492. var obj = JSON.parse(msg);
  493. return obj.needUpload == 'true'?obj.tzAlert:obj.tzMsg
  494. }else{
  495. return ''
  496. }
  497. }();
  498. case 17: return msg?JSON.parse(msg).doctorremind:'';
  499. default: return "";
  500. }
  501. }
  502. /**
  503. * 往指定元素后面添加
  504. */
  505. function appendAfterZd($lastZd, appendDom){
  506. if($lastZd != false){
  507. $lastZd.after(appendDom);
  508. } else {
  509. $('#doing_list').prepend(appendDom);
  510. }
  511. }
  512. function asessTime(timestamp){
  513. var today = new Date().format('yyyy-MM-dd');
  514. var date = new Date();
  515. date.setTime(timestamp);
  516. var dateStr = date.format('yyyy-MM-dd');
  517. if(!today.substring(0,4) == dateStr.substring(0,4)){
  518. return dateStr;
  519. }
  520. if(today == dateStr){
  521. return date.format('hh:mm');
  522. }
  523. return date.format('MM-dd');
  524. }
  525. function calcAge(timestamp){
  526. var date = new Date(timestamp);
  527. Y = date.getFullYear() + '-';
  528. M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  529. D = date.getDate() < 10 ?'0'+date.getDate():date.getDate();
  530. var birthday = Y+M+D;
  531. var r = birthday.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
  532. if(r==null)return false;
  533. var d= new Date(r[1], r[3]-1, r[4]);
  534. if(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]){
  535. var Y = new Date().getFullYear();
  536. if((Y-r[1])<=0){
  537. return "<1";
  538. }
  539. return (Y-r[1]);
  540. }
  541. }
  542. window.addEventListener("update", function(){
  543. getData(false, false);
  544. fzxzData();
  545. });
  546. window.addEventListener("xiaoxiUpdate", function(){
  547. getData(false, false);
  548. });