index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import { useEffect, useState } from 'react';
  2. import { useSelector } from 'react-redux'
  3. import { Table, Modal, message, Menu, Breadcrumb, Dropdown, Space, Form, Input, Button, Row, Col, Select } from 'antd';
  4. import { DownOutlined, PlusOutlined } from '@ant-design/icons';
  5. import AddFunc from './AddFunc';
  6. // import './index.less';
  7. import apiObj from '@api/index';
  8. import FuncContext from './func-context';
  9. import utils from '@utils/index'
  10. const { pickCheckedTreeIds } = utils;
  11. const { post, api, xPost } = apiObj;
  12. const { Option } = Select;
  13. function OrgManager() {
  14. useEffect(() => {
  15. //刷新列表
  16. getMenusTree();
  17. }, []);
  18. const [form] = Form.useForm();
  19. const [dataSource, setDataSource] = useState([]); //列表数据
  20. const [visible, setVisible] = useState(false); //删除禁用确认弹窗显示
  21. const [addVisible, setAddVisible] = useState(false); //新增页面显示
  22. const [modalType, setModalType] = useState(1);
  23. const [id, setId] = useState(''); //当前操作功能id
  24. const [funcDetail, setFuncDetail] = useState(null);
  25. const [typeId, setTypeId] = useState('');
  26. const [title, setTitle] = useState('');
  27. const [type, setType] = useState('');
  28. //从state中取出状态、类型列表
  29. const userInfo = useSelector(state => {
  30. return state.userInfo;
  31. });
  32. const { sysId, sysName } = userInfo;
  33. const tipText = {
  34. 1: '确定要删除该功能?',
  35. 2: '确定要禁用该功能?',
  36. };
  37. //获取表格数据
  38. function getMenusTree(param = {}) {
  39. post(api.getMenusTree, param).then((res) => {
  40. if (res.data.code === 200) {
  41. const data = res.data.data;
  42. setDataSource(data);
  43. }
  44. })
  45. }
  46. //启用/禁用
  47. function enable(flag, id, type) {
  48. const param = { id: id, status: flag };
  49. xPost(api.disableMenu, param).then((res) => {
  50. if (res.data.code === 200) {
  51. getMenusTree();
  52. setVisible(false);
  53. message.success((flag ? '启用' : '禁用') + "成功");
  54. } else {
  55. message.warning(res.data.msg || '操作失败');
  56. }
  57. }).catch(() => {
  58. message.error("接口出错");
  59. });
  60. }
  61. //删除
  62. function onDelete() {
  63. const param = { id: id };
  64. xPost(api.deleteMenu, param).then((res) => {
  65. if (res.data.code === 200) {
  66. getMenusTree();
  67. setVisible(false);
  68. message.success("删除成功");
  69. } else {
  70. message.warning(res.data.msg || '操作失败');
  71. }
  72. }).catch(() => {
  73. message.error("接口出错");
  74. });
  75. }
  76. //显示弹窗
  77. function showModal(name, type, flag, id) {
  78. setTitle(name);
  79. setAddVisible(type)
  80. setType(flag)
  81. setId(id)
  82. if (flag == 2) {
  83. getMenuById(id)
  84. }else if(flag == 1){
  85. setFuncDetail({
  86. status: 1,
  87. parentId: 0,
  88. parentName: sysName
  89. })
  90. }
  91. }
  92. //新增子功能
  93. function addSubFunc(name, type, flag, id, parentName) {
  94. setTitle(name);
  95. setAddVisible(type)
  96. setType(flag)
  97. setFuncDetail({
  98. status: 1,
  99. parentId: id,
  100. parentName: parentName,
  101. })
  102. }
  103. //弹窗确认事件
  104. function handleOk() {
  105. if (modalType == 1) {
  106. onDelete();
  107. } else if (modalType == 2) {
  108. enable(0, id);
  109. }
  110. }
  111. const messageBox = (type, id) => {
  112. setVisible(true)
  113. setId(id)
  114. setModalType(type)
  115. }
  116. //弹窗取消
  117. function handleCancel() {
  118. setVisible(false);
  119. }
  120. function getMenuById(id) {
  121. xPost(api.getMenuById, { id: id, softwareId: sysId }).then((res) => {
  122. const { data, code } = res.data;
  123. if (code === 200) {
  124. const detail = data
  125. detail.softwares = [detail.softwareId]
  126. setFuncDetail(detail)
  127. } else {
  128. message.warning(res.data.msg || '获取详情失败')
  129. }
  130. });
  131. }
  132. function cancel() {
  133. setAddVisible(false)
  134. setFuncDetail(null)
  135. }
  136. //表格渲染
  137. function RenderTable() {
  138. const columns = [
  139. { title: '组织机构层级', dataIndex: 'name', key: 'name' },
  140. {
  141. title: '类型', key: 'type', render: (row) => {
  142. if (row.children) {
  143. return '-'
  144. } else {
  145. return row.name;
  146. }
  147. }
  148. },
  149. {
  150. title: '链接地址', key: 'type', render: (row) => {
  151. if (row.children) {
  152. return '-'
  153. } else {
  154. return row.permission;
  155. }
  156. }
  157. },
  158. { title: '权限标识', dataIndex: 'code', key: 'code' },
  159. {
  160. title: '状态', key: 'status', render: (row) => {
  161. return (<span className={(row.status === '1') ? 'Enable' : 'Disable'}>{row.statusName}</span>);
  162. }
  163. },
  164. { title: '排序', dataIndex: 'orderNo', key: 'orderNo' },
  165. {
  166. title: '操作', width: 240, key: 'operation', render: (row) => {
  167. if (row.rootFlag) {
  168. return '-'
  169. }
  170. const menu = (
  171. <Menu>
  172. <Menu.Item key="0" className="menuItem">
  173. {row.status === '1' ? (<a onClick={() => messageBox(2, row.id)}>禁用</a>) : (<a onClick={() => enable(1, row.id)}>启用</a>)}
  174. </Menu.Item>
  175. <Menu.Item className="menuItem" key="1" onClick={() => messageBox(1, row.id)}>删除</Menu.Item>
  176. </Menu>
  177. );
  178. return (<Space size="middle">
  179. <a onClick={() => addSubFunc('新增子功能', true, 3, row.id, row.name)}>新增子功能</a>
  180. <a onClick={() => showModal('修改功能', true, 2, row.id, row.softwareId)}>修改</a>
  181. <Dropdown overlay={menu} trigger={['click']}>
  182. <a className="ant-dropdown-link">
  183. 更多 <DownOutlined />
  184. </a>
  185. </Dropdown>
  186. </Space>)
  187. }
  188. },
  189. ];
  190. return (
  191. <Table
  192. pagination={false}
  193. className="components-table-demo-nested"
  194. rowKey={record => record.id}
  195. columns={columns}
  196. dataSource={dataSource}
  197. />
  198. )
  199. }
  200. const onFinish = (values: any) => {
  201. getMenusTree(values);
  202. console.log('筛选项:', values);
  203. };
  204. function userChange() {
  205. setAddVisible(false)
  206. setFuncDetail(null)
  207. }
  208. return (
  209. <div className='wrapper'>
  210. <div className="table">
  211. <div className="table-header">
  212. <h2 className="table-title">功能管理</h2>
  213. <Button type="primary" icon={<PlusOutlined />} onClick={e => showModal('新增一级功能', true, 1)}>新增一级功能</Button>
  214. </div>
  215. <RenderTable />
  216. </div>
  217. {addVisible && funcDetail ?
  218. <Modal
  219. title={title}
  220. okText='确定'
  221. cancelText='取消'
  222. width={'45%'}
  223. visible={addVisible}
  224. onCancel={cancel}
  225. footer={null}
  226. forceRender={true}
  227. >
  228. <FuncContext.Provider value={{ type: type, id: id, detail: funcDetail }}>
  229. <AddFunc userChange={userChange} />
  230. </FuncContext.Provider>
  231. </Modal>
  232. : ''}
  233. <Modal
  234. title="提示"
  235. okText='确定'
  236. cancelText='取消'
  237. width={400}
  238. visible={visible}
  239. onOk={handleOk}
  240. onCancel={handleCancel}
  241. >
  242. <p>{tipText[modalType]}</p>
  243. </Modal>
  244. </div>
  245. )
  246. }
  247. export default OrgManager;