index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import React, { useRef } from 'react';
  3. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  4. import type { OpenApiItem } from '@/pages/system/OpenAPI/typings';
  5. import { useIntl } from '@@/plugin-locale/localeExports';
  6. import { CurdModel } from '@/components/BaseCrud/model';
  7. import { Drawer, message, Popconfirm, Tooltip } from 'antd';
  8. import {
  9. CloseCircleOutlined,
  10. EditOutlined,
  11. KeyOutlined,
  12. PlayCircleOutlined,
  13. } from '@ant-design/icons';
  14. import BaseCrud from '@/components/BaseCrud';
  15. import BaseService from '@/utils/BaseService';
  16. import autzModel from '@/components/Authorization/autz';
  17. import Authorization from '@/components/Authorization';
  18. import { observer } from '@formily/react';
  19. import type { ISchema } from '@formily/json-schema';
  20. import _ from 'lodash';
  21. const service = new BaseService<OpenApiItem>('open-api');
  22. const OpenAPI: React.FC = observer(() => {
  23. const intl = useIntl();
  24. const actionRef = useRef<ActionType>();
  25. const columns: ProColumns<OpenApiItem>[] = [
  26. {
  27. dataIndex: 'index',
  28. valueType: 'indexBorder',
  29. width: 48,
  30. },
  31. {
  32. title: 'clientId',
  33. dataIndex: 'id',
  34. sorter: true,
  35. defaultSortOrder: 'ascend',
  36. width: 200,
  37. },
  38. {
  39. title: intl.formatMessage({
  40. id: 'pages.table.name',
  41. defaultMessage: '名称',
  42. }),
  43. dataIndex: 'clientName',
  44. },
  45. {
  46. title: intl.formatMessage({
  47. id: 'pages.system.openApi.username',
  48. defaultMessage: '用户名',
  49. }),
  50. dataIndex: 'username',
  51. },
  52. {
  53. title: intl.formatMessage({
  54. id: 'pages.searchTable.titleStatus',
  55. defaultMessage: '状态',
  56. }),
  57. dataIndex: 'status',
  58. filters: true,
  59. align: 'center',
  60. renderText: (text: any) => text.text,
  61. valueType: 'select',
  62. hideInForm: true,
  63. onFilter: true,
  64. valueEnum: {
  65. default: {
  66. text: intl.formatMessage({
  67. id: 'pages.searchTable.titleStatus.all',
  68. defaultMessage: '全部',
  69. }),
  70. status: 'Default',
  71. },
  72. '1': {
  73. text: intl.formatMessage({
  74. id: 'pages.searchTable.titleStatus.normal',
  75. defaultMessage: '正常',
  76. }),
  77. status: '1',
  78. },
  79. '0': {
  80. text: intl.formatMessage({
  81. id: 'pages.searchTable.titleStatus.disable',
  82. defaultMessage: '禁用',
  83. }),
  84. status: '0',
  85. },
  86. },
  87. },
  88. {
  89. title: intl.formatMessage({
  90. id: 'pages.data.option',
  91. defaultMessage: '操作',
  92. }),
  93. valueType: 'option',
  94. align: 'center',
  95. width: 200,
  96. render: (text, record) => [
  97. <a
  98. key="editable"
  99. onClick={() => {
  100. const temp = _.omit(record, ['createTime', 'creatorId', 'status']);
  101. CurdModel.update(temp);
  102. }}
  103. >
  104. <Tooltip
  105. title={intl.formatMessage({
  106. id: 'pages.data.option.edit',
  107. defaultMessage: '编辑',
  108. })}
  109. >
  110. <EditOutlined />
  111. </Tooltip>
  112. </a>,
  113. <a
  114. key="auth"
  115. onClick={() => {
  116. autzModel.autzTarget.id = record.id;
  117. autzModel.autzTarget.name = record.clientName;
  118. autzModel.visible = true;
  119. }}
  120. >
  121. <Tooltip
  122. title={intl.formatMessage({
  123. id: 'pages.data.option.authorize',
  124. defaultMessage: '授权',
  125. })}
  126. >
  127. <KeyOutlined />
  128. </Tooltip>
  129. </a>,
  130. <a key="state">
  131. <Popconfirm
  132. title={intl.formatMessage({
  133. id: 'pages.data.option.disabled.tips',
  134. defaultMessage: '确认禁用?',
  135. })}
  136. onConfirm={async () => {
  137. await service.update({
  138. id: record.id,
  139. status: record.status ? 0 : 1,
  140. });
  141. message.success(
  142. intl.formatMessage({
  143. id: 'pages.data.option.success',
  144. defaultMessage: '操作成功!',
  145. }),
  146. );
  147. actionRef.current?.reload();
  148. }}
  149. >
  150. <Tooltip
  151. title={intl.formatMessage({
  152. id: `pages.data.option.${record.status ? 'disabled' : 'enabled'}`,
  153. defaultMessage: record.status ? '禁用' : '启用',
  154. })}
  155. >
  156. {record.status ? <CloseCircleOutlined /> : <PlayCircleOutlined />}
  157. </Tooltip>
  158. </Popconfirm>
  159. </a>,
  160. ],
  161. },
  162. ];
  163. const schema: ISchema = {
  164. type: 'object',
  165. properties: {
  166. layout: {
  167. type: 'void',
  168. 'x-component': 'FormGrid',
  169. 'x-component-props': {
  170. maxColumns: 2,
  171. },
  172. properties: {
  173. clientName: {
  174. title: intl.formatMessage({
  175. id: 'pages.table.name',
  176. defaultMessage: '名称',
  177. }),
  178. type: 'string',
  179. 'x-decorator': 'FormItem',
  180. 'x-component': 'Input',
  181. 'x-decorator-props': {
  182. labelCol: 6,
  183. wrapperCol: 18,
  184. },
  185. name: 'clientName',
  186. },
  187. enableOAuth2: {
  188. type: 'boolean',
  189. title: 'OAuth2',
  190. 'x-decorator': 'FormItem',
  191. 'x-component': 'Switch',
  192. 'x-decorator-props': {
  193. labelCol: 6,
  194. wrapperCol: 18,
  195. },
  196. name: 'enableOAuth2',
  197. },
  198. id: {
  199. title: 'clientId',
  200. type: 'string',
  201. 'x-decorator': 'FormItem',
  202. 'x-component': 'Input',
  203. 'x-decorator-props': {
  204. labelCol: 6,
  205. wrapperCol: 18,
  206. },
  207. name: 'clientId',
  208. },
  209. secureKey: {
  210. title: 'secureKey',
  211. type: 'string',
  212. 'x-decorator': 'FormItem',
  213. 'x-component': 'Input',
  214. 'x-decorator-props': {
  215. labelCol: 6,
  216. wrapperCol: 18,
  217. },
  218. name: 'secureKey',
  219. },
  220. username: {
  221. title: intl.formatMessage({
  222. id: 'pages.system.openApi.username',
  223. defaultMessage: '用户名',
  224. }),
  225. type: 'string',
  226. 'x-decorator': 'FormItem',
  227. 'x-component': 'Input',
  228. 'x-decorator-props': {
  229. labelWrap: false,
  230. wrapperWrap: false,
  231. labelCol: 6,
  232. wrapperCol: 18,
  233. },
  234. name: 'username',
  235. },
  236. password: {
  237. title: intl.formatMessage({
  238. id: 'pages.system.openApi.password',
  239. defaultMessage: '密码',
  240. }),
  241. type: 'string',
  242. 'x-decorator': 'FormItem',
  243. 'x-component': 'Input',
  244. 'x-decorator-props': {
  245. labelCol: 6,
  246. wrapperCol: 18,
  247. },
  248. name: 'password',
  249. },
  250. redirectUrl: {
  251. title: 'redirectUrl',
  252. type: 'string',
  253. 'x-decorator': 'FormItem',
  254. 'x-component': 'Input',
  255. 'x-visible': false,
  256. 'x-decorator-props': {
  257. fullness: false,
  258. gridSpan: 2,
  259. labelAlign: 'right',
  260. wrapperAlign: 'left',
  261. labelCol: 3,
  262. wrapperCol: 21,
  263. },
  264. name: 'redirectUrl',
  265. 'x-reactions': {
  266. dependencies: [
  267. {
  268. property: 'value',
  269. source: '.enableOAuth2',
  270. name: 'enableOAuth2',
  271. },
  272. ],
  273. fulfill: {
  274. state: {
  275. visible: '{{$deps.enableOAuth2}}',
  276. },
  277. },
  278. },
  279. },
  280. },
  281. },
  282. ipWhiteList: {
  283. title: intl.formatMessage({
  284. id: 'pages.system.openApi.ipWhileList',
  285. defaultMessage: 'IP白名单',
  286. }),
  287. type: 'string',
  288. 'x-decorator': 'FormItem',
  289. 'x-component': 'Input.TextArea',
  290. 'x-component-props': {},
  291. 'x-decorator-props': {
  292. labelCol: 3,
  293. wrapperCol: 21,
  294. },
  295. name: 'ipWhiteList',
  296. },
  297. description: {
  298. title: intl.formatMessage({
  299. id: 'pages.table.describe',
  300. defaultMessage: '描述',
  301. }),
  302. type: 'string',
  303. 'x-decorator': 'FormItem',
  304. 'x-component': 'Input.TextArea',
  305. 'x-component-props': {},
  306. 'x-decorator-props': {
  307. labelCol: 3,
  308. wrapperCol: 21,
  309. },
  310. name: 'description',
  311. },
  312. },
  313. };
  314. return (
  315. <PageContainer>
  316. <BaseCrud<OpenApiItem>
  317. columns={columns}
  318. service={service}
  319. title={intl.formatMessage({
  320. id: 'pages.system.openApi',
  321. defaultMessage: '第三方平台',
  322. })}
  323. schema={schema}
  324. modelConfig={{ width: 900 }}
  325. actionRef={actionRef}
  326. />
  327. <Drawer
  328. maskClosable={false}
  329. title={intl.formatMessage({
  330. id: 'pages.data.option.authorize',
  331. defaultMessage: '授权',
  332. })}
  333. width="70vw"
  334. visible={autzModel.visible}
  335. onClose={() => {
  336. autzModel.visible = false;
  337. }}
  338. >
  339. <Authorization
  340. close={() => {
  341. autzModel.visible = false;
  342. }}
  343. target={autzModel.autzTarget}
  344. />
  345. </Drawer>
  346. </PageContainer>
  347. );
  348. });
  349. export default OpenAPI;