index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-07-26 10:13:13
  4. * @LastEditTime: 2022-02-28 16:14:22
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /TracerMethodology_PC/src/pages/UserMana/index.js
  8. */
  9. import { PlusOutlined } from '@ant-design/icons';
  10. import { Button, Popconfirm,Form} from 'antd';
  11. import React, { useState, useRef } from 'react';
  12. import { PageContainer } from '@ant-design/pro-layout';
  13. import ProTable from '@ant-design/pro-table';
  14. import { ModalForm, ProFormText,ProFormDateRangePicker } from '@ant-design/pro-form';
  15. import UpdateForm from './updateForm';
  16. import DrawerContent from './component/drawer';
  17. import { getRoleList, roleBindUser, addList, editList, delList,roleBindMenu,roleHasBindUsers,roleHasBindMenus } from './service';
  18. import { getUserList } from '@/pages/PlatformMana/userMana/service';
  19. import moment from 'moment';
  20. import 'moment/locale/zh-cn';
  21. import locale from 'antd/es/date-picker/locale/zh_CN';
  22. import { getMenuList } from '@/pages/PlatformMana/menuManage/service';
  23. import './style.less';
  24. const UserMana = () => {
  25. const columns = [
  26. {
  27. title: '角色名',
  28. dataIndex: 'roleName',
  29. key: 'roleName',
  30. },
  31. {
  32. title: '变更人',
  33. dataIndex: 'modifyUserName',
  34. key: 'modifyUserName',
  35. hideInSearch: true,
  36. },
  37. {
  38. title: '变更日期',
  39. dataIndex: 'modifyTime',
  40. key: 'modifyTime',
  41. renderFormItem: (item, { type, defaultRender, formItemProps, fieldProps, ...rest }, form) => {
  42. if (type === 'form') {
  43. return null;
  44. }
  45. return (
  46. <Form.Item noStyle>
  47. <ProFormDateRangePicker fieldProps={{locale,onChange:(moment)=>{console.log(moment)} }} name="modifyTime" />
  48. </Form.Item>
  49. )
  50. },
  51. },
  52. {
  53. title: '操作',
  54. dataIndex: 'option',
  55. valueType: 'option',
  56. render: (_, record) => [
  57. <a
  58. key="config"
  59. onClick={() => {
  60. bindInitHandle(record,'user');
  61. }}
  62. >
  63. 用户
  64. </a>,
  65. <a
  66. key="config"
  67. onClick={() => {
  68. bindInitHandle(record,'menu');
  69. }}
  70. >
  71. 权限
  72. </a>,
  73. <a
  74. key="config"
  75. onClick={() => {
  76. handleUpdateModalVisible(true);
  77. setCurrentRow(record);
  78. }}
  79. >
  80. 编辑
  81. </a>,
  82. <Popconfirm
  83. key="subscribeAlert"
  84. title="是否确定删除?"
  85. onConfirm={() => {
  86. delUserHandler(record);
  87. }}
  88. >
  89. <a>删除</a>
  90. </Popconfirm>,
  91. ],
  92. },
  93. ];
  94. const DrawerTableUsersColumns = [
  95. {
  96. title: 'Id',
  97. dataIndex: 'id',
  98. key: 'id',
  99. hideInSearch: true,
  100. },
  101. {
  102. title: '姓名',
  103. dataIndex: 'name',
  104. key: 'name',
  105. },
  106. {
  107. title: '用户名',
  108. dataIndex: 'account',
  109. key: 'account',
  110. hideInSearch: true,
  111. },
  112. {
  113. title: '在职状态',
  114. dataIndex: 'hospitalStatus',
  115. key: 'hospitalStatus',
  116. hideInSearch: true,
  117. render:num=><>{num==0?'离职':'在职'}</>
  118. },
  119. ];
  120. const DrawerTableMenusColumns = [
  121. {
  122. title: 'Id',
  123. dataIndex: 'menuId',
  124. key: 'menuId',
  125. hideInSearch: true,
  126. },
  127. {
  128. title: '菜单名称',
  129. dataIndex: 'name',
  130. key: 'name',
  131. }
  132. ];
  133. const [createModalVisible, handleModalVisible] = useState(false);
  134. const [updateModalVisible, handleUpdateModalVisible] = useState(false);
  135. const actionRef = useRef();
  136. const [currentRow, setCurrentRow] = useState({});
  137. const [editRoleBindUsers, setEditRoleBindUsers] = useState(false);
  138. const [drawerVisible, setDrawerVisible] = useState(false);
  139. const [selectedUsers,setSelectedUsers] = useState([]);
  140. const [selectedMenus,setSelectedMenus] = useState([]);
  141. // const [shareParamsSetting,setShareParamsSetting] = useState(false); //是否分摊参数设置
  142. /**
  143. *
  144. * @param {Boolean} bool 弹窗展示状态
  145. */
  146. const updateModalVisibleChange = (bool) => {
  147. handleUpdateModalVisible(bool);
  148. if (!bool) setCurrentRow(undefined);
  149. };
  150. // 获取列表
  151. const getList = async (params = {}, sort, filter) => {
  152. // format: (value) => { return [value[0].format('YYYY-MM-DD'),value[1].format('YYYY-MM-DD')] }
  153. const {pageSize,current,modifyTime,roleName:keyword} = params;
  154. const query = modifyTime?{pageSize,current,keyword,startDate:moment(modifyTime[0]).format('YYYY-MM-DD'),endDate:moment(modifyTime[1]).format('YYYY-MM-DD')}:{pageSize,current,keyword}
  155. const res = await getRoleList(query);
  156. if(res){
  157. return {
  158. data: res.list,
  159. total: res.totalCount,
  160. success:true,
  161. };
  162. }
  163. };
  164. // 获取Drawer用户列表
  165. const getUsers = async (params = {}, sort, filter) => {
  166. const {roleId} = currentRow;
  167. const resp = await roleHasBindUsers({roleId});
  168. if(resp){
  169. const tempArr = resp.map(item=>item.id);
  170. setSelectedUsers(tempArr);
  171. }
  172. const res = await getUserList({...params,roleId:currentRow.roleId});
  173. if(res){
  174. return {
  175. data: res.list,
  176. total: res.totalCount,
  177. success: true,
  178. };
  179. }
  180. };
  181. // 获取Drawer菜单列表
  182. const getMenus = async (params = {}, sort, filter) => {
  183. const res = await getMenuList(params);
  184. if(res){
  185. return {
  186. data: res.list,
  187. total: res.totalCount,
  188. success:true,
  189. };
  190. }
  191. };
  192. /**
  193. *
  194. * @param {Object} value 删除项数据
  195. */
  196. const delUserHandler = async (value) => {
  197. const ids = [value.roleId];
  198. const resp = await delList({ ids });
  199. if (resp) {
  200. if (actionRef.current) {
  201. actionRef.current.reload();
  202. }
  203. }
  204. };
  205. // 绑定初始化
  206. /**
  207. *
  208. * @param {Object} record
  209. * @param {String} type user/menu
  210. */
  211. const bindInitHandle = async (record,type)=>{
  212. const {roleId} = record;
  213. setCurrentRow(record);
  214. if(type == 'user'){
  215. const resp = await roleHasBindUsers({roleId});
  216. if(resp){
  217. const tempArr = resp.map(item=>item.id);
  218. setSelectedUsers(tempArr);
  219. }
  220. setEditRoleBindUsers(true);
  221. }
  222. if(type == 'menu'){
  223. const resp = await roleHasBindMenus({roleId});
  224. if(resp){
  225. const tempArr = resp.map(item=>item.id);
  226. setSelectedMenus(tempArr);
  227. }
  228. setEditRoleBindUsers(false);
  229. }
  230. setDrawerVisible(true);
  231. setCurrentRow(record);
  232. }
  233. return (
  234. <PageContainer>
  235. <ProTable
  236. columns={columns}
  237. request={getList}
  238. actionRef={actionRef}
  239. rowKey="roleId"
  240. toolBarRender={() => [
  241. <Button
  242. key="button"
  243. icon={<PlusOutlined />}
  244. type="primary"
  245. onClick={() => {
  246. handleModalVisible(true);
  247. }}
  248. >
  249. 新增
  250. </Button>,
  251. ]}
  252. pagination={{
  253. pageSize: 10,
  254. }}
  255. />
  256. <ModalForm
  257. title="新增角色"
  258. width="800px"
  259. labelCol={{ span: 5, offset: 3 }}
  260. layout={'horizontal'}
  261. visible={createModalVisible}
  262. onVisibleChange={handleModalVisible}
  263. modalProps={{
  264. destroyOnClose:true
  265. }}
  266. onFinish={async (value) => {
  267. const success = await addList(value);
  268. // console.log({ success });
  269. if (success) {
  270. handleModalVisible(false);
  271. if (actionRef.current) {
  272. actionRef.current.reload();
  273. }
  274. }
  275. return true;
  276. }}
  277. >
  278. <ProFormText
  279. label="角色名"
  280. rules={[
  281. {
  282. required: true,
  283. message: '角色名是必填项!',
  284. },
  285. ]}
  286. width="sm"
  287. name="roleName"
  288. />
  289. <ProFormText
  290. label="备注"
  291. rules={[
  292. {
  293. required: false,
  294. message: '',
  295. },
  296. ]}
  297. width="sm"
  298. name="remark"
  299. />
  300. </ModalForm>
  301. {/* 更新 */}
  302. <UpdateForm
  303. onSubmit={async (value) => {
  304. // console.log({value});
  305. const { roleId, roleName, remark } = value;
  306. const success = await editList({ roleId, roleName, remark });
  307. if (success) {
  308. handleUpdateModalVisible(false);
  309. setCurrentRow(undefined);
  310. if (actionRef.current) {
  311. actionRef.current.reload();
  312. }
  313. }
  314. }}
  315. onCancel={() => {
  316. handleUpdateModalVisible(false);
  317. setCurrentRow(undefined);
  318. }}
  319. updateModalVisible={updateModalVisible}
  320. updateModalVisibleChange={updateModalVisibleChange}
  321. values={currentRow || {}}
  322. />
  323. {editRoleBindUsers ? (
  324. // 编辑用户
  325. <DrawerContent
  326. columns={DrawerTableUsersColumns}
  327. visible={drawerVisible}
  328. currentRow={currentRow}
  329. title="绑定用户"
  330. defaultSelected={selectedUsers}
  331. onVisibleChange={(bool) => setDrawerVisible(bool)}
  332. renderListFunc={getUsers}
  333. config={{tableSearch:true}}
  334. onFinishFunc={async (value, selectedRowKeys) => {
  335. const { roleId } = currentRow;
  336. const resp = await roleBindUser({ roleId, userIds: selectedRowKeys });
  337. const { status } = resp;
  338. if (status == 200) {
  339. setDrawerVisible(false);
  340. setCurrentRow(undefined);
  341. if (actionRef.current) {
  342. actionRef.current.reload();
  343. }
  344. }
  345. }}
  346. />
  347. ) : (
  348. // 编辑权限
  349. <DrawerContent
  350. columns={DrawerTableMenusColumns}
  351. visible={drawerVisible}
  352. currentRow={currentRow}
  353. title="绑定权限"
  354. defaultSelected={selectedMenus}
  355. onVisibleChange={(bool) => setDrawerVisible(bool)}
  356. renderListFunc={getMenus}
  357. config={{rowKeys:'menuId',tableSearch:false}}
  358. tableSearch={false}
  359. onFinishFunc={async (value, selectedRowKeys) => {
  360. const { roleId } = currentRow;
  361. const resp = await roleBindMenu({ roleId, menuIds: selectedRowKeys });
  362. const { status } = resp;
  363. if (status == 200) {
  364. setDrawerVisible(false);
  365. setCurrentRow(undefined);
  366. if (actionRef.current) {
  367. actionRef.current.reload();
  368. }
  369. }
  370. }}
  371. />
  372. )}
  373. </PageContainer>
  374. );
  375. };
  376. export default UserMana;