Commit 6ba335c3 by chenwq

```

feat(user-api): 添加用户创建和更新API接口

- 添加UserCreatePayload和UserUpdatePayload接口定义
- 实现create和update方法用于用户管理

feat(role-api): 为角色选项添加角色编码字段

- 在RoleOption接口中增加可选的roleCode属性

feat(data-scope): 新增数据域常量配置

- 创建dataScopeOptions常量包含数据域选项
- 提供getDataScopeLabel工具函数用于获取数据域标签

feat(account-modal): 实现账号管理模态框组件

- 创建AccountModal组件用于新增和编辑用户
- 包含基本信息、组织归属和角色分配表单
- 集成用户创建和更新API调用

refactor(account-manage): 集成账号管理页面与模态框

- 引入AccountModal组件到账号管理页面
- 实现新增和编辑用户的交互逻辑
- 添加查询参数处理和列表刷新功能

refactor(org-path): 添加组织路径查找工具函数

- 实现findOrgPath函数用于根据组织ID反查完整路径

refactor(role-manage): 使用统一的数据域标签显示

- 替换角色管理页面中的硬编码数据域映射
- 使用getDataScopeLabel函数统一处理数据域显示

refactor(store-approve): 统一数据域标签显示逻辑

- 使用getDataScopeLabel函数替换硬编码的数据域映射

refactor(store-listing): 统一数据域标签显示逻辑

- 使用getDataScopeLabel函数替换硬编码的数据域映射
```
1 parent c1c0f3dd
......@@ -38,6 +38,7 @@ export interface RolePageResult {
export interface RoleOption {
roleId: number;
roleName: string;
roleCode?: string;
}
export interface PermissionMatrix {
......
......@@ -27,6 +27,22 @@ export interface UserPageItem {
remark?: string;
}
export interface UserCreatePayload {
account: string;
name: string;
mobile: string;
email?: string;
orgId: number;
dataScope?: number;
status?: number;
roleIds?: number[];
remark?: string;
}
export interface UserUpdatePayload extends Omit<UserCreatePayload, 'account'> {
userId: number;
}
export interface UserPageResult {
records: UserPageItem[];
total: number;
......@@ -39,6 +55,14 @@ class User extends ApiClient {
page(params: UserPageParams) {
return this.post<UserPageResult>('/system/user/page', params);
}
create(payload: UserCreatePayload) {
return this.post<null>('/system/user/create', payload);
}
update(payload: UserUpdatePayload) {
return this.post<null>('/system/user/update', payload);
}
}
export const user = new User();
import type { FieldOption } from '@/components';
/** 系统通用的数据域选项与展示文案。 */
export const dataScopeOptions = [
{ label: '全部数据', value: 1 },
{ label: '本部门及下级', value: 2 },
{ label: '本部门', value: 3 },
{ label: '仅本人', value: 4 },
] satisfies FieldOption[];
const dataScopeLabels: Record<number, string> = Object.fromEntries(
dataScopeOptions.map((item) => [item.value, item.label]),
);
export function getDataScopeLabel(scope: number | undefined, fallback = '—'): string {
return scope === undefined ? fallback : (dataScopeLabels[scope] ?? fallback);
}
import { useEffect } from 'react';
import { message } from 'antd';
import { useMutation } from '@tanstack/react-query';
import { UButton, UCascader, UForm, UFormItem, UInput, UModal, USelect } from '@uj/ui';
import { user, type OrgNode, type RoleOption, type UserPageItem } from '@/api';
import { dataScopeOptions } from '@/constants/dataScope';
import { findOrgPath, toOrgCascaderOptions } from '../tools/account';
interface AccountFormValues {
account: string;
name: string;
mobile: string;
email?: string;
orgPath: number[];
dataScope?: number | '';
roleIds: number[];
}
export interface AccountModalProps {
open: boolean;
account?: UserPageItem;
orgTree: OrgNode[];
roleOptions: RoleOption[];
onCancel: () => void;
onSuccess: () => void;
}
export function AccountModal({
open,
account,
orgTree,
roleOptions,
onCancel,
onSuccess,
}: AccountModalProps) {
const [form] = UForm.useForm<AccountFormValues>();
const selectedRoleIds = UForm.useWatch('roleIds', form) ?? [];
const isEdit = Boolean(account);
const saveMutation = useMutation({
mutationFn: (values: AccountFormValues) => {
const orgId = values.orgPath.at(-1);
const payload = {
name: values.name.trim(),
mobile: values.mobile,
email: values.email?.trim() || undefined,
orgId: orgId as number,
dataScope: values.dataScope === '' ? undefined : values.dataScope,
roleIds: values.roleIds,
status: account?.status ?? 1,
};
return account
? user.update({ ...payload, userId: account.userId })
: user.create({ ...payload, account: values.account.trim() });
},
onSuccess: () => {
void message.success(isEdit ? '账号已保存' : '账号已创建');
onSuccess();
},
onError: (error: Error) => {
void message.error(error.message || '保存失败,请稍后重试');
},
});
useEffect(() => {
if (!open) return;
const selectedRoleIds = account?.roleCodes?.length
? roleOptions
.filter((item) => item.roleCode && account.roleCodes?.includes(item.roleCode))
.map((item) => item.roleId)
: [];
form.setFieldsValue({
account: account?.account ?? '',
name: account?.name ?? '',
mobile: account?.mobile ?? '',
email: account?.email ?? '',
orgPath: findOrgPath(orgTree, account?.orgId),
dataScope: account?.dataScope ?? '',
roleIds: selectedRoleIds,
});
}, [account, form, open, orgTree, roleOptions]);
return (
<UModal
open={open}
title={isEdit ? '编辑账号' : '新增账号'}
width={520}
okText="保存"
cancelText="取消"
confirmLoading={saveMutation.isPending}
onCancel={onCancel}
onOk={() => form.submit()}
styles={{ body: { maxHeight: '65vh', overflowY: 'auto', paddingTop: 8 } }}
>
<UForm
form={form}
layout="vertical"
requiredMark
onFinish={(values) => saveMutation.mutate(values)}
initialValues={{ roleIds: [] }}
>
<section>
<SectionTitle>基本信息</SectionTitle>
<div style={twoColumnStyle}>
<UFormItem
name="account"
label="账号"
rules={[
{ required: true, message: '请输入账号' },
{ min: 1, max: 20, message: '账号长度为 1-20 个字符' },
]}
>
<UInput disabled={isEdit} maxLength={20} placeholder="请输入登录账号" size="small" />
</UFormItem>
<UFormItem
name="name"
label="姓名"
rules={[
{ required: true, message: '请输入姓名' },
{ min: 1, max: 20, message: '姓名长度为 1-20 个字符' },
]}
>
<UInput maxLength={20} placeholder="请输入姓名" size="small" />
</UFormItem>
<UFormItem
name="mobile"
label="手机号"
rules={[
{ required: true, message: '请输入手机号' },
{ pattern: /^1\d{10}$/, message: '请输入正确的 11 位手机号' },
]}
>
<UInput
inputMode="numeric"
maxLength={11}
placeholder="请输入手机号"
size="small"
onChange={(event) => {
form.setFieldValue('mobile', event.target.value.replace(/\D/g, ''));
}}
/>
</UFormItem>
<UFormItem
name="email"
label="邮箱"
rules={[{ type: 'email', message: '请输入正确的邮箱地址' }]}
>
<UInput placeholder="选填" size="small" />
</UFormItem>
</div>
</section>
<section>
<SectionTitle>组织归属</SectionTitle>
<div style={twoColumnStyle}>
<UFormItem
name="orgPath"
label="所属组织"
rules={[{ required: true, message: '请选择所属组织' }]}
>
<UCascader
options={toOrgCascaderOptions(orgTree)}
changeOnSelect
placeholder="请选择所属组织"
showSearch
size="small"
style={{ width: '100%' }}
/>
</UFormItem>
<UFormItem name="dataScope" label="数据域">
<USelect
options={dataScopeOptions}
placeholder="请选择数据域"
size="small"
style={{ width: '100%' }}
/>
</UFormItem>
</div>
</section>
<section>
<SectionTitle>角色分配</SectionTitle>
<UFormItem name="roleIds" rules={[{ required: true, message: '请至少选择一个角色' }]}>
<div style={roleListStyle}>
{roleOptions.map((item) => (
<UButton
key={item.roleId}
type={selectedRoleIds.includes(item.roleId) ? 'primary' : 'default'}
size="small"
style={roleOptionStyle}
onClick={() => {
const nextRoleIds = selectedRoleIds.includes(item.roleId)
? selectedRoleIds.filter((id) => id !== item.roleId)
: [...selectedRoleIds, item.roleId];
form.setFields([{ name: 'roleIds', value: nextRoleIds, errors: [] }]);
}}
>
{item.roleName}
</UButton>
))}
</div>
</UFormItem>
</section>
</UForm>
</UModal>
);
}
const twoColumnStyle = { display: 'grid', gridTemplateColumns: '1fr 1fr', columnGap: 12 };
function SectionTitle({ children }: { children: string }) {
return (
<h3 style={sectionTitleStyle}>
<span style={sectionMarkerStyle} />
{children}
</h3>
);
}
const sectionTitleStyle = {
borderBottom: '1px solid #eef0f4',
alignItems: 'center',
display: 'flex',
gap: 6,
fontSize: 14,
lineHeight: '16px',
margin: '0 0 12px',
padding: '0 0 8px',
};
const sectionMarkerStyle = { backgroundColor: '#315ce8', borderRadius: 2, height: 10, width: 3 };
const roleListStyle = { display: 'flex', flexWrap: 'wrap', gap: 8 } as const;
const roleOptionStyle = {
alignItems: 'center',
display: 'flex',
height: 30,
margin: 0,
padding: '0 10px',
};
......@@ -10,7 +10,8 @@ import {
type SearchPagination,
} from '@/components';
import { P, PermButton } from '@/access';
import { org, role, user, type UserPageParams } from '@/api';
import { org, role, user, type UserPageItem, type UserPageParams } from '@/api';
import { AccountModal } from './components/AccountModal';
import { createAccountPageParams, toOrgCascaderOptions, withAllOption } from './tools/account';
const accountStatusOptions = [
......@@ -28,6 +29,7 @@ interface AccountRow {
dataScopeLabel: string;
status: number;
lastLoginAt: string;
source: UserPageItem;
}
function toParams(values: Record<string, unknown>, pagination: SearchPagination): UserPageParams {
......@@ -37,6 +39,8 @@ function toParams(values: Record<string, unknown>, pagination: SearchPagination)
export default function AccountManage() {
const [params, setParams] = useState<UserPageParams>({ current: 1, size: 20 });
const [queryVersion, setQueryVersion] = useState(0);
const [accountModalOpen, setAccountModalOpen] = useState(false);
const [editingAccount, setEditingAccount] = useState<UserPageItem>();
const {
data: result,
isLoading,
......@@ -62,6 +66,7 @@ export default function AccountManage() {
dataScopeLabel: item.dataScopeLabel ?? '—',
status: item.status,
lastLoginAt: item.lastLoginAt ?? '—',
source: item,
})) ?? [],
[result],
);
......@@ -122,7 +127,14 @@ export default function AccountManage() {
{ title: '最后登录', dataIndex: 'lastLoginAt', width: 170 },
];
const rowActions: RowActionSchema<AccountRow>[] = [
{ label: '编辑', permission: P.SYSTEM_USER_SAVE, onClick: () => undefined },
{
label: '编辑',
permission: P.SYSTEM_USER_SAVE,
onClick: (row) => {
setEditingAccount(row.source);
setAccountModalOpen(true);
},
},
{ label: '重置密码', permission: P.SYSTEM_USER_SAVE, onClick: () => undefined },
{ label: '停用', permission: P.SYSTEM_USER_SAVE, danger: true, onClick: () => undefined },
];
......@@ -135,7 +147,15 @@ export default function AccountManage() {
</UTitle>
<UText type="secondary">维护系统用户账号,分配角色与组织归属。</UText>
</div>
<PermButton permission={P.SYSTEM_USER_SAVE}>新增账号</PermButton>
<PermButton
permission={P.SYSTEM_USER_SAVE}
onClick={() => {
setEditingAccount(undefined);
setAccountModalOpen(true);
}}
>
新增账号
</PermButton>
</div>
<UCard size="small">
<SearchForm
......@@ -170,6 +190,17 @@ export default function AccountManage() {
}}
/>
</UCard>
<AccountModal
open={accountModalOpen}
account={editingAccount}
orgTree={orgTree}
roleOptions={roleOptions}
onCancel={() => setAccountModalOpen(false)}
onSuccess={() => {
setAccountModalOpen(false);
setQueryVersion((version) => version + 1);
}}
/>
</div>
);
}
......@@ -10,6 +10,17 @@ export function toOrgCascaderOptions(nodes: OrgNode[]): FieldOption[] {
}));
}
/** 根据组织 ID 反查级联选择器所需的完整路径。 */
export function findOrgPath(nodes: OrgNode[], targetId: number | undefined): number[] {
if (targetId === undefined) return [];
for (const node of nodes) {
if (node.id === targetId) return [node.id];
const childPath = findOrgPath(node.children ?? [], targetId);
if (childPath.length) return [node.id, ...childPath];
}
return [];
}
/** 为模块内的下拉选项统一补充“全部”。 */
export function withAllOption(options: FieldOption[]): FieldOption[] {
return [{ label: '全部', value: '' }, ...options];
......
......@@ -11,6 +11,7 @@ import {
} from '@/components';
import { P, PermButton } from '@/access';
import { role, type RolePageParams } from '@/api';
import { getDataScopeLabel } from '@/constants/dataScope';
import { getBaseList } from './tools/constants';
interface Row {
......@@ -59,9 +60,7 @@ export default function RoleManage() {
id: item.roleId,
name: item.roleName,
code: item.roleCode,
scope:
{ 1: '全部数据', 2: '本部门及下级', 3: '本部门', 4: '仅本人' }[item.defaultDataScope] ??
'—',
scope: getDataScopeLabel(item.defaultDataScope),
accounts: item.accountCount,
description: item.description ?? '—',
type: item.isBuiltin === 1 ? '内置' : '自定义',
......
import type { FieldOption } from '@/components';
import { dataScopeOptions } from '@/constants/dataScope';
/** 角色管理页专用的下拉选项。 */
......@@ -11,12 +12,7 @@ export const baseList = {
{ label: '启用', value: 1 },
{ label: '停用', value: 0 },
],
dataScope: [
{ label: '全部数据', value: 1 },
{ label: '本部门及下级', value: 2 },
{ label: '本部门', value: 3 },
{ label: '仅本人', value: 4 },
],
dataScope: dataScopeOptions,
} satisfies Record<string, FieldOption[]>;
export function getBaseList(key: keyof typeof baseList, includeAll = true): FieldOption[] {
......
......@@ -4,6 +4,7 @@ import { App } from 'antd';
import { Skeleton, UAlert, UButton, UCard, UResult, UTabs, UTitle, UText } from '@uj/ui';
import { P, PermButton, getLoginSession, useAccess } from '@/access';
import { SearchForm, type FieldSchema, type RowActionSchema } from '@/components';
import { getDataScopeLabel } from '@/constants/dataScope';
import {
store,
type ApprovalPageParams,
......@@ -242,11 +243,7 @@ export default function StoreApprove() {
drawer.row.actionable === true &&
detail.data?.status === 'PENDING';
const scope = getLoginSession()?.dataScope;
const dataScope =
scope == null
? '以账号授权范围为准'
: ({ 1: '全部数据', 2: '本部门及下级', 3: '本部门', 4: '仅本人' }[scope] ??
'以账号授权范围为准');
const dataScope = getDataScopeLabel(scope, '以账号授权范围为准');
if (!allowed)
return (
<UResult status="403" title="无门店审批访问权限" subTitle="请联系管理员确认账号权限。" />
......
......@@ -6,6 +6,7 @@ import { DownloadOutlined, SettingOutlined } from '@ant-design/icons';
import { Skeleton, UAlert, UButton, UCard, UResult, UText, UTitle } from '@uj/ui';
import { getLoginSession, getUserInfo, P, useAccess } from '@/access';
import { store, type ListingPageParams, type StoreId, type StoreListingRow } from '@/api';
import { getDataScopeLabel } from '@/constants/dataScope';
import {
DataTable,
SearchForm,
......@@ -152,10 +153,10 @@ export default function StoreListing() {
},
];
const scope = getUserInfo()?.dataScope ?? getLoginSession()?.dataScope;
const scopeLabel =
({ 1: '全部数据', 2: '本部门及下级', 3: '本部门', 4: '仅本人' } as Record<string, string>)[
String(scope)
] ?? '以账号授权范围为准';
const scopeLabel = getDataScopeLabel(
typeof scope === 'number' ? scope : undefined,
'以账号授权范围为准',
);
const onTableChange: TableProps<StoreListingRow>['onChange'] = (
_pagination,
_filters,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!