123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531 |
- var Request = GetRequest();
- var mbglType = "";
- mbglType = Request["type"];//当从慢病管理界面跳转到该页面的时候要返回上一个页面
- $(function() {
- var $viewTabs = $('#viewTabs'),
- $swiperWrapper = $('.swiper-wrapper'),
- $duringList = $('#duringList'),
- $qushiView = $('#qushiView'),
- $liebiaoView = $('#liebiaoView'),
- $xtEdit = $('.xt-edit'),
- $btns = $('.btns'),
- $cancelBtn = $('.cancel-btn'),
- $okBtn = $('.ok-btn'),
- $popMain = $('.pop-main'),
- $popBtns = $('.pop-btns'),
- $xtAdd = $('.xt-add'),
- $popCancelBtn = $('.pop-cancel-btn'),
- $selDate = $('#selDate'),
- $popEditBtn = $('.pop-edit-btn'),
- $popDelBtn = $('.pop-del-btn'),
- d1 = [],
- d2 = [],
- d3 = [],
- d4 = [],
- page = 0,
- pagesize = 10,
- dayNum = 6,
- swiper = null,
- thatIndex = 1,
- isTrue = false,
- isLoad = false,
- $zcNum = $('.zc-num'),
- $pgNum = $('.pg-num'),
- $pdNum = $('.pd-num');
- var scroller1 = new IScrollPullUpDown('wrapper6', {
- probeType: 2,
- bounceTime: 250,
- bounceEasing: 'quadratic',
- mouseWheel: false,
- scrollbars: true,
- fadeScrollbars: true,
- click:true,
- // preventDefault: false,
- interactiveScrollbars: false
- }, null, pullUpAction);
-
- var norTime = '',
- begindate = '',
- enddate = '',
- sn = 0;
- var charts = [];
-
- //判断最后的日期
- sendPost("patient/health_index/last", {}, "json", "get", function(){
- dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求失败'}).show();
- }, function(res){
- if(res.status == 200){
- var d0 = decodeURIComponent(Request["dateT"]);
- if(res.data.xt){
- var d1 = (res.data.xt.recordDate).split(' ')[0];
- var d2 = Date.parse(new Date(d0))
- var d3 = Date.parse(new Date(d1))
- if(d2<d3){
- //取大
- norTime = d1
- begindate = d1
- }else{
- norTime = d0
- begindate = d0
- }
- }else{
- norTime = d0
- begindate = d0
- }
- enddate = getDateFromCurrentDate(begindate, -dayNum)
- init()
- }else{
- dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'数据查询失败'}).show();
- }
- })
- function changeTabCon() {
- var qsv = $qushiView.css('display'),
- lbv = $liebiaoView.css('display');
- if(qsv === 'block') {
- lbv.hide();
- }
- }
- function init() {
- Promise.all([(function () {
- $swiperWrapper
- .append($('<li id="chart0" class="swiper-slide"> </li>'))
- .append($('<li id="chart1" class="swiper-slide"> </li>'))
- .append($('<li id="chart2" class="swiper-slide"> </li>'))
- .append($('<li id="chart3" class="swiper-slide"> </li>'));
- })()])
- .then(function(echarts) {
- swiper = new Swiper('.swiper-container', {
- paginationClickable: false,
- onSlideChangeStart: function(swiper) {
- var ind = swiper.activeIndex;
- if (ind < sn) {
- begindate = enddate;
- enddate = getDateFromCurrentDate( begindate, -dayNum);
- } else {
- enddate = begindate;
- begindate = getDateFromCurrentDate( begindate, dayNum);
- }
- sn = ind;
- loadPromise( thatIndex, enddate, begindate, sn);
- }
- });
- })
- .then(function () {
- $swiperWrapper.find('.swiper-slide');
- swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
- sn = swiper.slides.length - 1;
- checkLoad(thatIndex);
- })
- .catch(function(e) {
- console && console.error(e)
- })
- }
- function loadPromise( t, enddate, begindate, n) {
- Promise.all([
- queryChatByTypePromise(1, enddate, begindate, t)
- ])
- .then(function(res) {
- d1 = res[0];
- loadData(n);
- })
- .catch(function(e) {
- console && console.error(e)
- });
- }
- function loadData(n) {
- new Promise(function(resolve, reject) {
- var yAxisName = '单位(mmol/L)';
- var el = document.getElementById('chart' + n);
- if (typeof d1.list == 'undefined') {
- d1.list = [];
- }
- pData(d1.list);
- Promise.all([
- createChartPromise(el, 1, yAxisName, getxAxisData(d1.list), getSeriesData(d1.list))
- ])
- .then(function(echarts) {
- charts.push(echarts[0]);
- $(el).append('<div class="c-time">< ' + enddate + ' 至 ' + begindate + ' ></div>');
- })
- .then(function() {
-
- })
- .catch(function(e) {
- console && console.error(e)
- })
- });
- }
- function checkLoad(d) {
- loadPromise( d, enddate, begindate, sn);
- }
-
- function loadListPromise() {
- page++;
- Promise.all([
- queryListByTypeP(1, getDateFromCurrentDate(begindate, -2000), begindate, page, pagesize)
- ])
- .then(function(res) {
- $(".pullUp").hide();
- var data = res[0];
- if (data.list.length > 0) {
- for(var i = 0, len = data.list.length; i < len; i++) {
- var czrq = new Date(data.list[i]['czrq'].replace(/\-/g, "/")),
- recordDate = new Date(data.list[i]['recordDate'].replace(/\-/g, "/")),
- sortDate = new Date(data.list[i]['sortDate'].replace(/\-/g, "/"));
- data.list[i]['czrq'] = czrq.getFullYear() + '-' + (czrq.getMonth() + 1) + '-' + (czrq.getDate());
- data.list[i]['recordDate'] = recordDate.getFullYear() + '-' + (recordDate.getMonth() + 1) + '-' + (recordDate.getDate());
- data.list[i]['sortDate'] = sortDate.getFullYear() + '-' + (sortDate.getMonth() + 1) + '-' + (sortDate.getDate());
-
- for(var j = 1; j <= 7; j++) {
- var d = data.list[i]['time' + j];
- if(d) {
- data.list[i]['tahttime' + j] = d;
- data.list[i]['time' + j] = (d.toString()).substring(11, 16);
- }
- }
- }
- console.log(data.list);
- $('#lbvCon').append(template('xtTmp', {
- data: data.list
- }));
-
- if($xtEdit.css('display') == "none") {
- $('#lbvCon').find('.l-edit-icon').addClass('active')
- }
- scroller1.myScroll.refresh()
- } else {
- if(!isLoad) {
- dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无数据'}).show();
- $xtEdit.unbind('click');
- } else {
- dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无更多数据'}).show();
- }
- }
- isLoad = true;
- })
- .catch(function(e) {
- console && console.error(e)
- });
- }
- function getNormalLevel(index) {
- return [
- [],
- [7, 4],
- [11.1, 4],
- [7, 4],
- [11.1, 4],
- [7, 4],
- [11.1, 4],
- [7, 4]
- ][index];
- }
- function getxAxisData(data) {
- var a = [],
- b = [];
- b = _.chain(data).pluck('date').map(function(d) {
- return d.substr(5, 5)
- }).value();
- for(var i = b.length - 1; i >= 0; i--) {
- a.push(b[i]);
- }
- return a;
- }
- function pData(d) {
- var index = $duringList.find('li.curr').attr('data-index');
- var max = getNormalLevel(index)[0],
- min = getNormalLevel(index)[1],
- pgN = 0,
- pdN = 0,
- zcN = 0;
- for (var i = 0,len = d.length; i < len; i++) {
- var value = d[i].value1;
- if(value < min) {
- pdN += 1;
- }
- if(value > max) {
- pgN += 1;
- }
- if (value >= min && value <= max) {
- zcN += 1;
- }
- }
- $zcNum.html(zcN);
- $pgNum.html(pgN);
- $pdNum.html(pdN);
- }
- function getSeriesData(data) {
- var index = $duringList.find('li.curr').attr('data-index');
- var a = [],
- b = [];
- b = _.chain(data).pluck('value1').map(function(v) {
- // b = _.chain(data).pluck('value'+index).map(function(v) {
- var max = getNormalLevel(index)[0],
- min = getNormalLevel(index)[1];
- return buildData(v, max, min);
- }).value();
- for(var i = b.length - 1; i >= 0; i--) {
- if(!!b[i]) {
- a.push(b[i]);
- }
- }
- return a;
- }
- function queryListByTypeP(type, begindate, enddate, page, pagesize) {
- //拼请求内容
- d.show();
- var params = {};
- params.type = type;
- params.page = page;
- params.pagesize = pagesize;
- params.start = begindate + " 00:00:00";
- params.end = enddate + " 23:59:59";
- return new Promise(function(resolve, reject) {
- //发送ajax请求
- console.log(params)
- sendPost("patient/health_index/list", params, "json", "post", queryListFailed, function(res) {
- d.close();
- if(res.status == 200) {
- resolve(res)
- } else {
- queryListFailed(res);
- }
- });
- })
- }
- //上拉加载数据
- function pullUpAction(theScrollerTemp) {
- $(".pullUp").show();
- setTimeout(function() {
- loadListPromise();
- }, 1000);
- }
- bindEvents();
- function resetSwiper () {
- $swiperWrapper.find('.swiper-slide').html('');
- swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
- sn = swiper.slides.length - 1;
- begindate = norTime;
- }
- function pushHistory() {
- var state = {
- title: "血糖",
- url: "#"
- };
- setTimeout(function (){
- window.history.pushState(state, "血糖", "#");
- },1000);
- }
- pushHistory();
- function bindEvents() {
- if(mbglType != 0){
- setTimeout( function () {
- window.addEventListener("popstate", function(e) {
- location.href = 'health-record.html';
- }, false);
- }, 300);
- }
-
- //tab
- $viewTabs.on('click', 'li', function(e) {
- e.stopPropagation();
- var index = $(this).index();
- $(this).siblings().removeClass('curr');
- $(this).addClass('curr');
- if(index === 0) {
- $qushiView.show();
- $liebiaoView.hide();
- $xtEdit.hide();
- } else {
- $qushiView.hide();
- $liebiaoView.show();
- $xtEdit.show();
- var data = null;
- if(dayNum == 7) {
- data = d1.list;
- }
- if(dayNum == 30) {
- data = d2.list;
- }
- if(dayNum == 90) {
- data = d3.list;
- }
- if(dayNum == -1) {
- data = d4.list;
- }
- if(!isLoad) {
- // isLoad = true;
- loadListPromise();
- }
- }
- });
- //天数选择
- $selDate.on('click', 'li', function(e) {
- var index = $(this).index();
- $(this).siblings().removeClass('curr');
- $(this).addClass('curr');
- resetSwiper();
- switch(index) {
- case 0:
- dayNum = 6;
- break;
- case 1:
- dayNum = 30;
- break;
- case 2:
- dayNum = 90;
- break;
- case 3:
- dayNum = 365/2;
- break;
- }
- enddate = getDateFromCurrentDate(begindate, -dayNum);
- checkLoad(thatIndex);
- });
- //就餐时间段选择
- $duringList.on('tap', 'li', function(e) {
- e.stopPropagation();
- var d = $(this).attr('data-index');
- if(thatIndex == d) {
- return;
- }
- thatIndex = parseInt(d);
- $(this).closest('#duringList').find('li').removeClass('curr');
- $(this).addClass('curr');
- enddate = getDateFromCurrentDate(begindate, -dayNum);
- checkLoad(thatIndex);
- });
- $xtEdit.on('click', function() {
- $(this).hide();
- $btns.show();
- $xtAdd.hide();
- $liebiaoView.find('.l-edit-icon').addClass('active');
- });
- //取消
- $cancelBtn.on('click', function() {
- $liebiaoView.find('.l-edit-icon').removeClass('active');
- $xtEdit.show();
- $xtAdd.show();
- $btns.hide();
- });
- //完成
- $okBtn.on('click', function() {
- $liebiaoView.find('.l-edit-icon').removeClass('active');
- $xtEdit.show();
- $xtAdd.show();
- $btns.hide();
- });
- //修改列表
- $liebiaoView.on('click', '.l-item', function() {
- var lei = $(this).find('.l-edit-icon'),
- id = $(this).attr('data-id'),
- type = $(this).attr('data-type'),
- time = $(this).attr('data-time'),
- sn = $(this).attr('data-sn'),
- val = $(this).attr('data-val');
- if(lei.hasClass('active')) {
- $popMain.addClass('active');
- $popBtns.addClass('active');
- $popEditBtn.attr('data-sn', sn);
- $popEditBtn.attr('data-id', id);
- $popEditBtn.attr('data-type', type);
- $popEditBtn.attr('data-time', time);
- $popEditBtn.attr('data-val', val);
- $popDelBtn.attr('data-id', id);
- }
- });
- // 修改
- $popEditBtn.on('click', function() {
- var id = $(this).attr('data-id'),
- type = $(this).attr('data-type'),
- sn = $(this).attr('data-sn') == '' ? 0 : 1,
- time = $(this).attr('data-time'),
- val = $(this).attr('data-val');
- window.location.href = 'health-index-add-bloodsugar.html?id=' + id + '&type=' + type + '&time=' + time + '&val=' + val + '&sn=' + sn;
- });
- // 删除
- $popDelBtn.on('click', function() {
- var id = $(this).attr('data-id');
- sendPost("patient/health_index/modify?id=" + id, {}, "json", "post", queryListFailed, function(res) {
- d.close();
- if(res.status == 200) {
- dialog({
- contentType: 'tipsbox',
- bottom: true,
- skin: 'bk-popup',
- content: '删除成功'
- }).show();
- setTimeout(function() {
- location.reload();
- }, 2000);
- } else {
- dialog({
- contentType: 'tipsbox',
- bottom: true,
- skin: 'bk-popup',
- content: '删除失败'
- }).show();
- setTimeout(function() {
- location.reload();
- }, 2000);
- }
- });
- });
- $popCancelBtn.on('click', function() {
- $popMain.removeClass('active');
- $popBtns.removeClass('active');
- });
- }
- });
- var render = function(tmpl, data) {
- return tmpl.replace(/\{\{(\w+)\}\}/g, function(m, $1) {
- return data[$1];
- });
- },
- //根据sd获取n天前的时间
- getBeforeDate = function(sd, n) {
- var n = n;
- var d = new Date(sd.replace(/\-/g, "/") + ' 00:00:00');
- var year = d.getFullYear();
- var mon = d.getMonth() + 1;
- var day = d.getDate();
- if(day <= n) {
- if(mon > 1) {
- mon = mon - 1;
- } else {
- year = year - 1;
- mon = 12;
- }
- }
- d.setDate(d.getDate() - n);
- year = d.getFullYear();
- mon = d.getMonth() + 1;
- day = d.getDate();
- s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
- return s;
- };
-
- function getDateFromCurrentDate(fromDate,dayInterval){
- var curDate = new Date(Date.parse(fromDate.replace(/-/g,"/")));
- curDate.setDate(curDate.getDate()+dayInterval);
- var year = curDate.getFullYear();
- var month = (curDate.getMonth()+1)<10?"0"+(curDate.getMonth()+1):(curDate.getMonth()+1);
- var day = curDate.getDate()<10?"0"+curDate.getDate():curDate.getDate();
- return year+"-"+month+"-"+day;
- };
|