import React, { useCallback, useEffect, useMemo, useState } from 'react' import styles from './index.module.scss' import { Button, Checkbox, Modal, Radio } from 'antd' import MyPopconfirm from '@/components/MyPopconfirm' import { Z1_APIgetAuthByUserId, Z1_APIsetAuth } from '@/store/action/Z1user' import classNmaes from 'classnames' import { MessageFu } from '@/utils/message' export type UserListType = { id: number name: string perm: boolean } type Props = { authInfo: { id: number; name: string } closeFu: () => void } function Z1auth({ authInfo, closeFu }: Props) { const getAuthByUserIdFu = useCallback(async () => { const res = await Z1_APIgetAuthByUserId(authInfo.id) if (res.code === 0) { const info = res.data setResources(info.resources) setDataScopeIds(info.scope) setDataScope(info.dataScope || 1) } }, [authInfo.id]) useEffect(() => { getAuthByUserIdFu() }, [getAuthByUserIdFu]) // 页面权限 const [resources, setResources] = useState([]) // 选择古桥或者全部 const [dataScope, setDataScope] = useState(0) // 古桥权限 const [dataScopeIds, setDataScopeIds] = useState([]) // 多选框变化 const onChange1 = useCallback( (val: boolean, id: number) => { setResources( resources.map(v => ({ ...v, perm: v.id === id ? val : v.perm })) ) }, [resources] ) const onChange2 = useCallback( (val: boolean, id: number) => { setDataScopeIds( dataScopeIds.map(v => ({ ...v, perm: v.id === id ? val : v.perm })) ) }, [dataScopeIds] ) // 页面权限id集合 const ids1 = useMemo(() => { return resources.filter(v => v.perm).map(c => c.id) }, [resources]) // 古桥权限id集合 const ids2 = useMemo(() => { return dataScopeIds.filter(v => v.perm).map(c => c.id) }, [dataScopeIds]) // 点击确定 const btnOkFu = useCallback(async () => { const obj = { userId: authInfo.id, resources: ids1, dataScopeIds: ids2, dataScope } const res = await Z1_APIsetAuth(obj) if (res.code === 0) { MessageFu.success('授权成功!') closeFu() } }, [authInfo.id, closeFu, dataScope, ids1, ids2]) // 按钮是否能点击 const btnFlag = useMemo(() => { let txt = '' if (ids1.length === 0) txt = '至少选中一个页面权限' else if (dataScope === 2 && ids2.length === 0) txt = '至少选中一个数据权限' return txt }, [dataScope, ids1.length, ids2.length]) return (
页面权限:
{resources.map(c => ( onChange1(e.target.checked, c.id)} > {c.name} ))}
数据权限:
setDataScope(e.target.value)} options={[ { value: 1, label: '全部' }, { value: 2, label: '按古桥' } ]} />
{btnFlag}
) } const MemoZ1auth = React.memo(Z1auth) export default MemoZ1auth