| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace backend\modules\common\controllers;
- use Yii;
- use yii\data\Pagination;
- use common\helpers\ResultHelper;
- use common\models\common\Log;
- use common\enums\StatusEnum;
- use common\models\base\SearchModel;
- use backend\controllers\BaseController;
- /**
- * Class LogController
- * @package backend\modules\common\controllers
- * @author jianyan74 <751393839@qq.com>
- */
- class LogController extends BaseController
- {
- /**
- * @return string
- * @throws \yii\web\NotFoundHttpException
- */
- public function actionIndex()
- {
- $startTime = Yii::$app->request->get('start_time', date('Y-m-d', strtotime("-10 day")));
- $endTime = Yii::$app->request->get('end_time', date('Y-m-d', strtotime("+1 day")));
- $searchModel = new SearchModel([
- 'model' => Log::class,
- 'scenario' => 'default',
- 'partialMatchAttributes' => ['method', 'url'], // 模糊查询
- 'defaultOrder' => [
- 'id' => SORT_DESC,
- ],
- 'pageSize' => $this->pageSize,
- ]);
- $dataProvider = $searchModel
- ->search(Yii::$app->request->queryParams);
- $dataProvider->query
- ->andWhere(['status' => StatusEnum::ENABLED])
- ->andFilterWhere(['between', 'created_at', !empty($startTime) ? strtotime($startTime) : '', !empty($endTime) ? strtotime($endTime) : ''])
- ->with(['member']);
- return $this->render($this->action->id, [
- 'dataProvider' => $dataProvider,
- 'searchModel' => $searchModel,
- 'startTime' => $startTime,
- 'endTime' => $endTime,
- ]);
- }
- /**
- * @return string
- * @throws \yii\web\NotFoundHttpException
- */
- public function actionIpStatistics()
- {
- $ip = Yii::$app->request->get('ip');
- $start_time = Yii::$app->request->get('start_time', date('Y-m-d', strtotime("-10 day")));
- $end_time = Yii::$app->request->get('end_time', date('Y-m-d', strtotime("+1 day")));
- $data = Log::find()
- ->select(['ip', 'count(id) as count'])
- ->where(['status' => StatusEnum::ENABLED])
- ->andFilterWhere(['between', 'created_at', strtotime($start_time), strtotime($end_time)])
- ->andFilterWhere(['ip' => $ip])
- ->groupBy(['ip']);
- $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
- $models = $data->offset($pages->offset)
- ->orderBy('count desc, ip desc')
- ->limit($pages->limit)
- ->asArray()
- ->all();
- return $this->render($this->action->id, [
- 'models' => $models,
- 'pages' => $pages,
- 'ip' => $ip,
- 'start_time' => $start_time,
- 'end_time' => $end_time,
- ]);
- }
- /**
- * @return string
- */
- public function actionStatistics()
- {
- return $this->render($this->action->id, []);
- }
- /**
- * @param string $data
- * @return array|string
- */
- public function actionStat($type = '')
- {
- if (!empty($type)) {
- $data = Yii::$app->services->log->stat($type);
- return ResultHelper::json(200, '获取成功', $data);
- }
- return $this->renderAjax($this->action->id, []);
- }
- /**
- * @param string $data
- * @return array|string
- */
- public function actionFlowStat($type = '')
- {
- if (!empty($type)) {
- $data = Yii::$app->services->log->flowStat($type);
- return ResultHelper::json(200, '获取成功', $data);
- }
- return $this->renderAjax($this->action->id, []);
- }
- /**
- * 行为日志详情
- *
- * @param $id
- * @return string
- */
- public function actionView($id)
- {
- $this->layout = '@backend/views/layouts/blank';
- return $this->render($this->action->id, [
- 'model' => Log::findOne($id),
- ]);
- }
- }
|