TRM-coding | c4b4f3d | 2025-06-18 19:02:46 +0800 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import { Button, Modal } from 'antd'; |
| 3 | import { LogoutOutlined } from '@ant-design/icons'; |
| 4 | import { clearAuthInfo, getUserInfo } from '../utils/auth'; |
| 5 | |
| 6 | const LogoutButton = ({ style = {}, onLogout = null }) => { |
| 7 | const userInfo = getUserInfo(); |
| 8 | |
| 9 | const handleLogout = () => { |
| 10 | Modal.confirm({ |
| 11 | title: '确认退出', |
| 12 | content: '您确定要退出登录吗?', |
| 13 | okText: '确定', |
| 14 | cancelText: '取消', |
| 15 | onOk: () => { |
| 16 | // 清除认证信息,但保留记住的登录信息 |
| 17 | clearAuthInfo(false); |
| 18 | |
| 19 | // 执行回调函数 |
| 20 | if (onLogout) { |
| 21 | onLogout(); |
| 22 | } else { |
| 23 | // 默认跳转到登录页 |
| 24 | window.location.href = '/'; |
| 25 | } |
| 26 | } |
| 27 | }); |
| 28 | }; |
| 29 | |
| 30 | const handleCompleteLogout = () => { |
| 31 | Modal.confirm({ |
| 32 | title: '完全退出', |
| 33 | content: '这将清除所有保存的登录信息,包括"记住我"的设置。确定要继续吗?', |
| 34 | okText: '确定', |
| 35 | cancelText: '取消', |
| 36 | onOk: () => { |
| 37 | // 清除所有认证信息,包括记住的登录信息 |
| 38 | clearAuthInfo(true); |
| 39 | |
| 40 | // 执行回调函数 |
| 41 | if (onLogout) { |
| 42 | onLogout(); |
| 43 | } else { |
| 44 | // 默认跳转到登录页 |
| 45 | window.location.href = '/'; |
| 46 | } |
| 47 | } |
| 48 | }); |
| 49 | }; |
| 50 | |
| 51 | if (!userInfo) { |
| 52 | return null; |
| 53 | } |
| 54 | |
| 55 | return ( |
| 56 | <div style={style}> |
| 57 | <Button |
| 58 | type="default" |
| 59 | icon={<LogoutOutlined />} |
| 60 | onClick={handleLogout} |
| 61 | style={{ marginRight: 8 }} |
| 62 | > |
| 63 | 退出登录 |
| 64 | </Button> |
| 65 | <Button |
| 66 | type="link" |
| 67 | size="small" |
| 68 | onClick={handleCompleteLogout} |
| 69 | style={{ color: '#ff4d4f' }} |
| 70 | > |
| 71 | 完全退出 |
| 72 | </Button> |
| 73 | </div> |
| 74 | ); |
| 75 | }; |
| 76 | |
| 77 | export default LogoutButton; |