public function operatorLog($opId, $type, $content) { $log = new BackendOperatorLog(); $log->operator_id = $opId; $log->log_type = $type; $log->log_content = $content; $clientInfo = ['ip' => Util::UserIp(), 'userAgent' => $this->request->getUserAgent(), 'headers' => Util::GetAllHeaders()]; $log->log_client_info = json_encode($clientInfo); if (!$log->save()) { $error = ''; foreach ($log->getMessages() as $msg) { $error .= $msg . ','; } $this->di->get('logger')->error('数据库异常,记录API请求日志出错, GET:' . json_encode($_GET) . ', POST:' . $this->request->getRawBody() . ', error: ' . $error); } }
/** * 操作日志 */ public function opLogAction() { $this->setLeftNav('opLog'); $req = $this->request; $page = intval($req->getQuery('page', null, 1)); $page = $page > 0 ? $page : 1; $limit = $this->pageNavLimit; $filterOp = intval($req->getQuery('filterOp', null, -10000)); $filterType = intval($req->getQuery('filterType', null, -10000)); $where = []; $bindParams = []; if ($filterOp > -10000) { $where[] = 'Apps\\Common\\Models\\BackendOperatorLog.operator_id = :opId:'; $bindParams['opId'] = $filterOp == -10000 ? 0 : $filterOp; } if ($filterType > -10000) { $where[] = 'log_type = :opType:'; $bindParams['opType'] = $filterType; } $whereStr = implode(' AND ', $where); // 总数 $total = BackendOperatorLog::count(['conditions' => $whereStr, 'bind' => $bindParams]); // 操作员列表 $opList = BackendOperator::find(); $this->view->setVar('opList', $opList); // 操作类型 $this->view->setVar('opLogTypes', BackendOperatorLogType::find()); $data = BackendOperatorLog::query()->columns(['Apps\\Common\\Models\\BackendOperatorLog.log_id', 'op.operator_id', 'op.operator_show_name', 'logType.type_title log_type', 'Apps\\Common\\Models\\BackendOperatorLog.log_client_info', 'Apps\\Common\\Models\\BackendOperatorLog.log_addtime'])->where($whereStr)->bind($bindParams)->leftJoin('Apps\\Common\\Models\\BackendOperator', 'op.operator_id = Apps\\Common\\Models\\BackendOperatorLog.operator_id', 'op')->leftJoin('Apps\\Common\\Models\\BackendOperatorLogType', 'logType.type_id = Apps\\Common\\Models\\BackendOperatorLog.log_type', 'logType')->limit($limit, ($page - 1) * $limit)->orderBy('Apps\\Common\\Models\\BackendOperatorLog.log_id DESC')->execute(); $this->view->setVar('total', $total); $this->view->setVar('page', $page); $this->view->setVar('limit', $limit); $this->view->setVar('filterOp', $filterOp); $this->view->setVar('filterType', $filterType); $this->view->setVar('data', $data); }