IndexController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Models\Answer;
  4. use App\Models\Article;
  5. use App\Models\Question;
  6. use App\Models\User;
  7. use Carbon\Carbon;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Cookie;
  10. class IndexController extends AdminController
  11. {
  12. /**
  13. *显示后台首页
  14. */
  15. public function index()
  16. {
  17. $totalUserNum = User::count();
  18. $totalQuestionNum = Question::count();
  19. $totalArticleNum = Article::count();
  20. $totalAnswerNum = Answer::count();
  21. $userChart = $this->drawUserChart();
  22. $questionChart = $this->drawQuestionChart();
  23. $systemInfo = $this->getSystemInfo();
  24. return view("admin.index.index")->with(compact('totalUserNum','totalQuestionNum','totalArticleNum','totalAnswerNum','userChart','questionChart','systemInfo'));
  25. }
  26. /*显示或隐藏sidebar*/
  27. public function sidebar(Request $request){
  28. Cookie::forget('sidebar_collapse');
  29. $cookie = Cookie::forever('sidebar_collapse',$request->get('collapse'));
  30. return response()->json('ok')->withCookie($cookie);
  31. }
  32. private function drawUserChart()
  33. {
  34. /*生成Labels*/
  35. $labelTimes = $chartLabels = [];
  36. for( $i=0 ; $i < 7 ; $i++ ){
  37. $labelTimes[$i] = Carbon::createFromTimestamp( Carbon::today()->timestamp - (6-$i) * 24 * 3600 );
  38. $chartLabels[$i] = '"'.$labelTimes[$i]->month.'月-'.$labelTimes[$i]->day.'日'.'"';
  39. }
  40. $nowTime = Carbon::now();
  41. $users = User::where('created_at','>',$labelTimes[0])->where('created_at','<',$nowTime)->get();
  42. $registerRange = $verifyRange = $authRange = [0,0,0,0,0,0,0];
  43. for( $i=0 ; $i < 7 ; $i++ ){
  44. $startTime = $labelTimes[$i];
  45. $endTime = $nowTime;
  46. if(isset($labelTimes[$i+1])){
  47. $endTime = $labelTimes[$i+1];
  48. }
  49. foreach($users as $user){
  50. if( $user->created_at > $startTime && $user->created_at < $endTime ){
  51. $registerRange[$i]++;
  52. if( $user->status > 0 ){
  53. $verifyRange[$i]++;
  54. }
  55. if($user->userData && $user->userData->authentication_status === 1){
  56. $authRange[$i]++;
  57. }
  58. }
  59. }
  60. }
  61. return ['labels'=>$chartLabels,'registerUsers'=>$registerRange,'verifyUsers'=>$verifyRange,'authUsers'=>$authRange];
  62. }
  63. private function drawQuestionChart()
  64. {
  65. /*生成Labels*/
  66. $labelTimes = $chartLabels = [];
  67. for( $i=0 ; $i < 7 ; $i++ ){
  68. $labelTimes[$i] = Carbon::createFromTimestamp( Carbon::today()->timestamp - (6-$i) * 24 * 3600 );
  69. $chartLabels[$i] = '"'.$labelTimes[$i]->month.'月-'.$labelTimes[$i]->day.'日'.'"';
  70. }
  71. $nowTime = Carbon::now();
  72. $questions = Question::where('created_at','>',$labelTimes[0])->where('created_at','<',$nowTime)->get();
  73. $answers = Answer::where('created_at','>',$labelTimes[0])->where('created_at','<',$nowTime)->get();
  74. $articles = Article::where('created_at','>',$labelTimes[0])->where('created_at','<',$nowTime)->get();
  75. $questionRange = $answerRange = $articleRange = [0,0,0,0,0,0,0];
  76. for( $i=0 ; $i < 7 ; $i++ ){
  77. $startTime = $labelTimes[$i];
  78. $endTime = $nowTime;
  79. if(isset($labelTimes[$i+1])){
  80. $endTime = $labelTimes[$i+1];
  81. }
  82. /*问题统计*/
  83. foreach($questions as $question){
  84. if( $question->created_at > $startTime && $question->created_at < $endTime ){
  85. $questionRange[$i]++;
  86. }
  87. }
  88. /*回答统计*/
  89. foreach($answers as $answer){
  90. if( $answer->created_at > $startTime && $answer->created_at < $endTime ){
  91. $answerRange[$i]++;
  92. }
  93. }
  94. /*文章统计*/
  95. foreach($articles as $article){
  96. if( $article->created_at > $startTime && $article->created_at < $endTime ){
  97. $articleRange[$i]++;
  98. }
  99. }
  100. }
  101. return [
  102. 'labels' => $chartLabels,
  103. 'questionRange' => $questionRange,
  104. 'answerRange' => $answerRange,
  105. 'articleRange' => $articleRange,
  106. ];
  107. }
  108. private function getSystemInfo()
  109. {
  110. $systemInfo['phpVersion'] = PHP_VERSION;
  111. $systemInfo['runOS'] = PHP_OS;
  112. $systemInfo['maxUploadSize'] = ini_get('upload_max_filesize');
  113. $systemInfo['maxExecutionTime'] = ini_get('max_execution_time');
  114. $systemInfo['hostName'] = '';
  115. if(isset($_SERVER['SERVER_NAME'])){
  116. $systemInfo['hostName'] .= $_SERVER['SERVER_NAME'].' / ';
  117. }
  118. if(isset($_SERVER['SERVER_ADDR'])){
  119. $systemInfo['hostName'] .= $_SERVER['SERVER_ADDR'].' / ';
  120. }
  121. if(isset($_SERVER['SERVER_PORT'])){
  122. $systemInfo['hostName'] .= $_SERVER['SERVER_PORT'];
  123. }
  124. $systemInfo['serverInfo'] = '';
  125. if(isset($_SERVER['SERVER_SOFTWARE'])){
  126. $systemInfo['serverInfo'] = $_SERVER['SERVER_SOFTWARE'];
  127. }
  128. return $systemInfo;
  129. }
  130. }