| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\behaviors;
- use Yii;
- use yii\base\ActionFilter;
- use app\models\ActionLog as ActionLogModel;
- /**
- * 记录操作日志
- * Class ActionLog
- * @package app\modules\admin\behaviors
- */
- class ActionLog extends ActionFilter
- {
- public function beforeAction($action)
- {
- $params = require Yii::$app->basePath . '/config/action.php';
- $route = Yii::$app->controller->route;
- Yii::$app->cache->delete('actionLogId');
- if (isset($params[$route])) {
- $param = $params[$route];
- $getParams = get_params();
- $postParams = post_params();
- $headerParams = Yii::$app->getRequest()->getHeaders()->toArray();
- if (isset($postParams['password'])) {
- unset($postParams['password']);
- }
- $dataID = isset($param['key']) ? input_params($param['key']) : null;
- $type = isset($param['type']) ? $param['type'] : ($dataID ? 'edit' : 'add');
- $admin = Yii::$app->jwt->getAdmin();
- $adminId = $admin ? $admin->getId() : null;
- $actionLog = new ActionLogModel();
- $actionLog->route = $route;
- if ($adminId) {
- $actionLog->admin_id = $adminId;
- }
- $actionLog->type = $type;
- $actionLog->data_id = is_array($dataID) ? json_encode($dataID) : $dataID;
- $actionLog->get_params = $getParams ? json_encode($getParams) : '';
- $actionLog->post_params = $postParams ? json_encode($postParams) : '';
- $actionLog->header_params = $headerParams ? json_encode($headerParams) : '';
- if ($actionLog->save()) {
- Yii::$app->cache->set('actionLogId', Yii::$app->db->getLastInsertID());
- } else {
- Yii::error($actionLog->getErrors());
- }
- }
- return parent::beforeAction($action);
- }
- public function afterAction($action, $result)
- {
- $actionLogId = Yii::$app->cache->get('actionLogId');
- if ($actionLogId) {
- $actionLog = ActionLogModel::findOne($actionLogId);
- if ($actionLog) {
- $admin = Yii::$app->jwt->getAdmin();
- if ($actionLog->type == 'login' && $admin) {
- $actionLog->admin_id = $admin->getId();
- }
- $actionLog->response_params = json_encode($result);
- $actionLog->save();
- }
- }
- return parent::afterAction($action, $result);
- }
- }
|