index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { PageContainer } from '@ant-design/pro-components';
  2. import React, { useEffect, useState } from 'react';
  3. import { Button, Card, Form, Input, message, Modal, Select, Space, Table } from 'antd';
  4. import { PlusCircleOutlined, ReloadOutlined, SearchOutlined } from '@ant-design/icons';
  5. import Edit from '@/pages/EditionManagement/edit';
  6. import type { ColumnsType } from 'antd/es/table';
  7. import moment from 'moment/moment';
  8. import { delEdition, queryEdition } from '@/services/EditionManagement';
  9. interface DataType {
  10. version: string;
  11. platform: string;
  12. creator: string;
  13. download_url: string;
  14. created_at: string;
  15. record_id: string;
  16. content: string;
  17. }
  18. /**
  19. * 版本管理
  20. * @constructor
  21. */
  22. const EditionManagement: React.FC = () => {
  23. const [form] = Form.useForm();
  24. const [loading, setLoading] = useState(false);
  25. const [searchData, setSearchData] = useState<object | null>({});
  26. const [visible, setVisible] = useState(false);
  27. const [editData, setEditData] = useState(null);
  28. const [pagination, setPagination] = useState({ total: 0, current: 1, pageSize: 10 });
  29. const [dataList, setDataList]: any = useState([]);
  30. // 获取列表数据
  31. const getList = () => {
  32. const params = {
  33. q: 'page',
  34. current: pagination.current,
  35. pageSize: pagination.pageSize,
  36. ...searchData,
  37. };
  38. queryEdition(params).then((res) => {
  39. if (res && res.code === 0) {
  40. setDataList(res.data.list);
  41. setPagination(res.data.pagination);
  42. setLoading(false);
  43. }
  44. });
  45. };
  46. useEffect(() => {
  47. setLoading(true);
  48. getList();
  49. }, []);
  50. // 搜索
  51. const onFinish = () => {
  52. form.validateFields().then((data) => {
  53. setLoading(true);
  54. setSearchData(data);
  55. });
  56. };
  57. useEffect(() => {
  58. getList();
  59. }, [searchData]);
  60. // 重置
  61. const onReset = () => {
  62. form.resetFields();
  63. setLoading(true);
  64. setSearchData(null);
  65. };
  66. // 新增弹框
  67. const onAdd = () => {
  68. setEditData(null);
  69. setVisible(true);
  70. };
  71. // 打开编辑弹框
  72. const toEdit = (data: any) => {
  73. setEditData(data);
  74. setVisible(true);
  75. };
  76. // 分页切换
  77. const tableChange = (page: any) => {
  78. setLoading(true);
  79. const params = {
  80. q: 'page',
  81. current: page.current,
  82. pageSize: page.pageSize,
  83. ...searchData,
  84. };
  85. queryEdition(params).then((res) => {
  86. if (res.code === 0) {
  87. setDataList(res.data.list);
  88. setPagination(res.data.pagination);
  89. setLoading(false);
  90. }
  91. });
  92. };
  93. // 删除
  94. const toDel = (record: any) => {
  95. Modal.confirm({
  96. title: '删除',
  97. content: `是否确认删除${record.version}版本`,
  98. onOk: () => {
  99. delEdition(record?.record_id)
  100. .then((res) => {
  101. if (res.code === 0) {
  102. message.success('删除成功');
  103. } else {
  104. message.error('删除失败');
  105. }
  106. getList();
  107. })
  108. .catch((e) => {
  109. message.error(e?.message);
  110. });
  111. },
  112. });
  113. };
  114. // 编辑弹框回调
  115. const handleEdit = () => {
  116. setVisible(false);
  117. getList();
  118. };
  119. const columns: ColumnsType<DataType> = [
  120. {
  121. title: '序号',
  122. align: 'center',
  123. key: 'index',
  124. render: (_: any, row: any, index: number) => index + 1,
  125. },
  126. {
  127. title: '版本号',
  128. dataIndex: 'version',
  129. key: 'version',
  130. },
  131. {
  132. title: '设备类型',
  133. dataIndex: 'platform',
  134. key: 'platform',
  135. render: (v) => v && <span>{{ ANDROID: '安卓', IOS: 'iphone' }[v]}</span>,
  136. },
  137. {
  138. title: '是否强制更新',
  139. dataIndex: 'force_update',
  140. key: 'force_update',
  141. render: (v) => (
  142. <span style={{ color: `${v ? 'red' : 'green'}` }}>{v ? '强制' : '不强制'}</span>
  143. ),
  144. },
  145. {
  146. title: '创建时间',
  147. dataIndex: 'created_at',
  148. key: 'created_at',
  149. render: (v) => v && <span>{moment(v).format('YYYY-MM-DD HH:mm')}</span>,
  150. },
  151. {
  152. title: 'apk下载地址',
  153. dataIndex: 'download_url',
  154. key: 'download_url',
  155. render: (v) =>
  156. v && (
  157. <a href={v} target="_blank" rel="noreferrer">
  158. {v}
  159. </a>
  160. ),
  161. },
  162. {
  163. title: '更新内容',
  164. dataIndex: 'content',
  165. key: 'content',
  166. },
  167. {
  168. title: '创建者',
  169. dataIndex: 'creator',
  170. key: 'creator',
  171. },
  172. {
  173. title: '操作',
  174. key: 'action',
  175. render: (_, record) => (
  176. <Space size="middle">
  177. <a
  178. onClick={() => {
  179. toEdit(record);
  180. }}
  181. >
  182. 编辑
  183. </a>
  184. <a
  185. style={{ color: 'red' }}
  186. onClick={() => {
  187. toDel(record);
  188. }}
  189. >
  190. 删除
  191. </a>
  192. </Space>
  193. ),
  194. },
  195. ];
  196. const paginationProps = {
  197. showSizeChanger: true,
  198. showQuickJumper: true,
  199. showTotal: (total: number) => {
  200. return <span> 共 {total}条 </span>;
  201. },
  202. ...pagination,
  203. };
  204. return (
  205. <PageContainer>
  206. <div>
  207. <Card>
  208. <Form form={form} layout="inline" onFinish={onFinish}>
  209. <Form.Item name="version" label="版本号">
  210. <Input placeholder="请输入版本号" />
  211. </Form.Item>
  212. <Form.Item name="platform" label="设备类型">
  213. <Select placeholder="请选择设备类型" style={{ width: '175px' }}>
  214. <Select.Option value="IOS">iphone</Select.Option>
  215. <Select.Option value="ANDROID">安卓</Select.Option>
  216. </Select>
  217. </Form.Item>
  218. <Form.Item style={{ marginBottom: '10px' }}>
  219. <Space>
  220. <Button type="primary" htmlType="submit">
  221. <SearchOutlined />
  222. 查询
  223. </Button>
  224. <Button htmlType="button" onClick={onReset}>
  225. <ReloadOutlined />
  226. 重置
  227. </Button>
  228. </Space>
  229. </Form.Item>
  230. </Form>
  231. <Button htmlType="button" type="primary" style={{ margin: '20px 0' }} onClick={onAdd}>
  232. <PlusCircleOutlined />
  233. 新增版本
  234. </Button>
  235. <Table
  236. columns={columns}
  237. dataSource={dataList}
  238. rowKey={(record) => record.record_id}
  239. pagination={paginationProps}
  240. loading={loading}
  241. onChange={tableChange}
  242. />
  243. {visible && <Edit visible={visible} editCallback={handleEdit} params={editData} />}
  244. </Card>
  245. </div>
  246. </PageContainer>
  247. );
  248. };
  249. export default EditionManagement;