index.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 } from 'antd'
  6. import React, { useRef, useMemo, useState, useEffect } from 'react'
  7. import {
  8. fetchRoleStaffListByRoleId,
  9. updateRolePermission,
  10. getRolePermissions
  11. } from '@/services/user/api'
  12. import type { ColumnsType } from 'antd/lib/table'
  13. import { formatPermission } from '@/utils/utils'
  14. import RoleMenu from '../System/components/RoleMenu/roleMenu'
  15. import ConnectModal from '../System/components/ConnectModal'
  16. const Customer = () => {
  17. const { TabPane } = Tabs
  18. const formRef = useRef<FormInstance>(null)
  19. const columns: ColumnsType<API.RoleStaffListItem> = [
  20. {
  21. title: '用户',
  22. dataIndex: 'username',
  23. width: '15%'
  24. },
  25. {
  26. title: '手机',
  27. dataIndex: 'phone',
  28. width: '20%'
  29. },
  30. {
  31. title: '部门',
  32. dataIndex: 'departmentName',
  33. width: '20%'
  34. },
  35. {
  36. title: '岗位',
  37. dataIndex: 'position',
  38. width: '15%'
  39. },
  40. {
  41. title: '角色',
  42. dataIndex: 'age',
  43. width: '20%'
  44. },
  45. {
  46. title: '操作',
  47. dataIndex: 'opreate',
  48. width: '20%',
  49. render: () => (
  50. <span className="hover:text-hex-e7026e">
  51. <Delete fill="#fd3995" />
  52. </span>
  53. )
  54. }
  55. ]
  56. const { initialState } = useModel('@@initialState')
  57. const menuId = useMemo(() => {
  58. return initialState?.menuList?.find(item => item.name === '客户')?.id
  59. }, initialState.menuList)
  60. const [state, setState] = useState({
  61. id: '',
  62. roleStaff: [],
  63. rolePermission: {},
  64. activeKey: ''
  65. })
  66. const onSelect = (id: string) => {
  67. setState({ ...state, id })
  68. }
  69. const { run: tryGetRoleStaffList } = useRequest(
  70. (id: string) => fetchRoleStaffListByRoleId({ id }),
  71. {
  72. manual: true,
  73. onSuccess: result => {
  74. setState({ ...state, roleStaff: result })
  75. }
  76. }
  77. )
  78. const { run: tryGetRolePermissions } = useRequest((id: string) => getRolePermissions({ id }), {
  79. manual: true,
  80. onSuccess: (result: API.GetRolePermissionResultModel) => {
  81. const values = { ...formatPermission('init', result.permission) }
  82. setState({ ...state, rolePermission: values })
  83. }
  84. })
  85. useEffect(() => {
  86. if (state.id) {
  87. tryGetRoleStaffList(state.id)
  88. tryGetRolePermissions(state.id)
  89. }
  90. if (state.activeKey === '2') {
  91. formRef.current?.setFieldsValue({ ...state.rolePermission })
  92. }
  93. }, [state.id, state.activeKey])
  94. return (
  95. <div className="h-full w-full flex flex-row">
  96. <RoleMenu menuId={menuId} onSelect={onSelect} />
  97. <div className="w-max-3/4">
  98. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  99. <div className="absolute right-4 top-4 z-100">
  100. {state.id && (
  101. <ConnectModal
  102. title="关联员工"
  103. dataId={state.id}
  104. onSelect={() => tryGetRoleStaffList(state.id)}
  105. />
  106. )}
  107. </div>
  108. <Tabs
  109. defaultActiveKey="1"
  110. type="card"
  111. onChange={key => setState({ ...state, activeKey: key })}>
  112. <TabPane tab="员工列表" key="1">
  113. <Table<API.RoleStaffListItem>
  114. dataSource={state.roleStaff}
  115. columns={columns}
  116. rowKey={row => row.staffId}
  117. />
  118. </TabPane>
  119. <TabPane tab="角色权限" key="2">
  120. <div className="ml-4">
  121. {state.id && (
  122. <ProForm
  123. formRef={formRef}
  124. layout="horizontal"
  125. onFinish={async values => {
  126. const newValues = formatPermission('submit', values)
  127. await updateRolePermission({
  128. permission: JSON.stringify(newValues),
  129. id: state.id
  130. })
  131. message.success('设置成功')
  132. }}>
  133. <ProFormSwitch
  134. fieldProps={{
  135. onChange(checked) {
  136. if (!checked) {
  137. formRef.current?.setFieldsValue({ client: [] })
  138. }
  139. }
  140. }}
  141. name="showClient"
  142. label={
  143. <span className="flex items-center">
  144. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  145. 联系人
  146. </span>
  147. }
  148. />
  149. <ProFormDependency name={['showClient']}>
  150. {({ showClient }) => (
  151. <ProFormCheckbox.Group
  152. wrapperCol={{ offset: 1 }}
  153. name="client"
  154. options={[
  155. { value: 'access', label: '查看', disabled: !showClient },
  156. { value: 'add', label: '添加联系人', disabled: !showClient },
  157. { value: 'delete', label: '删除联系人', disabled: !showClient }
  158. ]}
  159. />
  160. )}
  161. </ProFormDependency>
  162. <ProFormSwitch
  163. fieldProps={{
  164. onChange(checked) {
  165. if (!checked) {
  166. formRef.current?.setFieldsValue({ company: [] })
  167. }
  168. }
  169. }}
  170. name="showCompany"
  171. label={
  172. <span className="flex items-center">
  173. <EveryUser className="mr-1" className="flex items-baseline mr-1" />
  174. 客户
  175. </span>
  176. }
  177. />
  178. <ProFormDependency name={['showCompany']}>
  179. {({ showCompany }) => (
  180. <ProFormCheckbox.Group
  181. wrapperCol={{ offset: 1 }}
  182. // initialValue={}
  183. name="company"
  184. options={[
  185. { value: 'access', label: '查看', disabled: !showCompany },
  186. { value: 'add', label: '添加客户', disabled: !showCompany },
  187. { value: 'delete', label: '删除客户', disabled: !showCompany }
  188. ]}
  189. />
  190. )}
  191. </ProFormDependency>
  192. </ProForm>
  193. )}
  194. </div>
  195. </TabPane>
  196. </Tabs>
  197. </div>
  198. </div>
  199. </div>
  200. )
  201. }
  202. export default Customer