where('username', 'like', "%{$username}%"); } if ($ip) { $query = $query->where('ip_address', 'like', "%{$ip}%"); } if ($status !== '') { $query = $query->where('login_status', $status); } if ($startTime) { $query = $query->where('login_time', '>=', $startTime); } if ($endTime) { $query = $query->where('login_time', '<=', $endTime); } $count = $query->count(); $list = $query->order('id desc') ->page($page, $limit) ->select(); return json([ 'code' => 0, 'msg' => '获取成功', 'count' => $count, 'data' => $list ]); } return View::fetch(); } /** * 操作日志列表 */ public function operation() { if (Request::isPost()) { $page = input('post.page', 1); $limit = input('post.limit', 10); $username = input('post.username'); $module = input('post.module'); $operation = input('post.operation'); $status = input('post.status'); $startTime = input('post.start_time'); $endTime = input('post.end_time'); $query = Db::name('yz_logs_operation'); // 搜索条件 if ($username) { $query = $query->where('username', 'like', "%{$username}%"); } if ($module) { $query = $query->where('module', 'like', "%{$module}%"); } if ($operation) { $query = $query->where('operation', 'like', "%{$operation}%"); } if ($status !== '') { $query = $query->where('status', $status); } if ($startTime) { $query = $query->where('operation_time', '>=', $startTime); } if ($endTime) { $query = $query->where('operation_time', '<=', $endTime); } $count = $query->count(); $list = $query->order('id desc') ->page($page, $limit) ->select(); return json([ 'code' => 0, 'msg' => '获取成功', 'count' => $count, 'data' => $list ]); } return View::fetch(); } /** * 记录操作日志 */ protected function recordOperation($operation, $status = 1, $error_message = '') { $data = [ 'username' => session('admin_username'), 'module' => '日志管理', 'operation' => $operation, 'request_method' => Request::method(), 'request_url' => Request::url(true), 'request_params' => json_encode(Request::param(), JSON_UNESCAPED_UNICODE), 'ip_address' => Request::ip(), 'status' => $status, 'error_message' => $error_message, 'operation_time' => date('Y-m-d H:i:s'), 'execution_time' => 0 ]; Db::name('yz_logs_operation')->insert($data); } /** * 删除登录日志 */ public function deleteLogin() { $id = input('post.id'); try { if (Db::name('yz_logs_login')->delete($id)) { $this->recordOperation('删除登录日志'); return json(['code' => 0, 'msg' => '删除成功']); } $this->recordOperation('删除登录日志', 0, '删除失败'); return json(['code' => 1, 'msg' => '删除失败']); } catch (\Exception $e) { $this->recordOperation('删除登录日志', 0, $e->getMessage()); return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]); } } /** * 删除操作日志 */ public function deleteOperation() { $id = input('post.id'); try { if (Db::name('yz_logs_operation')->delete($id)) { $this->recordOperation('删除操作日志'); return json(['code' => 0, 'msg' => '删除成功']); } $this->recordOperation('删除操作日志', 0, '删除失败'); return json(['code' => 1, 'msg' => '删除失败']); } catch (\Exception $e) { $this->recordOperation('删除操作日志', 0, $e->getMessage()); return json(['code' => 1, 'msg' => '删除失败:' . $e->getMessage()]); } } /** * 清空登录日志 */ public function clearLogin() { try { if (Db::name('yz_logs_login')->where('1=1')->delete()) { $this->recordOperation('清空登录日志'); return json(['code' => 0, 'msg' => '清空成功']); } $this->recordOperation('清空登录日志', 0, '清空失败'); return json(['code' => 1, 'msg' => '清空失败']); } catch (\Exception $e) { $this->recordOperation('清空登录日志', 0, $e->getMessage()); return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]); } } /** * 清空操作日志 */ public function clearOperation() { try { if (Db::name('yz_logs_operation')->where('1=1')->delete()) { $this->recordOperation('清空操作日志'); return json(['code' => 0, 'msg' => '清空成功']); } $this->recordOperation('清空操作日志', 0, '清空失败'); return json(['code' => 1, 'msg' => '清空失败']); } catch (\Exception $e) { $this->recordOperation('清空操作日志', 0, $e->getMessage()); return json(['code' => 1, 'msg' => '清空失败:' . $e->getMessage()]); } } }