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; |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 7 | |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 8 | let clickFunc = onClick |
| 9 | if(!roles || roles.length === 0 || roles.includes(levelRole)){ |
| 10 | clickFunc = onClick; |
| 11 | }else{ |
Krishya | dbfadaa | 2025-06-09 20:33:15 +0800 | [diff] [blame^] | 12 | clickFunc = (e) => { |
| 13 | e.preventDefault(); |
22301009 | 1e2aea7 | 2025-06-08 16:35:54 +0800 | [diff] [blame] | 14 | toast.error("权限不足"); |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | return ( |
| 19 | <button onClick={clickFunc} {...rest}> |
| 20 | {children} |
| 21 | </button> |
| 22 | ) |
| 23 | } |
| 24 | export default AuthButton; |