index.tsx 10 KB

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