22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame^] | 1 | import {useContext} from "react"; |
| 2 | import { UserContext } from "../context/UserContext"; |
| 3 | import toast from "react-hot-toast"; |
| 4 | const AuthButton = ({children, roles,onClick, ...rest }) => { |
| 5 | const {user} = useContext(UserContext); |
| 6 | const {levelRole} = user; |
| 7 | let clickFunc = onClick |
| 8 | if(!roles || roles.length === 0 || roles.includes(levelRole)){ |
| 9 | clickFunc = onClick; |
| 10 | }else{ |
| 11 | clickFunc = () => { |
| 12 | toast.error("权限不足"); |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | return ( |
| 17 | <button onClick={clickFunc} {...rest}> |
| 18 | {children} |
| 19 | </button> |
| 20 | ) |
| 21 | } |
| 22 | export default AuthButton; |