You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import { useRef, useState, useEffect } from 'react';
  2. import { connect, history } from '@umijs/max';
  3. import type { ActionType, ProFormInstance } from '@ant-design/pro-components';
  4. import {
  5. ProTable, ProForm
  6. } from '@ant-design/pro-components';
  7. import { Button, Image, ConfigProvider, Popconfirm, message, Modal, Radio, Row, Col } from 'antd';
  8. import { listSection, delSection } from '@/apis/api';
  9. import { Imageprefix } from '@/constants';
  10. import UploadModel from '@/components/Common/upload';
  11. const PagesMainInformationSectionTable: React.FC = ({ dispatch, openModel, getId }: any) => {
  12. const actionRef = useRef<ActionType>();
  13. const [list, setList] = useState<object[]>([])
  14. const [total, setTotal] = useState<number>(0)
  15. const [page, setPage] = useState<number>(1)
  16. const [pageSize, setPageSize] = useState<number>(10)
  17. const setId = (id: number) => {
  18. getId(id)
  19. }
  20. useEffect(() => {
  21. if (!openModel.openModal) {
  22. actionRef.current?.reload();
  23. }
  24. }, [openModel.openModal])
  25. const [uploadedFilephotoName, setUploadedFilephotoName] = useState<string>('');
  26. const uploadedFilephotoNameRef = useRef<string>('');
  27. const handleFileUploadedphoto = (filename: string) => {
  28. setUploadedFilephotoName(filename);
  29. };
  30. useEffect(() => {
  31. uploadedFilephotoNameRef.current = uploadedFilephotoName;
  32. }, [uploadedFilephotoName])
  33. return (
  34. <>
  35. <ConfigProvider
  36. theme={{
  37. token: {
  38. colorPrimary: '#4FBE70',
  39. colorLink: '#4FBE70',
  40. }
  41. }}
  42. >
  43. <ProTable
  44. scroll={{ y: 480, x: 'auto' }}
  45. bordered={true}
  46. actionRef={actionRef}
  47. dataSource={list}
  48. search={{ span: 8, labelWidth: 'auto' }}
  49. rowSelection={{
  50. type: 'checkbox'
  51. }}
  52. columns={[
  53. {
  54. title: 'ID',
  55. dataIndex: 'id',
  56. width: 200,
  57. search: false,
  58. },
  59. {
  60. title: '栏目名称',
  61. dataIndex: 'name',
  62. width: 200,
  63. },
  64. {
  65. title: '栏目编码',
  66. dataIndex: 'code',
  67. width: 200,
  68. },
  69. {
  70. title: '可见',
  71. dataIndex: 'display',
  72. width: 200,
  73. valueEnum: {
  74. 0: { text: '全部' },
  75. 1: {
  76. text: '可见',
  77. status: 'Processing'
  78. },
  79. 2: {
  80. text: '不可见',
  81. status: 'Error'
  82. },
  83. },
  84. },
  85. {
  86. title: '封面图',
  87. dataIndex: 'cover_img',
  88. search: false,
  89. width: 200,
  90. render: (_, record) => (<>
  91. <Image src={`${Imageprefix}${record.cover_img}`} width={40} height={40} />
  92. </>
  93. )
  94. },
  95. {
  96. title: '描述',
  97. dataIndex: 'description',
  98. width: 200,
  99. search: false
  100. },
  101. {
  102. title: '操作',
  103. key: 'option',
  104. valueType: 'option',
  105. render: (_, record, action) => [
  106. <Button key='1' type='link' onClick={() => {
  107. setId(record)
  108. dispatch({ type: 'openModel/getOpenModal', payload: true })
  109. }}>编辑</Button>,
  110. <Button size='small' type='link' onClick={() => {
  111. Modal.confirm({
  112. title: '更换图片',
  113. content: (<>
  114. <ProForm submitter={{ render: false }}>
  115. <UploadModel multiple={false} form_name="photo" image_length={1} uploadTxt="更换图片" image_type={3} imageUrl={record.photo} onUploadComplete={handleFileUploadedphoto}></UploadModel>
  116. </ProForm>
  117. </>),
  118. async onOk() {
  119. // let info = await GetCompanyInfo({ id: record.id });
  120. // let res = await updateCompany({ ...info.data, license_path: uploadedFilelicenseNameRef.current });
  121. // setUploadedFilelicenseName('');
  122. actionRef.current?.reload();
  123. },
  124. onCancel() {
  125. console.log('Cancel');
  126. },
  127. okText: '保存',
  128. cancelText: '取消',
  129. centered: true,
  130. });
  131. }}>更换图片</Button>,
  132. <Button key='1' type='link' onClick={() => {
  133. record.nextlevel = record.id // 添加下级栏目,把id赋值给nextlevel
  134. setId(record)
  135. dispatch({ type: 'openModel/getOpenModal', payload: true })
  136. }}>添加下级栏目</Button>,
  137. <Popconfirm
  138. title="是否删除"
  139. onConfirm={(e) => {
  140. delSection({ id: record.id }).then(res => {
  141. message.success('删除成功')
  142. actionRef.current?.reload();
  143. })
  144. }}
  145. okText="删除"
  146. cancelText="取消"
  147. >
  148. <Button danger type='link'>删除</Button>
  149. </Popconfirm>
  150. ],
  151. },
  152. ]}
  153. rowKey="id"
  154. pagination={{
  155. current: page,
  156. pageSize: pageSize,
  157. showSizeChanger: true,
  158. total: total,
  159. pageSizeOptions: [9, 18, 27, 99],
  160. onChange(page, pageSize) {
  161. setPage(page)
  162. setPageSize(pageSize)
  163. },
  164. onShowSizeChange(current, size) {
  165. setPage(current)
  166. setPageSize(size)
  167. }
  168. }}
  169. request={async (params = {} as Record<string, any>) =>
  170. listSection({
  171. page: page,
  172. pagesize: pageSize,
  173. sort: 'id',
  174. sortby: 'desc',
  175. keyword: params.name,
  176. }).then(res => {
  177. setList(res.data.sections)
  178. setTotal(res.data.total)
  179. })
  180. }
  181. headerTitle="栏目/频道"
  182. toolBarRender={() => [
  183. <Button type="primary" onClick={() => {
  184. dispatch({ type: 'openModel/getOpenModal', payload: true })
  185. }}>
  186. 添加栏目/频道
  187. </Button>
  188. ]}
  189. expandable={{
  190. childrenColumnName: 'childs', // 指定子节点数据的字段名
  191. }}
  192. />
  193. </ConfigProvider>
  194. </>
  195. );
  196. };
  197. export default connect(({ openModel }: any) => ({
  198. openModel
  199. }))(PagesMainInformationSectionTable);