import { Drawer, Popconfirm, message, Input, Alert, Tooltip, Space } from 'antd';
import type { ActionType, ProColumns } from '@ant-design/pro-table';
import { CustomTable } from '../CustomTable';
import { FormattedMessage, useDispatch, useIntl, useRequest, useSelector } from 'umi';
import { InfoCircleOutlined } from '@ant-design/icons';
import { memo, useEffect, useRef, useState } from 'react';
import { useGetAppId } from '../utils/hooks';
import { fetchUserList, validateUsers, cancelBindUsers, bindUsers } from '../server';
import moment from 'moment';
import AddUser from '../AddUser';
import React from 'react';
import { reloadReactNode } from '../utils/jsxformat';
import { formatUserRender } from '../utils';
interface BindUserProps {
/**
* @description feature 绑定的类型,目前支持如下。
*/
type: 'feature' | 'option' | 'policy' | 'role'; // 选择类型
/**
* @description 绑定的类型的id,如type选了feature,就要传入featureId。
*/
id: number; // 默认参数
/**
* @description type选了option,就需要传入flagId。
*/
flagId?: number;
/**
* @description 可以自定义触发内容,默认是用户
*/
children?: React.ReactNode;
/**
* @description 绑定的值发生变化刷新
*/
refresh?: () => void;
}
const BindUser = ({ id, flagId, type, children, refresh }: BindUserProps) => {
const [open, setOpen] = useState(false);
const [pageSize, setPageSize] = useState(10);
const actionRef = useRef();
const { formatMessage } = useIntl();
const dispatch = useDispatch();
const appId = useGetAppId();
const { appDetail } = useSelector<{ public: API.StoreState }, API.StoreState>(
(state) => state.public,
);
const { data, run } = useRequest(validateUsers(appId, flagId), { manual: true });
const { loading: cancelLoading, run: cancelRun } = useRequest(
cancelBindUsers(type, { appId, [`${type}Id`]: id, flagId }),
{ manual: true },
);
// 用户账号 绑定时间 到期时间
const columns: ProColumns[] = [
{
title: ,
dataIndex: 'accounts',
valueType: 'textarea',
hideInTable: true,
fieldProps: {
placeholder: formatMessage({ id: 'component.user.add.user.id.placeholder' }),
rows: 4,
allowClear: true,
},
colSize: 3,
renderFormItem: () => ,
},
{
title: ,
dataIndex: 'id',
ellipsis: true,
search: false,
hideInSearch: false,
renderText: (text, record) => formatUserRender(record),
},
{
title: ,
dataIndex: 'bindTime',
ellipsis: true,
search: false,
hideInSearch: false,
valueType: 'dateTime',
},
{
title: ,
dataIndex: 'expiredAt',
ellipsis: true,
search: false,
hideInSearch: false,
renderText: (text) =>
`${
text
? moment(text).format('YYYY-MM-DD HH:mm:ss')
: formatMessage({ id: 'component.user.bind.expires.always' })
}`,
},
{
title: ,
dataIndex: 'id',
search: false,
hideInSearch: false,
render: (text, record, _, action) => [
{
await cancelRun([record.id]);
message.success(formatMessage({ id: 'component.user.bind.unbind.success' }));
// @ts-ignore
action?.clearSelected();
action?.reload();
}}
>
,
],
},
];
const toolbar = {
title: formatMessage({ id: 'component.user.bind.toolbar.title' }),
actions: [
{
refresh?.();
actionRef?.current?.reload();
}}
key={`adduser${id}`}
type={type}
id={id}
flagId={flagId}
/>,
],
};
const tableAlert = ({ selectedRowKeys, selectedRows, onCleanSelected }) => [
{
label: ,
loading: cancelLoading,
onClick: async () => {
await cancelRun(selectedRowKeys);
message.success(formatMessage({ id: 'component.user.bind.batch.unbind.success' }));
onCleanSelected();
refresh?.();
actionRef?.current?.reload?.();
},
},
];
const tableExtraRender = () => (
{data?.notExist?.length > 0 ? (
{data?.notExist?.join(',')}
) : null}
{data?.noManagementPermission?.length > 0 ? (
{data?.noManagementPermission?.map((user) => user.account)?.join(',')}
) : null}
{data?.noPermission?.length > 0 ? (
user.account)?.join(',')})${formatMessage(
{
id: 'component.user.bind.permission',
defaultMessage: '授权',
},
)}?`}
onConfirm={async () => {
await bindUsers(
type,
data?.noPermission?.map((user) => ({
appId,
flagId,
userId: user.id,
expiredAt: appDetail.defaultExpirationTime
? appDetail.defaultExpirationTime + Math.round(moment().valueOf() / 1000)
: undefined,
[`${type}Id`]: id,
})),
);
message.success(
formatMessage({
id: 'component.user.bind.permission.success',
defaultMessage: '授权成功!',
}),
);
refresh?.();
actionRef?.current?.reload?.();
}}
>
}
>
:
{data?.noPermission?.map((user) => user.account)?.join(',')}
) : null}
}
/>
);
const triggerRender = () =>
reloadReactNode(
setOpen(true)}>
,
setOpen,
children,
);
useEffect(() => {
if (open) {
dispatch({
type: 'public/fetchAppDetail',
payload: appId,
});
}
}, [open, appId, dispatch]);
return (
<>
}
width="900px"
open={open}
keyboard={false}
maskClosable={false}
destroyOnClose
onClose={() => setOpen(false)}
>
{
if (param.accounts) {
run({ accounts: param.accounts?.trim()?.split(','), [`${type}Id`]: id });
} else {
run({ accounts: [], [`${type}Id`]: id });
}
setPageSize(param.pageSize);
return fetchUserList({ ...param, accounts: param?.accounts?.split(',') }, ...pars);
}}
rowSelection={{}}
tableExtraRender={() =>
data?.notExist?.length > 0 ||
data?.noManagementPermission?.length > 0 ||
data?.noPermission?.length > 0
? tableExtraRender()
: null
}
/>
{triggerRender()}
>
);
};
export default memo(BindUser);