index.tsx 11 KB

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