index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. import {useEffect,useState} from 'react';
  2. import { useSelector } from 'react-redux'
  3. /*import { useDispatch,useSelector } from 'react-redux'*/
  4. import { Table,Modal, message, Menu, Breadcrumb, Dropdown, Space, Form, Input, Button, Row, Col, Select } from 'antd';
  5. import { DownOutlined,PlusOutlined } from '@ant-design/icons';
  6. import AddRole from './AddRole';
  7. import './index.less';
  8. import apiObj from '@api/index';
  9. import RoleContext from './role-context';
  10. import utils from '@utils/index'
  11. const {post,api,xPost} = apiObj;
  12. const { pickCheckedTreeIds } = utils;
  13. const { Option } = Select;
  14. function RoleManager(){
  15. useEffect(() => {
  16. getTableData();
  17. },[]);
  18. const [dataSource, setDataSource] = useState([]); //列表数据
  19. const [roleDetail, setRoleDetail] = useState(null); //角色详情数据
  20. const [visible,setVisible] = useState(false); //删除禁用确认弹窗显示
  21. const [addVisible,setAddVisible] = useState(false); //新增页面显示
  22. const [confirmLoading, setConfirmLoading] = useState(false);
  23. //弹窗类型:1删除 2有用户删除 3禁用 4有用户禁用
  24. const [modalType,setModalType] = useState(1);
  25. const [operId,setOperId] = useState(''); //当前操作的角色id
  26. const [total, setTotal] = useState(0);
  27. //从state中取出状态、类型列表
  28. const staticInfo =useSelector(state => {
  29. return state.staticInfo;
  30. });
  31. const {statusList} = staticInfo;
  32. const tipText={
  33. 1:'确定要删除该角色?',
  34. 2:'当前角色存在用户,删除后用户将一并被删除',
  35. 3:'确定要禁用该角色?',
  36. 4:'当前角色存在用户,禁用后用户将一并被禁用',
  37. };
  38. let params = {
  39. pages: 1,
  40. current: 1,
  41. size: 15
  42. }
  43. //获取表格数据
  44. function getTableData(){
  45. post(api.getRolePage, params).then((res)=>{
  46. if(res.data.code===200){
  47. const data = res.data.data;
  48. setDataSource(data.records);
  49. setTotal(data.total)
  50. }
  51. })
  52. }
  53. //启用/禁用
  54. function enable(flag,id) {
  55. const param = {roleId:id||operId,status:flag};
  56. xPost(api.disableRole,param).then((res)=>{
  57. if(res.data.code===200){
  58. getTableData();
  59. setVisible(false);
  60. message.success((flag?'启用':'禁用')+"成功");
  61. }else{
  62. message.warning(res.data.msg||'操作失败');
  63. }
  64. }).catch(()=>{
  65. message.error("接口出错");
  66. });
  67. }
  68. //删除
  69. function onDelete(){
  70. const param = {roleId:operId};
  71. xPost(api.deleteRole,param).then((res)=>{
  72. if(res.data.code===200){
  73. getTableData();
  74. setVisible(false);
  75. message.success("删除成功");
  76. }else{
  77. message.warning(res.data.msg||'操作失败');
  78. }
  79. }).catch(()=>{
  80. message.error("接口出错");
  81. });
  82. }
  83. //显示弹窗
  84. function showModal(type,id){
  85. setModalType(type);
  86. setOperId(id);
  87. setVisible(true);
  88. }
  89. //弹窗确认事件
  90. function handleOk(){
  91. if("1,2".indexOf(modalType)>-1){
  92. onDelete();
  93. }else if("3,4".indexOf(modalType)>-1){
  94. enable(0);
  95. }
  96. }
  97. //弹窗取消
  98. function handleCancel(){
  99. setVisible(false);
  100. setAddVisible(false);
  101. }
  102. //新增角色弹窗
  103. function showAddRole(){
  104. setAddVisible(true);
  105. }
  106. function getRoleDetail(id){
  107. return xPost(api.getRoleById,{roleId:id});
  108. }
  109. //修改角色
  110. async function editRole(id){
  111. const data = await getRoleDetail(id);
  112. const detail = data.data;
  113. if(detail.code===200){
  114. const content= detail.data;
  115. const menuData = content.loginUserMenuResourceTree;
  116. const loginSoftwares = content.loginSoftwares;
  117. let softwares=[],sids=[];
  118. for(let i=0;i<menuData.length;i++){
  119. const obj = pickCheckedTreeIds(menuData[i]);
  120. softwares.push(obj);
  121. }
  122. for (let j = 0; j < loginSoftwares.length; j++) {
  123. const obj = loginSoftwares[j];
  124. if (obj.relation == '1') {
  125. sids.push(obj.id);
  126. }
  127. }
  128. const fData = Object.assign({},content,{sids,softwares});
  129. setRoleDetail(fData);
  130. }
  131. setAddVisible(true);
  132. }
  133. //保存
  134. function addRole(formData){
  135. let url = api.addRole;
  136. const arr = formData.softwares.filter((it)=>{
  137. if(Object.keys(it).length&&it.softwareMenuIds&&it.softwareMenuIds.length){
  138. return it;
  139. }
  140. });
  141. if(roleDetail){ //修改角色
  142. formData.id=roleDetail.id;
  143. url=api.updateRole;
  144. }
  145. formData.softwares=arr;
  146. post(url,formData).then((res)=>{
  147. if(res.data.code===200){
  148. getTableData();
  149. setAddVisible(false);
  150. setRoleDetail(null)
  151. message.success("添加成功");
  152. }else{
  153. message.warning(res.data.msg||'操作失败');
  154. }
  155. }).catch(()=>{
  156. message.error("接口出错");
  157. });
  158. }
  159. function goBack(){
  160. setAddVisible(false);
  161. setRoleDetail(null)
  162. }
  163. function onSizeChange(current, pageSize) {
  164. params.current = current
  165. params.size = pageSize
  166. getTableData()
  167. }
  168. function changePage(page, pageSize) {
  169. params.current = page
  170. params.size = pageSize
  171. getTableData()
  172. }
  173. //表格渲染
  174. function RenderTable(){
  175. const columns = [
  176. { title: '角色名称', dataIndex: 'name', key: 'name' },
  177. { title: '角色描述', dataIndex: 'remark', key: 'remark' },
  178. { title: '状态', key: 'statusName',render:(row) => {
  179. return (<span className={ (row.status==='1')?'Enable':'Disable' }>{row.statusName}</span>)
  180. }},
  181. /*{ title: '创建时间', dataIndex: 'gmtCreate', key: 'gmtCreate' },*/
  182. { title: '操作', key: 'operation', render: (row,i) => {
  183. //console.log(21,row)
  184. if(row.rootFlag){
  185. return '-'
  186. }
  187. return (<Space size="middle">
  188. <a onClick={()=>editRole(row.id)}>修改</a>
  189. {row.status === '1' ? (<a className="disable" onClick={()=>showModal(row.hasUserFlag?4:3,row.id)}>禁用</a>):(<a onClick={()=>enable(1,row.id)}>启用</a>)}
  190. <a className="delete" onClick={()=>showModal(row.hasUserFlag?2:1,row.id)}>删除</a>
  191. </Space>) }},
  192. ];
  193. return (
  194. <Table
  195. className="components-table-demo-nested"
  196. rowKey={record => record.id}
  197. columns={columns}
  198. dataSource={dataSource}
  199. pagination={{
  200. pageNo: 1,
  201. pageSize: params.size,
  202. pageSizeOptions: ['15', '30', '60', '120'],
  203. showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条数据`,
  204. onShowSizeChange: (current, pageSize) => onSizeChange(current, pageSize), // 改变每页数量时更新显示
  205. onChange: (page, pageSize) => changePage(page, pageSize),//点击页码事件
  206. total: total
  207. }}
  208. />
  209. )
  210. }
  211. //筛选项渲染
  212. const [form] = Form.useForm();
  213. const onFinish = (values) => {
  214. params = {
  215. ...values,
  216. ...params
  217. }
  218. getTableData();
  219. };
  220. const onReset = () => {
  221. form.resetFields();
  222. getTableData();
  223. };
  224. if(addVisible){
  225. return (
  226. <RoleContext.Provider value={{save:addRole,detail:roleDetail}}>
  227. <AddRole back={goBack}/>
  228. </RoleContext.Provider>
  229. )
  230. }
  231. return (
  232. <div className='wrapper'>
  233. <div className="filter-box">
  234. <Form form={form} name="control-hooks" onFinish={onFinish}>
  235. <Row gutter={24}>
  236. <Col span={5} key={0}>
  237. <Form.Item name="name" label="角色名称">
  238. <Input placeholder='角色名称' autoComplete='off'/>
  239. </Form.Item>
  240. </Col>
  241. <Col span={5} key={2}>
  242. <Form.Item name="status" label="当前状态">
  243. <Select
  244. allowClear
  245. >
  246. {statusList.map((item)=>{
  247. return (
  248. <Option value={item.name} key={item.name}>{item.val}</Option>
  249. )
  250. })}
  251. </Select>
  252. </Form.Item>
  253. </Col>
  254. <Col span={9} key={3}>
  255. <Form.Item>
  256. <Button type="primary" htmlType="submit">
  257. 查询
  258. </Button>
  259. <Button htmlType="button" onClick={onReset}>
  260. 重置
  261. </Button>
  262. </Form.Item>
  263. </Col>
  264. </Row>
  265. </Form>
  266. </div>
  267. <div className="table">
  268. <div className="table-header">
  269. <h2 className="table-title">组织管理</h2>
  270. <Button type="primary" icon={<PlusOutlined />} onClick={()=> showAddRole()}>新增角色</Button>
  271. </div>
  272. <RenderTable />
  273. </div>
  274. <Modal
  275. title="提示"
  276. okText='确定'
  277. cancelText='取消'
  278. width={400}
  279. visible={visible}
  280. onOk={handleOk}
  281. confirmLoading={confirmLoading}
  282. onCancel={handleCancel}
  283. >
  284. <p>{tipText[modalType]}</p>
  285. </Modal>
  286. </div>
  287. )
  288. }
  289. export default RoleManager;