subMonths($i); $result[] = $lastMonth->format('Y年m月'); $i ++; //不能超过上线日期 if ($lastMonth->getTimestamp() <= strtotime($releaseDate)){ break; } } } //是否显示年 if ($showYear){ while ($startYear <= $thisYear) { $result[] = $startYear.'年'; $startYear ++; } } return $this->response($result); } public function mood(int $uid, string $typeStr) { $categories = []; $seriesData = []; //显示的数据类型 $showType = $this->getShowType($typeStr); switch ($showType) { case self::SHOW_TYPE_DAY: $number = function($str) use(&$number){ $replaceArr = ['近', '日']; foreach ($replaceArr as $search){ $str = str_replace($search, '', $str); } return chinese2Number($str); }; for ($i = $number($typeStr); $i >= 0; $i --) { $day = Carbon::now()->subDays($i)->toDateString(); $categories[] = date('m-d', strtotime($day)); $betweenTime = [ date('Y-m-d', strtotime($day)), date('Y-m-d 23:59:59', strtotime($day)), ]; $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first(); $seriesData[] = $mood ? $mood->score : 0; } break; case self::SHOW_TYPE_MONTH: $month = function($str){ return str_replace('月', '', str_replace('年', '-', $str)); }; //此月的最后一天 $lastDay = Carbon::parse($month($typeStr))->lastOfMonth()->format('d'); for ($i = 0; $i < $lastDay; $i ++) { $day = Carbon::parse($month($typeStr))->addDays($i)->toDateString(); $categories[] = $i % 5 == 0 ? date('d', strtotime($day)) : ''; $betweenTime = [ date('Y-m-d', strtotime($day)), date('Y-m-d 23:59:59', strtotime($day)), ]; $mood = DiaryMood::where('uid', $uid)->whereBetween('created_at', $betweenTime)->first(); $seriesData[] = $mood ? $mood->score : 0; } break; } $result = [ 'categories' => $categories, 'seriesData' => $seriesData ]; return $this->response($result); } public function sleep(int $uid, string $type) { $series = []; if ($type == '本月'){ $month = Carbon::now()->format('Y-m'); $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString(); $betweenTime = [ $month.'-01', date('Y-m-d 23:59:59', strtotime($lastDay)), ]; }else{ $month = function($str){ return str_replace('月', '', str_replace('年', '-', $str)); }; $month = $month($type); $lastDay = Carbon::parse($month)->lastOfMonth()->toDateString(); $betweenTime = [ $month.'-01', date('Y-m-d 23:59:59', strtotime($lastDay)), ]; } $levels = DiarySleepLevel::orderBy('rank', 'desc')->get(['id','name','color']); $allCount = DiarySleep::whereBetween('created_at', $betweenTime)->count(); $levels->each(function($item) use($betweenTime, $allCount){ $item->data = DiarySleep::where('level_id', $item->id)->whereBetween('created_at', $betweenTime)->count(); $item->percent = $allCount ? sprintf("%.2f", $item->data / $allCount * 100) : 0; }); $result = [ 'levels' => $levels, 'month' => date('Y年m月', strtotime($month)) ]; return $this->response($result); } }