yishengim.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 = "ys_zdList_"+ docInfo.code;
  27. msg_del_list_key = "ys_msg_del_list_"+ docInfo.code;
  28. msg_cache_key = "ys_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('sousuoyisheng');
  37. if(searchPage){
  38. searchPage.hide();
  39. }else{
  40. searchPage = mui.preload({
  41. id:"sousuoyisheng",
  42. url: 'sousuoyisheng.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. if(groupPhoto.length>=5){
  78. cssArr = ["top: 0;left: 50%;margin-left: -8px;","top: 12px;left: 2px;","top: 12px;left: 28px;","top: 27px;left: 5px;","top: 27px;left: 24px;"];
  79. }else if(groupPhoto.length==4){
  80. cssArr = ["top: 5px;left: 3px;","top: 5px;left: 25px;","top: 27px;left: 3px;"," top: 27px;left: 25px;"];
  81. }else if(groupPhoto.length==3){
  82. cssArr = ["top: 5px;left: 50%;margin-left: -10px;","top: 20px;left: 3px;","top: 20px;left: 25px;"];
  83. }else if(groupPhoto.length==2){
  84. cssArr = ["top: 5px;left: 50%;margin-left: -10px;"," top: 19px;left: 24px;"];
  85. }else if(groupPhoto.length==1 || groupPhoto.length==0){
  86. cssArr = ["position: absolute;width: 50px;height: 50px;border-radius: 50px;background-size: 50px;overflow: hidden;"];
  87. }
  88. for(var i in groupPhoto){
  89. if(i>4) break;
  90. if(groupPhoto[i].avatar){
  91. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  92. '<img src="'+ getImgUrl(groupPhoto[i].avatar) +'"/>'+
  93. '</div>';
  94. }else{
  95. images+='<div class="div-image" style="'+cssArr[i]+'">'+
  96. '<img src="../../../images/d-default.png"/>'+
  97. '</div>';
  98. }
  99. }
  100. if(groupPhoto.length==0){
  101. images+='<div class="div-image" style="'+cssArr[0]+'">'+
  102. '<img src="../../../images/d-default.png"/>'+
  103. '</div>';
  104. }
  105. return images;
  106. },
  107. initPage = function(){
  108. var msg = plus.storage.getItem(msg_cache_key);
  109. if(msg){
  110. msg = JSON.parse(msg);
  111. render(msg, true, false);
  112. getData(false, false);
  113. } else {
  114. getData(true, false);
  115. }
  116. },
  117. render= function(data, isInit, isPullDown){
  118. plus.storage.setItem(msg_cache_key, JSON.stringify(data));
  119. if(data.length==0){
  120. $doingViewList.hide();
  121. $noResultWrap.show();
  122. $(".lin-search").hide();
  123. myScroll.refresh();
  124. myScroll.endPullDownToRefresh();
  125. mui.later(function(){
  126. $(".l-pullDown").hide();
  127. }, 200)
  128. } else {
  129. if(isInit){
  130. //初始化
  131. $doingViewList.html( template("ys_li_tmpl", {list: data}) );
  132. } else {
  133. //更新
  134. var v;
  135. var $lastDom = $('#doing_list li.zd');
  136. $lastDom = $lastDom.length? $($lastDom[$lastDom.length-1]) : undefined;
  137. for(var i=data.length-1; i>=0; i--){
  138. v = data[i];
  139. var $dom = $('#doing_list li[data-code="'+ v.code +'"]');
  140. if($dom.length){
  141. if($dom.attr('data-timestamp')==v.timestamp){
  142. $dom.find('.last-content').text(v.content);
  143. $dom.find('.red-dot').text(v.count).toggle(v.count>0&&v.my_role==0);
  144. } else {
  145. $dom.remove();
  146. $lastDom ? $lastDom.after( template('ys_li_tmpl', {list: [v]}) )
  147. : $doingViewList.prepend( template('ys_li_tmpl', {list: [v]}) );
  148. }
  149. } else {
  150. $lastDom ? $lastDom.after( template('ys_li_tmpl', {list: [v]}) )
  151. : $doingViewList.prepend( template('ys_li_tmpl', {list: [v]}) );
  152. }
  153. }
  154. if($('#doing_list:hidden').length){
  155. $doingViewList.show();
  156. $noResultWrap.hide();
  157. $(".lin-search").show();
  158. $(".l-pullDown").show();
  159. }
  160. if(isPullDown)
  161. myScroll.endPullDownToRefresh();
  162. else
  163. myScroll.refresh();
  164. }
  165. loadPhoto();
  166. }
  167. },
  168. getData = function(isInit, isPullDown) {
  169. var count = 0;
  170. var resultArr = [];
  171. imClient.Sessions.getSessionsWithDoctor(docInfo.code, 1, 2000, function(rs){
  172. var d, isNoRs = true;
  173. for(var k in rs){
  174. d = rs[k];
  175. if(d.business_type==1 && d.type!=0 && d.type!=1 && d.name!="系统消息"){
  176. isNoRs = false;
  177. var dateStr = asessTime(d.create_date);
  178. var p = {
  179. count: d.unread_count || 0,
  180. time: d.last_content==""? "" : dateStr,
  181. code: d.id,
  182. name: d.name,
  183. last_content: formatMsg( d.last_content, d.last_content_type),
  184. timestamp: d.create_date,
  185. photo:"",
  186. type: d.type, //2:p2p, 3:group
  187. my_role: d.my_role
  188. }
  189. count += p.count;
  190. resultArr.push(p);
  191. }
  192. }
  193. if(isNoRs){
  194. render([], isInit, isPullDown);
  195. } else {
  196. render(soreByTimeStamp(resultArr), isInit, isPullDown);
  197. //刷新未读消息数
  198. sendPost("/doctor/message/messages",{}, null, function(res){
  199. if(res.status == 200){
  200. var data = res.data;
  201. var xxWD = data.imMsgCount?JSON.parse(data.imMsgCount):'';
  202. mui.fire(plus.webview.getWebviewById("xiaoxi.html"), "imMsgCount", {patient: xxWD.patient,patientEnd:xxWD.patientEnd,doctor:xxWD.doctor});
  203. }
  204. })
  205. }
  206. }, function(msg){
  207. errHandle(isPullDown);
  208. console.error("e:"+JSON.stringify(msg))
  209. })
  210. },
  211. loadPhoto = function(){
  212. $.each($('#doing_list li[data-load!="1"]'), function(i, v) {
  213. imClient.Sessions.getParticipantsAvatars($(v).attr('data-code'), function(rs){
  214. $(v).attr('data-load', 1);
  215. if(rs.length>0){
  216. var $photo = $(v).find('.c-avatar-m');
  217. if($(v).attr('data-type')==2){
  218. for(var k in rs){
  219. if(docInfo.code != rs[k].id){
  220. $photo.html('<img src="'+ getImgUrl(rs[k].avatar) +'">');
  221. break;
  222. }
  223. }
  224. } else {
  225. $photo.html(setPhoto(rs));
  226. }
  227. }
  228. }, function(){
  229. })
  230. });
  231. },
  232. errHandle = function(isPullDown){
  233. if(isPullDown) myScroll.endPullDownToRefresh();
  234. mui.toast("数据加载失败,请检查网络无误后下拉刷新。");
  235. },
  236. soreByTimeStamp = function(resultArr){
  237. orginalData = resultArr;
  238. var resArr = [];
  239. if(!isEmpty(msg_del_list)){
  240. _.each(resultArr,function(v){
  241. var isAdd = false;
  242. _.each(msg_del_list,function(val){
  243. if(msg_del_list[v.code]!=v.timestamp.toString()){
  244. if(!isAdd){
  245. resArr.push(v);
  246. isAdd = true;
  247. }
  248. }
  249. })
  250. })
  251. }else{
  252. resArr = resultArr;
  253. }
  254. var aArr = [];
  255. var bArr = [];
  256. for(var x=0;x<resultArr.length;x++){
  257. resultArr[x].isZD = false;
  258. for(var i=zdList.length-1;i>=0;i--){
  259. if(zdList[i]==resultArr[x].code){
  260. resultArr[x].isZD = true;
  261. aArr.push(resultArr[x]);
  262. break;
  263. }
  264. }
  265. }
  266. _.chain(resArr)
  267. // 根据date日期字段排序
  268. .sortBy("timestamp").reverse()
  269. .each(function(v){
  270. if(!(_.contains(zdList,v.code))){
  271. bArr.push(v);
  272. }
  273. });
  274. var formatResult = aArr.concat(bArr);
  275. return formatResult;
  276. },
  277. isEmpty = function(value){
  278. return (Array.isArray(value) && value.length === 0)
  279. || (Object.prototype.isPrototypeOf(value) && Object.keys(value).length === 0);
  280. },
  281. // 绑定页面事件
  282. bindEvents = function () {
  283. /**
  284. * 搜索按钮点击
  285. */
  286. $('.lin-search').on('tap', function() {
  287. mui.fire(searchPage, "initSearch", null);
  288. searchPage.show();
  289. })
  290. $('#doing_list')
  291. /*屏蔽置顶和删除功能
  292. .on('touchstart', 'div.zdan', function(){ // 置顶按钮
  293. $zdDom = $(this);
  294. })
  295. .on('touchstart', 'div.qxzdan', function(){ // 取消置顶按钮
  296. var $li = $(this).closest('li');
  297. var code = $li.attr('data-code');
  298. zdList = _.without(zdList,code);
  299. plus.storage.setItem(zdListKey, JSON.stringify(zdList));
  300. var data = soreByTimeStamp(orginalData);
  301. var v;
  302. for(var i=0; i<data.length; i++){
  303. v =data[i];
  304. if(v.code == code){
  305. $li.remove();
  306. var $aLi = $doingViewList.find("li:eq("+ i +")");
  307. if($aLi.length>0){
  308. $aLi.before( template('ys_li_tmpl', {list: [v]}) );
  309. } else {
  310. $doingViewList.append( template('ys_li_tmpl', {list: [v]}) );
  311. }
  312. break;
  313. }
  314. }
  315. })
  316. .on('touchstart', 'div.scan', function(){ // 删除按钮
  317. var $li = $(this).closest('li');
  318. var code = $li.attr('data-code');
  319. msg_del_list[code] = $li.attr('data-timestamp');
  320. var newList = [];
  321. if(zdList){
  322. for(var i=0; i<zdList.length; i++){
  323. if(zdList[i] != code){
  324. newList.push(zdList[i]);
  325. }
  326. }
  327. }
  328. zdList = newList;
  329. plus.storage.setItem(zdListKey, JSON.stringify(zdList));
  330. saveMsgDelList();
  331. $li.remove();
  332. myScroll.refresh();
  333. return false;
  334. })
  335. .on('touchstart', 'li', function(e){ //左滑动作开始
  336. e.preventDefault();
  337. startScrollX = e.originalEvent.targetTouches[0].screenX;
  338. })
  339. .on('touchmove', 'li', function(e){ //左滑动作移动
  340. if(xScroll != true) return;
  341. e.preventDefault();
  342. scrollX = e.originalEvent.targetTouches[0].screenX - startScrollX;
  343. var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-70);
  344. if(scrollX<0 && scrollX >= offLeft)
  345. $(e.currentTarget).css('left', scrollX + 'px');
  346. })
  347. .on('touchend', 'li', function(e){ //左滑动作结束
  348. e.preventDefault();
  349. if(scrollX<-50){
  350. $scrollDom = $(e.currentTarget);
  351. var offLeft = $(e.currentTarget).find('div.operate').children('div').length * (-70);
  352. $scrollDom.animate({left: offLeft}, "fast");
  353. } else{
  354. startScrollX = undefined;
  355. $scrollDom = undefined;
  356. $(e.currentTarget).animate({left: '0px'}, "fast");
  357. }
  358. scrollX = undefined;
  359. xScroll = false;
  360. myScroll.options.isLeftScroll = false;
  361. })
  362. */
  363. .on('tap','li',function() {
  364. var userAgent = JSON.parse(plus.storage.getItem("userAgent"));
  365. if(userAgent.observer){
  366. mui.toast("观察者模式无法发送消息");
  367. }else{
  368. var $this = $(this),
  369. code = $this.attr('data-code'),
  370. name = $this.attr('data-name'),
  371. type = $this.attr('data-type');
  372. if(type==2){//医生对医生1对1
  373. openWebview("../../message/html/p2p.html",{sessionId: code, sessionName: name, type:"doctor-to-doctor"});
  374. }else{
  375. openWebview("../../message/html/tuanduiqunliao.html",{sessionId: code, sessionName: name});
  376. }
  377. }
  378. })
  379. /**
  380. * 取消左移块的事件
  381. */
  382. $('body').on('touchstart', function(e){
  383. if($scrollDom){
  384. $scrollDom.animate({left: '0px'}, "fast", undefined, function(){
  385. if($zdDom){
  386. var code = $zdDom.closest('li').prependTo("#doing_list").addClass("zd").attr('data-code');
  387. $zdDom.html("取消<br/>置顶").removeClass("zdan").addClass("qxzdan");
  388. $zdDom = undefined;
  389. var newList = [];
  390. if(zdList){
  391. for(var i=0; i<zdList.length; i++){
  392. if(zdList[i] != code){
  393. newList.push(zdList[i]);
  394. }
  395. }
  396. newList.push(code);
  397. } else {
  398. newList = [code];
  399. }
  400. zdList = newList;
  401. plus.storage.setItem(zdListKey, JSON.stringify(zdList));
  402. }
  403. });
  404. return false;
  405. }
  406. }).on('touchmove', function(e){
  407. e.preventDefault();
  408. })
  409. };
  410. // 页面业务处理流程开始
  411. new Promise(function(resolve, reject) {
  412. // TODO 临时放开
  413. //resolve(true);
  414. mui.plusReady(function() {
  415. // plus已经准备好,可以往下执行
  416. resolve(true);
  417. });
  418. }).then(function() {
  419. // 获取基础环境信息
  420. return getBaseEnvPromise().then(function(env) {
  421. baseEnv = env;
  422. }).then(function() {
  423. // 获取登录医生信息
  424. loginerInfo = getLoginerInfo();
  425. loaded();
  426. initPage();
  427. bindEvents();// 绑定页面事件
  428. })
  429. }).catch(function(e) {
  430. plus.nativeUI.closeWaiting();
  431. console && console.error(e);
  432. });
  433. var xScroll = 0;
  434. var yOff = 0, xOff = 0, myScroll;
  435. var $scrollDom, $zdDom;
  436. /**
  437. * 初始化iscroll
  438. */
  439. function loaded () {
  440. var deceleration = mui.os.ios?0.003:0.0009;
  441. mui('.mui-scroll-wrapper').scroll({
  442. bounce: false,
  443. indicators: true, //是否显示滚动条
  444. deceleration:deceleration
  445. });
  446. myScroll = mui(".mui-scroll").pullToRefresh({
  447. down: {
  448. callback: function() {
  449. var self = this;
  450. setTimeout(function() {
  451. getData(false, true);
  452. }, 1000);
  453. var zkHome = plus.webview.getWebviewById("home1.html");
  454. if(zkHome) {
  455. mui.fire(zkHome,'refreshBaseInfo');
  456. }
  457. }
  458. },
  459. scrollLeft: function(){
  460. xScroll = true;
  461. }
  462. });
  463. return;
  464. }
  465. function saveMsgDelList(){
  466. plus.storage.setItem(msg_del_list_key, JSON.stringify(msg_del_list));
  467. }
  468. /**
  469. * 获取最后一个置顶元素,如无置顶元素,返回false
  470. */
  471. function getTopDom(){
  472. var $d;
  473. for(var i=0; i<zdList.length; i++){
  474. $d = $('#doing_list li[data-code="'+ zdList[i] +'"]');
  475. if($d.length > 0)
  476. return $d;
  477. }
  478. return false;
  479. }
  480. function formatMsg(msg, type){
  481. switch(parseInt(type)){
  482. case 1: return msg;
  483. case 2: return "[图片]";
  484. case 3: return "[语音]";
  485. case 4:
  486. msg = msg.replace(/\n/g, "\\n");
  487. msg = msg.replace(/\r/g, "\\r");
  488. msg = JSON.parse(msg);
  489. var msgType = msg.type || 0;
  490. if(msgType==0){
  491. return "[健康教育]";
  492. }else if(msgType==1){
  493. return "[健康记录]";
  494. }else{
  495. return "[健康指导]";
  496. }
  497. case 5:
  498. msg = JSON.parse(msg);
  499. return "已向"+msg.doctor_name+"求助";
  500. case 6: return msg;
  501. case 7: return msg;
  502. case 12: return "[视频]";
  503. case 13: return msg;
  504. }
  505. }
  506. /**
  507. * 往指定元素后面添加
  508. */
  509. function appendAfterZd($lastZd, appendDom){
  510. if($lastZd != false){
  511. $lastZd.after(appendDom);
  512. } else {
  513. $('#doing_list').prepend(appendDom);
  514. }
  515. }
  516. function asessTime(timestamp){
  517. var today = new Date().format('yyyy-MM-dd');
  518. var date = new Date();
  519. date.setTime(timestamp);
  520. var dateStr = date.format('yyyy-MM-dd');
  521. if(!today.substring(0,4) == dateStr.substring(0,4)){
  522. return dateStr;
  523. }
  524. if(today == dateStr){
  525. return date.format('hh:mm');
  526. }
  527. return date.format('MM-dd');
  528. }
  529. window.addEventListener("update", function(){
  530. getData(false, false);
  531. });
  532. window.addEventListener("xiaoxiUpdate", function(){
  533. getData(false, false);
  534. });