index.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. import { useModel, useRequest } from 'umi'
  2. import { Delete, DataLock } 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, Radio, Space, 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. import { permData } from '../Customer'
  20. import styles from './index.less'
  21. import classNames from 'classnames'
  22. const Product = () => {
  23. const { TabPane } = Tabs
  24. const formRef = useRef<FormInstance>(null)
  25. const formRef2 = useRef<FormInstance>(null)
  26. const { initialState } = useModel('@@initialState')
  27. const menuId = useMemo(() => {
  28. return initialState?.menuList?.find(item => item.name === '产品')?.id
  29. }, initialState.menuList)
  30. const [state, setState] = useState({
  31. id: '',
  32. roleStaff: [],
  33. rolePermission: {},
  34. dataPermission: {},
  35. activeKey: ''
  36. })
  37. const onSelect = (id: string) => {
  38. setState({ ...state, id })
  39. }
  40. const { run: tryGetRoleStaffList } = useRequest(
  41. (id: string) => fetchRoleStaffListByRoleId({ id }),
  42. {
  43. manual: true,
  44. onSuccess: result => {
  45. setState({ ...state, roleStaff: result })
  46. }
  47. }
  48. )
  49. const { run: tryGetRolePermissions } = useRequest((id: string) => getRolePermissions({ id }), {
  50. manual: true,
  51. onSuccess: (result: API.GetRolePermissionResultModel) => {
  52. const values = {
  53. building: [],
  54. curing: [],
  55. highway: [],
  56. gs: [],
  57. personal: [],
  58. enterprise: [],
  59. ...formatPermission('init', result.permission)
  60. }
  61. // 特殊处理云版switch
  62. if (values.showBuilding || values.showCuring || values.showHighway || values.showGs) {
  63. values.showCloud = true
  64. delete values.showBuilding
  65. delete values.showCuring
  66. delete values.showHighway
  67. delete values.showGs
  68. }
  69. // 特殊处理大司空2.0
  70. if (values.showPersonal || values.showEnterprise) {
  71. values.showRoad = true
  72. delete values.showPersonal
  73. delete values.showEnterprise
  74. }
  75. setState({
  76. ...state,
  77. rolePermission: values,
  78. dataPermission: result.dataPermission ? JSON.parse(result.dataPermission) : null
  79. })
  80. formRef.current?.setFieldsValue({ ...values })
  81. formRef2.current?.setFieldsValue(
  82. result.dataPermission ? JSON.parse(result.dataPermission) : null
  83. )
  84. }
  85. })
  86. const { run: tryDeleteStaff } = useRequest(
  87. (params: API.DeleteStaff) => {
  88. return deleteStaff(params)
  89. },
  90. {
  91. manual: true,
  92. onSuccess: () => {
  93. message.success('移除员工成功')
  94. tryGetRoleStaffList(state.id)
  95. }
  96. }
  97. )
  98. useEffect(() => {
  99. if (state.id) {
  100. tryGetRoleStaffList(state.id)
  101. tryGetRolePermissions(state.id)
  102. }
  103. if (state.activeKey === '2') {
  104. formRef.current?.setFieldsValue({ ...state.rolePermission })
  105. }
  106. if (state.activeKey === '3') {
  107. formRef2.current?.setFieldsValue({ ...state.dataPermission })
  108. }
  109. }, [state.id, state.activeKey])
  110. const columns: ColumnsType<API.RoleStaffListItem> = [
  111. {
  112. title: '用户',
  113. dataIndex: 'username',
  114. width: '15%'
  115. },
  116. {
  117. title: '手机',
  118. dataIndex: 'phone',
  119. width: '20%'
  120. },
  121. {
  122. title: '部门',
  123. dataIndex: 'departmentName',
  124. width: '20%'
  125. },
  126. {
  127. title: '岗位',
  128. dataIndex: 'position',
  129. width: '15%'
  130. },
  131. {
  132. title: '角色',
  133. dataIndex: 'age',
  134. width: '20%'
  135. },
  136. {
  137. title: '操作',
  138. dataIndex: 'opreate',
  139. width: '20%',
  140. render: (_, record) => (
  141. <Popconfirm
  142. title="确认删除吗?"
  143. okText="确认"
  144. cancelText="取消"
  145. onConfirm={() => tryDeleteStaff({ id: state.id, staffId: record.staffId })}>
  146. <div className="pl-2 text-hex-fd3995 cursor-pointer hover:text-hex-e7026e">
  147. <Delete />
  148. </div>
  149. </Popconfirm>
  150. )
  151. }
  152. ]
  153. return (
  154. <div className="h-full w-full flex flex-row">
  155. <RoleMenu menuId={menuId} onSelect={onSelect} itemCount={state.roleStaff?.length || 0} />
  156. <div className="w-max-3/4">
  157. <div className="ml-8 bg-white p-4 shadow-md shadow-hex-3e2c5a relative">
  158. <div className="absolute right-4 top-4 z-100">
  159. {state.id && (
  160. <ConnectModal
  161. title="关联员工"
  162. dataId={state.id}
  163. postUrl="/role/staff/add"
  164. closeAfterSelect={false}
  165. onReload={() => tryGetRoleStaffList(state.id)}
  166. />
  167. )}
  168. </div>
  169. <Tabs
  170. defaultActiveKey="1"
  171. type="card"
  172. onChange={key => setState({ ...state, activeKey: key })}>
  173. <TabPane tab="员工列表" key="1">
  174. <Table<API.RoleStaffListItem>
  175. dataSource={state.roleStaff}
  176. columns={columns}
  177. rowKey={row => row.staffId}
  178. />
  179. </TabPane>
  180. <TabPane tab="角色权限" key="2">
  181. <div className="ml-4">
  182. {state.id && (
  183. <ProForm
  184. formRef={formRef}
  185. layout="horizontal"
  186. onFinish={async values => {
  187. const newValues = formatPermission('submit', values)
  188. await updateRolePermission({
  189. permission: JSON.stringify(newValues),
  190. id: state.id
  191. })
  192. message.success('设置成功')
  193. }}>
  194. <ProFormSwitch
  195. fieldProps={{
  196. onChange(checked) {
  197. if (!checked) {
  198. formRef.current?.setFieldsValue({ lockstore: [] })
  199. }
  200. }
  201. }}
  202. name="showLockstore"
  203. label={
  204. <span className="flex items-center">
  205. <DataLock theme="outline" className="flex items-baseline mr-1" />
  206. 软件锁-公共锁库
  207. </span>
  208. }
  209. />
  210. <ProFormDependency name={['showLockstore']}>
  211. {({ showLockstore }) => (
  212. <ProFormCheckbox.Group
  213. wrapperCol={{ offset: 1 }}
  214. name="lockstore"
  215. options={[
  216. { value: 'access', label: '查看', disabled: !showLockstore },
  217. { value: 'delete_record', label: '删除记录', disabled: !showLockstore }
  218. ]}
  219. />
  220. )}
  221. </ProFormDependency>
  222. <ProFormSwitch
  223. fieldProps={{
  224. onChange(checked) {
  225. if (!checked) {
  226. formRef.current?.setFieldsValue({
  227. building: [],
  228. curing: [],
  229. highway: [],
  230. gs: []
  231. })
  232. }
  233. }
  234. }}
  235. name="showCloud"
  236. label={<span className="flex items-center">云版管理</span>}
  237. />
  238. <div className={classNames('ml-6', styles.laberContent)}>
  239. <ProFormDependency name={['showCloud']}>
  240. {({ showCloud }) => (
  241. <>
  242. <ProFormCheckbox.Group
  243. wrapperCol={{ offset: 1 }}
  244. name="building"
  245. label="大司空云计价"
  246. options={[{ value: 'access', label: '查看', disabled: !showCloud }]}
  247. />
  248. <ProFormCheckbox.Group
  249. wrapperCol={{ offset: 1 }}
  250. name="curing"
  251. label="养护云造价"
  252. options={[{ value: 'access', label: '查看', disabled: !showCloud }]}
  253. />
  254. <ProFormCheckbox.Group
  255. wrapperCol={{ offset: 1 }}
  256. name="highway"
  257. label="公路云计价"
  258. options={[{ value: 'access', label: '查看', disabled: !showCloud }]}
  259. />
  260. <ProFormCheckbox.Group
  261. wrapperCol={{ offset: 1 }}
  262. name="gs"
  263. label="大司空概算"
  264. options={[{ value: 'access', label: '查看', disabled: !showCloud }]}
  265. />
  266. </>
  267. )}
  268. </ProFormDependency>
  269. </div>
  270. <ProFormSwitch
  271. fieldProps={{
  272. onChange(checked) {
  273. if (!checked) {
  274. formRef.current?.setFieldsValue({ personal: [], enterprise: [] })
  275. }
  276. }
  277. }}
  278. name="showRoad"
  279. label={<span className="flex items-center">大司空2.0</span>}
  280. />
  281. <div className={['ml-6', styles.laberContent].join(' ')}>
  282. <ProFormDependency name={['showRoad']}>
  283. {({ showRoad }) => (
  284. <>
  285. <ProFormCheckbox.Group
  286. wrapperCol={{ offset: 1 }}
  287. name="personal"
  288. label="个人版"
  289. options={[{ value: 'access', label: '查看', disabled: !showRoad }]}
  290. />
  291. <ProFormCheckbox.Group
  292. wrapperCol={{ offset: 1 }}
  293. name="enterprise"
  294. label="企业版"
  295. options={[
  296. { value: 'access', label: '查看', disabled: !showRoad },
  297. { value: 'add', label: '添加企业', disabled: !showRoad }
  298. ]}
  299. />
  300. </>
  301. )}
  302. </ProFormDependency>
  303. </div>
  304. </ProForm>
  305. )}
  306. </div>
  307. </TabPane>
  308. <TabPane tab="数据权限" key="3">
  309. {state.id && (
  310. <ProForm
  311. formRef={formRef2}
  312. layout="vertical"
  313. onFinish={async values => {
  314. try {
  315. await updatePermDataByRoleId({
  316. id: state.id,
  317. dataPermission: JSON.stringify(values)
  318. })
  319. } catch (error) {
  320. return message.error(error.toString())
  321. }
  322. message.success('设置成功')
  323. return true
  324. }}>
  325. <FormItem name="access" label="客户模块可见" required>
  326. <Radio.Group>
  327. <Space direction="vertical">
  328. {permData.map(item => (
  329. <Radio key={item.value} value={item.value}>
  330. {item.label}
  331. </Radio>
  332. ))}
  333. </Space>
  334. </Radio.Group>
  335. </FormItem>
  336. </ProForm>
  337. )}
  338. </TabPane>
  339. </Tabs>
  340. </div>
  341. </div>
  342. </div>
  343. )
  344. }
  345. export default Product