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 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. import { useEffect, useRef, useState } from 'react';
  2. import { connect } from '@umijs/max';
  3. import type { ActionType } from '@ant-design/pro-components';
  4. import {
  5. ProTable, ProFormSelect
  6. } from '@ant-design/pro-components';
  7. import { Button, ConfigProvider, message, Space, Descriptions, Radio, Row, Popconfirm, Modal, Flex, InputNumber } from 'antd';
  8. import { GetCompanyJobList, getCompanyList, GetCompanyInfo, PostJobExport, PostcompanymMultieditPulishJob, PostcompanymMultieditstatusjob, PostcompanyJobDel } from '@/apis/api';
  9. import { Imageprefix } from '@/constants';
  10. const selectfieldNames = { label: 'full_name', value: 'id' };
  11. const PagesMainCompanyPostTable: React.FC = ({ dispatch, getId, openModel }: 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 [companyId, setCompanyId] = useState<number>(0);
  18. const [expandedRowKeys, setExpandedRowKeys] = useState<string[]>([])
  19. const [openMultieditPreview, setOpenMultieditPreview] = useState(false);
  20. const [openMultieditPreview2, setOpenMultieditPreview2] = useState(false);
  21. const [ids, setIds] = useState<number[]>([])
  22. const [status, setStatus] = useState<number>(1)
  23. const [usefuLife, setUsefuLife] = useState<number>(1)
  24. const [disabledDate, setDisabledDate] = useState<Date | String | null>(null);
  25. const setId = (id: number) => {
  26. getId(id)
  27. }
  28. useEffect(() => {
  29. if (!openModel.openModal) {
  30. actionRef.current?.reload();
  31. }
  32. return () => {
  33. sessionStorage.removeItem('company_info')
  34. sessionStorage.removeItem('post_company_info')
  35. sessionStorage.removeItem('vip_company_info')
  36. };
  37. }, [openModel.openModal])
  38. const handleTimeChange = (value) => {
  39. setUsefuLife(value)
  40. const today = new Date();
  41. let end_date = new Date(today);
  42. end_date.setDate(today.getDate() + value);
  43. // 格式化日期为 YYYY-MM-DD
  44. const formatDate = (date: Date): string => {
  45. const year = date.getFullYear();
  46. const month = String(date.getMonth() + 1).padStart(2, '0');
  47. const day = String(date.getDate()).padStart(2, '0');
  48. return `${year}-${month}-${day}`;
  49. };
  50. const formattedEndDate = formatDate(end_date);
  51. setDisabledDate(formattedEndDate);
  52. console.log(formattedEndDate);
  53. };
  54. return (
  55. <>
  56. <ConfigProvider
  57. theme={{
  58. token: {
  59. colorPrimary: '#4FBE70',
  60. colorLink: '#4FBE70',
  61. }
  62. }}
  63. >
  64. <ProTable
  65. size='small'
  66. scroll={{ y: 480, x: 'auto' }}
  67. bordered={true}
  68. actionRef={actionRef}
  69. dataSource={list}
  70. search={{ span: 8, labelWidth: 'auto' }}
  71. tableAlertRender={({
  72. selectedRowKeys,
  73. selectedRows,
  74. onCleanSelected,
  75. }) => {
  76. setIds(selectedRowKeys)
  77. return (
  78. <Space size={24}>
  79. <span>
  80. 已选 {selectedRowKeys.length} 项
  81. <a style={{ marginInlineStart: 8 }} onClick={onCleanSelected}>
  82. 取消选择
  83. </a>
  84. </span>
  85. </Space>
  86. );
  87. }}
  88. tableAlertOptionRender={({
  89. onCleanSelected,
  90. }) => {
  91. return (
  92. <Space>
  93. <Button type="primary" onClick={() => {
  94. if (ids.length === 0) {
  95. message.error('请选择要操作的数据')
  96. } else {
  97. setOpenMultieditPreview(true)
  98. }
  99. }}>
  100. 批量发布
  101. </Button>,
  102. <Button type="primary" onClick={() => {
  103. if (ids.length === 0) {
  104. message.error('请选择要操作的数据')
  105. } else {
  106. setOpenMultieditPreview2(true)
  107. }
  108. }}>
  109. 批量更改状态
  110. </Button>,
  111. <Button type="primary" onClick={() => {
  112. Modal.confirm({
  113. title: '批量删除',
  114. content: '确定要删除所选的数据吗',
  115. onOk() {
  116. const promises = ids.map(item => {
  117. return PostcompanyJobDel({ id: item });
  118. });
  119. // 等待所有请求完成
  120. Promise.all(promises)
  121. .then(() => {
  122. message.success('批量删除成功');
  123. actionRef.current?.clearSelected();
  124. actionRef.current?.reload();
  125. })
  126. .catch(error => {
  127. console.error('批量删除成功:', error);
  128. message.error('批量删除成功');
  129. });
  130. },
  131. onCancel() {
  132. console.log('Cancel');
  133. },
  134. okText: '确定',
  135. cancelText: '取消',
  136. centered: true
  137. });
  138. }}>
  139. 批量删除
  140. </Button>,
  141. </Space>
  142. );
  143. }}
  144. rowSelection={{
  145. type: 'checkbox'
  146. }}
  147. columns={[
  148. {
  149. title: '筛选日期',
  150. dataIndex: 'date_range',
  151. hidden: true,
  152. valueType: 'dateRange',
  153. },
  154. {
  155. title: 'ID',
  156. dataIndex: 'id',
  157. width: 200
  158. },
  159. {
  160. title: '所属企业',
  161. dataIndex: 'full_name',
  162. width: 200,
  163. valueType: 'select',
  164. renderFormItem: () => {
  165. return (
  166. <ProFormSelect
  167. fieldProps={{
  168. fieldNames: selectfieldNames,
  169. defaultValue: sessionStorage.getItem('post_company_info') ? JSON.parse(sessionStorage.getItem('post_company_info')).full_name : null,
  170. }}
  171. request={async (keyword) =>
  172. getCompanyList({ page: 1, pagesize: 100, keyword: keyword.keyWords }).then(res => {
  173. return res.data.list;
  174. })
  175. }
  176. placeholder="请选择所属企业"
  177. onChange={(value) => {
  178. setCompanyId(value)
  179. GetCompanyInfo({ id: value }).then(res => {
  180. sessionStorage.setItem('post_company_info', JSON.stringify(res.data))
  181. })
  182. }}
  183. />
  184. )
  185. }
  186. },
  187. {
  188. title: '职位名称',
  189. dataIndex: 'name',
  190. width: 200
  191. },
  192. {
  193. title: '点击次数',
  194. dataIndex: 'click_count',
  195. width: 200,
  196. search: false,
  197. },
  198. {
  199. title: '职位状态',
  200. dataIndex: 'status',
  201. width: 200,
  202. colSize: 16,
  203. valueType: 'radio',
  204. initialValue: '0',
  205. valueEnum: {
  206. 0: { text: '全部' },
  207. 1: {
  208. text: '发布中',
  209. status: 'Processing'
  210. },
  211. 2: {
  212. text: '未发布',
  213. status: 'Error'
  214. },
  215. 3: {
  216. text: '暂停',
  217. status: 'Error'
  218. },
  219. 4: {
  220. text: '已锁定',
  221. status: 'Error'
  222. },
  223. 5: {
  224. text: '回收站',
  225. status: 'Error'
  226. },
  227. 6: {
  228. text: '已过期',
  229. status: 'Error'
  230. }
  231. },
  232. },
  233. {
  234. title: '有效期(天)',
  235. dataIndex: 'useful_life',
  236. width: 200,
  237. search: false,
  238. },
  239. {
  240. title: '发布时间',
  241. dataIndex: 'publish_date',
  242. width: 200,
  243. search: false,
  244. },
  245. {
  246. title: '到期时间',
  247. dataIndex: 'disabled_date',
  248. width: 200,
  249. search: false,
  250. },
  251. {
  252. title: '操作',
  253. key: 'option',
  254. valueType: 'option',
  255. width: 200,
  256. render: (_, record, action) => [
  257. <Button key='1' type='link' onClick={() => {
  258. setId(record.id)
  259. dispatch({ type: 'openModel/getOpenModal', payload: true })
  260. }}>编辑</Button>,
  261. <Popconfirm
  262. title="是否删除"
  263. onConfirm={(e) => {
  264. PostcompanyJobDel({ id: record.id }).then(res => {
  265. message.success('删除成功')
  266. actionRef.current?.reload();
  267. })
  268. }}
  269. okText="删除"
  270. cancelText="取消"
  271. >
  272. <Button danger type='link'>删除</Button>
  273. </Popconfirm>,
  274. ],
  275. },
  276. ]}
  277. rowKey="id"
  278. pagination={{
  279. current: page,
  280. pageSize: pageSize,
  281. showSizeChanger: true,
  282. total: total,
  283. pageSizeOptions: [9, 18, 27, 99],
  284. onChange(page, pageSize) {
  285. setPage(page)
  286. setPageSize(pageSize)
  287. },
  288. onShowSizeChange(current, size) {
  289. setPage(current)
  290. setPageSize(size)
  291. }
  292. }}
  293. request={async (params = {} as Record<string, any>) =>
  294. GetCompanyJobList({
  295. page: page,
  296. pagesize: pageSize,
  297. sort: 'id',
  298. sortby: 'desc',
  299. keyword: params.name,
  300. expired: params.expired,
  301. status: params.status ? params.status : 0,
  302. company_id: sessionStorage.getItem('post_company_info') ? JSON.parse(sessionStorage.getItem('post_company_info')).id : companyId,
  303. start_date: params.date_range ? params.date_range[0] : '',
  304. end_date: params.date_range ? params.date_range[1] : '',
  305. }).then(res => {
  306. setList(res.data.rows)
  307. setTotal(res.data.total)
  308. })
  309. }
  310. onReset={() => {
  311. if (sessionStorage.getItem('post_company_info')) {
  312. sessionStorage.removeItem('post_company_info')
  313. }
  314. setCompanyId(0)
  315. actionRef.current?.reload();
  316. }}
  317. headerTitle="职位列表"
  318. toolBarRender={() => [
  319. <Button type="primary" onClick={() => {
  320. message.info('数据导出需要时间,请耐心等待')
  321. PostJobExport().then(res => {
  322. let url = Imageprefix + res.data.excel_url;
  323. window.open(url);
  324. message.info('数据导出完毕')
  325. })
  326. }}>
  327. 导出数据
  328. </Button>,
  329. <Button type="primary" onClick={() => {
  330. dispatch({ type: 'openModel/getOpenModal', payload: true })
  331. }}>
  332. 添加职位
  333. </Button>
  334. ]}
  335. />
  336. <Modal
  337. open={openMultieditPreview}
  338. title='请选择对批量数据进行何种操作'
  339. centered
  340. maskClosable={false}
  341. destroyOnClose
  342. width={400}
  343. onCancel={() => {
  344. setOpenMultieditPreview(false)
  345. }}
  346. onOk={() => {
  347. if (usefuLife && disabledDate && status) {
  348. PostcompanymMultieditPulishJob({ id: ids, useful_life: usefuLife, disabled_date: disabledDate, status: status }).then(res => {
  349. setOpenMultieditPreview(false)
  350. actionRef.current?.clearSelected();
  351. actionRef.current?.reload();
  352. })
  353. } else {
  354. message.error('请完成表单')
  355. return false
  356. }
  357. }
  358. }
  359. >
  360. <Flex vertical justify='center' style={{ minHeight: 100 }}>
  361. <Space direction='vertical'>
  362. <h4 style={{ fontWeight: 'bold' }}>状态:</h4>
  363. <Radio.Group size='small' value={status} onChange={(e) => {
  364. setStatus(e.target.value)
  365. }}>
  366. <Radio value={1}>发布中</Radio>
  367. <Radio value={2}>未发布</Radio>
  368. <Radio value={3}>暂停</Radio>
  369. <Radio value={4}>已锁定</Radio>
  370. <Radio value={5}>回收站</Radio>
  371. <Radio value={6}>已过期</Radio>
  372. </Radio.Group>
  373. <h4 style={{ fontWeight: 'bold' }}>职位有效天数:</h4>
  374. <InputNumber min={1} placeholder='请输入职位有效天数' onChange={(value) => {
  375. handleTimeChange(value)
  376. }} style={{ width: '100%' }} />
  377. </Space>
  378. </Flex>
  379. </Modal >
  380. <Modal
  381. open={openMultieditPreview2}
  382. title='请选择对批量数据进行何种操作'
  383. centered
  384. maskClosable={false}
  385. destroyOnClose
  386. width={400}
  387. onCancel={() => {
  388. setOpenMultieditPreview2(false)
  389. }}
  390. onOk={() => {
  391. if (status) {
  392. PostcompanymMultieditstatusjob({ id: ids, status: status }).then(res => {
  393. setOpenMultieditPreview2(false)
  394. actionRef.current?.clearSelected();
  395. actionRef.current?.reload();
  396. })
  397. } else {
  398. message.error('请完成表单')
  399. return false
  400. }
  401. }
  402. }
  403. >
  404. <Flex vertical justify='center' style={{ minHeight: 100 }}>
  405. <Space direction='vertical'>
  406. <h4 style={{ fontWeight: 'bold' }}>状态:</h4>
  407. <Radio.Group size='small' value={status} onChange={(e) => {
  408. setStatus(e.target.value)
  409. }}>
  410. <Radio value={1}>发布中</Radio>
  411. <Radio value={2}>未发布</Radio>
  412. <Radio value={3}>暂停</Radio>
  413. <Radio value={4}>已锁定</Radio>
  414. <Radio value={5}>回收站</Radio>
  415. <Radio value={6}>已过期</Radio>
  416. </Radio.Group>
  417. </Space>
  418. </Flex>
  419. </Modal >
  420. </ConfigProvider>
  421. </>
  422. );
  423. };
  424. export default connect(({ dictModel, openModel }: any) => ({
  425. dictModel,
  426. openModel
  427. }))(PagesMainCompanyPostTable);