xuetang-view.js 12 KB

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