index.tsx 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. import { useModel, useRequest } from 'umi'
  2. import { Association, 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 } from 'antd'
  6. import React, { useRef, useMemo, useState, useEffect } from 'react'
  7. import {
  8. fetchRoleStaffListByRoleId,
  9. updateRolePermission,
  10. getRolePermissions,
  11. deleteStaff
  12. } from '@/services/user/api'
  13. import type { ColumnsType } from 'antd/lib/table'
  14. import RoleMenu from '../System/components/RoleMenu'
  15. import ConnectModal from '../System/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 = { system: [], business: [], ...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. <Popconfirm
  109. title="确认删除吗?"
  110. okText="确认"
  111. cancelText="取消"
  112. onConfirm={() => tryDeleteStaff({ id: state.id, staffId: record.staffId })}>
  113. <span className="hover:text-hex-e7026e cursor-pointer">
  114. <Delete fill="#fd3995" />
  115. </span>
  116. </Popconfirm>
  117. )}
  118. </>
  119. )
  120. }
  121. ]
  122. return (
  123. <div className="h-full w-full flex flex-row">
  124. <RoleMenu menuId={menuId} onSelect={onSelect} itemCount={state.roleStaff?.length || 0} />
  125. <div className="w-max-3/4">
  126. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  127. <div className="absolute right-4 top-4 z-100">
  128. {state.id && (
  129. <ConnectModal
  130. title="关联员工"
  131. dataId={state.id}
  132. closeAfterSelect={false}
  133. onReload={() => tryGetRoleStaffList(state.id)}
  134. postUrl="/role/staff/add"
  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="showSystem"
  165. fieldProps={{
  166. onChange(checked) {
  167. if (!checked) {
  168. formRef.current?.setFieldsValue({ system: [] })
  169. }
  170. }
  171. }}
  172. label={
  173. <span className="flex items-center">
  174. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  175. 角色权限管理
  176. </span>
  177. }
  178. />
  179. <ProFormDependency name={['showSystem']}>
  180. {({ showSystem }) => (
  181. <ProFormCheckbox.Group
  182. wrapperCol={{ offset: 1 }}
  183. // initialValue={}
  184. name="system"
  185. options={[
  186. { value: 'system', label: '系统管理', disabled: !showSystem },
  187. { value: 'workbench', label: '工作台', disabled: !showSystem },
  188. { value: 'customer', label: '客户', disabled: !showSystem },
  189. { value: 'hr', label: '人资', disabled: !showSystem },
  190. { value: 'product', label: '产品', disabled: !showSystem }
  191. // { value: '4', label: '开票合同', disabled: !showAuth },
  192. // { value: '5', label: '考勤', disabled: !showAuth },
  193. // { value: '7', label: '财务费用', disabled: !showAuth }
  194. ]}
  195. />
  196. )}
  197. </ProFormDependency>
  198. <ProFormSwitch
  199. name="showBusiness"
  200. label={
  201. <span className="flex items-center">
  202. <Association className="mr-1" className="flex items-baseline mr-1" />
  203. 业务参数
  204. </span>
  205. }
  206. />
  207. <ProFormDependency name={['showBusiness']}>
  208. {({ showBusiness }) => (
  209. <ProFormCheckbox.Group
  210. wrapperCol={{ offset: 1 }}
  211. name="business"
  212. options={[
  213. { value: 'attendance', label: '考勤', disabled: !showBusiness },
  214. { value: 'contact', label: '客户', disabled: !showBusiness }
  215. ]}
  216. />
  217. )}
  218. </ProFormDependency>
  219. </ProForm>
  220. )}
  221. </div>
  222. </TabPane>
  223. </Tabs>
  224. </div>
  225. </div>
  226. </div>
  227. )
  228. }
  229. export default System