xuetang-view.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. var Request = GetRequest();
  2. var mbglType = "";
  3. mbglType = Request["type"];//当从慢病管理界面跳转到该页面的时候要返回上一个页面
  4. $(function() {
  5. var $viewTabs = $('#viewTabs'),
  6. $swiperWrapper = $('.swiper-wrapper'),
  7. $duringList = $('#duringList'),
  8. $qushiView = $('#qushiView'),
  9. $liebiaoView = $('#liebiaoView'),
  10. $xtEdit = $('.xt-edit'),
  11. $btns = $('.btns'),
  12. $cancelBtn = $('.cancel-btn'),
  13. $okBtn = $('.ok-btn'),
  14. $popMain = $('.pop-main'),
  15. $popBtns = $('.pop-btns'),
  16. $xtAdd = $('.xt-add'),
  17. $popCancelBtn = $('.pop-cancel-btn'),
  18. $selDate = $('#selDate'),
  19. $popEditBtn = $('.pop-edit-btn'),
  20. $popDelBtn = $('.pop-del-btn'),
  21. d1 = [],
  22. d2 = [],
  23. d3 = [],
  24. d4 = [],
  25. page = 0,
  26. pagesize = 10,
  27. dayNum = 6,
  28. swiper = null,
  29. thatIndex = 1,
  30. isTrue = false,
  31. isLoad = false,
  32. $zcNum = $('.zc-num'),
  33. $pgNum = $('.pg-num'),
  34. $pdNum = $('.pd-num');
  35. var scroller1 = new IScrollPullUpDown('wrapper6', {
  36. probeType: 2,
  37. bounceTime: 250,
  38. bounceEasing: 'quadratic',
  39. mouseWheel: false,
  40. scrollbars: true,
  41. fadeScrollbars: true,
  42. click:true,
  43. // preventDefault: false,
  44. interactiveScrollbars: false
  45. }, null, pullUpAction);
  46. var norTime = '',
  47. begindate = '',
  48. enddate = '',
  49. sn = 0;
  50. var charts = [];
  51. //判断最后的日期
  52. sendPost("patient/health_index/last", {}, "json", "get", function(){
  53. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求失败'}).show();
  54. }, function(res){
  55. if(res.status == 200){
  56. var d0 = decodeURIComponent(Request["dateT"]);
  57. if(res.data.xt){
  58. var d1 = (res.data.xt.recordDate).split(' ')[0];
  59. var d2 = Date.parse(new Date(d0))
  60. var d3 = Date.parse(new Date(d1))
  61. if(d2<d3){
  62. //取大
  63. norTime = d1
  64. begindate = d1
  65. }else{
  66. norTime = d0
  67. begindate = d0
  68. }
  69. }else{
  70. norTime = d0
  71. begindate = d0
  72. }
  73. enddate = getDateFromCurrentDate(begindate, -dayNum)
  74. init()
  75. }else{
  76. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'数据查询失败'}).show();
  77. }
  78. })
  79. function changeTabCon() {
  80. var qsv = $qushiView.css('display'),
  81. lbv = $liebiaoView.css('display');
  82. if(qsv === 'block') {
  83. lbv.hide();
  84. }
  85. }
  86. function init() {
  87. Promise.all([(function () {
  88. $swiperWrapper
  89. .append($('<li id="chart0" class="swiper-slide"> </li>'))
  90. .append($('<li id="chart1" class="swiper-slide"> </li>'))
  91. .append($('<li id="chart2" class="swiper-slide"> </li>'))
  92. .append($('<li id="chart3" class="swiper-slide"> </li>'));
  93. })()])
  94. .then(function(echarts) {
  95. swiper = new Swiper('.swiper-container', {
  96. paginationClickable: false,
  97. onSlideChangeStart: function(swiper) {
  98. var ind = swiper.activeIndex;
  99. if (ind < sn) {
  100. begindate = enddate;
  101. enddate = getDateFromCurrentDate( begindate, -dayNum);
  102. } else {
  103. enddate = begindate;
  104. begindate = getDateFromCurrentDate( begindate, dayNum);
  105. }
  106. sn = ind;
  107. loadPromise( thatIndex, enddate, begindate, sn);
  108. }
  109. });
  110. })
  111. .then(function () {
  112. $swiperWrapper.find('.swiper-slide');
  113. swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
  114. sn = swiper.slides.length - 1;
  115. checkLoad(thatIndex);
  116. })
  117. .catch(function(e) {
  118. console && console.error(e)
  119. })
  120. }
  121. function loadPromise( t, enddate, begindate, n) {
  122. Promise.all([
  123. queryChatByTypePromise(1, enddate, begindate, t)
  124. ])
  125. .then(function(res) {
  126. d1 = res[0];
  127. loadData(n);
  128. })
  129. .catch(function(e) {
  130. console && console.error(e)
  131. });
  132. }
  133. function loadData(n) {
  134. new Promise(function(resolve, reject) {
  135. var yAxisName = '单位(mmol/L)';
  136. var el = document.getElementById('chart' + n);
  137. if (typeof d1.list == 'undefined') {
  138. d1.list = [];
  139. }
  140. pData(d1.list);
  141. Promise.all([
  142. createChartPromise(el, 1, yAxisName, getxAxisData(d1.list), getSeriesData(d1.list))
  143. ])
  144. .then(function(echarts) {
  145. charts.push(echarts[0]);
  146. $(el).append('<div class="c-time">< ' + enddate + ' 至 ' + begindate + ' ></div>');
  147. })
  148. .then(function() {
  149. })
  150. .catch(function(e) {
  151. console && console.error(e)
  152. })
  153. });
  154. }
  155. function checkLoad(d) {
  156. loadPromise( d, enddate, begindate, sn);
  157. }
  158. function loadListPromise() {
  159. page++;
  160. Promise.all([
  161. queryListByTypeP(1, getDateFromCurrentDate(begindate, -2000), begindate, page, pagesize)
  162. ])
  163. .then(function(res) {
  164. $(".pullUp").hide();
  165. var data = res[0];
  166. if (data.list.length > 0) {
  167. for(var i = 0, len = data.list.length; i < len; i++) {
  168. var czrq = new Date(data.list[i]['czrq'].replace(/\-/g, "/")),
  169. recordDate = new Date(data.list[i]['recordDate'].replace(/\-/g, "/")),
  170. sortDate = new Date(data.list[i]['sortDate'].replace(/\-/g, "/"));
  171. data.list[i]['czrq'] = czrq.getFullYear() + '-' + (czrq.getMonth() + 1) + '-' + (czrq.getDate());
  172. data.list[i]['recordDate'] = recordDate.getFullYear() + '-' + (recordDate.getMonth() + 1) + '-' + (recordDate.getDate());
  173. data.list[i]['sortDate'] = sortDate.getFullYear() + '-' + (sortDate.getMonth() + 1) + '-' + (sortDate.getDate());
  174. for(var j = 1; j <= 7; j++) {
  175. var d = data.list[i]['time' + j];
  176. if(d) {
  177. data.list[i]['tahttime' + j] = d;
  178. data.list[i]['time' + j] = (d.toString()).substring(11, 16);
  179. }
  180. }
  181. }
  182. console.log(data.list);
  183. $('#lbvCon').append(template('xtTmp', {
  184. data: data.list
  185. }));
  186. if($xtEdit.css('display') == "none") {
  187. $('#lbvCon').find('.l-edit-icon').addClass('active')
  188. }
  189. scroller1.myScroll.refresh()
  190. } else {
  191. if(!isLoad) {
  192. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无数据'}).show();
  193. $xtEdit.unbind('click');
  194. } else {
  195. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无更多数据'}).show();
  196. }
  197. }
  198. isLoad = true;
  199. })
  200. .catch(function(e) {
  201. console && console.error(e)
  202. });
  203. }
  204. function getNormalLevel(index) {
  205. return [
  206. [],
  207. [7, 4],
  208. [11.1, 4],
  209. [7, 4],
  210. [11.1, 4],
  211. [7, 4],
  212. [11.1, 4],
  213. [7, 4]
  214. ][index];
  215. }
  216. function getxAxisData(data) {
  217. var a = [],
  218. b = [];
  219. b = _.chain(data).pluck('date').map(function(d) {
  220. return d.substr(5, 5)
  221. }).value();
  222. for(var i = b.length - 1; i >= 0; i--) {
  223. a.push(b[i]);
  224. }
  225. return a;
  226. }
  227. function pData(d) {
  228. var index = $duringList.find('li.curr').attr('data-index');
  229. var max = getNormalLevel(index)[0],
  230. min = getNormalLevel(index)[1],
  231. pgN = 0,
  232. pdN = 0,
  233. zcN = 0;
  234. for (var i = 0,len = d.length; i < len; i++) {
  235. var value = d[i].value1;
  236. if(value < min) {
  237. pdN += 1;
  238. }
  239. if(value > max) {
  240. pgN += 1;
  241. }
  242. if (value >= min && value <= max) {
  243. zcN += 1;
  244. }
  245. }
  246. $zcNum.html(zcN);
  247. $pgNum.html(pgN);
  248. $pdNum.html(pdN);
  249. }
  250. function getSeriesData(data) {
  251. var index = $duringList.find('li.curr').attr('data-index');
  252. var a = [],
  253. b = [];
  254. b = _.chain(data).pluck('value1').map(function(v) {
  255. // b = _.chain(data).pluck('value'+index).map(function(v) {
  256. var max = getNormalLevel(index)[0],
  257. min = getNormalLevel(index)[1];
  258. return buildData(v, max, min);
  259. }).value();
  260. for(var i = b.length - 1; i >= 0; i--) {
  261. if(!!b[i]) {
  262. a.push(b[i]);
  263. }
  264. }
  265. return a;
  266. }
  267. function queryListByTypeP(type, begindate, enddate, page, pagesize) {
  268. //拼请求内容
  269. d.show();
  270. var params = {};
  271. params.type = type;
  272. params.page = page;
  273. params.pagesize = pagesize;
  274. params.start = begindate + " 00:00:00";
  275. params.end = enddate + " 23:59:59";
  276. return new Promise(function(resolve, reject) {
  277. //发送ajax请求
  278. console.log(params)
  279. sendPost("patient/health_index/list", params, "json", "post", queryListFailed, function(res) {
  280. d.close();
  281. if(res.status == 200) {
  282. resolve(res)
  283. } else {
  284. queryListFailed(res);
  285. }
  286. });
  287. })
  288. }
  289. //上拉加载数据
  290. function pullUpAction(theScrollerTemp) {
  291. $(".pullUp").show();
  292. setTimeout(function() {
  293. loadListPromise();
  294. }, 1000);
  295. }
  296. bindEvents();
  297. function resetSwiper () {
  298. $swiperWrapper.find('.swiper-slide').html('');
  299. swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
  300. sn = swiper.slides.length - 1;
  301. begindate = norTime;
  302. }
  303. function pushHistory() {
  304. var state = {
  305. title: "血糖",
  306. url: "#"
  307. };
  308. setTimeout(function (){
  309. window.history.pushState(state, "血糖", "#");
  310. },1000);
  311. }
  312. pushHistory();
  313. function bindEvents() {
  314. if(mbglType != 0){
  315. setTimeout( function () {
  316. window.addEventListener("popstate", function(e) {
  317. location.href = 'health-record.html';
  318. }, false);
  319. }, 300);
  320. }
  321. //tab
  322. $viewTabs.on('click', 'li', function(e) {
  323. e.stopPropagation();
  324. var index = $(this).index();
  325. $(this).siblings().removeClass('curr');
  326. $(this).addClass('curr');
  327. if(index === 0) {
  328. $qushiView.show();
  329. $liebiaoView.hide();
  330. $xtEdit.hide();
  331. } else {
  332. $qushiView.hide();
  333. $liebiaoView.show();
  334. $xtEdit.show();
  335. var data = null;
  336. if(dayNum == 7) {
  337. data = d1.list;
  338. }
  339. if(dayNum == 30) {
  340. data = d2.list;
  341. }
  342. if(dayNum == 90) {
  343. data = d3.list;
  344. }
  345. if(dayNum == -1) {
  346. data = d4.list;
  347. }
  348. if(!isLoad) {
  349. // isLoad = true;
  350. loadListPromise();
  351. }
  352. }
  353. });
  354. //天数选择
  355. $selDate.on('click', 'li', function(e) {
  356. var index = $(this).index();
  357. $(this).siblings().removeClass('curr');
  358. $(this).addClass('curr');
  359. resetSwiper();
  360. switch(index) {
  361. case 0:
  362. dayNum = 6;
  363. break;
  364. case 1:
  365. dayNum = 30;
  366. break;
  367. case 2:
  368. dayNum = 90;
  369. break;
  370. case 3:
  371. dayNum = 365/2;
  372. break;
  373. }
  374. enddate = getDateFromCurrentDate(begindate, -dayNum);
  375. checkLoad(thatIndex);
  376. });
  377. //就餐时间段选择
  378. $duringList.on('tap', 'li', function(e) {
  379. e.stopPropagation();
  380. var d = $(this).attr('data-index');
  381. if(thatIndex == d) {
  382. return;
  383. }
  384. thatIndex = parseInt(d);
  385. $(this).closest('#duringList').find('li').removeClass('curr');
  386. $(this).addClass('curr');
  387. enddate = getDateFromCurrentDate(begindate, -dayNum);
  388. checkLoad(thatIndex);
  389. });
  390. $xtEdit.on('click', function() {
  391. $(this).hide();
  392. $btns.show();
  393. $xtAdd.hide();
  394. $liebiaoView.find('.l-edit-icon').addClass('active');
  395. });
  396. //取消
  397. $cancelBtn.on('click', function() {
  398. $liebiaoView.find('.l-edit-icon').removeClass('active');
  399. $xtEdit.show();
  400. $xtAdd.show();
  401. $btns.hide();
  402. });
  403. //完成
  404. $okBtn.on('click', function() {
  405. $liebiaoView.find('.l-edit-icon').removeClass('active');
  406. $xtEdit.show();
  407. $xtAdd.show();
  408. $btns.hide();
  409. });
  410. //修改列表
  411. $liebiaoView.on('click', '.l-item', function() {
  412. var lei = $(this).find('.l-edit-icon'),
  413. id = $(this).attr('data-id'),
  414. type = $(this).attr('data-type'),
  415. time = $(this).attr('data-time'),
  416. sn = $(this).attr('data-sn'),
  417. val = $(this).attr('data-val');
  418. if(lei.hasClass('active')) {
  419. $popMain.addClass('active');
  420. $popBtns.addClass('active');
  421. $popEditBtn.attr('data-sn', sn);
  422. $popEditBtn.attr('data-id', id);
  423. $popEditBtn.attr('data-type', type);
  424. $popEditBtn.attr('data-time', time);
  425. $popEditBtn.attr('data-val', val);
  426. $popDelBtn.attr('data-id', id);
  427. }
  428. });
  429. // 修改
  430. $popEditBtn.on('click', function() {
  431. var id = $(this).attr('data-id'),
  432. type = $(this).attr('data-type'),
  433. sn = $(this).attr('data-sn') == '' ? 0 : 1,
  434. time = $(this).attr('data-time'),
  435. val = $(this).attr('data-val');
  436. window.location.href = 'health-index-add-bloodsugar.html?id=' + id + '&type=' + type + '&time=' + time + '&val=' + val + '&sn=' + sn;
  437. });
  438. // 删除
  439. $popDelBtn.on('click', function() {
  440. var id = $(this).attr('data-id');
  441. sendPost("patient/health_index/modify?id=" + id, {}, "json", "post", queryListFailed, function(res) {
  442. d.close();
  443. if(res.status == 200) {
  444. dialog({
  445. contentType: 'tipsbox',
  446. bottom: true,
  447. skin: 'bk-popup',
  448. content: '删除成功'
  449. }).show();
  450. setTimeout(function() {
  451. location.reload();
  452. }, 2000);
  453. } else {
  454. dialog({
  455. contentType: 'tipsbox',
  456. bottom: true,
  457. skin: 'bk-popup',
  458. content: '删除失败'
  459. }).show();
  460. setTimeout(function() {
  461. location.reload();
  462. }, 2000);
  463. }
  464. });
  465. });
  466. $popCancelBtn.on('click', function() {
  467. $popMain.removeClass('active');
  468. $popBtns.removeClass('active');
  469. });
  470. }
  471. });
  472. var render = function(tmpl, data) {
  473. return tmpl.replace(/\{\{(\w+)\}\}/g, function(m, $1) {
  474. return data[$1];
  475. });
  476. },
  477. //根据sd获取n天前的时间
  478. getBeforeDate = function(sd, n) {
  479. var n = n;
  480. var d = new Date(sd.replace(/\-/g, "/") + ' 00:00:00');
  481. var year = d.getFullYear();
  482. var mon = d.getMonth() + 1;
  483. var day = d.getDate();
  484. if(day <= n) {
  485. if(mon > 1) {
  486. mon = mon - 1;
  487. } else {
  488. year = year - 1;
  489. mon = 12;
  490. }
  491. }
  492. d.setDate(d.getDate() - n);
  493. year = d.getFullYear();
  494. mon = d.getMonth() + 1;
  495. day = d.getDate();
  496. s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
  497. return s;
  498. };
  499. function getDateFromCurrentDate(fromDate,dayInterval){
  500. var curDate = new Date(Date.parse(fromDate.replace(/-/g,"/")));
  501. curDate.setDate(curDate.getDate()+dayInterval);
  502. var year = curDate.getFullYear();
  503. var month = (curDate.getMonth()+1)<10?"0"+(curDate.getMonth()+1):(curDate.getMonth()+1);
  504. var day = curDate.getDate()<10?"0"+curDate.getDate():curDate.getDate();
  505. return year+"-"+month+"-"+day;
  506. };