index.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import type { ActionType, ProColumns } from '@jetlinks/pro-table';
  3. import ProTable from '@jetlinks/pro-table';
  4. import { useRef, useState } from 'react';
  5. import { useIntl } from '@@/plugin-locale/localeExports';
  6. import { BadgeStatus, PermissionButton } from '@/components';
  7. import SearchComponent from '@/components/SearchComponent';
  8. import { DeleteOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons';
  9. import { StatusColorEnum } from '@/components/BadgeStatus';
  10. import SaveModal from './save';
  11. import PasswordModal from './password';
  12. import Service from './service';
  13. import { message } from 'antd';
  14. export const service = new Service('platforms');
  15. export default () => {
  16. const actionRef = useRef<ActionType>();
  17. const intl = useIntl();
  18. const [param, setParam] = useState({});
  19. const [saveVisible, setSaveVisible] = useState(false);
  20. const [passwordVisible, setPasswordVisible] = useState(false);
  21. const { permission } = PermissionButton.usePermission('system/Platforms');
  22. const deleteById = async (id: string) => {
  23. const resp: any = await service.remove(id);
  24. if (resp.status === 200) {
  25. message.success('操作成功');
  26. actionRef.current?.reload();
  27. }
  28. };
  29. const columns: ProColumns<any>[] = [
  30. {
  31. dataIndex: 'name',
  32. title: '名称',
  33. },
  34. {
  35. dataIndex: 'accessName',
  36. title: '用户名',
  37. },
  38. {
  39. dataIndex: 'role',
  40. title: '角色',
  41. },
  42. {
  43. dataIndex: 'state',
  44. title: intl.formatMessage({
  45. id: 'pages.searchTable.titleStatus',
  46. defaultMessage: '状态',
  47. }),
  48. width: 160,
  49. valueType: 'select',
  50. renderText: (record) =>
  51. record ? (
  52. <BadgeStatus
  53. status={record.value}
  54. text={record.text}
  55. statusNames={{
  56. started: StatusColorEnum.processing,
  57. disable: StatusColorEnum.error,
  58. notActive: StatusColorEnum.warning,
  59. }}
  60. />
  61. ) : (
  62. ''
  63. ),
  64. valueEnum: {
  65. disable: {
  66. text: '禁用',
  67. status: 'offline',
  68. },
  69. started: {
  70. text: '正常',
  71. status: 'started',
  72. },
  73. },
  74. },
  75. {
  76. dataIndex: 'description',
  77. title: intl.formatMessage({
  78. id: 'pages.system.description',
  79. defaultMessage: '说明',
  80. }),
  81. hideInSearch: true,
  82. },
  83. {
  84. title: intl.formatMessage({
  85. id: 'pages.data.option',
  86. defaultMessage: '操作',
  87. }),
  88. valueType: 'option',
  89. align: 'center',
  90. width: 200,
  91. render: (_, record) => [
  92. <PermissionButton
  93. key={'update'}
  94. type={'link'}
  95. style={{ padding: 0 }}
  96. isPermission={permission.update}
  97. tooltip={{
  98. title: intl.formatMessage({
  99. id: 'pages.data.option.edit',
  100. defaultMessage: '编辑',
  101. }),
  102. }}
  103. onClick={() => {}}
  104. >
  105. <EditOutlined />
  106. </PermissionButton>,
  107. <PermissionButton
  108. key={'empowerment'}
  109. type={'link'}
  110. style={{ padding: 0 }}
  111. isPermission={permission.update}
  112. tooltip={{
  113. title: '赋权',
  114. }}
  115. onClick={() => {}}
  116. >
  117. <EditOutlined />
  118. </PermissionButton>,
  119. <PermissionButton
  120. key={'api'}
  121. type={'link'}
  122. style={{ padding: 0 }}
  123. tooltip={{
  124. title: '查看API',
  125. }}
  126. onClick={() => {}}
  127. >
  128. <EditOutlined />
  129. </PermissionButton>,
  130. <PermissionButton
  131. key={'password'}
  132. type={'link'}
  133. style={{ padding: 0 }}
  134. isPermission={permission.action}
  135. tooltip={{
  136. title: '重置密码',
  137. }}
  138. onClick={() => {
  139. setPasswordVisible(true);
  140. }}
  141. >
  142. <EditOutlined />
  143. </PermissionButton>,
  144. <PermissionButton
  145. key={'delete'}
  146. type={'link'}
  147. style={{ padding: 0 }}
  148. isPermission={permission.delete}
  149. disabled={record.state.value === 'started'}
  150. popConfirm={{
  151. title: '确认删除?',
  152. disabled: record.state.value === 'started',
  153. onConfirm: () => {
  154. deleteById(record.id);
  155. },
  156. }}
  157. tooltip={{
  158. title:
  159. record.state.value === 'started' ? <span>请先禁用,再删除</span> : <span>删除</span>,
  160. }}
  161. onClick={() => {}}
  162. >
  163. <DeleteOutlined />
  164. </PermissionButton>,
  165. ],
  166. },
  167. ];
  168. return (
  169. <PageContainer>
  170. <SearchComponent
  171. field={columns}
  172. onSearch={async (data) => {
  173. // 重置分页数据
  174. actionRef.current?.reset?.();
  175. setParam(data);
  176. }}
  177. />
  178. <ProTable
  179. rowKey="id"
  180. search={false}
  181. params={param}
  182. columns={columns}
  183. actionRef={actionRef}
  184. headerTitle={
  185. <PermissionButton
  186. key="button"
  187. type="primary"
  188. isPermission={permission.add}
  189. onClick={() => {
  190. setSaveVisible(true);
  191. }}
  192. icon={<PlusOutlined />}
  193. >
  194. {intl.formatMessage({
  195. id: 'pages.data.option.add',
  196. defaultMessage: '新增',
  197. })}
  198. </PermissionButton>
  199. }
  200. />
  201. <SaveModal
  202. visible={saveVisible}
  203. onCancel={() => {
  204. setSaveVisible(false);
  205. }}
  206. onReload={() => {
  207. actionRef.current?.reload();
  208. }}
  209. />
  210. <PasswordModal
  211. visible={passwordVisible}
  212. onCancel={() => {
  213. setPasswordVisible(false);
  214. }}
  215. onReload={() => {
  216. actionRef.current?.reload();
  217. }}
  218. />
  219. </PageContainer>
  220. );
  221. };