| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 | <?phpnamespace app\api\controller;use app\common\controller\Api;use think\Db;/** * 健康检测数据 */class Jiankangdata extends Api{    protected $noNeedLogin = ['*'];    protected $noNeedRight = ['*'];    public function submit(){        //今天的数据都改为非最后一条        $map = [            'user_id' => $this->auth->id,            'createdate' => date('Y-m-d'),        ];        Db::name('jiankangdata')->where($map)->update(['is_last'=>0]);        //新写入        $data = [            'user_id' => $this->auth->id,            'xueya_low'  => $this->request->post('xueya_low',0),            'xueya_high' => $this->request->post('xueya_high',0),            'xuetang' => $this->request->post('xuetang',0),            'createtime' => time(),            'updatetime' => time(),            'createdate' => date('Y-m-d'),            'is_last'    => 1, //默认就是最后一条        ];        Db::name('jiankangdata')->insert($data);        $this->success('提交成功');    }    public function lists(){        $list = Db::name('jiankangdata')->where('user_id',$this->auth->id)->autopage()->order('id desc')->select();        $this->success(1, $list);    }    public function data(){        $type = $this->request->post('type','week','trim');        $page = $this->request->post('page',1,'intval');        if($page < 1){ $page = 1;}        if($type == 'week'){            $start_time = strtotime('monday this week') - (7 * 86400 * ($page - 1));            $ended_time = strtotime('sunday this week') - (7 * 86400 * ($page - 1));            $start_date = date('Y-m-d', $start_time);            $ended_date = date('Y-m-d', $ended_time);            $days = 7;            for($i=0;$i<$days;$i++){                $date_array[] = date('m.d', 86400*$i + $start_time);            }        }        if($type == 'month'){            $start_time =  strtotime('first day of -'.($page - 1).' month');            $start_date = date('Y-m-01',$start_time);            $ended_date = date('Y-m-t', $start_time);            $days = date('t',$start_time);            for($i=0;$i<$days;$i++){                $date_array[] = date('m.d', 86400*$i + strtotime($start_date));            }        }//        dump($start_date);//        dump($ended_date);//        dump($date_array);        $list = Db::name('jiankangdata')            ->where('user_id',$this->auth->id)            ->whereBetween('createdate',[$start_date,$ended_date])            ->where('is_last',1)  //只拿之后一条            ->order('id','asc')->select();        $xueya_low = [];        $xueya_high = [];        $xuetang = [];        foreach($date_array as $date){            $xueya_low[$date] = 0;            $xueya_high[$date] = 0;            $xuetang[$date] = 0;            foreach($list as $k=>$v){                if(date('m.d',strtotime($v['createdate'])) == $date){                    $xueya_low[$date] = $v['xueya_low'];                    $xueya_high[$date] = $v['xueya_high'];                    $xuetang[$date] = $v['xuetang'];                }            }        }//        dump($xueya_low);//        dump($xueya_high);//        dump($xuetang);        $rs = [            'start_date' => date('Y.m.d',strtotime($start_date)),            'end_date' => date('Y.m.d',strtotime($ended_date)),            'date_array' => $date_array,            'xueya_low' => array_values($xueya_low),            'xueya_high' => array_values($xueya_high),            'xuetang' => array_values($xuetang),        ];        $this->success(1,$rs);    }}
 |