yaowei-view.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. page = 0,
  22. pagesize = 10,
  23. d1 = [],
  24. d2 = [],
  25. d3 = [],
  26. d4 = [],
  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 = '',
  50. begindate = '',
  51. enddate = '',
  52. sn = 0;
  53. var charts = [];
  54. //判断最后的日期
  55. sendPost("patient/health_index/last", {}, "json", "get", function(){
  56. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'请求失败'}).show();
  57. }, function(res){
  58. if(res.status == 200){
  59. var d0 = decodeURIComponent(Request["dateT"]);
  60. if(res.data.sg){
  61. var d1 = (res.data.sg.recordDate).split(' ')[0];
  62. var d2 = Date.parse(new Date(d0))
  63. var d3 = Date.parse(new Date(d1))
  64. if(d2<d3){
  65. //取大
  66. norTime = d1
  67. begindate = d1
  68. }else{
  69. norTime = d0
  70. begindate = d0
  71. }
  72. }else{
  73. norTime = d0
  74. begindate = d0
  75. }
  76. enddate = getDateFromCurrentDate(begindate, -dayNum)
  77. init()
  78. }else{
  79. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'数据查询失败'}).show();
  80. }
  81. })
  82. function changeTabCon () {
  83. var qsv = $qushiView.css('display'),
  84. lbv = $liebiaoView.css('display');
  85. if (qsv === 'block') {
  86. lbv.hide();
  87. }
  88. }
  89. function getxAxisData(data) {
  90. var a = [],
  91. b = [];
  92. b = _.chain(data).pluck('date').map(function(d) {
  93. return d.substr(5, 5)
  94. }).value();
  95. for (var i = b.length - 1; i >= 0; i--) {
  96. a.push(b[i]);
  97. }
  98. return a;
  99. }
  100. function getSeriesData(data) {
  101. var a = [],
  102. b = [];
  103. b = _.chain(data).pluck('value1').map(function(v) {
  104. return v;
  105. }).value();
  106. for (var i = b.length - 1; i >= 0; i--) {
  107. a.push(b[i]);
  108. }
  109. return a;
  110. }
  111. function queryListByTypeP(type,begindate,enddate,page,pagesize) {
  112. //拼请求内容
  113. d.show();
  114. var params = {};
  115. params.type = type;
  116. params.page = page;
  117. params.pagesize = pagesize;
  118. params.start = begindate+" 00:00:00";
  119. params.end = enddate+" 23:59:59";
  120. return new Promise(function(resolve, reject) {
  121. //发送ajax请求
  122. sendPost("patient/health_index/list", params, "json", "post", queryListFailed, function (res) {
  123. d.close();
  124. if (res.status == 200) {
  125. resolve(res)
  126. } else {
  127. queryListFailed(res);
  128. }
  129. });
  130. })
  131. }
  132. function loadListPromise () {
  133. page++;
  134. Promise.all([
  135. queryListByTypeP( 4, getDateFromCurrentDate(norTime, -2000), norTime, page, pagesize)
  136. ])
  137. .then(function(res) {
  138. $(".pullUp").hide();
  139. var data = res[0];
  140. if (data.list.length > 0) {
  141. $xyList.append(template('xyTmp',{data:data.list}));
  142. scroller1.myScroll.refresh();
  143. } else {
  144. if(!isLoad) {
  145. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无数据'}).show();
  146. $xtEdit.unbind('click');
  147. } else {
  148. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'暂无更多数据'}).show();
  149. }
  150. }
  151. isLoad = true;
  152. })
  153. .catch(function(e) {
  154. console && console.error(e)
  155. });
  156. }
  157. //初始化swiper
  158. function init() {
  159. Promise.all([(function () {
  160. $swiperWrapper
  161. .append($('<li id="chart0" class="swiper-slide"> </li>'))
  162. .append($('<li id="chart1" class="swiper-slide"> </li>'))
  163. .append($('<li id="chart2" class="swiper-slide"> </li>'))
  164. .append($('<li id="chart3" class="swiper-slide"> </li>'));
  165. })()])
  166. .then(function(echarts) {
  167. swiper = new Swiper('.swiper-container', {
  168. paginationClickable: false,
  169. onSlideChangeStart: function(swiper) {
  170. //切换
  171. var ind = swiper.activeIndex;
  172. if (ind < sn) {
  173. begindate = enddate;
  174. enddate = getDateFromCurrentDate( begindate, -dayNum);
  175. } else {
  176. enddate = begindate;
  177. begindate = getDateFromCurrentDate( begindate, dayNum);
  178. }
  179. sn = ind;
  180. loadPromise(sn, enddate, begindate);
  181. }
  182. });
  183. })
  184. .then(function () {
  185. $swiperWrapper.find('.swiper-slide');
  186. swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
  187. sn = swiper.slides.length - 1;
  188. loadPromise( sn, enddate, begindate);
  189. })
  190. .catch(function(e) {
  191. console && console.error(e)
  192. })
  193. }
  194. function loadPromise ( n, enddate, begindate) {
  195. Promise.all([
  196. queryChatByTypePromise( 4, enddate, begindate, 0)
  197. ])
  198. .then(function(res) {
  199. d1 = res[0];
  200. loadData(n);
  201. })
  202. .catch(function(e) {
  203. console && console.error(e)
  204. });
  205. }
  206. //初始化echart
  207. function loadData(n) {
  208. new Promise(function(resolve, reject) {
  209. var yAxisName = '单位(cm)';
  210. var el = document.getElementById('chart' + n);
  211. Promise.all([
  212. createChartPromise( el, 4, yAxisName, getxAxisData(d1.list), getSeriesData(d1.list))
  213. ])
  214. .then(function(echarts) {
  215. charts.push(echarts[0]);
  216. $(el).append('<div class="c-time">< ' + enddate + ' 至 ' + begindate + ' ></div>');
  217. })
  218. .then(function() {
  219. })
  220. .catch(function(e) {
  221. console && console.error(e)
  222. })
  223. });
  224. }
  225. function pushHistory() {
  226. var state = {
  227. title: "腰围",
  228. url: "#"
  229. };
  230. window.history.pushState(state, "腰围", "#");
  231. }
  232. pushHistory();
  233. function bindEvents() {
  234. setTimeout( function () {
  235. window.addEventListener("popstate", function(e) {
  236. location.href = 'health-record.html';
  237. }, false);
  238. }, 300);
  239. $viewTabs.on('click','li',function(e) {
  240. e.stopPropagation();
  241. var index = $(this).index();
  242. $(this).siblings().removeClass('curr');
  243. $(this).addClass('curr');
  244. if (index === 0) {
  245. $qushiView.show();
  246. $liebiaoView.hide();
  247. $xtEdit.hide();
  248. } else {
  249. $qushiView.hide();
  250. $liebiaoView.show();
  251. $xtEdit.show();
  252. var data = null;
  253. if (dayNum == 6) {
  254. data = d1.list;
  255. }
  256. if (dayNum == 30) {
  257. data = d2.list;
  258. }
  259. if (dayNum == 90) {
  260. data = d3.list;
  261. }
  262. if (dayNum == -1) {
  263. data = d4.list;
  264. }
  265. if (!isLoad) {
  266. // isLoad = true;
  267. loadListPromise();
  268. }
  269. // $xyList.html(template('xyTmp',{data:data}));
  270. }
  271. });
  272. $selDate.on( 'click', 'li', function (e) {
  273. var index = $(this).index();
  274. $(this).siblings().removeClass('curr');
  275. $(this).addClass('curr');
  276. console.log(index);
  277. $swiperWrapper.find('.swiper-slide').html('');
  278. swiper.slideTo(swiper.slides.length - 1, 0, false); //切换到最后一个slide
  279. sn = swiper.slides.length - 1;
  280. begindate = norTime;
  281. switch(index) {
  282. case 0:
  283. dayNum = 6;
  284. break;
  285. case 1:
  286. dayNum = 30;
  287. break;
  288. case 2:
  289. dayNum = 90;
  290. break;
  291. case 3:
  292. dayNum = 365/2;
  293. break;
  294. }
  295. enddate = getDateFromCurrentDate(begindate, -dayNum);
  296. loadPromise( sn, enddate, begindate);
  297. });
  298. $xtEdit.on( 'click', function () {
  299. $(this).hide();
  300. $btns.show();
  301. $xtAdd.hide();
  302. $liebiaoView.find('.l-edit-icon').addClass('active');
  303. });
  304. $cancelBtn.on( 'click',function () {
  305. $liebiaoView.find('.l-edit-icon').removeClass('active');
  306. $xtEdit.show();
  307. $xtAdd.show();
  308. $btns.hide();
  309. });
  310. $okBtn.on( 'click', function () {
  311. $liebiaoView.find('.l-edit-icon').removeClass('active');
  312. $xtEdit.show();
  313. $xtAdd.show();
  314. $btns.hide();
  315. });
  316. $liebiaoView.on( 'click', '.l-item', function () {
  317. var lei = $(this).find('.l-edit-icon'),
  318. id = $(this).attr('data-id'),
  319. val = $(this).attr('data-val'),
  320. sn = $(this).attr('data-sn'),
  321. time = $(this).attr('data-time');
  322. if (lei.hasClass('active')) {
  323. $popMain.addClass('active');
  324. $popBtns.addClass('active');
  325. if (sn != '') {
  326. $popEditBtn.hide();
  327. }
  328. $popEditBtn.attr('data-id',id);
  329. $popEditBtn.attr('data-val',val);
  330. $popEditBtn.attr('data-time',time);
  331. $popDelBtn.attr('data-id',id);
  332. }
  333. });
  334. // 修改
  335. $popEditBtn.on( 'click', function () {
  336. var id = $(this).attr('data-id'),
  337. val = $(this).attr('data-val'),
  338. time = $(this).attr('data-time');
  339. window.location.href = 'health-index-add-waistline.html?id=' + id + '&val=' + val + '&time=' + time;
  340. });
  341. // 删除
  342. $popDelBtn.on( 'click', function () {
  343. var id = $(this).attr('data-id');
  344. sendPost("patient/health_index/modify?id=" + id, {}, "json", "post", queryListFailed, function (res) {
  345. d.close();
  346. if (res.status == 200) {
  347. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'删除成功'}).show();
  348. setTimeout(function(){
  349. location.reload();
  350. },2000);
  351. } else {
  352. dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:'删除失败'}).show();
  353. setTimeout(function(){
  354. location.reload();
  355. },2000);
  356. }
  357. });
  358. });//
  359. $popCancelBtn.on('click',function () {
  360. $popMain.removeClass('active');
  361. $popBtns.removeClass('active');
  362. });//
  363. $popMain.on( 'click', function () {
  364. $(this).removeClass('active');
  365. $popBtns.removeClass('active');
  366. });
  367. }
  368. bindEvents();
  369. });
  370. //根据sd获取n天前的时间;
  371. function getDateFromCurrentDate(fromDate,dayInterval){
  372. var curDate = new Date(Date.parse(fromDate.replace(/-/g,"/")));
  373. curDate.setDate(curDate.getDate()+dayInterval);
  374. var year = curDate.getFullYear();
  375. var month = (curDate.getMonth()+1)<10?"0"+(curDate.getMonth()+1):(curDate.getMonth()+1);
  376. var day = curDate.getDate()<10?"0"+curDate.getDate():curDate.getDate();
  377. return year+"-"+month+"-"+day;
  378. };