Z1auth.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React, { useCallback, useEffect, useMemo, useState } from 'react'
  2. import styles from './index.module.scss'
  3. import { Button, Checkbox, Modal, Radio } from 'antd'
  4. import MyPopconfirm from '@/components/MyPopconfirm'
  5. import { Z1_APIgetAuthByUserId, Z1_APIsetAuth } from '@/store/action/Z1user'
  6. import classNmaes from 'classnames'
  7. import { MessageFu } from '@/utils/message'
  8. export type UserListType = {
  9. id: number
  10. name: string
  11. perm: boolean
  12. }
  13. type Props = {
  14. authInfo: { id: number; name: string }
  15. closeFu: () => void
  16. }
  17. function Z1auth({ authInfo, closeFu }: Props) {
  18. const getAuthByUserIdFu = useCallback(async () => {
  19. const res = await Z1_APIgetAuthByUserId(authInfo.id)
  20. if (res.code === 0) {
  21. const info = res.data
  22. setResources(info.resources)
  23. setDataScopeIds(info.scope)
  24. setDataScope(info.dataScope || 1)
  25. }
  26. }, [authInfo.id])
  27. useEffect(() => {
  28. getAuthByUserIdFu()
  29. }, [getAuthByUserIdFu])
  30. // 页面权限
  31. const [resources, setResources] = useState<UserListType[]>([])
  32. // 选择古桥或者全部
  33. const [dataScope, setDataScope] = useState(0)
  34. // 古桥权限
  35. const [dataScopeIds, setDataScopeIds] = useState<UserListType[]>([])
  36. // 多选框变化
  37. const onChange1 = useCallback(
  38. (val: boolean, id: number) => {
  39. setResources(
  40. resources.map(v => ({
  41. ...v,
  42. perm: v.id === id ? val : v.perm
  43. }))
  44. )
  45. },
  46. [resources]
  47. )
  48. const onChange2 = useCallback(
  49. (val: boolean, id: number) => {
  50. setDataScopeIds(
  51. dataScopeIds.map(v => ({
  52. ...v,
  53. perm: v.id === id ? val : v.perm
  54. }))
  55. )
  56. },
  57. [dataScopeIds]
  58. )
  59. // 页面权限id集合
  60. const ids1 = useMemo(() => {
  61. return resources.filter(v => v.perm).map(c => c.id)
  62. }, [resources])
  63. // 古桥权限id集合
  64. const ids2 = useMemo(() => {
  65. return dataScopeIds.filter(v => v.perm).map(c => c.id)
  66. }, [dataScopeIds])
  67. // 点击确定
  68. const btnOkFu = useCallback(async () => {
  69. const obj = {
  70. userId: authInfo.id,
  71. resources: ids1,
  72. dataScopeIds: ids2,
  73. dataScope
  74. }
  75. const res = await Z1_APIsetAuth(obj)
  76. if (res.code === 0) {
  77. MessageFu.success('授权成功!')
  78. closeFu()
  79. }
  80. }, [authInfo.id, closeFu, dataScope, ids1, ids2])
  81. // 按钮是否能点击
  82. const btnFlag = useMemo(() => {
  83. let txt = ''
  84. if (ids1.length === 0) txt = '至少选中一个页面权限'
  85. else if (dataScope === 2 && ids2.length === 0) txt = '至少选中一个数据权限'
  86. return txt
  87. }, [dataScope, ids1.length, ids2.length])
  88. return (
  89. <Modal
  90. wrapClassName={styles.Z1auth}
  91. open={true}
  92. title={`${authInfo.name} - 权限管理`}
  93. footer={
  94. [] // 设置footer为空,去掉 取消 确定默认按钮
  95. }
  96. >
  97. <div className='Z1aEmain'>
  98. <div className='Z1aRow'>
  99. <div className='Z1aRowll'>页面权限:</div>
  100. <div className='Z1aRowrr'>
  101. {resources.map(c => (
  102. <Checkbox
  103. key={c.id}
  104. checked={c.perm}
  105. onChange={e => onChange1(e.target.checked, c.id)}
  106. >
  107. {c.name}
  108. </Checkbox>
  109. ))}
  110. </div>
  111. </div>
  112. <div className='Z1aRow'>
  113. <div className='Z1aRowll'>数据权限:</div>
  114. <div className='Z1aRowrr'>
  115. <Radio.Group
  116. value={dataScope}
  117. onChange={e => setDataScope(e.target.value)}
  118. options={[
  119. { value: 1, label: '全部' },
  120. { value: 2, label: '按古桥' }
  121. ]}
  122. />
  123. </div>
  124. </div>
  125. <div className='Z1aRow' hidden={dataScope !== 2}>
  126. <div className='Z1aRowll'>
  127. <Checkbox
  128. indeterminate={ids2.length > 0 && ids2.length < dataScopeIds.length}
  129. onChange={e => {
  130. const perm = e.target.checked
  131. setDataScopeIds(
  132. dataScopeIds.map(v => ({
  133. ...v,
  134. perm
  135. }))
  136. )
  137. }}
  138. checked={ids2.length === dataScopeIds.length}
  139. >
  140. {ids2.length === dataScopeIds.length ? '反' : '全'}选
  141. </Checkbox>
  142. </div>
  143. <div className='Z1aRowrr Z1aRowrr2'>
  144. {dataScopeIds.map(c => (
  145. <Checkbox
  146. key={c.id}
  147. checked={c.perm}
  148. onChange={e => onChange2(e.target.checked, c.id)}
  149. >
  150. {c.name}
  151. </Checkbox>
  152. ))}
  153. </div>
  154. </div>
  155. <div className={classNmaes('Z1aErr', btnFlag ? 'Z1aErrAc' : '')}>{btnFlag}</div>
  156. </div>
  157. <div className='Z1aEbtn'>
  158. <Button type='primary' onClick={btnOkFu} disabled={!!btnFlag}>
  159. 提交
  160. </Button>
  161. &emsp;
  162. <MyPopconfirm txtK='取消' onConfirm={closeFu} />
  163. </div>
  164. </Modal>
  165. )
  166. }
  167. const MemoZ1auth = React.memo(Z1auth)
  168. export default MemoZ1auth