page.tsx 5.78 KB
"use client";

import { useCallback, useEffect, useState } from "react";
import { X } from "lucide-react";

import {
  Card,
  Drawer,
  Field,
  GhostButton,
  Pagination,
  SearchInput,
  Select,
  Spinner,
  SysPage,
  Toolbar,
} from "@/components/system/kit";
import { listOaOperLogs, type OaOperLog } from "@/lib/auth";

const MODULE_LABEL: Record<string, string> = {
  user: "用户", role: "角色", dept: "部门", post: "岗位", source: "资源",
};
const ACTION_LABEL: Record<string, string> = {
  create: "新增", update: "编辑", delete: "删除", batchDelete: "批量删除",
  assignRole: "分配角色", assignPost: "分配岗位", resetPwd: "重置密码",
  dataScope: "数据范围", sync: "同步资源",
};

export default function LogPage() {
  const [logs, setLogs] = useState<OaOperLog[]>([]);
  const [total, setTotal] = useState(0);
  const [loading, setLoading] = useState(true);
  const [page, setPage] = useState(1);
  const [pageSize, setPageSize] = useState(20);
  const [module, setModule] = useState("");
  const [kwInput, setKwInput] = useState("");
  const [keyword, setKeyword] = useState("");
  const [preview, setPreview] = useState<OaOperLog | null>(null);

  const load = useCallback(async () => {
    setLoading(true);
    try {
      const d = await listOaOperLogs({ module, keyword, limit: pageSize, offset: (page - 1) * pageSize });
      setLogs(d.items);
      setTotal(d.total);
    } finally {
      setLoading(false);
    }
  }, [module, keyword, page, pageSize]);
  useEffect(() => { load(); }, [load]);

  const apply = () => { setKeyword(kwInput.trim()); setPage(1); };

  return (
    <SysPage title="操作日志" subtitle="记录系统管理内的所有增删改与授权操作,便于追责与回溯">
      <Toolbar>
        <SearchInput value={kwInput} onChange={setKwInput} onEnter={apply} placeholder="操作人 / 对象 / 动作" className="w-56" />
        <Select value={module} onChange={(v) => (setModule(v), setPage(1))}>
          <option value="">全部模块</option>
          {Object.entries(MODULE_LABEL).map(([v, l]) => <option key={v} value={v}>{l}</option>)}
        </Select>
        {(keyword || module) && <GhostButton onClick={() => (setKwInput(""), setKeyword(""), setModule(""), setPage(1))}><X className="h-4 w-4" /> 重置</GhostButton>}
      </Toolbar>

      <Card>
        <div className="min-h-0 flex-1 overflow-auto">
          <table className="w-full table-fixed text-sm">
            <colgroup>
              <col className="w-44" /><col className="w-24" /><col className="w-24" /><col className="w-48" /><col className="w-32" /><col className="w-28" />
            </colgroup>
            <thead className="sticky top-0 z-10 bg-blue-50/95 text-left backdrop-blur">
              <tr className="border-b border-blue-100 text-slate-500">
                <th className="px-4 py-2.5 font-semibold">时间</th>
                <th className="px-4 py-2.5 font-semibold">模块</th>
                <th className="px-4 py-2.5 font-semibold">动作</th>
                <th className="px-4 py-2.5 font-semibold">对象</th>
                <th className="px-4 py-2.5 font-semibold">操作人</th>
                <th className="px-4 py-2.5 font-semibold">IP</th>
              </tr>
            </thead>
            <tbody>
              {loading ? (
                <tr><td colSpan={6}><Spinner /></td></tr>
              ) : logs.length === 0 ? (
                <tr><td colSpan={6} className="py-16 text-center text-slate-400">暂无日志</td></tr>
              ) : (
                logs.map((l) => (
                  <tr key={l.id} onClick={() => setPreview(l)} className="cursor-pointer border-b border-slate-50 transition hover:bg-blue-50/50">
                    <td className="truncate px-4 py-2.5 text-slate-500">{l.createdAt ? l.createdAt.slice(0, 19).replace("T", " ") : "—"}</td>
                    <td className="px-4 py-2.5"><span className="rounded bg-slate-100 px-1.5 py-0.5 text-xs text-slate-600">{MODULE_LABEL[l.module] ?? l.module}</span></td>
                    <td className="px-4 py-2.5 text-slate-600">{ACTION_LABEL[l.action] ?? l.action}</td>
                    <td className="truncate px-4 py-2.5 text-slate-500">{l.targetName || "—"}</td>
                    <td className="truncate px-4 py-2.5 text-slate-500">{l.operatorName || "—"}</td>
                    <td className="truncate px-4 py-2.5 text-slate-400">{l.clientIp || "—"}</td>
                  </tr>
                ))
              )}
            </tbody>
          </table>
        </div>
        <Pagination page={page} pageSize={pageSize} total={total} onPage={setPage} onPageSize={(s) => (setPageSize(s), setPage(1))} />
      </Card>

      <Drawer open={!!preview} onClose={() => setPreview(null)} title="操作详情" subtitle={preview ? (preview.createdAt ?? "").slice(0, 19).replace("T", " ") : ""}>
        {preview && (
          <div>
            <Field label="模块">{MODULE_LABEL[preview.module] ?? preview.module}</Field>
            <Field label="动作">{ACTION_LABEL[preview.action] ?? preview.action}</Field>
            <Field label="对象类型">{preview.targetType || "—"}</Field>
            <Field label="对象">{preview.targetName || "—"}</Field>
            <Field label="操作人">{preview.operatorName || "—"}</Field>
            <Field label="IP">{preview.clientIp || "—"}</Field>
            <Field label="结果">{preview.result}</Field>
            <div className="pt-3">
              <div className="mb-1.5 text-xs font-medium text-slate-400">变更明细</div>
              <pre className="max-h-64 overflow-auto whitespace-pre-wrap rounded-xl bg-slate-50 p-3 text-xs text-slate-700">
                {preview.detail ? JSON.stringify(preview.detail, null, 2) : "—"}
              </pre>
            </div>
          </div>
        )}
      </Drawer>
    </SysPage>
  );
}