fed8ff3d3a
HQ 列表主操作居右、列设置贴最右侧;订单筛选状态可多选,导出同步过滤。 Co-authored-by: Cursor <cursoragent@cursor.com>
287 lines
11 KiB
TypeScript
287 lines
11 KiB
TypeScript
import { useEffect, useState } from 'react';
|
|
import {
|
|
Button, Drawer, Form, Input, Modal, Radio, Select, Space, Table, Tag, Typography, message,
|
|
} from 'antd';
|
|
import type { ColumnsType } from 'antd/es/table';
|
|
import { HQ_ADMIN_ROLES } from '@dukang/shared-types';
|
|
import { request, type HqProfile, type Paginated } from '../lib/api';
|
|
import { ACCOUNT_STATUS_LABELS, ADMIN_OPTIONS_PAGE_SIZE, fmtTime } from '../lib/constants';
|
|
import { useAdminList } from '../lib/useAdminList';
|
|
import { useAdminListColumns } from '../lib/useAdminListColumns';
|
|
import { AdminListHeader } from '../components/AdminListHeader';
|
|
import { AdminPrimaryLink } from '../components/AdminPrimaryLink';
|
|
|
|
|
|
type Row = {
|
|
id: string;
|
|
phone: string;
|
|
loginName: string | null;
|
|
hasPassword: boolean;
|
|
name: string;
|
|
adminRole: string;
|
|
status: string;
|
|
lastLoginAt: string | null;
|
|
createdAt: string;
|
|
cityIds?: string[];
|
|
};
|
|
|
|
type CityOption = { id: string; name: string };
|
|
|
|
const ROLE_OPTIONS = HQ_ADMIN_ROLES.map((r) => ({ value: r.value, label: r.label }));
|
|
const ROLE_LABELS = Object.fromEntries(HQ_ADMIN_ROLES.map((r) => [r.value, r.label]));
|
|
|
|
export default function HqAccountsPage() {
|
|
const [profile, setProfile] = useState<HqProfile | null>(null);
|
|
const [form] = Form.useForm();
|
|
const [editForm] = Form.useForm();
|
|
const [createForm] = Form.useForm();
|
|
const [filters, setFilters] = useState<Record<string, string>>({});
|
|
const { data, loading, page, pageSize, setPage, setPageSize, reload } = useAdminList<Row>(
|
|
'/admin/hq-accounts',
|
|
() => {
|
|
const qs = new URLSearchParams();
|
|
if (filters.phone) qs.set('phone', filters.phone);
|
|
if (filters.adminRole) qs.set('adminRole', filters.adminRole);
|
|
if (filters.status) qs.set('status', filters.status);
|
|
return qs;
|
|
},
|
|
[filters],
|
|
);
|
|
const [detail, setDetail] = useState<Row | null>(null);
|
|
const [drawerOpen, setDrawerOpen] = useState(false);
|
|
const [createOpen, setCreateOpen] = useState(false);
|
|
const [saving, setSaving] = useState(false);
|
|
const [cities, setCities] = useState<CityOption[]>([]);
|
|
const credentialType = Form.useWatch('credentialType', createForm) ?? 'phone';
|
|
const editRole = Form.useWatch('adminRole', editForm);
|
|
const createRole = Form.useWatch('adminRole', createForm);
|
|
const isSuperAdmin = profile?.adminRole === 'SUPER_ADMIN';
|
|
|
|
useEffect(() => {
|
|
request<HqProfile>('/admin/auth/me').then(setProfile).catch(() => {});
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (!isSuperAdmin) return;
|
|
request<Paginated<CityOption>>(`/admin/cities?pageSize=${ADMIN_OPTIONS_PAGE_SIZE}`)
|
|
.then((res) => setCities(res.items))
|
|
.catch(() => {});
|
|
}, [isSuperAdmin]);
|
|
|
|
const cityOptions = cities.map((c) => ({ value: c.id, label: c.name }));
|
|
|
|
const baseColumns: ColumnsType<Row> = [
|
|
{
|
|
title: '姓名',
|
|
dataIndex: 'name',
|
|
render: (v, row) =>
|
|
isSuperAdmin ? (
|
|
<AdminPrimaryLink
|
|
onClick={() => {
|
|
setDetail(row);
|
|
editForm.setFieldsValue({
|
|
name: row.name,
|
|
phone: row.phone,
|
|
loginName: row.loginName,
|
|
adminRole: row.adminRole,
|
|
status: row.status,
|
|
cityIds: row.cityIds ?? [],
|
|
});
|
|
setDrawerOpen(true);
|
|
}}
|
|
>
|
|
{v}
|
|
</AdminPrimaryLink>
|
|
) : (
|
|
v
|
|
),
|
|
},
|
|
{ title: '用户名', dataIndex: 'loginName', width: 120, render: (v) => v || '—' },
|
|
{ title: '手机', dataIndex: 'phone', width: 130 },
|
|
{
|
|
title: '登录方式',
|
|
width: 110,
|
|
render: (_, r) => (
|
|
<Space size={4}>
|
|
{r.hasPassword ? <Tag color="blue">密码</Tag> : null}
|
|
<Tag>短信</Tag>
|
|
</Space>
|
|
),
|
|
},
|
|
{ title: '角色', dataIndex: 'adminRole', width: 130, render: (r) => ROLE_LABELS[r] || r },
|
|
{
|
|
title: '城市',
|
|
width: 160,
|
|
render: (_, r) => {
|
|
if (!r.cityIds?.length) return <Typography.Text type="secondary">全国</Typography.Text>;
|
|
const names = r.cityIds
|
|
.map((id) => cities.find((c) => c.id === id)?.name || id)
|
|
.join('、');
|
|
return names;
|
|
},
|
|
},
|
|
{ title: '状态', dataIndex: 'status', width: 90, render: (s) => <Tag>{ACCOUNT_STATUS_LABELS[s] || s}</Tag> },
|
|
{ title: '最后登录', dataIndex: 'lastLoginAt', width: 160, render: fmtTime },
|
|
{
|
|
title: '操作', width: 80,
|
|
render: (_, row) => (
|
|
<Button type="link" size="small" disabled={!isSuperAdmin} onClick={() => {
|
|
setDetail(row);
|
|
editForm.setFieldsValue({
|
|
name: row.name,
|
|
phone: row.phone,
|
|
loginName: row.loginName,
|
|
adminRole: row.adminRole,
|
|
status: row.status,
|
|
cityIds: row.cityIds ?? [],
|
|
});
|
|
setDrawerOpen(true);
|
|
}}>编辑</Button>
|
|
),
|
|
},
|
|
];
|
|
|
|
const { columns, settingsButton, settingsModal } = useAdminListColumns('hq-accounts', baseColumns, { page, pageSize });
|
|
|
|
return (
|
|
<div>
|
|
{settingsModal}
|
|
<AdminListHeader
|
|
title="HQ 账户"
|
|
settings={settingsButton}
|
|
actions={
|
|
isSuperAdmin ? (
|
|
<Button
|
|
type="primary"
|
|
onClick={() => {
|
|
createForm.resetFields();
|
|
createForm.setFieldsValue({ credentialType: 'phone', adminRole: 'OPS', cityIds: [] });
|
|
setCreateOpen(true);
|
|
}}
|
|
>
|
|
新建账户
|
|
</Button>
|
|
) : null
|
|
}
|
|
/>
|
|
<Form form={form} layout="inline" style={{ marginBottom: 16 }} onFinish={(v) => { setFilters(v); setPage(1); }}>
|
|
<Form.Item name="phone" label="手机"><Input allowClear /></Form.Item>
|
|
<Form.Item name="adminRole" label="角色">
|
|
<Select allowClear style={{ width: 160 }} options={ROLE_OPTIONS} />
|
|
</Form.Item>
|
|
<Form.Item><Button type="primary" htmlType="submit">查询</Button></Form.Item>
|
|
</Form>
|
|
<Table scroll={{ x: 'max-content' }} rowKey="id" loading={loading} columns={columns} dataSource={data?.items ?? []}
|
|
pagination={{ current: page, pageSize, total: data?.total ?? 0, showSizeChanger: true, onChange: (p, ps) => { setPage(p); setPageSize(ps); } }} />
|
|
<Drawer title="编辑 HQ 账户" width={460} open={drawerOpen} onClose={() => setDrawerOpen(false)}
|
|
extra={
|
|
<Button type="primary" loading={saving} onClick={async () => {
|
|
if (!detail) return;
|
|
try {
|
|
const v = await editForm.validateFields();
|
|
const payload: Record<string, unknown> = { ...v };
|
|
if (!payload.password) delete payload.password;
|
|
if (!String(payload.loginName ?? '').trim()) delete payload.loginName;
|
|
setSaving(true);
|
|
await request(`/admin/hq-accounts/${detail.id}`, { method: 'PUT', body: JSON.stringify(payload) });
|
|
message.success('已保存');
|
|
setDrawerOpen(false);
|
|
void reload();
|
|
} catch (e) {
|
|
if (e instanceof Error && e.message) message.error(e.message);
|
|
} finally {
|
|
setSaving(false);
|
|
}
|
|
}}>保存</Button>
|
|
}>
|
|
<Form form={editForm} layout="vertical">
|
|
<Form.Item name="name" label="姓名" rules={[{ required: true }]}><Input /></Form.Item>
|
|
<Form.Item
|
|
name="phone"
|
|
label="手机号"
|
|
rules={[
|
|
{ required: true, message: '请输入手机号' },
|
|
{ pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' },
|
|
]}
|
|
>
|
|
<Input maxLength={11} />
|
|
</Form.Item>
|
|
<Form.Item name="loginName" label="用户名"><Input placeholder="用于密码登录" /></Form.Item>
|
|
<Form.Item name="password" label="新密码" extra="留空则不修改">
|
|
<Input.Password placeholder="至少 6 位" />
|
|
</Form.Item>
|
|
<Form.Item name="adminRole" label="角色">
|
|
<Select options={ROLE_OPTIONS} />
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="cityIds"
|
|
label="负责城市"
|
|
extra={editRole === 'CITY_STORE_SERVICE' ? '城市门店服务必须至少选一个城市' : '不选表示全国可见'}
|
|
rules={editRole === 'CITY_STORE_SERVICE' ? [{ required: true, type: 'array', min: 1, message: '请至少勾选一个城市' }] : []}
|
|
>
|
|
<Select mode="multiple" allowClear showSearch optionFilterProp="label" placeholder="不选=全国" options={cityOptions} />
|
|
</Form.Item>
|
|
<Form.Item name="status" label="状态">
|
|
<Select options={Object.entries(ACCOUNT_STATUS_LABELS).map(([value, label]) => ({ value, label }))} />
|
|
</Form.Item>
|
|
</Form>
|
|
</Drawer>
|
|
<Modal
|
|
title="新建 HQ 账户"
|
|
open={createOpen}
|
|
onCancel={() => setCreateOpen(false)}
|
|
onOk={async () => {
|
|
try {
|
|
const v = await createForm.validateFields();
|
|
await request('/admin/hq-accounts', { method: 'POST', body: JSON.stringify(v) });
|
|
message.success('已创建');
|
|
setCreateOpen(false);
|
|
createForm.resetFields();
|
|
void reload();
|
|
} catch (e) {
|
|
if (e instanceof Error && e.message) message.error(e.message);
|
|
}
|
|
}}
|
|
>
|
|
<Form form={createForm} layout="vertical" initialValues={{ credentialType: 'phone', adminRole: 'OPS', cityIds: [] }}>
|
|
<Form.Item name="credentialType" label="创建方式">
|
|
<Radio.Group>
|
|
<Radio value="phone">手机号账户(短信登录)</Radio>
|
|
<Radio value="password">用户名密码账户</Radio>
|
|
</Radio.Group>
|
|
</Form.Item>
|
|
<Form.Item name="name" label="姓名" rules={[{ required: true }]}><Input /></Form.Item>
|
|
<Form.Item name="adminRole" label="角色">
|
|
<Select options={ROLE_OPTIONS} />
|
|
</Form.Item>
|
|
<Form.Item
|
|
name="cityIds"
|
|
label="负责城市"
|
|
extra={createRole === 'CITY_STORE_SERVICE' ? '城市门店服务必须至少选一个城市' : '不选表示全国可见'}
|
|
rules={createRole === 'CITY_STORE_SERVICE' ? [{ required: true, type: 'array', min: 1, message: '请至少勾选一个城市' }] : []}
|
|
>
|
|
<Select mode="multiple" allowClear showSearch optionFilterProp="label" placeholder="不选=全国" options={cityOptions} />
|
|
</Form.Item>
|
|
{credentialType === 'phone' ? (
|
|
<Form.Item name="phone" label="手机号" rules={[{ required: true, message: '请输入手机号' }]}>
|
|
<Input maxLength={11} />
|
|
</Form.Item>
|
|
) : (
|
|
<>
|
|
<Form.Item name="loginName" label="用户名" rules={[{ required: true, message: '请输入用户名' }]}>
|
|
<Input autoComplete="off" />
|
|
</Form.Item>
|
|
<Form.Item name="password" label="密码" rules={[{ required: true, message: '请输入密码' }, { min: 6, message: '至少 6 位' }]}>
|
|
<Input.Password autoComplete="new-password" />
|
|
</Form.Item>
|
|
<Form.Item name="phone" label="手机号(可选)" extra="不填则自动生成占位手机号,用于满足账号唯一约束">
|
|
<Input maxLength={11} />
|
|
</Form.Item>
|
|
</>
|
|
)}
|
|
</Form>
|
|
</Modal>
|
|
</div>
|
|
);
|
|
}
|