index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import {
  4. ArrowDownOutlined,
  5. BarsOutlined,
  6. BugOutlined,
  7. DeleteOutlined,
  8. EditOutlined,
  9. PlusOutlined,
  10. UnorderedListOutlined,
  11. } from '@ant-design/icons';
  12. import { Button, message, Popconfirm, Space, Tooltip, Upload } from 'antd';
  13. import { useRef, useState } from 'react';
  14. import { useIntl } from '@@/plugin-locale/localeExports';
  15. import { downloadObject } from '@/utils/util';
  16. import Service from '@/pages/notice/Config/service';
  17. import { observer } from '@formily/react';
  18. import SearchComponent from '@/components/SearchComponent';
  19. import { getMenuPathByParams, MENUS_CODE } from '@/utils/menu';
  20. import { history, useLocation } from 'umi';
  21. import { model } from '@formily/reactive';
  22. import moment from 'moment';
  23. import { ProTableCard } from '@/components';
  24. import NoticeConfig from '@/components/ProTableCard/CardItems/noticeConfig';
  25. import Debug from '@/pages/notice/Config/Debug';
  26. import Log from '@/pages/notice/Config/Log';
  27. import { typeList } from '@/components/ProTableCard/CardItems/noticeTemplate';
  28. export const service = new Service('notifier/config');
  29. export const state = model<{
  30. current?: ConfigItem;
  31. debug?: boolean;
  32. log?: boolean;
  33. }>({
  34. debug: false,
  35. log: false,
  36. });
  37. const Config = observer(() => {
  38. const intl = useIntl();
  39. const actionRef = useRef<ActionType>();
  40. const location = useLocation<{ id: string }>();
  41. const id = (location as any).query?.id;
  42. const columns: ProColumns<ConfigItem>[] = [
  43. {
  44. dataIndex: 'name',
  45. title: '配置名称',
  46. },
  47. {
  48. dataIndex: 'provider',
  49. title: '通知方式',
  50. renderText: (text, record) => typeList[record.type][record.provider],
  51. },
  52. {
  53. dataIndex: 'description',
  54. title: '说明',
  55. },
  56. {
  57. title: intl.formatMessage({
  58. id: 'pages.data.option',
  59. defaultMessage: '操作',
  60. }),
  61. valueType: 'option',
  62. align: 'center',
  63. width: 200,
  64. render: (text, record) => [
  65. <a
  66. key="edit"
  67. onClick={async () => {
  68. // setLoading(true);
  69. state.current = record;
  70. history.push(getMenuPathByParams(MENUS_CODE['notice/Config/Detail'], id));
  71. }}
  72. >
  73. <Tooltip
  74. title={intl.formatMessage({
  75. id: 'pages.data.option.edit',
  76. defaultMessage: '编辑',
  77. })}
  78. >
  79. <EditOutlined />
  80. </Tooltip>
  81. </a>,
  82. <a
  83. onClick={() =>
  84. downloadObject(
  85. record,
  86. `通知配置${record.name}-${moment(new Date()).format('YYYY/MM/DD HH:mm:ss')}`,
  87. )
  88. }
  89. key="download"
  90. >
  91. <Tooltip
  92. title={intl.formatMessage({
  93. id: 'pages.data.option.download',
  94. defaultMessage: '下载配置',
  95. })}
  96. >
  97. <ArrowDownOutlined />
  98. </Tooltip>
  99. </a>,
  100. <a
  101. key="debug"
  102. onClick={() => {
  103. state.debug = true;
  104. state.current = record;
  105. }}
  106. >
  107. <Tooltip
  108. title={intl.formatMessage({
  109. id: 'pages.notice.option.debug',
  110. defaultMessage: '调试',
  111. })}
  112. >
  113. <BugOutlined />
  114. </Tooltip>
  115. </a>,
  116. <a
  117. key="record"
  118. onClick={() => {
  119. state.log = true;
  120. }}
  121. >
  122. <Tooltip
  123. title={intl.formatMessage({
  124. id: 'pages.data.option.record',
  125. defaultMessage: '通知记录',
  126. })}
  127. >
  128. <BarsOutlined />
  129. </Tooltip>
  130. </a>,
  131. <a key="remove">
  132. <Popconfirm
  133. onConfirm={async () => {
  134. await service.remove(record.id);
  135. message.success(
  136. intl.formatMessage({
  137. id: 'pages.data.option.success',
  138. defaultMessage: '操作成功!',
  139. }),
  140. );
  141. actionRef.current?.reload();
  142. }}
  143. title="确认删除?"
  144. >
  145. <Tooltip
  146. title={intl.formatMessage({
  147. id: 'pages.data.option.remove',
  148. defaultMessage: '删除',
  149. })}
  150. >
  151. <DeleteOutlined />
  152. </Tooltip>
  153. </Popconfirm>
  154. </a>,
  155. ],
  156. },
  157. ];
  158. const [param, setParam] = useState({});
  159. return (
  160. <PageContainer>
  161. <SearchComponent
  162. defaultParam={[{ column: 'type$IN', value: id }]}
  163. field={columns}
  164. onSearch={(data) => {
  165. actionRef.current?.reset?.();
  166. setParam(data);
  167. }}
  168. />
  169. <ProTableCard<ConfigItem>
  170. rowKey="id"
  171. actionRef={actionRef}
  172. search={false}
  173. params={param}
  174. columns={columns}
  175. headerTitle={
  176. <Space>
  177. <Button
  178. onClick={() => {
  179. state.current = undefined;
  180. history.push(getMenuPathByParams(MENUS_CODE['notice/Config/Detail'], id));
  181. }}
  182. key="button"
  183. icon={<PlusOutlined />}
  184. type="primary"
  185. >
  186. {intl.formatMessage({
  187. id: 'pages.data.option.add',
  188. defaultMessage: '新增',
  189. })}
  190. </Button>
  191. <Upload
  192. key={'import'}
  193. showUploadList={false}
  194. beforeUpload={(file) => {
  195. const reader = new FileReader();
  196. reader.readAsText(file);
  197. reader.onload = async (result) => {
  198. const text = result.target?.result as string;
  199. if (!file.type.includes('json')) {
  200. message.warning('文件内容格式错误');
  201. return;
  202. }
  203. try {
  204. const data = JSON.parse(text || '{}');
  205. if (Array.isArray(data)) {
  206. message.warning('文件内容格式错误');
  207. return;
  208. }
  209. const res: any = await service.savePatch(data);
  210. if (res.status === 200) {
  211. message.success('操作成功');
  212. actionRef.current?.reload();
  213. }
  214. } catch {
  215. message.warning('文件内容格式错误');
  216. }
  217. };
  218. return false;
  219. }}
  220. >
  221. <Button style={{ marginLeft: 12 }}>导入</Button>
  222. </Upload>
  223. <Popconfirm
  224. title={'确认导出当前页数据?'}
  225. onConfirm={async () => {
  226. const resp: any = await service.queryNoPagingPost({ ...param, paging: false });
  227. if (resp.status === 200) {
  228. downloadObject(resp.result, '通知配置数据');
  229. message.success('导出成功');
  230. } else {
  231. message.error('导出错误');
  232. }
  233. }}
  234. >
  235. <Button>导出</Button>
  236. </Popconfirm>
  237. </Space>
  238. }
  239. gridColumn={3}
  240. request={async (params) =>
  241. service.query({ ...params, sorts: [{ name: 'createTime', order: 'desc' }] })
  242. }
  243. cardRender={(record) => (
  244. <NoticeConfig
  245. {...record}
  246. type={id}
  247. actions={[
  248. <Button
  249. key="edit"
  250. onClick={async () => {
  251. // setLoading(true);
  252. state.current = record;
  253. history.push(getMenuPathByParams(MENUS_CODE['notice/Config/Detail'], id));
  254. }}
  255. >
  256. <EditOutlined />
  257. 编辑
  258. </Button>,
  259. <Button
  260. key="debug"
  261. onClick={() => {
  262. state.debug = true;
  263. state.current = record;
  264. }}
  265. >
  266. <BugOutlined />
  267. 调试
  268. </Button>,
  269. <Button
  270. key="export"
  271. onClick={() =>
  272. downloadObject(
  273. record,
  274. `通知配置${record.name}-${moment(new Date()).format('YYYY/MM/DD HH:mm:ss')}`,
  275. )
  276. }
  277. >
  278. <ArrowDownOutlined />
  279. 导出
  280. </Button>,
  281. <Button
  282. key="log"
  283. onClick={() => {
  284. state.log = true;
  285. state.current = record;
  286. }}
  287. >
  288. <UnorderedListOutlined />
  289. 通知记录
  290. </Button>,
  291. <Popconfirm
  292. key="delete"
  293. title="确认删除?"
  294. onConfirm={async () => {
  295. await service.remove(record.id);
  296. actionRef.current?.reset?.();
  297. }}
  298. >
  299. <Button key="delete">
  300. <DeleteOutlined />
  301. </Button>
  302. </Popconfirm>,
  303. ]}
  304. />
  305. )}
  306. />
  307. <Debug />
  308. {state.log && <Log />}
  309. </PageContainer>
  310. );
  311. });
  312. export default Config;