index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { useAjax } from '@/Hook/useAjax';
  2. import {
  3. apiDistributorInfo,
  4. apiDistributorInfoPageList,
  5. apiDistributorInfoRemove,
  6. apiDistributorInfoUpdate,
  7. } from '@/services/distribution/info';
  8. import { PlusCircleOutlined } from '@ant-design/icons';
  9. import {
  10. ActionType,
  11. BetaSchemaForm,
  12. PageContainer,
  13. ProFormInstance,
  14. } from '@ant-design/pro-components';
  15. import { useModel } from '@umijs/max';
  16. import { Button, message } from 'antd';
  17. import { useEffect, useRef, useState } from 'react';
  18. import { formConfig } from './formConfig';
  19. import Item from './item';
  20. import { columns } from './tableConfig';
  21. import MyProTable from '@/components/MyProTable';
  22. type DataItem = {
  23. name: string;
  24. state: string;
  25. };
  26. const Page: React.FC = () => {
  27. let { state, getLabelAndClassList } = useModel('global');
  28. let [open, setOpen] = useState<any>(null); //付费配置
  29. let [editValues, setEditValues] = useState<any>({});
  30. let [workDirection, setWorkDirection] = useState<any>(null);
  31. const [userManage, setUserManage] = useState<any>(null); //账号管理
  32. const [editSelectedRow, setEditSelectedRow] = useState<any[]>([]); //小说列表选择
  33. // ======================API=======================
  34. let DistributorInfoPageList = useAjax((params) => apiDistributorInfoPageList(params), {
  35. type: 'table',
  36. }); //分销商分页列表
  37. let DistributorInfoUpdate = useAjax((params) => apiDistributorInfoUpdate(params)); //修改
  38. let DistributorInfo = useAjax((params) => apiDistributorInfo(params)); //新增
  39. let DistributorInfoRemove = useAjax((distributorId) => apiDistributorInfoRemove(distributorId)); //分销商删除
  40. const formRef = useRef<ProFormInstance>();
  41. const actionRef = useRef<ActionType>();
  42. // 获取标签和分类
  43. useEffect(() => {
  44. getLabelAndClassList({ workDirection });
  45. }, [workDirection]);
  46. // 删除or批量删除
  47. const del = (id: any) => {
  48. DistributorInfoRemove.run(id).then((res) => {
  49. if (res.code === 200) {
  50. actionRef?.current?.reload();
  51. message.success('删除成功');
  52. setEditSelectedRow([]);
  53. }
  54. });
  55. };
  56. // 提交表单
  57. const submit = async (values: any) => {
  58. let api = editValues?.id ? DistributorInfoUpdate : DistributorInfo;
  59. if (editValues?.id) {
  60. values.id = editValues?.id;
  61. }
  62. api.run(values).then((res) => {
  63. if (res.code === 200) {
  64. actionRef?.current?.reload();
  65. message.success('操作成功!');
  66. closeForm(false);
  67. }
  68. });
  69. };
  70. // 关闭表单弹窗和重置表单内容
  71. const closeForm = (b: boolean, values?: any) => {
  72. if (!b) {
  73. setEditValues({});
  74. formRef?.current?.resetFields?.();
  75. setOpen(b);
  76. } else {
  77. setEditValues(values);
  78. setWorkDirection(values.workDirection);
  79. setTimeout(() => {
  80. formRef?.current?.setFieldsValue(values);
  81. }, 100);
  82. setOpen(b);
  83. }
  84. };
  85. return (
  86. <PageContainer title={false} tabProps={{ type: 'card' }}>
  87. <MyProTable<any, any>
  88. // 实例
  89. actionRef={actionRef}
  90. // 标题
  91. headerTitle={'短篇小说列表'}
  92. // 唯一key
  93. rowKey={(r) => r.id}
  94. // 按钮
  95. toolBarRender={() => {
  96. return [
  97. <Button
  98. type="primary"
  99. onClick={() => {
  100. setOpen(true);
  101. }}
  102. >
  103. <PlusCircleOutlined />
  104. 新增分销商
  105. </Button>,
  106. ];
  107. }}
  108. //宽度自适应
  109. scroll={{ x: true }}
  110. // 加载
  111. loading={DistributorInfoPageList?.loading}
  112. // 搜索的配置
  113. search={{
  114. labelWidth: 120,
  115. searchGutter: [10, 15],
  116. }}
  117. // 数据请求
  118. request={async (params) => {
  119. return await DistributorInfoPageList.run(params);
  120. }}
  121. // 表
  122. columns={columns({ del, edit: closeForm, setUserManage })}
  123. />
  124. {/* 新增分销商 */}
  125. <BetaSchemaForm<DataItem>
  126. layout="horizontal"
  127. title={!editValues?.id ? '新增分销商' : '编辑分销商'}
  128. formRef={formRef}
  129. open={open}
  130. onOpenChange={(b) => {
  131. !b && closeForm(b);
  132. }}
  133. layoutType={'ModalForm'}
  134. rowProps={{
  135. gutter: [16, 16],
  136. }}
  137. labelCol={{ span: 6 }}
  138. colProps={{
  139. span: 20,
  140. }}
  141. grid={true}
  142. onFinish={submit}
  143. columns={formConfig()}
  144. loading={DistributorInfo?.loading || DistributorInfoUpdate?.loading}
  145. />
  146. {/* 账号管理 */}
  147. <Item data={userManage} onClose={setUserManage} />
  148. </PageContainer>
  149. );
  150. };
  151. export default Page;