Refactor employee management
This commit is contained in:
@@ -21,6 +21,8 @@ No test or lint commands are currently configured. When adding tests, use Vitest
|
|||||||
This is a React 19 + TypeScript frontend for the Zhejiang Beifan Trace Management Platform (溯源管理平台). It provides:
|
This is a React 19 + TypeScript frontend for the Zhejiang Beifan Trace Management Platform (溯源管理平台). It provides:
|
||||||
- Public query interface for serial number verification
|
- Public query interface for serial number verification
|
||||||
- Admin dashboard for QR code generation and company management
|
- Admin dashboard for QR code generation and company management
|
||||||
|
- Employee management with automatic employee serial generation
|
||||||
|
- Aftersales work-order management for admins and technicians
|
||||||
- User authentication and profile management
|
- User authentication and profile management
|
||||||
|
|
||||||
**Tech Stack**: React 19, TypeScript, Vite 7, Ant Design 6, React Router v7, Axios
|
**Tech Stack**: React 19, TypeScript, Vite 7, Ant Design 6, React Router v7, Axios
|
||||||
@@ -90,7 +92,8 @@ src/
|
|||||||
- `dashboardApi` - Dashboard statistics
|
- `dashboardApi` - Dashboard statistics
|
||||||
- `employeeSerialApi` - Employee serial management
|
- `employeeSerialApi` - Employee serial management
|
||||||
- `aftersalesApi` - Aftersales work orders (admin + public)
|
- `aftersalesApi` - Aftersales work orders (admin + public)
|
||||||
- `usersApi` - User management (admin only); also exposes `assignable` for technician/admin picker
|
- `employeesApi` - Employee management (admin only): create/list/update/delete/reset password
|
||||||
|
- `usersApi` - Assignable technician/admin picker via `assignable`
|
||||||
- Auth token automatically added via axios interceptor
|
- Auth token automatically added via axios interceptor
|
||||||
- All API calls return typed responses based on `src/types/index.ts`
|
- All API calls return typed responses based on `src/types/index.ts`
|
||||||
|
|
||||||
@@ -103,6 +106,18 @@ src/
|
|||||||
- Public routes (no auth): `/login`, `/query`, `/aftersales/:serialNumber`
|
- Public routes (no auth): `/login`, `/query`, `/aftersales/:serialNumber`
|
||||||
- `PublicQuery` auto-redirects scanned `zjbf-sh-*` serials to `/aftersales/:serialNumber`
|
- `PublicQuery` auto-redirects scanned `zjbf-sh-*` serials to `/aftersales/:serialNumber`
|
||||||
- Shared public-page chrome (logo + 备案 footer) lives in `components/PublicLayout.tsx`
|
- Shared public-page chrome (logo + 备案 footer) lives in `components/PublicLayout.tsx`
|
||||||
|
- `/admin/employee-serials` is the employee management page despite the legacy route name.
|
||||||
|
- Technicians should only see/use the aftersales module; admins see all admin menu items.
|
||||||
|
|
||||||
|
### Roles and Employee Management
|
||||||
|
- `UserRole` is limited to `admin` / `technician` / `employee`.
|
||||||
|
- `admin`: full backend access.
|
||||||
|
- `technician`: work-order module access only.
|
||||||
|
- `employee`: no backend login access.
|
||||||
|
- Employee creation fields are name, phone, employee number, position, and role.
|
||||||
|
- Password field is shown and required only for `admin` and `technician`.
|
||||||
|
- Employee creation uses `employeesApi.create`, and the backend automatically creates the employee serial; do not implement a separate "create then assign code" primary flow.
|
||||||
|
- Employee rows should display generated `employeeSerials` from the employee list response.
|
||||||
|
|
||||||
### Aftersales Conventions
|
### Aftersales Conventions
|
||||||
- Aftersales serial format is `zjbf-sh-YYMMDDNN` (daily sequence), e.g. `zjbf-sh-26052801`.
|
- Aftersales serial format is `zjbf-sh-YYMMDDNN` (daily sequence), e.g. `zjbf-sh-26052801`.
|
||||||
|
|||||||
@@ -25,11 +25,10 @@ frontend/
|
|||||||
│ │ ├── PublicQuery.tsx
|
│ │ ├── PublicQuery.tsx
|
||||||
│ │ ├── Dashboard.tsx
|
│ │ ├── Dashboard.tsx
|
||||||
│ │ ├── Manage.tsx
|
│ │ ├── Manage.tsx
|
||||||
│ │ ├── EmployeeSerials.tsx
|
│ │ ├── EmployeeSerials.tsx # 员工管理(主档 + 自动员工码)
|
||||||
│ │ ├── Aftersales.tsx # 售后工单列表(管理后台)
|
│ │ ├── Aftersales.tsx # 售后工单列表(管理后台)
|
||||||
│ │ ├── AftersalesDetail.tsx # 售后工单详情(管理后台)
|
│ │ ├── AftersalesDetail.tsx # 售后工单详情(管理后台)
|
||||||
│ │ ├── AftersalesConfirm.tsx # 售后工单扫码确认(公开)
|
│ │ ├── AftersalesConfirm.tsx # 售后工单扫码确认(公开)
|
||||||
│ │ ├── Users.tsx # 用户管理(仅管理员)
|
|
||||||
│ │ └── Profile.tsx
|
│ │ └── Profile.tsx
|
||||||
│ ├── services/ # API 服务层
|
│ ├── services/ # API 服务层
|
||||||
│ │ └── api.ts
|
│ │ └── api.ts
|
||||||
@@ -105,15 +104,18 @@ VITE_API_BASE_URL=/api
|
|||||||
- 查看序列号列表
|
- 查看序列号列表
|
||||||
- 吊销企业/序列号
|
- 吊销企业/序列号
|
||||||
- 查看序列号二维码
|
- 查看序列号二维码
|
||||||
- 员工管理(员工赋码生成与维护)
|
- 员工管理
|
||||||
|
- 创建员工时录入姓名、电话、工号、岗位、角色
|
||||||
|
- 角色仅保留管理员、技术员、员工
|
||||||
|
- 管理员/技术员有后台登录权限,创建时显示并必填初始密码
|
||||||
|
- 员工无后台权限,创建时不显示密码框
|
||||||
|
- 创建员工后自动生成员工码,列表直接展示员工码
|
||||||
- 售后工单
|
- 售后工单
|
||||||
- 技术员创建工单、填写处理结果、提交客户确认
|
- 技术员创建工单、填写处理结果、提交客户确认
|
||||||
- 服务类型:软件故障 / 硬件故障 / 售后维保
|
- 服务类型:软件故障 / 硬件故障 / 售后维保
|
||||||
- 新建和详情字段使用“问题描述反馈”
|
- 新建和详情字段使用“问题描述反馈”
|
||||||
- 管理员可进行工单分配(重新分配技术员)或强制关闭工单
|
- 管理员可进行工单分配(重新分配技术员)或强制关闭工单
|
||||||
- 工单状态机:待处理 → 待客户确认 → 已完成 / 已退回
|
- 工单状态机:待处理 → 待客户确认 → 已完成 / 已退回
|
||||||
- 用户管理(仅管理员可见)
|
|
||||||
- 创建技术员/管理员账号、修改角色、重置密码、删除用户
|
|
||||||
- 用户资料管理
|
- 用户资料管理
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|||||||
+6
-1
@@ -31,6 +31,11 @@ const AdminRoutes = () => {
|
|||||||
return <AdminLayout />;
|
return <AdminLayout />;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const AdminIndexRedirect = () => {
|
||||||
|
const user = authApi.getCurrentUser();
|
||||||
|
return <Navigate to={user?.role === 'technician' ? 'aftersales' : 'dashboard'} replace />;
|
||||||
|
};
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
return (
|
return (
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
@@ -46,7 +51,7 @@ function App() {
|
|||||||
|
|
||||||
<Route element={<PrivateRoute />}>
|
<Route element={<PrivateRoute />}>
|
||||||
<Route element={<AdminRoutes />}>
|
<Route element={<AdminRoutes />}>
|
||||||
<Route path="/admin" element={<Navigate to="dashboard" replace />} />
|
<Route path="/admin" element={<AdminIndexRedirect />} />
|
||||||
<Route path="/admin/dashboard" element={<DashboardPage />} />
|
<Route path="/admin/dashboard" element={<DashboardPage />} />
|
||||||
<Route path="/admin/manage" element={<ManagePage />} />
|
<Route path="/admin/manage" element={<ManagePage />} />
|
||||||
<Route path="/admin/employee-serials" element={<EmployeeSerialsPage />} />
|
<Route path="/admin/employee-serials" element={<EmployeeSerialsPage />} />
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { useEffect } from 'react';
|
||||||
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
|
import { Outlet, useNavigate, useLocation } from 'react-router-dom';
|
||||||
import { Layout, Menu, Dropdown, Avatar, message, Modal } from 'antd';
|
import { Layout, Menu, Dropdown, Avatar, message, Modal } from 'antd';
|
||||||
import {
|
import {
|
||||||
@@ -19,8 +20,15 @@ function AdminLayout() {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const location = useLocation();
|
const location = useLocation();
|
||||||
const user = authApi.getCurrentUser();
|
const user = authApi.getCurrentUser();
|
||||||
|
const isTechnician = user?.role === 'technician';
|
||||||
|
|
||||||
const menuItems = [
|
useEffect(() => {
|
||||||
|
if (isTechnician && !location.pathname.includes('/aftersales') && !location.pathname.includes('/profile')) {
|
||||||
|
navigate('/admin/aftersales', { replace: true });
|
||||||
|
}
|
||||||
|
}, [isTechnician, location.pathname, navigate]);
|
||||||
|
|
||||||
|
const adminMenuItems = [
|
||||||
{
|
{
|
||||||
key: 'dashboard',
|
key: 'dashboard',
|
||||||
icon: <DashboardOutlined />,
|
icon: <DashboardOutlined />,
|
||||||
@@ -46,6 +54,15 @@ function AdminLayout() {
|
|||||||
onClick: () => navigate('/admin/aftersales'),
|
onClick: () => navigate('/admin/aftersales'),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
const technicianMenuItems = [
|
||||||
|
{
|
||||||
|
key: 'aftersales',
|
||||||
|
icon: <ToolOutlined />,
|
||||||
|
label: '售后工单',
|
||||||
|
onClick: () => navigate('/admin/aftersales'),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
const menuItems = isTechnician ? technicianMenuItems : adminMenuItems;
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
Modal.confirm({
|
Modal.confirm({
|
||||||
|
|||||||
@@ -1,416 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import {
|
|
||||||
Card,
|
|
||||||
Table,
|
|
||||||
Button,
|
|
||||||
Space,
|
|
||||||
Input,
|
|
||||||
Select,
|
|
||||||
Tag,
|
|
||||||
Modal,
|
|
||||||
Form,
|
|
||||||
message,
|
|
||||||
Pagination,
|
|
||||||
} from 'antd';
|
|
||||||
import {
|
|
||||||
UsergroupAddOutlined,
|
|
||||||
PlusOutlined,
|
|
||||||
EditOutlined,
|
|
||||||
DeleteOutlined,
|
|
||||||
KeyOutlined,
|
|
||||||
} from '@ant-design/icons';
|
|
||||||
import { usersApi, authApi } from '@/services/api';
|
|
||||||
import type { User, UserRole, CreateUserRequest, UpdateUserRequest } from '@/types';
|
|
||||||
|
|
||||||
const ROLE_LABEL: Record<UserRole, string> = {
|
|
||||||
admin: '管理员',
|
|
||||||
technician: '技术员',
|
|
||||||
employee: '员工(不可登录后台)',
|
|
||||||
user: '普通用户',
|
|
||||||
};
|
|
||||||
|
|
||||||
const ROLE_COLOR: Record<UserRole, string> = {
|
|
||||||
admin: 'red',
|
|
||||||
technician: 'blue',
|
|
||||||
employee: 'green',
|
|
||||||
user: 'default',
|
|
||||||
};
|
|
||||||
|
|
||||||
function EmployeeAccountsPanel() {
|
|
||||||
const currentUser = authApi.getCurrentUser();
|
|
||||||
|
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [page, setPage] = useState(1);
|
|
||||||
const [limit, setLimit] = useState(10);
|
|
||||||
const [total, setTotal] = useState(0);
|
|
||||||
const [search, setSearch] = useState('');
|
|
||||||
const [roleFilter, setRoleFilter] = useState<UserRole | undefined>();
|
|
||||||
|
|
||||||
const [createVisible, setCreateVisible] = useState(false);
|
|
||||||
const [createLoading, setCreateLoading] = useState(false);
|
|
||||||
const [createForm] = Form.useForm<CreateUserRequest>();
|
|
||||||
|
|
||||||
const [editingUser, setEditingUser] = useState<User | null>(null);
|
|
||||||
const [editLoading, setEditLoading] = useState(false);
|
|
||||||
const [editForm] = Form.useForm<UpdateUserRequest>();
|
|
||||||
|
|
||||||
const [resetPasswordUser, setResetPasswordUser] = useState<User | null>(null);
|
|
||||||
const [resetLoading, setResetLoading] = useState(false);
|
|
||||||
const [resetForm] = Form.useForm<{ newPassword: string }>();
|
|
||||||
|
|
||||||
const loadUsers = async () => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const result = await usersApi.list({
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
search: search || undefined,
|
|
||||||
role: roleFilter,
|
|
||||||
});
|
|
||||||
setUsers(result.data || []);
|
|
||||||
setTotal(result.pagination?.total || 0);
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '加载员工账号列表失败');
|
|
||||||
setUsers([]);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
loadUsers();
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [page, limit, search, roleFilter]);
|
|
||||||
|
|
||||||
const handleCreate = async (values: CreateUserRequest) => {
|
|
||||||
setCreateLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.create(values);
|
|
||||||
message.success('员工账号创建成功');
|
|
||||||
setCreateVisible(false);
|
|
||||||
createForm.resetFields();
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '创建失败');
|
|
||||||
} finally {
|
|
||||||
setCreateLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const openEdit = (user: User) => {
|
|
||||||
setEditingUser(user);
|
|
||||||
editForm.setFieldsValue({
|
|
||||||
name: user.name,
|
|
||||||
email: user.email,
|
|
||||||
role: user.role,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEdit = async (values: UpdateUserRequest) => {
|
|
||||||
if (!editingUser) return;
|
|
||||||
setEditLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.update(editingUser.id, values);
|
|
||||||
message.success('员工账号更新成功');
|
|
||||||
setEditingUser(null);
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '更新失败');
|
|
||||||
} finally {
|
|
||||||
setEditLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleResetPassword = async (values: { newPassword: string }) => {
|
|
||||||
if (!resetPasswordUser) return;
|
|
||||||
setResetLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.resetPassword(resetPasswordUser.id, values.newPassword);
|
|
||||||
message.success('密码重置成功');
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '重置失败');
|
|
||||||
} finally {
|
|
||||||
setResetLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDelete = (user: User) => {
|
|
||||||
Modal.confirm({
|
|
||||||
title: '确认删除',
|
|
||||||
content: `确定要删除员工账号 "${user.username}" 吗?此操作不可恢复!`,
|
|
||||||
okText: '确定',
|
|
||||||
okType: 'danger',
|
|
||||||
cancelText: '取消',
|
|
||||||
onOk: async () => {
|
|
||||||
try {
|
|
||||||
await usersApi.delete(user.id);
|
|
||||||
message.success('删除成功');
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '删除失败');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns = [
|
|
||||||
{
|
|
||||||
title: '用户名',
|
|
||||||
dataIndex: 'username',
|
|
||||||
key: 'username',
|
|
||||||
render: (text: string) => <span style={{ fontFamily: 'monospace' }}>{text}</span>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '姓名',
|
|
||||||
dataIndex: 'name',
|
|
||||||
key: 'name',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '邮箱',
|
|
||||||
dataIndex: 'email',
|
|
||||||
key: 'email',
|
|
||||||
render: (text?: string) => text || '-',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '角色',
|
|
||||||
dataIndex: 'role',
|
|
||||||
key: 'role',
|
|
||||||
width: 100,
|
|
||||||
render: (role: UserRole) => <Tag color={ROLE_COLOR[role]}>{ROLE_LABEL[role]}</Tag>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '创建时间',
|
|
||||||
dataIndex: 'createdAt',
|
|
||||||
key: 'createdAt',
|
|
||||||
width: 170,
|
|
||||||
render: (date: string) => new Date(date).toLocaleString('zh-CN'),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '操作',
|
|
||||||
key: 'actions',
|
|
||||||
width: 260,
|
|
||||||
render: (_: any, record: User) => (
|
|
||||||
<Space>
|
|
||||||
<Button type="link" size="small" icon={<EditOutlined />} onClick={() => openEdit(record)}>
|
|
||||||
编辑
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="link"
|
|
||||||
size="small"
|
|
||||||
icon={<KeyOutlined />}
|
|
||||||
onClick={() => setResetPasswordUser(record)}
|
|
||||||
>
|
|
||||||
重置密码
|
|
||||||
</Button>
|
|
||||||
{record.id !== currentUser?.id && (
|
|
||||||
<Button
|
|
||||||
type="link"
|
|
||||||
size="small"
|
|
||||||
danger
|
|
||||||
icon={<DeleteOutlined />}
|
|
||||||
onClick={() => handleDelete(record)}
|
|
||||||
>
|
|
||||||
删除
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Space>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Card
|
|
||||||
title={
|
|
||||||
<Space>
|
|
||||||
<UsergroupAddOutlined />
|
|
||||||
<span>员工账号管理</span>
|
|
||||||
</Space>
|
|
||||||
}
|
|
||||||
extra={
|
|
||||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => setCreateVisible(true)}>
|
|
||||||
新建员工账号
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Space style={{ marginBottom: 16 }}>
|
|
||||||
<Input.Search
|
|
||||||
placeholder="搜索用户名/姓名/邮箱"
|
|
||||||
allowClear
|
|
||||||
style={{ width: 260 }}
|
|
||||||
onSearch={(v) => {
|
|
||||||
setPage(1);
|
|
||||||
setSearch(v);
|
|
||||||
}}
|
|
||||||
onChange={(e) => {
|
|
||||||
if (!e.target.value) {
|
|
||||||
setPage(1);
|
|
||||||
setSearch('');
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Select
|
|
||||||
placeholder="角色筛选"
|
|
||||||
allowClear
|
|
||||||
style={{ width: 140 }}
|
|
||||||
value={roleFilter}
|
|
||||||
onChange={(v) => {
|
|
||||||
setPage(1);
|
|
||||||
setRoleFilter(v);
|
|
||||||
}}
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Space>
|
|
||||||
|
|
||||||
<Table columns={columns} dataSource={users} rowKey="id" loading={loading} pagination={false} />
|
|
||||||
<div style={{ marginTop: 16 }}>
|
|
||||||
<Pagination
|
|
||||||
current={page}
|
|
||||||
pageSize={limit}
|
|
||||||
total={total}
|
|
||||||
onChange={(newPage, newLimit) => {
|
|
||||||
setPage(newPage);
|
|
||||||
setLimit(newLimit);
|
|
||||||
}}
|
|
||||||
showSizeChanger
|
|
||||||
showTotal={(t) => `共计 ${t} 条记录`}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title="新建员工账号"
|
|
||||||
open={createVisible}
|
|
||||||
onCancel={() => {
|
|
||||||
setCreateVisible(false);
|
|
||||||
createForm.resetFields();
|
|
||||||
}}
|
|
||||||
footer={null}
|
|
||||||
width={480}
|
|
||||||
>
|
|
||||||
<Form form={createForm} layout="vertical" onFinish={handleCreate} initialValues={{ role: 'technician' }}>
|
|
||||||
<Form.Item
|
|
||||||
name="username"
|
|
||||||
label="用户名"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入用户名' },
|
|
||||||
{ min: 3, max: 50, message: '用户名长度 3-50' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input placeholder="登录用用户名" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item
|
|
||||||
name="password"
|
|
||||||
label="初始密码"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入密码' },
|
|
||||||
{ min: 6, message: '密码至少 6 位' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input.Password placeholder="至少 6 位" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="name" label="姓名" rules={[{ required: true, message: '请输入姓名' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="role" label="角色" rules={[{ required: true, message: '请选择角色' }]}>
|
|
||||||
<Select
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button onClick={() => setCreateVisible(false)}>取消</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={createLoading}>
|
|
||||||
创建
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title={editingUser ? `编辑员工账号:${editingUser.username}` : '编辑员工账号'}
|
|
||||||
open={!!editingUser}
|
|
||||||
onCancel={() => setEditingUser(null)}
|
|
||||||
footer={null}
|
|
||||||
width={480}
|
|
||||||
>
|
|
||||||
<Form form={editForm} layout="vertical" onFinish={handleEdit}>
|
|
||||||
<Form.Item name="name" label="姓名">
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="role" label="角色">
|
|
||||||
<Select
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button onClick={() => setEditingUser(null)}>取消</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={editLoading}>
|
|
||||||
保存
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title={resetPasswordUser ? `重置密码:${resetPasswordUser.username}` : '重置密码'}
|
|
||||||
open={!!resetPasswordUser}
|
|
||||||
onCancel={() => {
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
}}
|
|
||||||
footer={null}
|
|
||||||
width={420}
|
|
||||||
>
|
|
||||||
<Form form={resetForm} layout="vertical" onFinish={handleResetPassword}>
|
|
||||||
<Form.Item
|
|
||||||
name="newPassword"
|
|
||||||
label="新密码"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入新密码' },
|
|
||||||
{ min: 6, message: '密码至少 6 位' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input.Password placeholder="至少 6 位" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
取消
|
|
||||||
</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={resetLoading}>
|
|
||||||
确认重置
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
</Card>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default EmployeeAccountsPanel;
|
|
||||||
+197
-167
@@ -11,7 +11,6 @@ import {
|
|||||||
Form,
|
Form,
|
||||||
message,
|
message,
|
||||||
Pagination,
|
Pagination,
|
||||||
InputNumber,
|
|
||||||
} from 'antd';
|
} from 'antd';
|
||||||
import {
|
import {
|
||||||
UserOutlined,
|
UserOutlined,
|
||||||
@@ -19,30 +18,49 @@ import {
|
|||||||
EditOutlined,
|
EditOutlined,
|
||||||
DeleteOutlined,
|
DeleteOutlined,
|
||||||
KeyOutlined,
|
KeyOutlined,
|
||||||
IdcardOutlined,
|
|
||||||
} from '@ant-design/icons';
|
} from '@ant-design/icons';
|
||||||
import { usersApi, authApi, employeeSerialApi } from '@/services/api';
|
import { authApi, employeesApi } from '@/services/api';
|
||||||
import type { User, UserRole, CreateUserRequest, UpdateUserRequest, EmployeeSerial } from '@/types';
|
import type { User, UserRole, CreateUserRequest, UpdateUserRequest, EmployeeSerial } from '@/types';
|
||||||
|
|
||||||
const ROLE_LABEL: Record<UserRole, string> = {
|
const ROLE_LABEL: Record<UserRole, string> = {
|
||||||
admin: '管理员',
|
admin: '管理员',
|
||||||
technician: '技术员',
|
technician: '技术员',
|
||||||
employee: '员工(不可登录后台)',
|
employee: '员工',
|
||||||
user: '普通用户',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const ROLE_COLOR: Record<UserRole, string> = {
|
const ROLE_COLOR: Record<UserRole, string> = {
|
||||||
admin: 'red',
|
admin: 'red',
|
||||||
technician: 'blue',
|
technician: 'blue',
|
||||||
employee: 'green',
|
employee: 'green',
|
||||||
user: 'default',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const BACKEND_ROLES: UserRole[] = ['admin', 'technician'];
|
||||||
|
const ROLE_OPTIONS = (Object.keys(ROLE_LABEL) as UserRole[]).map((value) => ({
|
||||||
|
value,
|
||||||
|
label: ROLE_LABEL[value],
|
||||||
|
}));
|
||||||
|
|
||||||
|
const canLoginBackend = (role?: UserRole) => !!role && BACKEND_ROLES.includes(role);
|
||||||
|
|
||||||
|
function renderEmployeeSerial(serials?: EmployeeSerial[]) {
|
||||||
|
const activeSerial = serials?.find((serial) => serial.isActive) || serials?.[0];
|
||||||
|
if (!activeSerial) return '-';
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Space direction="vertical" size={0}>
|
||||||
|
<span style={{ fontFamily: 'monospace' }}>{activeSerial.serialNumber}</span>
|
||||||
|
<Tag color={activeSerial.isActive ? 'green' : 'red'}>
|
||||||
|
{activeSerial.isActive ? '有效' : '已吊销'}
|
||||||
|
</Tag>
|
||||||
|
</Space>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function EmployeeSerialsPage() {
|
function EmployeeSerialsPage() {
|
||||||
const currentUser = authApi.getCurrentUser();
|
const currentUser = authApi.getCurrentUser();
|
||||||
const isAdmin = currentUser?.role === 'admin';
|
const isAdmin = currentUser?.role === 'admin';
|
||||||
|
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
const [employees, setEmployees] = useState<User[]>([]);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [page, setPage] = useState(1);
|
const [page, setPage] = useState(1);
|
||||||
const [limit, setLimit] = useState(10);
|
const [limit, setLimit] = useState(10);
|
||||||
@@ -53,55 +71,59 @@ function EmployeeSerialsPage() {
|
|||||||
const [createVisible, setCreateVisible] = useState(false);
|
const [createVisible, setCreateVisible] = useState(false);
|
||||||
const [createLoading, setCreateLoading] = useState(false);
|
const [createLoading, setCreateLoading] = useState(false);
|
||||||
const [createForm] = Form.useForm<CreateUserRequest>();
|
const [createForm] = Form.useForm<CreateUserRequest>();
|
||||||
|
const createRole = Form.useWatch('role', createForm);
|
||||||
|
|
||||||
const [editingUser, setEditingUser] = useState<User | null>(null);
|
const [editingEmployee, setEditingEmployee] = useState<User | null>(null);
|
||||||
const [editLoading, setEditLoading] = useState(false);
|
const [editLoading, setEditLoading] = useState(false);
|
||||||
const [editForm] = Form.useForm<UpdateUserRequest>();
|
const [editForm] = Form.useForm<UpdateUserRequest>();
|
||||||
|
|
||||||
const [resetPasswordUser, setResetPasswordUser] = useState<User | null>(null);
|
const [resetPasswordEmployee, setResetPasswordEmployee] = useState<User | null>(null);
|
||||||
const [resetLoading, setResetLoading] = useState(false);
|
const [resetLoading, setResetLoading] = useState(false);
|
||||||
const [resetForm] = Form.useForm<{ newPassword: string }>();
|
const [resetForm] = Form.useForm<{ newPassword: string }>();
|
||||||
|
|
||||||
const [assignUser, setAssignUser] = useState<User | null>(null);
|
const loadEmployees = async () => {
|
||||||
const [assignLoading, setAssignLoading] = useState(false);
|
|
||||||
const [assignForm] = Form.useForm<{ companyName: string; position: string; quantity: number }>();
|
|
||||||
|
|
||||||
const [serialsVisible, setSerialsVisible] = useState(false);
|
|
||||||
const [serialsLoading, setSerialsLoading] = useState(false);
|
|
||||||
const [serials, setSerials] = useState<EmployeeSerial[]>([]);
|
|
||||||
|
|
||||||
const loadUsers = async () => {
|
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
const result = await usersApi.list({
|
const result = await employeesApi.list({
|
||||||
page,
|
page,
|
||||||
limit,
|
limit,
|
||||||
search: search || undefined,
|
search: search || undefined,
|
||||||
role: roleFilter,
|
role: roleFilter,
|
||||||
});
|
});
|
||||||
setUsers(result.data || []);
|
setEmployees(result.data || []);
|
||||||
setTotal(result.pagination?.total || 0);
|
setTotal(result.pagination?.total || 0);
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err?.response?.data?.message || err.message || '加载员工列表失败');
|
message.error(err?.response?.data?.message || err.message || '加载员工列表失败');
|
||||||
setUsers([]);
|
setEmployees([]);
|
||||||
} finally {
|
} finally {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadUsers();
|
loadEmployees();
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [page, limit, search, roleFilter]);
|
}, [page, limit, search, roleFilter]);
|
||||||
|
|
||||||
|
const openCreate = () => {
|
||||||
|
createForm.resetFields();
|
||||||
|
createForm.setFieldsValue({ role: 'employee' });
|
||||||
|
setCreateVisible(true);
|
||||||
|
};
|
||||||
|
|
||||||
const handleCreate = async (values: CreateUserRequest) => {
|
const handleCreate = async (values: CreateUserRequest) => {
|
||||||
|
const payload = { ...values, username: values.employeeNo };
|
||||||
|
if (!canLoginBackend(values.role)) {
|
||||||
|
delete payload.password;
|
||||||
|
}
|
||||||
|
|
||||||
setCreateLoading(true);
|
setCreateLoading(true);
|
||||||
try {
|
try {
|
||||||
await usersApi.create(values);
|
await employeesApi.create(payload);
|
||||||
message.success('员工创建成功');
|
message.success('员工创建成功,员工码已自动生成');
|
||||||
setCreateVisible(false);
|
setCreateVisible(false);
|
||||||
createForm.resetFields();
|
createForm.resetFields();
|
||||||
loadUsers();
|
loadEmployees();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err?.response?.data?.message || err.message || '创建失败');
|
message.error(err?.response?.data?.message || err.message || '创建失败');
|
||||||
} finally {
|
} finally {
|
||||||
@@ -109,23 +131,26 @@ function EmployeeSerialsPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const openEdit = (user: User) => {
|
const openEdit = (employee: User) => {
|
||||||
setEditingUser(user);
|
setEditingEmployee(employee);
|
||||||
editForm.setFieldsValue({
|
editForm.setFieldsValue({
|
||||||
name: user.name,
|
name: employee.name,
|
||||||
email: user.email,
|
email: employee.email,
|
||||||
role: user.role,
|
phone: employee.phone,
|
||||||
|
employeeNo: employee.employeeNo,
|
||||||
|
position: employee.position,
|
||||||
|
role: employee.role,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleEdit = async (values: UpdateUserRequest) => {
|
const handleEdit = async (values: UpdateUserRequest) => {
|
||||||
if (!editingUser) return;
|
if (!editingEmployee) return;
|
||||||
setEditLoading(true);
|
setEditLoading(true);
|
||||||
try {
|
try {
|
||||||
await usersApi.update(editingUser.id, values);
|
await employeesApi.update(editingEmployee.id, values);
|
||||||
message.success('员工资料更新成功');
|
message.success('员工资料更新成功');
|
||||||
setEditingUser(null);
|
setEditingEmployee(null);
|
||||||
loadUsers();
|
loadEmployees();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err?.response?.data?.message || err.message || '更新失败');
|
message.error(err?.response?.data?.message || err.message || '更新失败');
|
||||||
} finally {
|
} finally {
|
||||||
@@ -134,12 +159,12 @@ function EmployeeSerialsPage() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleResetPassword = async (values: { newPassword: string }) => {
|
const handleResetPassword = async (values: { newPassword: string }) => {
|
||||||
if (!resetPasswordUser) return;
|
if (!resetPasswordEmployee) return;
|
||||||
setResetLoading(true);
|
setResetLoading(true);
|
||||||
try {
|
try {
|
||||||
await usersApi.resetPassword(resetPasswordUser.id, values.newPassword);
|
await employeesApi.resetPassword(resetPasswordEmployee.id, values.newPassword);
|
||||||
message.success('密码重置成功');
|
message.success('密码重置成功');
|
||||||
setResetPasswordUser(null);
|
setResetPasswordEmployee(null);
|
||||||
resetForm.resetFields();
|
resetForm.resetFields();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err?.response?.data?.message || err.message || '重置失败');
|
message.error(err?.response?.data?.message || err.message || '重置失败');
|
||||||
@@ -148,18 +173,18 @@ function EmployeeSerialsPage() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleDelete = (user: User) => {
|
const handleDelete = (employee: User) => {
|
||||||
Modal.confirm({
|
Modal.confirm({
|
||||||
title: '确认删除',
|
title: '确认删除',
|
||||||
content: `确定要删除员工 "${user.username}" 吗?`,
|
content: `确定要删除员工 "${employee.name}" 吗?`,
|
||||||
okText: '确定',
|
okText: '确定',
|
||||||
okType: 'danger',
|
okType: 'danger',
|
||||||
cancelText: '取消',
|
cancelText: '取消',
|
||||||
onOk: async () => {
|
onOk: async () => {
|
||||||
try {
|
try {
|
||||||
await usersApi.delete(user.id);
|
await employeesApi.delete(employee.id);
|
||||||
message.success('删除成功');
|
message.success('删除成功');
|
||||||
loadUsers();
|
loadEmployees();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err?.response?.data?.message || err.message || '删除失败');
|
message.error(err?.response?.data?.message || err.message || '删除失败');
|
||||||
}
|
}
|
||||||
@@ -167,78 +192,47 @@ function EmployeeSerialsPage() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const openAssignCode = (user: User) => {
|
|
||||||
setAssignUser(user);
|
|
||||||
assignForm.setFieldsValue({
|
|
||||||
position: user.role === 'technician' ? '技术员' : user.role === 'admin' ? '管理员' : '员工',
|
|
||||||
quantity: 1,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleAssignCode = async (values: { companyName: string; position: string; quantity: number }) => {
|
|
||||||
if (!assignUser) return;
|
|
||||||
setAssignLoading(true);
|
|
||||||
try {
|
|
||||||
await employeeSerialApi.generate({
|
|
||||||
companyName: values.companyName,
|
|
||||||
position: values.position,
|
|
||||||
employeeName: assignUser.name,
|
|
||||||
quantity: values.quantity,
|
|
||||||
});
|
|
||||||
message.success('赋码成功');
|
|
||||||
setAssignUser(null);
|
|
||||||
assignForm.resetFields();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '赋码失败');
|
|
||||||
} finally {
|
|
||||||
setAssignLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const openSerials = async (user: User) => {
|
|
||||||
setSerialsVisible(true);
|
|
||||||
setSerialsLoading(true);
|
|
||||||
try {
|
|
||||||
const result = await employeeSerialApi.list({ page: 1, limit: 200, search: user.name || user.username });
|
|
||||||
const list = (result.data || []).filter((s) => s.employeeName === user.name);
|
|
||||||
setSerials(list);
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '加载赋码记录失败');
|
|
||||||
setSerials([]);
|
|
||||||
} finally {
|
|
||||||
setSerialsLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns = useMemo(
|
const columns = useMemo(
|
||||||
() => [
|
() => [
|
||||||
{ title: '用户名', dataIndex: 'username', key: 'username', render: (v: string) => <span style={{ fontFamily: 'monospace' }}>{v}</span> },
|
{ title: '姓名', dataIndex: 'name', key: 'name', width: 120 },
|
||||||
{ title: '姓名', dataIndex: 'name', key: 'name' },
|
{ title: '电话', dataIndex: 'phone', key: 'phone', width: 140, render: (v?: string) => v || '-' },
|
||||||
{ title: '邮箱', dataIndex: 'email', key: 'email', render: (v?: string) => v || '-' },
|
{ title: '工号', dataIndex: 'employeeNo', key: 'employeeNo', width: 130, render: (v?: string) => v || '-' },
|
||||||
|
{ title: '岗位', dataIndex: 'position', key: 'position', width: 140, render: (v?: string) => v || '-' },
|
||||||
{
|
{
|
||||||
title: '角色',
|
title: '角色',
|
||||||
dataIndex: 'role',
|
dataIndex: 'role',
|
||||||
key: 'role',
|
key: 'role',
|
||||||
|
width: 110,
|
||||||
render: (role: UserRole) => <Tag color={ROLE_COLOR[role]}>{ROLE_LABEL[role]}</Tag>,
|
render: (role: UserRole) => <Tag color={ROLE_COLOR[role]}>{ROLE_LABEL[role]}</Tag>,
|
||||||
},
|
},
|
||||||
{ title: '创建时间', dataIndex: 'createdAt', key: 'createdAt', render: (v: string) => new Date(v).toLocaleString('zh-CN') },
|
{
|
||||||
|
title: '员工码',
|
||||||
|
dataIndex: 'employeeSerials',
|
||||||
|
key: 'employeeSerials',
|
||||||
|
width: 190,
|
||||||
|
render: (serials?: EmployeeSerial[]) => renderEmployeeSerial(serials),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: '创建时间',
|
||||||
|
dataIndex: 'createdAt',
|
||||||
|
key: 'createdAt',
|
||||||
|
width: 170,
|
||||||
|
render: (v: string) => new Date(v).toLocaleString('zh-CN'),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
title: '操作',
|
title: '操作',
|
||||||
key: 'actions',
|
key: 'actions',
|
||||||
|
width: 240,
|
||||||
render: (_: unknown, record: User) => (
|
render: (_: unknown, record: User) => (
|
||||||
<Space wrap>
|
<Space wrap>
|
||||||
<Button type="link" size="small" icon={<IdcardOutlined />} onClick={() => openAssignCode(record)}>
|
|
||||||
赋码
|
|
||||||
</Button>
|
|
||||||
<Button type="link" size="small" onClick={() => openSerials(record)}>
|
|
||||||
赋码记录
|
|
||||||
</Button>
|
|
||||||
<Button type="link" size="small" icon={<EditOutlined />} onClick={() => openEdit(record)}>
|
<Button type="link" size="small" icon={<EditOutlined />} onClick={() => openEdit(record)}>
|
||||||
编辑
|
编辑
|
||||||
</Button>
|
</Button>
|
||||||
<Button type="link" size="small" icon={<KeyOutlined />} onClick={() => setResetPasswordUser(record)}>
|
{canLoginBackend(record.role) && (
|
||||||
重置密码
|
<Button type="link" size="small" icon={<KeyOutlined />} onClick={() => setResetPasswordEmployee(record)}>
|
||||||
</Button>
|
重置密码
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
{record.id !== currentUser?.id && (
|
{record.id !== currentUser?.id && (
|
||||||
<Button type="link" size="small" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record)}>
|
<Button type="link" size="small" danger icon={<DeleteOutlined />} onClick={() => handleDelete(record)}>
|
||||||
删除
|
删除
|
||||||
@@ -262,7 +256,7 @@ function EmployeeSerialsPage() {
|
|||||||
}
|
}
|
||||||
extra={
|
extra={
|
||||||
isAdmin && (
|
isAdmin && (
|
||||||
<Button type="primary" icon={<PlusOutlined />} onClick={() => setCreateVisible(true)}>
|
<Button type="primary" icon={<PlusOutlined />} onClick={openCreate}>
|
||||||
新建员工
|
新建员工
|
||||||
</Button>
|
</Button>
|
||||||
)
|
)
|
||||||
@@ -270,9 +264,9 @@ function EmployeeSerialsPage() {
|
|||||||
>
|
>
|
||||||
<Space style={{ marginBottom: 16 }}>
|
<Space style={{ marginBottom: 16 }}>
|
||||||
<Input.Search
|
<Input.Search
|
||||||
placeholder="搜索用户名/姓名/邮箱"
|
placeholder="搜索姓名/电话/工号/岗位"
|
||||||
allowClear
|
allowClear
|
||||||
style={{ width: 280 }}
|
style={{ width: 300 }}
|
||||||
onSearch={(v) => {
|
onSearch={(v) => {
|
||||||
setPage(1);
|
setPage(1);
|
||||||
setSearch(v);
|
setSearch(v);
|
||||||
@@ -287,17 +281,17 @@ function EmployeeSerialsPage() {
|
|||||||
<Select
|
<Select
|
||||||
placeholder="角色筛选"
|
placeholder="角色筛选"
|
||||||
allowClear
|
allowClear
|
||||||
style={{ width: 180 }}
|
style={{ width: 160 }}
|
||||||
value={roleFilter}
|
value={roleFilter}
|
||||||
onChange={(v) => {
|
onChange={(v) => {
|
||||||
setPage(1);
|
setPage(1);
|
||||||
setRoleFilter(v);
|
setRoleFilter(v);
|
||||||
}}
|
}}
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({ value: k, label: ROLE_LABEL[k] }))}
|
options={ROLE_OPTIONS}
|
||||||
/>
|
/>
|
||||||
</Space>
|
</Space>
|
||||||
|
|
||||||
<Table columns={columns} dataSource={users} rowKey="id" loading={loading} pagination={false} />
|
<Table columns={columns} dataSource={employees} rowKey="id" loading={loading} pagination={false} />
|
||||||
<div style={{ marginTop: 16 }}>
|
<div style={{ marginTop: 16 }}>
|
||||||
<Pagination
|
<Pagination
|
||||||
current={page}
|
current={page}
|
||||||
@@ -313,98 +307,134 @@ function EmployeeSerialsPage() {
|
|||||||
</div>
|
</div>
|
||||||
</Card>
|
</Card>
|
||||||
|
|
||||||
<Modal title="新建员工" open={createVisible} onCancel={() => setCreateVisible(false)} footer={null} width={480}>
|
<Modal
|
||||||
|
title="新建员工"
|
||||||
|
open={createVisible}
|
||||||
|
onCancel={() => setCreateVisible(false)}
|
||||||
|
footer={null}
|
||||||
|
width={520}
|
||||||
|
>
|
||||||
<Form form={createForm} layout="vertical" onFinish={handleCreate} initialValues={{ role: 'employee' }}>
|
<Form form={createForm} layout="vertical" onFinish={handleCreate} initialValues={{ role: 'employee' }}>
|
||||||
<Form.Item name="username" label="用户名" rules={[{ required: true, message: '请输入用户名' }, { min: 3, max: 50, message: '用户名长度 3-50' }]}>
|
<Form.Item name="name" label="姓名" rules={[{ required: true, message: '请输入姓名' }]}>
|
||||||
<Input />
|
<Input />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item name="password" label="初始密码" rules={[{ required: true, message: '请输入密码' }, { min: 6, message: '密码至少 6 位' }]}>
|
<Form.Item name="phone" label="电话" rules={[{ required: true, message: '请输入电话' }]}>
|
||||||
<Input.Password />
|
<Input />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
<Form.Item name="employeeNo" label="工号" rules={[{ required: true, message: '请输入工号' }]}>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="position" label="岗位" rules={[{ required: true, message: '请输入岗位' }]}>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="role" label="角色" rules={[{ required: true, message: '请选择角色' }]}>
|
||||||
|
<Select
|
||||||
|
options={ROLE_OPTIONS}
|
||||||
|
onChange={(role: UserRole) => {
|
||||||
|
if (!canLoginBackend(role)) {
|
||||||
|
createForm.setFieldValue('password', undefined);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Form.Item>
|
||||||
|
{canLoginBackend(createRole) && (
|
||||||
|
<Form.Item
|
||||||
|
name="password"
|
||||||
|
label="初始密码"
|
||||||
|
rules={[
|
||||||
|
{ required: true, message: '请输入密码' },
|
||||||
|
{ min: 6, message: '密码至少 6 位' },
|
||||||
|
]}
|
||||||
|
>
|
||||||
|
<Input.Password />
|
||||||
|
</Form.Item>
|
||||||
|
)}
|
||||||
|
<Form.Item>
|
||||||
|
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
||||||
|
<Button onClick={() => setCreateVisible(false)}>取消</Button>
|
||||||
|
<Button type="primary" htmlType="submit" loading={createLoading}>
|
||||||
|
创建
|
||||||
|
</Button>
|
||||||
|
</Space>
|
||||||
|
</Form.Item>
|
||||||
|
</Form>
|
||||||
|
</Modal>
|
||||||
|
|
||||||
|
<Modal
|
||||||
|
title={editingEmployee ? `编辑员工:${editingEmployee.name}` : '编辑员工'}
|
||||||
|
open={!!editingEmployee}
|
||||||
|
onCancel={() => setEditingEmployee(null)}
|
||||||
|
footer={null}
|
||||||
|
width={520}
|
||||||
|
>
|
||||||
|
<Form form={editForm} layout="vertical" onFinish={handleEdit}>
|
||||||
<Form.Item name="name" label="姓名" rules={[{ required: true, message: '请输入姓名' }]}>
|
<Form.Item name="name" label="姓名" rules={[{ required: true, message: '请输入姓名' }]}>
|
||||||
<Input />
|
<Input />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
|
<Form.Item name="phone" label="电话" rules={[{ required: true, message: '请输入电话' }]}>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="employeeNo" label="工号" rules={[{ required: true, message: '请输入工号' }]}>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
|
<Form.Item name="position" label="岗位" rules={[{ required: true, message: '请输入岗位' }]}>
|
||||||
|
<Input />
|
||||||
|
</Form.Item>
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
||||||
<Input />
|
<Input />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item name="role" label="角色" rules={[{ required: true, message: '请选择角色' }]}>
|
<Form.Item name="role" label="角色" rules={[{ required: true, message: '请选择角色' }]}>
|
||||||
<Select options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({ value: k, label: ROLE_LABEL[k] }))} />
|
<Select options={ROLE_OPTIONS} />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item>
|
<Form.Item>
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
||||||
<Button onClick={() => setCreateVisible(false)}>取消</Button>
|
<Button onClick={() => setEditingEmployee(null)}>取消</Button>
|
||||||
<Button type="primary" htmlType="submit" loading={createLoading}>创建</Button>
|
<Button type="primary" htmlType="submit" loading={editLoading}>
|
||||||
|
保存
|
||||||
|
</Button>
|
||||||
</Space>
|
</Space>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Form>
|
</Form>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<Modal title={editingUser ? `编辑员工:${editingUser.username}` : '编辑员工'} open={!!editingUser} onCancel={() => setEditingUser(null)} footer={null} width={480}>
|
<Modal
|
||||||
<Form form={editForm} layout="vertical" onFinish={handleEdit}>
|
title={resetPasswordEmployee ? `重置密码:${resetPasswordEmployee.name}` : '重置密码'}
|
||||||
<Form.Item name="name" label="姓名"><Input /></Form.Item>
|
open={!!resetPasswordEmployee}
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}><Input /></Form.Item>
|
onCancel={() => {
|
||||||
<Form.Item name="role" label="角色">
|
setResetPasswordEmployee(null);
|
||||||
<Select options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({ value: k, label: ROLE_LABEL[k] }))} />
|
resetForm.resetFields();
|
||||||
</Form.Item>
|
}}
|
||||||
<Form.Item>
|
footer={null}
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
width={420}
|
||||||
<Button onClick={() => setEditingUser(null)}>取消</Button>
|
>
|
||||||
<Button type="primary" htmlType="submit" loading={editLoading}>保存</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal title={resetPasswordUser ? `重置密码:${resetPasswordUser.username}` : '重置密码'} open={!!resetPasswordUser} onCancel={() => setResetPasswordUser(null)} footer={null} width={420}>
|
|
||||||
<Form form={resetForm} layout="vertical" onFinish={handleResetPassword}>
|
<Form form={resetForm} layout="vertical" onFinish={handleResetPassword}>
|
||||||
<Form.Item name="newPassword" label="新密码" rules={[{ required: true, message: '请输入新密码' }, { min: 6, message: '密码至少 6 位' }]}>
|
<Form.Item
|
||||||
|
name="newPassword"
|
||||||
|
label="新密码"
|
||||||
|
rules={[
|
||||||
|
{ required: true, message: '请输入新密码' },
|
||||||
|
{ min: 6, message: '密码至少 6 位' },
|
||||||
|
]}
|
||||||
|
>
|
||||||
<Input.Password />
|
<Input.Password />
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
<Form.Item>
|
<Form.Item>
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
||||||
<Button onClick={() => setResetPasswordUser(null)}>取消</Button>
|
<Button
|
||||||
<Button type="primary" htmlType="submit" loading={resetLoading}>确认重置</Button>
|
onClick={() => {
|
||||||
|
setResetPasswordEmployee(null);
|
||||||
|
resetForm.resetFields();
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
取消
|
||||||
|
</Button>
|
||||||
|
<Button type="primary" htmlType="submit" loading={resetLoading}>
|
||||||
|
确认重置
|
||||||
|
</Button>
|
||||||
</Space>
|
</Space>
|
||||||
</Form.Item>
|
</Form.Item>
|
||||||
</Form>
|
</Form>
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<Modal title={assignUser ? `给 ${assignUser.name} 赋码` : '员工赋码'} open={!!assignUser} onCancel={() => setAssignUser(null)} footer={null} width={500}>
|
|
||||||
<Form form={assignForm} layout="vertical" onFinish={handleAssignCode}>
|
|
||||||
<Form.Item name="companyName" label="企业名称" rules={[{ required: true, message: '请输入企业名称' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="position" label="职位" rules={[{ required: true, message: '请输入职位' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="quantity" label="生成数量" initialValue={1} rules={[{ required: true, message: '请输入生成数量' }]}>
|
|
||||||
<InputNumber min={1} max={1000} style={{ width: '100%' }} />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button onClick={() => setAssignUser(null)}>取消</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={assignLoading}>确认赋码</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal title="员工赋码记录" open={serialsVisible} onCancel={() => setSerialsVisible(false)} footer={null} width={900}>
|
|
||||||
<Table
|
|
||||||
rowKey="serialNumber"
|
|
||||||
loading={serialsLoading}
|
|
||||||
dataSource={serials}
|
|
||||||
pagination={false}
|
|
||||||
columns={[
|
|
||||||
{ title: '序列号', dataIndex: 'serialNumber', key: 'serialNumber' },
|
|
||||||
{ title: '企业名称', dataIndex: 'companyName', key: 'companyName' },
|
|
||||||
{ title: '职位', dataIndex: 'position', key: 'position' },
|
|
||||||
{ title: '员工姓名', dataIndex: 'employeeName', key: 'employeeName' },
|
|
||||||
{ title: '状态', dataIndex: 'isActive', key: 'isActive', render: (v: boolean) => <Tag color={v ? 'green' : 'red'}>{v ? '有效' : '已吊销'}</Tag> },
|
|
||||||
{ title: '创建时间', dataIndex: 'createdAt', key: 'createdAt', render: (v: string) => new Date(v).toLocaleString('zh-CN') },
|
|
||||||
]}
|
|
||||||
/>
|
|
||||||
</Modal>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -15,7 +15,7 @@ function LoginPage() {
|
|||||||
const handleLogin = async (values: { username: string; password: string; remember?: boolean }) => {
|
const handleLogin = async (values: { username: string; password: string; remember?: boolean }) => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
try {
|
try {
|
||||||
await authApi.login(values.username, values.password);
|
const user = await authApi.login(values.username, values.password);
|
||||||
|
|
||||||
if (values.remember) {
|
if (values.remember) {
|
||||||
localStorage.setItem('rememberedUsername', values.username);
|
localStorage.setItem('rememberedUsername', values.username);
|
||||||
@@ -25,7 +25,7 @@ function LoginPage() {
|
|||||||
|
|
||||||
message.success('登录成功!');
|
message.success('登录成功!');
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
navigate('/admin/dashboard');
|
navigate(user.role === 'technician' ? '/admin/aftersales' : '/admin/dashboard');
|
||||||
}, 500);
|
}, 500);
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
message.error(error.message || '登录失败,请重试');
|
message.error(error.message || '登录失败,请重试');
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ import './styles/PublicQuery.css';
|
|||||||
interface EmployeeSerialResult {
|
interface EmployeeSerialResult {
|
||||||
serialNumber: string;
|
serialNumber: string;
|
||||||
companyName: string;
|
companyName: string;
|
||||||
department: string;
|
position: string;
|
||||||
employeeName: string;
|
employeeName: string;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
@@ -164,11 +164,11 @@ function PublicQueryPage() {
|
|||||||
<span className="label">企业名称</span>
|
<span className="label">企业名称</span>
|
||||||
<span className="value">{result.companyName}</span>
|
<span className="value">{result.companyName}</span>
|
||||||
</div>
|
</div>
|
||||||
{serialType === 'employee' && (result as EmployeeSerialResult).department && (
|
{serialType === 'employee' && (result as EmployeeSerialResult).position && (
|
||||||
<>
|
<>
|
||||||
<div className="detail-item">
|
<div className="detail-item">
|
||||||
<span className="label">职位</span>
|
<span className="label">职位</span>
|
||||||
<span className="value">{(result as EmployeeSerialResult).department}</span>
|
<span className="value">{(result as EmployeeSerialResult).position}</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="detail-item">
|
<div className="detail-item">
|
||||||
<span className="label">员工姓名</span>
|
<span className="label">员工姓名</span>
|
||||||
|
|||||||
@@ -1,433 +0,0 @@
|
|||||||
import { useEffect, useState } from 'react';
|
|
||||||
import {
|
|
||||||
Card,
|
|
||||||
Table,
|
|
||||||
Button,
|
|
||||||
Space,
|
|
||||||
Input,
|
|
||||||
Select,
|
|
||||||
Tag,
|
|
||||||
Modal,
|
|
||||||
Form,
|
|
||||||
message,
|
|
||||||
Pagination,
|
|
||||||
} from 'antd';
|
|
||||||
import {
|
|
||||||
UserOutlined,
|
|
||||||
PlusOutlined,
|
|
||||||
EditOutlined,
|
|
||||||
DeleteOutlined,
|
|
||||||
KeyOutlined,
|
|
||||||
} from '@ant-design/icons';
|
|
||||||
import { usersApi, authApi } from '@/services/api';
|
|
||||||
import type { User, UserRole, CreateUserRequest, UpdateUserRequest } from '@/types';
|
|
||||||
|
|
||||||
const ROLE_LABEL: Record<UserRole, string> = {
|
|
||||||
admin: '管理员',
|
|
||||||
technician: '技术员',
|
|
||||||
employee: '员工(不可登录后台)',
|
|
||||||
user: '普通用户',
|
|
||||||
};
|
|
||||||
|
|
||||||
const ROLE_COLOR: Record<UserRole, string> = {
|
|
||||||
admin: 'red',
|
|
||||||
technician: 'blue',
|
|
||||||
employee: 'green',
|
|
||||||
user: 'default',
|
|
||||||
};
|
|
||||||
|
|
||||||
function UsersPage() {
|
|
||||||
const currentUser = authApi.getCurrentUser();
|
|
||||||
|
|
||||||
const [users, setUsers] = useState<User[]>([]);
|
|
||||||
const [loading, setLoading] = useState(true);
|
|
||||||
const [page, setPage] = useState(1);
|
|
||||||
const [limit, setLimit] = useState(10);
|
|
||||||
const [total, setTotal] = useState(0);
|
|
||||||
const [search, setSearch] = useState('');
|
|
||||||
const [roleFilter, setRoleFilter] = useState<UserRole | undefined>();
|
|
||||||
|
|
||||||
const [createVisible, setCreateVisible] = useState(false);
|
|
||||||
const [createLoading, setCreateLoading] = useState(false);
|
|
||||||
const [createForm] = Form.useForm<CreateUserRequest>();
|
|
||||||
|
|
||||||
const [editingUser, setEditingUser] = useState<User | null>(null);
|
|
||||||
const [editLoading, setEditLoading] = useState(false);
|
|
||||||
const [editForm] = Form.useForm<UpdateUserRequest>();
|
|
||||||
|
|
||||||
const [resetPasswordUser, setResetPasswordUser] = useState<User | null>(null);
|
|
||||||
const [resetLoading, setResetLoading] = useState(false);
|
|
||||||
const [resetForm] = Form.useForm<{ newPassword: string }>();
|
|
||||||
|
|
||||||
const loadUsers = async () => {
|
|
||||||
setLoading(true);
|
|
||||||
try {
|
|
||||||
const result = await usersApi.list({
|
|
||||||
page,
|
|
||||||
limit,
|
|
||||||
search: search || undefined,
|
|
||||||
role: roleFilter,
|
|
||||||
});
|
|
||||||
setUsers(result.data || []);
|
|
||||||
setTotal(result.pagination?.total || 0);
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '加载用户列表失败');
|
|
||||||
setUsers([]);
|
|
||||||
} finally {
|
|
||||||
setLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
loadUsers();
|
|
||||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
||||||
}, [page, limit, search, roleFilter]);
|
|
||||||
|
|
||||||
const handleCreate = async (values: CreateUserRequest) => {
|
|
||||||
setCreateLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.create(values);
|
|
||||||
message.success('用户创建成功');
|
|
||||||
setCreateVisible(false);
|
|
||||||
createForm.resetFields();
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '创建失败');
|
|
||||||
} finally {
|
|
||||||
setCreateLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const openEdit = (user: User) => {
|
|
||||||
setEditingUser(user);
|
|
||||||
editForm.setFieldsValue({
|
|
||||||
name: user.name,
|
|
||||||
email: user.email,
|
|
||||||
role: user.role,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleEdit = async (values: UpdateUserRequest) => {
|
|
||||||
if (!editingUser) return;
|
|
||||||
setEditLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.update(editingUser.id, values);
|
|
||||||
message.success('用户更新成功');
|
|
||||||
setEditingUser(null);
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '更新失败');
|
|
||||||
} finally {
|
|
||||||
setEditLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleResetPassword = async (values: { newPassword: string }) => {
|
|
||||||
if (!resetPasswordUser) return;
|
|
||||||
setResetLoading(true);
|
|
||||||
try {
|
|
||||||
await usersApi.resetPassword(resetPasswordUser.id, values.newPassword);
|
|
||||||
message.success('密码重置成功');
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '重置失败');
|
|
||||||
} finally {
|
|
||||||
setResetLoading(false);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleDelete = (user: User) => {
|
|
||||||
Modal.confirm({
|
|
||||||
title: '确认删除',
|
|
||||||
content: `确定要删除用户 "${user.username}" 吗?此操作不可恢复!`,
|
|
||||||
okText: '确定',
|
|
||||||
okType: 'danger',
|
|
||||||
cancelText: '取消',
|
|
||||||
onOk: async () => {
|
|
||||||
try {
|
|
||||||
await usersApi.delete(user.id);
|
|
||||||
message.success('删除成功');
|
|
||||||
loadUsers();
|
|
||||||
} catch (err: any) {
|
|
||||||
message.error(err?.response?.data?.message || err.message || '删除失败');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const columns = [
|
|
||||||
{
|
|
||||||
title: '用户名',
|
|
||||||
dataIndex: 'username',
|
|
||||||
key: 'username',
|
|
||||||
render: (text: string) => <span style={{ fontFamily: 'monospace' }}>{text}</span>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '姓名',
|
|
||||||
dataIndex: 'name',
|
|
||||||
key: 'name',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '邮箱',
|
|
||||||
dataIndex: 'email',
|
|
||||||
key: 'email',
|
|
||||||
render: (text?: string) => text || '-',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '角色',
|
|
||||||
dataIndex: 'role',
|
|
||||||
key: 'role',
|
|
||||||
width: 100,
|
|
||||||
render: (role: UserRole) => <Tag color={ROLE_COLOR[role]}>{ROLE_LABEL[role]}</Tag>,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '创建时间',
|
|
||||||
dataIndex: 'createdAt',
|
|
||||||
key: 'createdAt',
|
|
||||||
width: 170,
|
|
||||||
render: (date: string) => new Date(date).toLocaleString('zh-CN'),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
title: '操作',
|
|
||||||
key: 'actions',
|
|
||||||
width: 260,
|
|
||||||
render: (_: any, record: User) => (
|
|
||||||
<Space>
|
|
||||||
<Button
|
|
||||||
type="link"
|
|
||||||
size="small"
|
|
||||||
icon={<EditOutlined />}
|
|
||||||
onClick={() => openEdit(record)}
|
|
||||||
>
|
|
||||||
编辑
|
|
||||||
</Button>
|
|
||||||
<Button
|
|
||||||
type="link"
|
|
||||||
size="small"
|
|
||||||
icon={<KeyOutlined />}
|
|
||||||
onClick={() => setResetPasswordUser(record)}
|
|
||||||
>
|
|
||||||
重置密码
|
|
||||||
</Button>
|
|
||||||
{record.id !== currentUser?.id && (
|
|
||||||
<Button
|
|
||||||
type="link"
|
|
||||||
size="small"
|
|
||||||
danger
|
|
||||||
icon={<DeleteOutlined />}
|
|
||||||
onClick={() => handleDelete(record)}
|
|
||||||
>
|
|
||||||
删除
|
|
||||||
</Button>
|
|
||||||
)}
|
|
||||||
</Space>
|
|
||||||
),
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div>
|
|
||||||
<Card
|
|
||||||
title={
|
|
||||||
<Space>
|
|
||||||
<UserOutlined />
|
|
||||||
<span>用户管理</span>
|
|
||||||
</Space>
|
|
||||||
}
|
|
||||||
extra={
|
|
||||||
<Button
|
|
||||||
type="primary"
|
|
||||||
icon={<PlusOutlined />}
|
|
||||||
onClick={() => setCreateVisible(true)}
|
|
||||||
>
|
|
||||||
新建用户
|
|
||||||
</Button>
|
|
||||||
}
|
|
||||||
>
|
|
||||||
<Space style={{ marginBottom: 16 }}>
|
|
||||||
<Input.Search
|
|
||||||
placeholder="搜索用户名/姓名/邮箱"
|
|
||||||
allowClear
|
|
||||||
style={{ width: 260 }}
|
|
||||||
onSearch={(v) => {
|
|
||||||
setPage(1);
|
|
||||||
setSearch(v);
|
|
||||||
}}
|
|
||||||
onChange={(e) => {
|
|
||||||
if (!e.target.value) {
|
|
||||||
setPage(1);
|
|
||||||
setSearch('');
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<Select
|
|
||||||
placeholder="角色筛选"
|
|
||||||
allowClear
|
|
||||||
style={{ width: 140 }}
|
|
||||||
value={roleFilter}
|
|
||||||
onChange={(v) => {
|
|
||||||
setPage(1);
|
|
||||||
setRoleFilter(v);
|
|
||||||
}}
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Space>
|
|
||||||
|
|
||||||
<Table
|
|
||||||
columns={columns}
|
|
||||||
dataSource={users}
|
|
||||||
rowKey="id"
|
|
||||||
loading={loading}
|
|
||||||
pagination={false}
|
|
||||||
/>
|
|
||||||
<div style={{ marginTop: 16 }}>
|
|
||||||
<Pagination
|
|
||||||
current={page}
|
|
||||||
pageSize={limit}
|
|
||||||
total={total}
|
|
||||||
onChange={(newPage, newLimit) => {
|
|
||||||
setPage(newPage);
|
|
||||||
setLimit(newLimit);
|
|
||||||
}}
|
|
||||||
showSizeChanger
|
|
||||||
showTotal={(t) => `共计 ${t} 条记录`}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</Card>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title="新建用户"
|
|
||||||
open={createVisible}
|
|
||||||
onCancel={() => {
|
|
||||||
setCreateVisible(false);
|
|
||||||
createForm.resetFields();
|
|
||||||
}}
|
|
||||||
footer={null}
|
|
||||||
width={480}
|
|
||||||
>
|
|
||||||
<Form form={createForm} layout="vertical" onFinish={handleCreate} initialValues={{ role: 'technician' }}>
|
|
||||||
<Form.Item
|
|
||||||
name="username"
|
|
||||||
label="用户名"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入用户名' },
|
|
||||||
{ min: 3, max: 50, message: '用户名长度 3-50' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input placeholder="登录用用户名" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item
|
|
||||||
name="password"
|
|
||||||
label="初始密码"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入密码' },
|
|
||||||
{ min: 6, message: '密码至少 6 位' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input.Password placeholder="至少 6 位" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="name" label="姓名" rules={[{ required: true, message: '请输入姓名' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="role" label="角色" rules={[{ required: true, message: '请选择角色' }]}>
|
|
||||||
<Select
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button onClick={() => setCreateVisible(false)}>取消</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={createLoading}>
|
|
||||||
创建
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title={editingUser ? `编辑用户:${editingUser.username}` : '编辑用户'}
|
|
||||||
open={!!editingUser}
|
|
||||||
onCancel={() => setEditingUser(null)}
|
|
||||||
footer={null}
|
|
||||||
width={480}
|
|
||||||
>
|
|
||||||
<Form form={editForm} layout="vertical" onFinish={handleEdit}>
|
|
||||||
<Form.Item name="name" label="姓名">
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="email" label="邮箱" rules={[{ type: 'email', message: '请输入有效邮箱' }]}>
|
|
||||||
<Input />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item name="role" label="角色">
|
|
||||||
<Select
|
|
||||||
options={(Object.keys(ROLE_LABEL) as UserRole[]).map((k) => ({
|
|
||||||
value: k,
|
|
||||||
label: ROLE_LABEL[k],
|
|
||||||
}))}
|
|
||||||
/>
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button onClick={() => setEditingUser(null)}>取消</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={editLoading}>
|
|
||||||
保存
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
|
|
||||||
<Modal
|
|
||||||
title={resetPasswordUser ? `重置密码:${resetPasswordUser.username}` : '重置密码'}
|
|
||||||
open={!!resetPasswordUser}
|
|
||||||
onCancel={() => {
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
}}
|
|
||||||
footer={null}
|
|
||||||
width={420}
|
|
||||||
>
|
|
||||||
<Form form={resetForm} layout="vertical" onFinish={handleResetPassword}>
|
|
||||||
<Form.Item
|
|
||||||
name="newPassword"
|
|
||||||
label="新密码"
|
|
||||||
rules={[
|
|
||||||
{ required: true, message: '请输入新密码' },
|
|
||||||
{ min: 6, message: '密码至少 6 位' },
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
<Input.Password placeholder="至少 6 位" />
|
|
||||||
</Form.Item>
|
|
||||||
<Form.Item>
|
|
||||||
<Space style={{ width: '100%', justifyContent: 'flex-end' }}>
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
setResetPasswordUser(null);
|
|
||||||
resetForm.resetFields();
|
|
||||||
}}
|
|
||||||
>
|
|
||||||
取消
|
|
||||||
</Button>
|
|
||||||
<Button type="primary" htmlType="submit" loading={resetLoading}>
|
|
||||||
重置
|
|
||||||
</Button>
|
|
||||||
</Space>
|
|
||||||
</Form.Item>
|
|
||||||
</Form>
|
|
||||||
</Modal>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export default UsersPage;
|
|
||||||
+19
-17
@@ -445,6 +445,13 @@ export const aftersalesApi = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const usersApi = {
|
export const usersApi = {
|
||||||
|
assignable: async () => {
|
||||||
|
const response = await apiClient.get('/users/assignable');
|
||||||
|
return (response.data?.data || []) as User[];
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const employeesApi = {
|
||||||
list: async (filter?: UserListFilter) => {
|
list: async (filter?: UserListFilter) => {
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (filter?.page && filter.page > 1) params.append('page', String(filter.page));
|
if (filter?.page && filter.page > 1) params.append('page', String(filter.page));
|
||||||
@@ -452,29 +459,29 @@ export const usersApi = {
|
|||||||
if (filter?.role) params.append('role', filter.role);
|
if (filter?.role) params.append('role', filter.role);
|
||||||
if (filter?.search) params.append('search', filter.search);
|
if (filter?.search) params.append('search', filter.search);
|
||||||
|
|
||||||
const url = params.toString() ? `/users?${params.toString()}` : '/users';
|
const url = params.toString() ? `/employees?${params.toString()}` : '/employees';
|
||||||
const response = await apiClient.get(url);
|
const response = await apiClient.get(url);
|
||||||
return response.data as UserListResponse;
|
return response.data as UserListResponse;
|
||||||
},
|
},
|
||||||
|
|
||||||
create: async (data: CreateUserRequest) => {
|
create: async (data: CreateUserRequest) => {
|
||||||
const response = await apiClient.post('/users', data);
|
const response = await apiClient.post('/employees', data);
|
||||||
if (response.data.user) {
|
if (response.data.employee) {
|
||||||
return response.data.user as User;
|
return response.data.employee as User;
|
||||||
}
|
}
|
||||||
throw new Error(response.data.error || '创建用户失败');
|
throw new Error(response.data.error || '创建员工失败');
|
||||||
},
|
},
|
||||||
|
|
||||||
update: async (id: number, data: UpdateUserRequest) => {
|
update: async (id: number, data: UpdateUserRequest) => {
|
||||||
const response = await apiClient.patch(`/users/${id}`, data);
|
const response = await apiClient.patch(`/employees/${id}`, data);
|
||||||
if (response.data.user) {
|
if (response.data.employee) {
|
||||||
return response.data.user as User;
|
return response.data.employee as User;
|
||||||
}
|
}
|
||||||
throw new Error(response.data.error || '更新用户失败');
|
throw new Error(response.data.error || '更新员工失败');
|
||||||
},
|
},
|
||||||
|
|
||||||
resetPassword: async (id: number, newPassword: string) => {
|
resetPassword: async (id: number, newPassword: string) => {
|
||||||
const response = await apiClient.post(`/users/${id}/reset-password`, { newPassword });
|
const response = await apiClient.post(`/employees/${id}/reset-password`, { newPassword });
|
||||||
if (response.data.message) {
|
if (response.data.message) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -482,15 +489,10 @@ export const usersApi = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
delete: async (id: number) => {
|
delete: async (id: number) => {
|
||||||
const response = await apiClient.delete(`/users/${id}`);
|
const response = await apiClient.delete(`/employees/${id}`);
|
||||||
if (response.data.message) {
|
if (response.data.message) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
throw new Error(response.data.error || '删除用户失败');
|
throw new Error(response.data.error || '删除员工失败');
|
||||||
},
|
|
||||||
|
|
||||||
assignable: async () => {
|
|
||||||
const response = await apiClient.get('/users/assignable');
|
|
||||||
return (response.data?.data || []) as User[];
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
+15
-3
@@ -1,25 +1,35 @@
|
|||||||
export type UserRole = 'admin' | 'technician' | 'employee' | 'user';
|
export type UserRole = 'admin' | 'technician' | 'employee';
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
id: number;
|
id: number;
|
||||||
username: string;
|
username: string;
|
||||||
name: string;
|
name: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
phone?: string;
|
||||||
|
employeeNo?: string;
|
||||||
|
position?: string;
|
||||||
role: UserRole;
|
role: UserRole;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
|
employeeSerials?: EmployeeSerial[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface CreateUserRequest {
|
export interface CreateUserRequest {
|
||||||
username: string;
|
username?: string;
|
||||||
password: string;
|
password?: string;
|
||||||
name: string;
|
name: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
phone: string;
|
||||||
|
employeeNo: string;
|
||||||
|
position: string;
|
||||||
role: UserRole;
|
role: UserRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateUserRequest {
|
export interface UpdateUserRequest {
|
||||||
name?: string;
|
name?: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
|
phone?: string;
|
||||||
|
employeeNo?: string;
|
||||||
|
position?: string;
|
||||||
role?: UserRole;
|
role?: UserRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,10 +145,12 @@ export interface CompanyFilter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface EmployeeSerial {
|
export interface EmployeeSerial {
|
||||||
|
id?: number;
|
||||||
serialNumber: string;
|
serialNumber: string;
|
||||||
companyName: string;
|
companyName: string;
|
||||||
position: string;
|
position: string;
|
||||||
employeeName: string;
|
employeeName: string;
|
||||||
|
employeeId?: number;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt?: string;
|
updatedAt?: string;
|
||||||
|
|||||||
Reference in New Issue
Block a user