feat: 用户管理新增批量创建用户(逐行解析、失败跳过并汇总展示)
This commit is contained in:
@@ -19,8 +19,8 @@ export const getUser = () => {
|
||||
return http.get("/user/info");
|
||||
};
|
||||
|
||||
export const createUser = (data: any) => {
|
||||
return http.post("/user/create_org_user", data);
|
||||
export const createUser = (data: any, config?: any) => {
|
||||
return http.post("/user/create_org_user", data, config);
|
||||
};
|
||||
|
||||
export const getUserOrg = () => {
|
||||
|
||||
@@ -56,8 +56,9 @@ export const setupInterceptors = (instance: AxiosInstance) => {
|
||||
res,
|
||||
);
|
||||
|
||||
const silent = res.config?.silent === true;
|
||||
if (res.status !== 200) {
|
||||
toast.error(res?.data?.msg || "请求失败,请稍后再试");
|
||||
if (!silent) toast.error(res?.data?.msg || "请求失败,请稍后再试");
|
||||
return Promise.reject(res.data);
|
||||
}
|
||||
// 文件下载
|
||||
@@ -75,7 +76,7 @@ export const setupInterceptors = (instance: AxiosInstance) => {
|
||||
["401", "402"].includes(code) &&
|
||||
router.state.location.pathname !== "/login"
|
||||
) {
|
||||
toast.error(msg || "未登录/无权限");
|
||||
if (!silent) toast.error(msg || "未登录/无权限");
|
||||
useUserStore.setState({ token: null });
|
||||
router.navigate({
|
||||
to:
|
||||
@@ -92,7 +93,7 @@ export const setupInterceptors = (instance: AxiosInstance) => {
|
||||
}
|
||||
// 失败统一弹窗处理
|
||||
if (["555", "666", "999", "401", "402", "700"].includes(code)) {
|
||||
toast.error(msg || "系统错误,请稍后再试");
|
||||
if (!silent) toast.error(msg || "系统错误,请稍后再试");
|
||||
}
|
||||
return Promise.reject(res.data);
|
||||
},
|
||||
|
||||
199
src/routes/admin/user/-components/BatchCreateUserDialog.tsx
Normal file
199
src/routes/admin/user/-components/BatchCreateUserDialog.tsx
Normal file
@@ -0,0 +1,199 @@
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import { createUser } from "@/apis/user";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import {
|
||||
Dialog,
|
||||
DialogClose,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from "@/components/ui/dialog";
|
||||
import { Spinner } from "@/components/ui/spinner";
|
||||
import { Textarea } from "@/components/ui/textarea";
|
||||
|
||||
type BatchCreateUserDialogProps = {
|
||||
open: boolean;
|
||||
setOpen: (open: boolean) => void;
|
||||
refetch?: () => void;
|
||||
};
|
||||
|
||||
type FailedItem = {
|
||||
line: string;
|
||||
msg: string;
|
||||
};
|
||||
|
||||
const PHONE_RE = /^1\d{10}$/;
|
||||
|
||||
const parseText = (text: string) => {
|
||||
const rows: { phone: string; realName: string; line: string }[] = [];
|
||||
const failed: FailedItem[] = [];
|
||||
|
||||
text.split("\n").forEach((raw) => {
|
||||
const line = raw.trim();
|
||||
if (!line) return;
|
||||
const [phone, ...rest] = line.split(/[,,\t\s]+/).filter(Boolean);
|
||||
if (!phone || rest.length === 0) {
|
||||
failed.push({ line, msg: "格式错误,应为「手机号,姓名」" });
|
||||
return;
|
||||
}
|
||||
if (!PHONE_RE.test(phone)) {
|
||||
failed.push({ line, msg: "手机号格式不正确" });
|
||||
return;
|
||||
}
|
||||
rows.push({ phone, realName: rest.join(" "), line });
|
||||
});
|
||||
|
||||
return { rows, failed };
|
||||
};
|
||||
|
||||
export function BatchCreateUserDialog({
|
||||
open,
|
||||
setOpen,
|
||||
refetch,
|
||||
}: BatchCreateUserDialogProps) {
|
||||
const [text, setText] = useState("");
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [progress, setProgress] = useState({ done: 0, total: 0 });
|
||||
const [failed, setFailed] = useState<FailedItem[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
setText("");
|
||||
setIsPending(false);
|
||||
setProgress({ done: 0, total: 0 });
|
||||
setFailed([]);
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const lineCount = useMemo(
|
||||
() => text.split("\n").filter((line) => line.trim()).length,
|
||||
[text],
|
||||
);
|
||||
|
||||
const onOpenChange = (nextOpen: boolean) => {
|
||||
if (isPending) return;
|
||||
setOpen(nextOpen);
|
||||
};
|
||||
|
||||
const onSubmit = async () => {
|
||||
const { rows, failed: invalid } = parseText(text);
|
||||
if (rows.length === 0) {
|
||||
if (invalid.length > 0) {
|
||||
setFailed(invalid);
|
||||
} else {
|
||||
toast.error("请输入用户数据");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const failures: FailedItem[] = [...invalid];
|
||||
setIsPending(true);
|
||||
setProgress({ done: 0, total: rows.length });
|
||||
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
const { phone, realName, line } = rows[index];
|
||||
try {
|
||||
await createUser({ phone, realName }, { silent: true });
|
||||
} catch (err: any) {
|
||||
failures.push({ line, msg: err?.msg || "创建失败" });
|
||||
}
|
||||
setProgress({ done: index + 1, total: rows.length });
|
||||
}
|
||||
|
||||
setIsPending(false);
|
||||
refetch?.();
|
||||
|
||||
const failedCount = failures.length;
|
||||
const successCount = rows.length - (failedCount - invalid.length);
|
||||
if (failedCount > 0) {
|
||||
setFailed(failures);
|
||||
toast.error(`创建完成:成功 ${successCount} 条,失败 ${failedCount} 条`);
|
||||
} else {
|
||||
toast.success(`创建成功:共 ${rows.length} 条`);
|
||||
setOpen(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent className="w-[480px] max-w-[calc(100%-2rem)]">
|
||||
<DialogHeader>
|
||||
<DialogTitle>
|
||||
{failed.length > 0 ? "批量创建失败项" : "批量创建用户"}
|
||||
</DialogTitle>
|
||||
<DialogDescription>
|
||||
{failed.length > 0
|
||||
? `以下 ${failed.length} 条数据创建失败,修正后可重新提交`
|
||||
: "每行一个用户,格式为「手机号,姓名」,支持逗号、空格或制表符分隔"}
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
{failed.length > 0 ? (
|
||||
<div className="max-h-[320px] space-y-1 overflow-auto rounded-md border p-2 text-xs">
|
||||
{failed.map((item, index) => (
|
||||
<div
|
||||
key={`${item.line}-${index}`}
|
||||
className="flex items-start gap-2 rounded p-1.5 bg-red-50"
|
||||
>
|
||||
<span className="shrink-0 text-black/40">{index + 1}.</span>
|
||||
<span className="min-w-0 flex-1 break-all font-mono">
|
||||
{item.line}
|
||||
</span>
|
||||
<span className="shrink-0 text-red-600">{item.msg}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-2">
|
||||
<Textarea
|
||||
value={text}
|
||||
onChange={(event) => setText(event.target.value)}
|
||||
placeholder={"13800000001,张三\n13800000002,李四"}
|
||||
className="h-[240px] resize-none font-mono"
|
||||
disabled={isPending}
|
||||
/>
|
||||
<div className="flex items-center justify-between text-xs text-black/50">
|
||||
<span>已输入 {lineCount} 条</span>
|
||||
{isPending && (
|
||||
<span>
|
||||
正在创建 {progress.done}/{progress.total}...
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<DialogFooter>
|
||||
{failed.length > 0 ? (
|
||||
<>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setFailed([])}
|
||||
disabled={isPending}
|
||||
>
|
||||
返回修改
|
||||
</Button>
|
||||
<DialogClose asChild>
|
||||
<Button>关闭</Button>
|
||||
</DialogClose>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline" disabled={isPending}>
|
||||
取消
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button onClick={onSubmit} disabled={isPending}>
|
||||
{isPending && <Spinner />}开始创建
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import { createFileRoute } from "@tanstack/react-router";
|
||||
import { Plus } from "lucide-react";
|
||||
import { ListPlus, Plus } from "lucide-react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { DataTable } from "@/components/data-table";
|
||||
import { useFetchList } from "./-hooks/useFetchList";
|
||||
import { useState } from "react";
|
||||
import { EditUserDialog } from "./-components/EditUserDialog";
|
||||
import { BatchCreateUserDialog } from "./-components/BatchCreateUserDialog";
|
||||
import { CancelUserConfirmDialog } from "./-components/CancelUserConfirmDialog";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Pagination } from "@/components/pagination";
|
||||
@@ -133,6 +134,10 @@ export function useColumns({
|
||||
function RouteComponent() {
|
||||
const [currentEntity, setCurrentEntity] = useState<any>(null);
|
||||
|
||||
// 批量创建用户
|
||||
const [openBatchCreateUserDialog, setOpenBatchCreateUserDialog] =
|
||||
useState(false);
|
||||
|
||||
// 修改用户
|
||||
const [openEditUserDialog, setOpenEditUserDialog] = useState(false);
|
||||
const handleEditUser = (record?: any) => {
|
||||
@@ -179,6 +184,11 @@ function RouteComponent() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<BatchCreateUserDialog
|
||||
open={openBatchCreateUserDialog}
|
||||
setOpen={setOpenBatchCreateUserDialog}
|
||||
refetch={query?.refetch}
|
||||
/>
|
||||
<EditUserDialog
|
||||
open={openEditUserDialog}
|
||||
setOpen={setOpenEditUserDialog}
|
||||
@@ -207,6 +217,13 @@ function RouteComponent() {
|
||||
<Plus />
|
||||
新增
|
||||
</Button>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => setOpenBatchCreateUserDialog(true)}
|
||||
>
|
||||
<ListPlus />
|
||||
批量创建
|
||||
</Button>
|
||||
</div>
|
||||
<div className="relative overflow-hidden">
|
||||
<DataTable table={table} query={query} />
|
||||
|
||||
Reference in New Issue
Block a user