Jiankangdata.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. /**
  6. * 健康检测数据
  7. */
  8. class Jiankangdata extends Api
  9. {
  10. protected $noNeedLogin = ['*'];
  11. protected $noNeedRight = ['*'];
  12. public function submit(){
  13. $map = [
  14. 'user_id' => $this->auth->id,
  15. 'createdate' => date('Y-m-d'),
  16. ];
  17. $check = Db::name('jiankangdata')->where($map)->find();
  18. $data = [
  19. 'user_id' => $this->auth->id,
  20. 'xueya_low' => $this->request->post('xueya_low'),
  21. 'xueya_high' => $this->request->post('xueya_high'),
  22. 'xuetang' => $this->request->post('xuetang'),
  23. 'createtime' => time(),
  24. 'updatetime' => time(),
  25. 'createdate' => date('Y-m-d'),
  26. ];
  27. if($check){
  28. Db::name('jiankangdata')->where($map)->update($data);
  29. }else{
  30. Db::name('jiankangdata')->insert($data);
  31. }
  32. $this->success('提交成功');
  33. }
  34. public function data(){
  35. $type = $this->request->post('type','week','trim');
  36. $page = $this->request->post('page',1,'intval');
  37. if($page < 1){ $page = 1;}
  38. if($type == 'week'){
  39. $start_time = strtotime('monday this week') - (7 * 86400 * ($page - 1));
  40. $ended_time = strtotime('sunday this week') - (7 * 86400 * ($page - 1));
  41. $start_date = date('Y-m-d', $start_time);
  42. $ended_date = date('Y-m-d', $ended_time);
  43. $days = 7;
  44. for($i=0;$i<$days;$i++){
  45. $date_array[] = date('m.d', 86400*$i + $start_time);
  46. }
  47. }
  48. if($type == 'month'){
  49. $start_time = strtotime('first day of -'.($page - 1).' month');
  50. $start_date = date('Y-m-01',$start_time);
  51. $ended_date = date('Y-m-t', $start_time);
  52. $days = date('t',$start_time);
  53. for($i=0;$i<$days;$i++){
  54. $date_array[] = date('m.d', 86400*$i + strtotime($start_date));
  55. }
  56. }
  57. // dump($start_date);
  58. // dump($ended_date);
  59. // dump($date_array);
  60. $list = Db::name('jiankangdata')->where('user_id',$this->auth->id)->whereBetween('createdate',[$start_date,$ended_date])->order('id','asc')->select();
  61. $xueya_low = [];
  62. $xueya_high = [];
  63. $xuetang = [];
  64. foreach($date_array as $date){
  65. $xueya_low[$date] = 0;
  66. $xueya_high[$date] = 0;
  67. $xuetang[$date] = 0;
  68. foreach($list as $k=>$v){
  69. if(date('m.d',strtotime($v['createdate'])) == $date){
  70. $xueya_low[$date] = $v['xueya_low'];
  71. $xueya_high[$date] = $v['xueya_high'];
  72. $xuetang[$date] = $v['xuetang'];
  73. }
  74. }
  75. }
  76. // dump($xueya_low);
  77. // dump($xueya_high);
  78. // dump($xuetang);
  79. $rs = [
  80. 'start_date' => date('Y.m.d',strtotime($start_date)),
  81. 'end_date' => date('Y.m.d',strtotime($ended_date)),
  82. 'date_array' => $date_array,
  83. 'xueya_low' => array_values($xueya_low),
  84. 'xueya_high' => array_values($xueya_high),
  85. 'xuetang' => array_values($xuetang),
  86. ];
  87. $this->success(1,$rs);
  88. }
  89. }