index.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 } from 'antd'
  6. import React, { useRef, useMemo, useState, useEffect } from 'react'
  7. import {
  8. fetchRoleStaffListByRoleId,
  9. updateRolePermission,
  10. getRolePermissions,
  11. // updatePermDataByRoleId,
  12. deleteStaff
  13. } from '@/services/user/api'
  14. import type { ColumnsType } from 'antd/lib/table'
  15. import { formatPermission } from '@/utils/utils'
  16. import RoleMenu from '../System/components/RoleMenu'
  17. import ConnectModal from '../System/components/ConnectModal'
  18. // import FormItem from 'antd/lib/form/FormItem'
  19. // const permData = [
  20. // {
  21. // label: '本人',
  22. // value: 'oneself'
  23. // },
  24. // {
  25. // label: '本人及下属',
  26. // value: 'underling'
  27. // },
  28. // {
  29. // label: '本部门',
  30. // value: 'department'
  31. // },
  32. // {
  33. // label: '本部门及下属部门',
  34. // value: 'departmentAll'
  35. // },
  36. // {
  37. // label: '全部',
  38. // value: 'all'
  39. // }
  40. // ]
  41. const Hr = () => {
  42. const { TabPane } = Tabs
  43. const formRef = useRef<FormInstance>(null)
  44. const formRef2 = useRef<FormInstance>(null)
  45. const { initialState } = useModel('@@initialState')
  46. const menuId = useMemo(() => {
  47. return initialState?.menuList?.find(item => item.name === '人资')?.id
  48. }, initialState.menuList)
  49. const [state, setState] = useState({
  50. id: '',
  51. roleStaff: [],
  52. rolePermission: {},
  53. dataPermission: {},
  54. activeKey: ''
  55. })
  56. const onSelect = (id: string) => {
  57. setState({ ...state, id })
  58. }
  59. const { run: tryGetRoleStaffList } = useRequest(
  60. (id: string) => fetchRoleStaffListByRoleId({ id }),
  61. {
  62. manual: true,
  63. onSuccess: result => {
  64. setState({ ...state, roleStaff: result })
  65. }
  66. }
  67. )
  68. const { run: tryGetRolePermissions } = useRequest((id: string) => getRolePermissions({ id }), {
  69. manual: true,
  70. onSuccess: (result: API.GetRolePermissionResultModel) => {
  71. const values = { department: [], ...formatPermission('init', result.permission) }
  72. setState({
  73. ...state,
  74. rolePermission: values,
  75. dataPermission: result.dataPermission ? JSON.parse(result.dataPermission) : null
  76. })
  77. formRef.current?.setFieldsValue({ ...values })
  78. formRef2.current?.setFieldsValue(
  79. result.dataPermission ? JSON.parse(result.dataPermission) : null
  80. )
  81. }
  82. })
  83. const { run: tryDeleteStaff } = useRequest(
  84. (params: API.DeleteStaff) => {
  85. return deleteStaff(params)
  86. },
  87. {
  88. manual: true,
  89. onSuccess: () => {
  90. message.success('移除员工成功')
  91. tryGetRoleStaffList(state.id)
  92. }
  93. }
  94. )
  95. useEffect(() => {
  96. if (state.id) {
  97. tryGetRoleStaffList(state.id)
  98. tryGetRolePermissions(state.id)
  99. }
  100. if (state.activeKey === '2') {
  101. // console.log('333', state.rolePermission)
  102. formRef.current?.setFieldsValue({ ...state.rolePermission })
  103. }
  104. if (state.activeKey === '3') {
  105. formRef2.current?.setFieldsValue({ ...state.dataPermission })
  106. }
  107. }, [state.id, state.activeKey])
  108. const columns: ColumnsType<API.RoleStaffListItem> = [
  109. {
  110. title: '用户',
  111. dataIndex: 'username',
  112. width: '15%'
  113. },
  114. {
  115. title: '手机',
  116. dataIndex: 'phone',
  117. width: '20%'
  118. },
  119. {
  120. title: '部门',
  121. dataIndex: 'departmentName',
  122. width: '20%'
  123. },
  124. {
  125. title: '岗位',
  126. dataIndex: 'position',
  127. width: '15%'
  128. },
  129. {
  130. title: '角色',
  131. dataIndex: 'age',
  132. width: '20%'
  133. },
  134. {
  135. title: '操作',
  136. dataIndex: 'opreate',
  137. width: '20%',
  138. render: (_, record) => (
  139. <Popconfirm
  140. title="确认删除吗?"
  141. okText="确认"
  142. cancelText="取消"
  143. onConfirm={() => tryDeleteStaff({ id: state.id, staffId: record.staffId })}>
  144. <div className="pl-2 text-hex-fd3995 cursor-pointer hover:text-hex-e7026e">
  145. <Delete />
  146. </div>
  147. </Popconfirm>
  148. )
  149. }
  150. ]
  151. return (
  152. <div className="h-full w-full flex flex-row">
  153. <RoleMenu menuId={menuId} onSelect={onSelect} itemCount={state.roleStaff?.length || 0} />
  154. <div className="w-max-3/4">
  155. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  156. <div className="absolute right-4 top-4 z-100">
  157. {state.id && (
  158. <ConnectModal
  159. title="关联员工"
  160. dataId={state.id}
  161. postUrl="/role/staff/add"
  162. closeAfterSelect={false}
  163. onReload={() => tryGetRoleStaffList(state.id)}
  164. />
  165. )}
  166. </div>
  167. <Tabs
  168. defaultActiveKey="1"
  169. type="card"
  170. onChange={key => setState({ ...state, activeKey: key })}>
  171. <TabPane tab="员工列表" key="1">
  172. <Table<API.RoleStaffListItem>
  173. dataSource={state.roleStaff}
  174. columns={columns}
  175. rowKey={row => row.staffId}
  176. />
  177. </TabPane>
  178. <TabPane tab="角色权限" key="2">
  179. <div className="ml-4">
  180. {state.id && (
  181. <ProForm
  182. formRef={formRef}
  183. layout="horizontal"
  184. onFinish={async values => {
  185. const newValues = formatPermission('submit', values)
  186. await updateRolePermission({
  187. permission: JSON.stringify(newValues),
  188. id: state.id
  189. })
  190. message.success('设置成功')
  191. }}>
  192. <ProFormSwitch
  193. fieldProps={{
  194. onChange(checked) {
  195. if (!checked) {
  196. formRef.current?.setFieldsValue({ employee: [] })
  197. }
  198. }
  199. }}
  200. name="showEmployee"
  201. label={
  202. <span className="flex items-center">
  203. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  204. 部门与员工
  205. </span>
  206. }
  207. />
  208. <ProFormDependency name={['showEmployee']}>
  209. {({ showEmployee }) => (
  210. <ProFormCheckbox.Group
  211. wrapperCol={{ offset: 1 }}
  212. name="employee"
  213. options={[
  214. { value: 'access', label: '查看', disabled: !showEmployee },
  215. { value: 'add', label: '添加', disabled: !showEmployee },
  216. { value: 'delete', label: '删除', disabled: !showEmployee },
  217. { value: 'add_department', label: '新增部门', disabled: !showEmployee },
  218. { value: 'del_department', label: '删除部门', disabled: !showEmployee }
  219. ]}
  220. />
  221. )}
  222. </ProFormDependency>
  223. <ProFormSwitch
  224. fieldProps={{
  225. onChange(checked) {
  226. if (!checked) {
  227. formRef.current?.setFieldsValue({ notification: [] })
  228. }
  229. }
  230. }}
  231. name="showNotification"
  232. label={
  233. <span className="flex items-center">
  234. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  235. 通知中心
  236. </span>
  237. }
  238. />
  239. <ProFormDependency name={['showNotification']}>
  240. {({ showNotification }) => (
  241. <ProFormCheckbox.Group
  242. wrapperCol={{ offset: 1 }}
  243. name="notification"
  244. options={[
  245. { value: 'access', label: '查看', disabled: !showNotification },
  246. { value: 'add', label: '添加', disabled: !showNotification }
  247. ]}
  248. />
  249. )}
  250. </ProFormDependency>
  251. </ProForm>
  252. )}
  253. </div>
  254. </TabPane>
  255. {/* <TabPane tab="数据权限" key="3">
  256. {state.id && (
  257. <ProForm
  258. formRef={formRef2}
  259. layout="vertical"
  260. onFinish={async values => {
  261. try {
  262. await updatePermDataByRoleId({
  263. id: state.id,
  264. dataPermission: JSON.stringify(values)
  265. })
  266. } catch (error) {
  267. return message.error(error.toString())
  268. }
  269. message.success('设置成功')
  270. return true
  271. }}>
  272. <FormItem name="access" label="联系人/客户可见" required>
  273. <Radio.Group>
  274. <Space direction="vertical">
  275. {permData.map(item => (
  276. <Radio key={item.value} value={item.value}>
  277. {item.label}
  278. </Radio>
  279. ))}
  280. </Space>
  281. </Radio.Group>
  282. </FormItem>
  283. </ProForm>
  284. )}
  285. </TabPane> */}
  286. </Tabs>
  287. </div>
  288. </div>
  289. </div>
  290. )
  291. }
  292. export default Hr