index.tsx 8.6 KB

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