index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. import { PageContainer } from '@ant-design/pro-layout';
  2. import { useEffect, useRef, useState } from 'react';
  3. import {
  4. EditOutlined,
  5. KeyOutlined,
  6. CloseCircleOutlined,
  7. PlayCircleOutlined,
  8. } from '@ant-design/icons';
  9. import { Menu, Tooltip, Popconfirm, message } from 'antd';
  10. import type { ProColumns, ActionType } from '@jetlinks/pro-table';
  11. import BaseCrud from '@/components/BaseCrud';
  12. import { CurdModel } from '@/components/BaseCrud/model';
  13. import BaseService from '@/utils/BaseService';
  14. import { observer } from '@formily/react';
  15. import { Store } from 'jetlinks-store';
  16. import SystemConst from '@/utils/const';
  17. import { useIntl } from '@@/plugin-locale/localeExports';
  18. const menu = (
  19. <Menu>
  20. <Menu.Item key="1">1st item</Menu.Item>
  21. <Menu.Item key="2">2nd item</Menu.Item>
  22. <Menu.Item key="3">3rd item</Menu.Item>
  23. </Menu>
  24. );
  25. export const service = new BaseService<UserItem>('user');
  26. const User = observer(() => {
  27. const intl = useIntl();
  28. const actionRef = useRef<ActionType>();
  29. const [model, setModel] = useState(CurdModel.model);
  30. useEffect(() => {
  31. const modelSubscription = Store.subscribe(SystemConst.BASE_CURD_MODEL, setModel);
  32. return () => modelSubscription.unsubscribe();
  33. }, [CurdModel.model]);
  34. const columns: ProColumns<UserItem>[] = [
  35. {
  36. dataIndex: 'index',
  37. valueType: 'indexBorder',
  38. width: 48,
  39. },
  40. {
  41. title: intl.formatMessage({
  42. id: 'pages.system.user.name',
  43. defaultMessage: '姓名',
  44. }),
  45. dataIndex: 'name',
  46. copyable: true,
  47. ellipsis: true,
  48. align: 'center',
  49. tip: intl.formatMessage({
  50. id: 'pages.system.name.tips',
  51. defaultMessage: '姓名过长会自动收缩',
  52. }),
  53. sorter: true,
  54. defaultSortOrder: 'ascend',
  55. formItemProps: {
  56. rules: [
  57. {
  58. required: true,
  59. message: '此项为必填项',
  60. },
  61. ],
  62. },
  63. search: {
  64. transform: (value) => ({ name$LIKE: value }),
  65. },
  66. },
  67. {
  68. title: intl.formatMessage({
  69. id: 'pages.table.username',
  70. defaultMessage: '用户名',
  71. }),
  72. dataIndex: 'username',
  73. copyable: true,
  74. ellipsis: true,
  75. align: 'center',
  76. tip: intl.formatMessage({
  77. id: 'pages.system.userName.tips',
  78. defaultMessage: '用户名过长会自动收缩',
  79. }),
  80. formItemProps: {
  81. rules: [
  82. {
  83. required: true,
  84. message: '此项为必填项',
  85. },
  86. ],
  87. },
  88. search: {
  89. transform: (value) => ({ username$LIKE: value }),
  90. },
  91. },
  92. {
  93. title: intl.formatMessage({
  94. id: 'pages.searchTable.titleStatus',
  95. defaultMessage: '状态',
  96. }),
  97. dataIndex: 'status',
  98. filters: true,
  99. align: 'center',
  100. onFilter: true,
  101. valueType: 'select',
  102. valueEnum: {
  103. all: {
  104. text: intl.formatMessage({
  105. id: 'pages.searchTable.titleStatus.all',
  106. defaultMessage: '全部',
  107. }),
  108. status: 'Default',
  109. },
  110. 1: {
  111. text: intl.formatMessage({
  112. id: 'pages.searchTable.titleStatus.normal',
  113. defaultMessage: '正常',
  114. }),
  115. status: 1,
  116. },
  117. 0: {
  118. text: intl.formatMessage({
  119. id: 'pages.searchTable.titleStatus.disable',
  120. defaultMessage: '禁用',
  121. }),
  122. status: 0,
  123. },
  124. },
  125. },
  126. {
  127. title: intl.formatMessage({
  128. id: 'pages.data.option',
  129. defaultMessage: '操作',
  130. }),
  131. valueType: 'option',
  132. align: 'center',
  133. width: 200,
  134. render: (text, record) => [
  135. <a
  136. key="editable"
  137. onClick={() => {
  138. CurdModel.update(record);
  139. CurdModel.model = 'edit';
  140. setModel('edit');
  141. }}
  142. >
  143. <Tooltip
  144. title={intl.formatMessage({
  145. id: 'pages.data.option.edit',
  146. defaultMessage: '编辑',
  147. })}
  148. >
  149. <EditOutlined />
  150. </Tooltip>
  151. </a>,
  152. <a onClick={() => console.log('授权')}>
  153. <Tooltip
  154. title={intl.formatMessage({
  155. id: 'pages.data.option.authorize',
  156. defaultMessage: '授权',
  157. })}
  158. >
  159. <KeyOutlined />
  160. </Tooltip>
  161. </a>,
  162. <a href={record.id} target="_blank" rel="noopener noreferrer" key="view">
  163. <Popconfirm
  164. title={intl.formatMessage({
  165. id: 'pages.data.option.disabled.tips',
  166. defaultMessage: '确认禁用?',
  167. })}
  168. onConfirm={async () => {
  169. await service.update({
  170. id: record.id,
  171. status: record.status ? 0 : 1,
  172. });
  173. message.success(
  174. intl.formatMessage({
  175. id: 'pages.data.option.success',
  176. defaultMessage: '操作成功!',
  177. }),
  178. );
  179. actionRef.current?.reload();
  180. }}
  181. >
  182. <Tooltip
  183. title={intl.formatMessage({
  184. id: `pages.data.option.${record.status ? 'disabled' : 'enabled'}`,
  185. defaultMessage: record.status ? '禁用' : '启用',
  186. })}
  187. >
  188. {record.status ? <CloseCircleOutlined /> : <PlayCircleOutlined />}
  189. </Tooltip>
  190. </Popconfirm>
  191. </a>,
  192. ],
  193. },
  194. ];
  195. const schema = {
  196. type: 'object',
  197. properties: {
  198. name: {
  199. title: intl.formatMessage({
  200. id: 'pages.system.user.name',
  201. defaultMessage: '姓名',
  202. }),
  203. type: 'string',
  204. 'x-decorator': 'FormItem',
  205. 'x-component': 'Input',
  206. 'x-component-props': {},
  207. 'x-decorator-props': {},
  208. name: 'name',
  209. required: true,
  210. _designableId: '1jq1ln7nzji',
  211. 'x-index': 0,
  212. },
  213. username: {
  214. title: intl.formatMessage({
  215. id: 'pages.table.username',
  216. defaultMessage: '用户名',
  217. }),
  218. type: 'string',
  219. 'x-decorator': 'FormItem',
  220. 'x-component': 'Input',
  221. 'x-component-props': {},
  222. 'x-decorator-props': {},
  223. name: 'username',
  224. required: true,
  225. _designableId: '9vf50ad9n1h',
  226. 'x-index': 1,
  227. },
  228. password: {
  229. type: 'string',
  230. title: intl.formatMessage({
  231. id: 'pages.system.user.password',
  232. defaultMessage: '密码',
  233. }),
  234. 'x-decorator': 'FormItem',
  235. 'x-component': 'Password',
  236. 'x-component-props': {
  237. checkStrength: true,
  238. },
  239. 'x-hidden': model === 'edit',
  240. 'x-reactions': [
  241. {
  242. dependencies: ['.confirmPassword'],
  243. fulfill: {
  244. state: {
  245. errors:
  246. '{{$deps[0] && $self.value && $self.value !==$deps[0] ? "确认密码不匹配" : ""}}',
  247. },
  248. },
  249. },
  250. ],
  251. 'x-decorator-props': {},
  252. name: 'password',
  253. required: false,
  254. _designableId: 'weg6kt6izlt',
  255. 'x-index': 2,
  256. },
  257. confirmPassword: {
  258. type: 'string',
  259. title: intl.formatMessage({
  260. id: 'pages.system.user.confirmPassword',
  261. defaultMessage: '确认密码?',
  262. }),
  263. 'x-decorator': 'FormItem',
  264. 'x-component': 'Password',
  265. 'x-hidden': model === 'edit',
  266. 'x-component-props': {
  267. checkStrength: true,
  268. },
  269. 'x-reactions': [
  270. {
  271. dependencies: ['.password'],
  272. fulfill: {
  273. state: {
  274. errors:
  275. '{{$deps[0] && $self.value && $self.value !== $deps[0] ? "确认密码不匹配" : ""}}',
  276. },
  277. },
  278. },
  279. ],
  280. 'x-decorator-props': {},
  281. name: 'confirmPassword',
  282. required: false,
  283. _designableId: 'mhsm2fk573e',
  284. 'x-index': 3,
  285. },
  286. },
  287. _designableId: 'zd740kqp5hf',
  288. };
  289. return (
  290. <PageContainer>
  291. <BaseCrud<UserItem>
  292. actionRef={actionRef}
  293. columns={columns}
  294. service={service}
  295. title={intl.formatMessage({
  296. id: 'pages.system.user',
  297. defaultMessage: '用户管理',
  298. })}
  299. menu={menu}
  300. schema={schema}
  301. />
  302. </PageContainer>
  303. );
  304. });
  305. export default User;