index.tsx 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { fetchInitialCredentials, fetchProductList, fetchUpdateProduct } from '@/services/product';
  2. import { ActionType, PageContainer, ProColumns, ProTable } from '@ant-design/pro-components';
  3. import { useIntl, useNavigate } from '@umijs/max';
  4. import { Button, Popconfirm, Tag, message } from 'antd';
  5. import { useRef, useState } from 'react';
  6. import { ProductForm } from './components/Form';
  7. import { CredentialModal } from './Detail/components/CredentialModal';
  8. export default function ProductPage() {
  9. const intl = useIntl();
  10. const navigate = useNavigate();
  11. const actionRef = useRef<ActionType>();
  12. const [credData, setCredData] = useState<API.FetchInitialCredentialsResp | null>(null);
  13. const [credOpen, setCredOpen] = useState(false);
  14. const handleCreateSuccess = async (ticket?: string) => {
  15. actionRef.current?.reload();
  16. if (!ticket) return;
  17. const res = await fetchInitialCredentials({ ticket });
  18. setCredData(res.data ?? null);
  19. setCredOpen(true);
  20. };
  21. const handleToggleStatus = async (record: API.ProductItem) => {
  22. await fetchUpdateProduct({
  23. id: record.id,
  24. name: record.name,
  25. status: record.status === 1 ? 0 : 1,
  26. });
  27. message.success('操作成功');
  28. actionRef.current?.reload();
  29. };
  30. const columns: ProColumns<API.ProductItem>[] = [
  31. {
  32. title: '名称',
  33. dataIndex: 'name',
  34. render: (_, r) => <a onClick={() => navigate(`/admin/products/${r.id}`)}>{r.name}</a>,
  35. },
  36. { title: 'Code', dataIndex: 'code', copyable: true },
  37. { title: 'App Key', dataIndex: 'appKey', copyable: true },
  38. {
  39. title: '状态',
  40. dataIndex: 'status',
  41. render: (_, r) => (
  42. <Tag color={r.status === 1 ? 'success' : 'default'}>{r.status === 1 ? '启用' : '禁用'}</Tag>
  43. ),
  44. },
  45. { title: '创建时间', dataIndex: 'createTime', valueType: 'dateTime' },
  46. {
  47. title: '操作',
  48. valueType: 'option',
  49. render: (_, r) => [
  50. <ProductForm
  51. key="edit"
  52. mode="edit"
  53. initialValues={r}
  54. onSuccess={() => actionRef.current?.reload()}
  55. trigger={<a>编辑</a>}
  56. />,
  57. <Popconfirm
  58. key="status"
  59. title={r.status === 1 ? '确认禁用?' : '确认启用?'}
  60. onConfirm={() => handleToggleStatus(r)}
  61. >
  62. <a className={r.status === 1 ? 'text-red-500' : ''}>{r.status === 1 ? '禁用' : '启用'}</a>
  63. </Popconfirm>,
  64. ],
  65. },
  66. ];
  67. return (
  68. <PageContainer title={intl.formatMessage({ id: 'admin.product.title' })}>
  69. <ProTable<API.ProductItem>
  70. actionRef={actionRef}
  71. columns={columns}
  72. rowKey="id"
  73. search={false}
  74. request={fetchProductList}
  75. toolBarRender={() => [
  76. <ProductForm
  77. key="create"
  78. mode="add"
  79. onSuccess={handleCreateSuccess}
  80. trigger={
  81. <Button type="primary">{intl.formatMessage({ id: 'admin.product.create' })}</Button>
  82. }
  83. />,
  84. ]}
  85. />
  86. <CredentialModal open={credOpen} data={credData} onClose={() => setCredOpen(false)} />
  87. </PageContainer>
  88. );
  89. }