LogController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use common\helpers\ResultHelper;
  6. use common\models\common\Log;
  7. use common\enums\StatusEnum;
  8. use common\models\base\SearchModel;
  9. use backend\controllers\BaseController;
  10. /**
  11. * Class LogController
  12. * @package backend\modules\common\controllers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class LogController extends BaseController
  16. {
  17. /**
  18. * @return string
  19. * @throws \yii\web\NotFoundHttpException
  20. */
  21. public function actionIndex()
  22. {
  23. $startTime = Yii::$app->request->get('start_time', date('Y-m-d', strtotime("-10 day")));
  24. $endTime = Yii::$app->request->get('end_time', date('Y-m-d', strtotime("+1 day")));
  25. $searchModel = new SearchModel([
  26. 'model' => Log::class,
  27. 'scenario' => 'default',
  28. 'partialMatchAttributes' => ['method', 'url'], // 模糊查询
  29. 'defaultOrder' => [
  30. 'id' => SORT_DESC,
  31. ],
  32. 'pageSize' => $this->pageSize,
  33. ]);
  34. $dataProvider = $searchModel
  35. ->search(Yii::$app->request->queryParams);
  36. $dataProvider->query
  37. ->andWhere(['status' => StatusEnum::ENABLED])
  38. ->andFilterWhere(['between', 'created_at', !empty($startTime) ? strtotime($startTime) : '', !empty($endTime) ? strtotime($endTime) : ''])
  39. ->with(['member']);
  40. return $this->render($this->action->id, [
  41. 'dataProvider' => $dataProvider,
  42. 'searchModel' => $searchModel,
  43. 'startTime' => $startTime,
  44. 'endTime' => $endTime,
  45. ]);
  46. }
  47. /**
  48. * @return string
  49. * @throws \yii\web\NotFoundHttpException
  50. */
  51. public function actionIpStatistics()
  52. {
  53. $ip = Yii::$app->request->get('ip');
  54. $start_time = Yii::$app->request->get('start_time', date('Y-m-d', strtotime("-10 day")));
  55. $end_time = Yii::$app->request->get('end_time', date('Y-m-d', strtotime("+1 day")));
  56. $data = Log::find()
  57. ->select(['ip', 'count(id) as count'])
  58. ->where(['status' => StatusEnum::ENABLED])
  59. ->andFilterWhere(['between', 'created_at', strtotime($start_time), strtotime($end_time)])
  60. ->andFilterWhere(['ip' => $ip])
  61. ->groupBy(['ip']);
  62. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  63. $models = $data->offset($pages->offset)
  64. ->orderBy('count desc, ip desc')
  65. ->limit($pages->limit)
  66. ->asArray()
  67. ->all();
  68. return $this->render($this->action->id, [
  69. 'models' => $models,
  70. 'pages' => $pages,
  71. 'ip' => $ip,
  72. 'start_time' => $start_time,
  73. 'end_time' => $end_time,
  74. ]);
  75. }
  76. /**
  77. * @return string
  78. */
  79. public function actionStatistics()
  80. {
  81. return $this->render($this->action->id, []);
  82. }
  83. /**
  84. * @param string $data
  85. * @return array|string
  86. */
  87. public function actionStat($type = '')
  88. {
  89. if (!empty($type)) {
  90. $data = Yii::$app->services->log->stat($type);
  91. return ResultHelper::json(200, '获取成功', $data);
  92. }
  93. return $this->renderAjax($this->action->id, []);
  94. }
  95. /**
  96. * @param string $data
  97. * @return array|string
  98. */
  99. public function actionFlowStat($type = '')
  100. {
  101. if (!empty($type)) {
  102. $data = Yii::$app->services->log->flowStat($type);
  103. return ResultHelper::json(200, '获取成功', $data);
  104. }
  105. return $this->renderAjax($this->action->id, []);
  106. }
  107. /**
  108. * 行为日志详情
  109. *
  110. * @param $id
  111. * @return string
  112. */
  113. public function actionView($id)
  114. {
  115. $this->layout = '@backend/views/layouts/blank';
  116. return $this->render($this->action->id, [
  117. 'model' => Log::findOne($id),
  118. ]);
  119. }
  120. }
粤ICP备19079148号