index.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. import { useModel, useRequest } from 'umi'
  2. import { Delete, EveryUser } from '@icon-park/react'
  3. import ProForm, { ProFormCheckbox, ProFormDependency, ProFormSwitch } from '@ant-design/pro-form'
  4. import type { FormInstance } from 'antd'
  5. import { message, Table, Tabs, Popconfirm, Popover } from 'antd'
  6. import React, { useRef, useMemo, useState, useEffect } from 'react'
  7. import RoleMenu from './components/RoleMenu/roleMenu'
  8. import {
  9. fetchRoleStaffListByRoleId,
  10. updateRolePermission,
  11. getRolePermissions,
  12. deleteStaff
  13. } from '@/services/user/api'
  14. import type { ColumnsType } from 'antd/lib/table'
  15. import ConnectModal from './components/ConnectModal'
  16. import { formatPermission } from '@/utils/utils'
  17. const System = () => {
  18. const { TabPane } = Tabs
  19. const formRef = useRef<FormInstance>(null)
  20. const { initialState } = useModel('@@initialState')
  21. const menuId = useMemo(() => {
  22. return initialState?.menuList?.find(item => item.name === '系统管理')?.id
  23. }, initialState.menuList)
  24. const [state, setState] = useState({
  25. id: '',
  26. roleStaff: [],
  27. rolePermission: {},
  28. activeKey: ''
  29. })
  30. const onSelect = (id: string) => {
  31. setState({ ...state, id })
  32. }
  33. const { run: tryGetRoleStaffList } = useRequest(
  34. (id: string) => fetchRoleStaffListByRoleId({ id }),
  35. {
  36. manual: true,
  37. onSuccess: result => {
  38. setState({ ...state, roleStaff: result })
  39. }
  40. }
  41. )
  42. const { run: tryGetRolePermissions } = useRequest((id: string) => getRolePermissions({ id }), {
  43. manual: true,
  44. onSuccess: (result: API.GetRolePermissionResultModel) => {
  45. const values = { ...formatPermission('init', result.permission) }
  46. setState({ ...state, rolePermission: values })
  47. // formRef.current?.setFieldsValue({ ...values })
  48. }
  49. })
  50. const { run: tryDeleteStaff } = useRequest(
  51. (params: API.DeleteStaff) => {
  52. return deleteStaff(params)
  53. },
  54. {
  55. manual: true,
  56. onSuccess: () => {
  57. message.success('移除员工成功')
  58. tryGetRoleStaffList(state.id)
  59. }
  60. }
  61. )
  62. useEffect(() => {
  63. if (state.id) {
  64. tryGetRoleStaffList(state.id)
  65. tryGetRolePermissions(state.id)
  66. }
  67. if (state.activeKey === '2') {
  68. formRef.current?.setFieldsValue({ ...state.rolePermission })
  69. }
  70. return () => {
  71. formRef.current?.resetFields()
  72. }
  73. }, [state.id, state.activeKey])
  74. const columns: ColumnsType<API.RoleStaffListItem> = [
  75. {
  76. title: '用户',
  77. dataIndex: 'username',
  78. width: '15%'
  79. },
  80. {
  81. title: '手机',
  82. dataIndex: 'phone',
  83. width: '20%'
  84. },
  85. {
  86. title: '部门',
  87. dataIndex: 'departmentName',
  88. width: '20%'
  89. },
  90. {
  91. title: '岗位',
  92. dataIndex: 'position',
  93. width: '15%'
  94. },
  95. {
  96. title: '角色',
  97. dataIndex: 'age',
  98. width: '20%'
  99. },
  100. {
  101. title: '操作',
  102. dataIndex: 'opreate',
  103. width: '20%',
  104. render: (_, record) => (
  105. // console.log(record.staffId)
  106. <>
  107. {state.id && (
  108. <Popover>
  109. <Popconfirm
  110. title="确认删除吗?"
  111. okText="确认"
  112. cancelText="取消"
  113. onConfirm={() => tryDeleteStaff({ id: state.id, staffId: record.staffId })}>
  114. <span className="hover:text-hex-e7026e cursor-pointer">
  115. <Delete fill="#fd3995" />
  116. </span>
  117. </Popconfirm>
  118. </Popover>
  119. )}
  120. </>
  121. )
  122. }
  123. ]
  124. return (
  125. <div className="h-full w-full flex flex-row">
  126. <RoleMenu menuId={menuId} onSelect={onSelect} />
  127. <div className="w-max-3/4">
  128. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  129. <div className="absolute right-4 top-4 z-100">
  130. {state.id && (
  131. <ConnectModal
  132. title="关联员工"
  133. dataId={state.id}
  134. onSelect={() => tryGetRoleStaffList(state.id)}
  135. />
  136. )}
  137. </div>
  138. <Tabs
  139. defaultActiveKey="1"
  140. type="card"
  141. onChange={key => setState({ ...state, activeKey: key })}>
  142. <TabPane tab="员工列表" key="1">
  143. <Table<API.RoleStaffListItem>
  144. dataSource={state.roleStaff}
  145. columns={columns}
  146. rowKey={row => row.staffId}
  147. />
  148. </TabPane>
  149. <TabPane tab="角色权限" key="2">
  150. <div className="ml-4">
  151. {state.id && (
  152. <ProForm
  153. layout="horizontal"
  154. formRef={formRef}
  155. onFinish={async values => {
  156. const newValues = formatPermission('submit', values)
  157. await updateRolePermission({
  158. permission: JSON.stringify(newValues),
  159. id: state.id
  160. })
  161. message.success('设置成功')
  162. }}>
  163. {/* <ProFormSwitch
  164. name="showHome"
  165. label={
  166. <span className="flex items-center">
  167. <Icon type="home" className="mr-1" className="flex items-baseline mr-1" />
  168. 后台首页
  169. </span>
  170. }
  171. /> */}
  172. <ProFormSwitch
  173. name="showSystem"
  174. label={
  175. <span className="flex items-center">
  176. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  177. 角色权限管理
  178. </span>
  179. }
  180. />
  181. <ProFormDependency name={['showSystem']}>
  182. {({ showSystem }) => (
  183. <ProFormCheckbox.Group
  184. wrapperCol={{ offset: 1 }}
  185. // initialValue={}
  186. name="system"
  187. options={[
  188. { value: 'system', label: '系统管理', disabled: !showSystem },
  189. { value: 'customer', label: '客户', disabled: !showSystem }
  190. // { value: '3', label: '产品', disabled: !showAuth },
  191. // { value: '4', label: '开票合同', disabled: !showAuth },
  192. // { value: '5', label: '考勤', disabled: !showAuth },
  193. // { value: '6', label: '人资', disabled: !showAuth },
  194. // { value: '7', label: '财务费用', disabled: !showAuth }
  195. ]}
  196. />
  197. )}
  198. </ProFormDependency>
  199. {/* <ProFormSwitch
  200. name="showAudit"
  201. label={
  202. <span className="flex items-center">
  203. <Icon
  204. type="inspection"
  205. className="mr-1"
  206. className="flex items-baseline mr-1"
  207. />
  208. 审批流程
  209. </span>
  210. }
  211. />
  212. <ProFormDependency name={['showAudit']}>
  213. {({ showAudit }) => (
  214. <ProFormCheckbox.Group
  215. wrapperCol={{ offset: 1 }}
  216. name="audit"
  217. options={[
  218. { value: '1', label: '流程角色', disabled: !showAudit },
  219. { value: '2', label: '开票合同', disabled: !showAudit },
  220. { value: '3', label: '财务费用', disabled: !showAudit },
  221. { value: '4', label: '人资管理', disabled: !showAudit },
  222. { value: '5', label: '考勤', disabled: !showAudit }
  223. ]}
  224. />
  225. )}
  226. </ProFormDependency>
  227. <ProFormSwitch
  228. name="reset"
  229. label={
  230. <span className="flex items-center">
  231. <Icon
  232. type="association"
  233. className="mr-1"
  234. className="flex items-baseline mr-1"
  235. />
  236. 业务参数
  237. </span>
  238. }
  239. />
  240. <ProFormDependency name={['reset']}>
  241. {({ reset }) => (
  242. <ProFormCheckbox.Group
  243. wrapperCol={{ offset: 1 }}
  244. name="audit"
  245. options={[
  246. { value: '1', label: '客户', disabled: !reset },
  247. { value: '2', label: '产品', disabled: !reset }
  248. ]}
  249. />
  250. )}
  251. </ProFormDependency> */}
  252. </ProForm>
  253. )}
  254. </div>
  255. </TabPane>
  256. </Tabs>
  257. </div>
  258. </div>
  259. </div>
  260. )
  261. }
  262. export default System