tizhong-view.js 13 KB

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