8a48765a46
集中保留原版权声明,移除文件头检查和全局测试忽略规则。验证:全量单测、受影响用例复测及静态检查通过。
234 lines
8.3 KiB
React
234 lines
8.3 KiB
React
import React, { useState, useContext } from 'react';
|
||
import { Link } from 'react-router-dom';
|
||
import { isMiniProgramWebView } from '../../../helpers/miniProgram';
|
||
import { Button as AntButton, Divider, Tooltip } from 'antd';
|
||
import { Share2, BookOpen, Download, Wallet } from 'lucide-react';
|
||
import NewYearButton from './NewYearButton';
|
||
import NotificationButton from './NotificationButton';
|
||
import ThemeToggle from './ThemeToggle';
|
||
import LanguageSelector from './LanguageSelector';
|
||
import UserArea from './UserArea';
|
||
import PromotionPosterModal from '../../promotion/PromotionPosterModal';
|
||
import { StatusContext } from '../../../context/Status';
|
||
import { isHeaderActionButtonVisible } from '../../../helpers/navigationVisibility';
|
||
import { useWeyAiDownloadEntry } from '../../../hooks/common/useWeyAiWebEntry';
|
||
|
||
const ActionButtons = ({
|
||
isAuthPage = false,
|
||
isNewYear,
|
||
unreadCount,
|
||
onNoticeOpen,
|
||
theme,
|
||
onThemeToggle,
|
||
currentLang,
|
||
onLanguageChange,
|
||
userState,
|
||
isLoading,
|
||
isMobile,
|
||
headerNavModules,
|
||
sidebarModulesAdmin,
|
||
isOrgUser,
|
||
logout,
|
||
navigate,
|
||
t,
|
||
}) => {
|
||
const [promotionVisible, setPromotionVisible] = useState(false);
|
||
const [statusState] = useContext(StatusContext);
|
||
const isLoggedIn = userState?.user?.username;
|
||
const userGroup = userState?.user?.group || 'default';
|
||
// WeyAi 桌面端下载快捷入口:判定与用户中心侧栏项共用 useWeyAiDownloadEntry
|
||
// (下载页开关 + 登录 + 网页端已配置时沿用其开放范围);管理员可在「功能按钮」
|
||
// 里全局关闭,企业员工顶栏可在 rightActions 里单独关闭
|
||
const { visible: weyaiDownloadVisible } = useWeyAiDownloadEntry();
|
||
|
||
// 顶栏功能按钮显示配置(新手教程 / 推广 / 充值)。
|
||
// 数据源与 HeaderNavModules 一致,走公开的 /api/status(StatusContext / localStorage 缓存),
|
||
// 普通用户也能读取;不要用 /api/option/,那是管理员(RootAuth)专用接口,普通用户会 403。
|
||
let buttonConfig = null;
|
||
try {
|
||
const raw =
|
||
statusState?.status?.HeaderActionButtons ||
|
||
(localStorage.getItem('status')
|
||
? JSON.parse(localStorage.getItem('status')).HeaderActionButtons
|
||
: null);
|
||
if (raw) {
|
||
buttonConfig = JSON.parse(raw);
|
||
}
|
||
} catch (e) {
|
||
buttonConfig = null;
|
||
}
|
||
|
||
// 企业员工:教程/推广/充值 这三个主操作按钮由 HeaderActionButtons 控制,
|
||
// 但企业顶栏配置走 headerNavModules.rightActions,故对 org 用户额外按 rightActions 隐藏。
|
||
// 注意 key 映射:推广按钮在 HeaderActionButtons 里叫 'pricing',在 rightActions 里叫 'promotion'。
|
||
const orgRightActionKey = {
|
||
tutorial: 'tutorial',
|
||
pricing: 'promotion',
|
||
recharge: 'recharge',
|
||
weyaiDownload: 'weyaiDownload',
|
||
};
|
||
|
||
// 判断某个功能按钮是否显示:无配置、配置非数组(脏数据)或配置缺失该项时默认显示。
|
||
// 认证页(登录/注册/企业登录等)一律隐藏主操作按钮:充值/教程/推广要么需要登录态,
|
||
// 要么会把用户带离认证流程,行业惯例认证页顶栏只留最小化工具。
|
||
const isButtonVisible = (key) => {
|
||
if (isAuthPage) return false;
|
||
if (isOrgUser) {
|
||
const ra = orgRightActionKey[key];
|
||
if (ra && headerNavModules?.rightActions?.[ra] === false) return false;
|
||
}
|
||
return isHeaderActionButtonVisible(buttonConfig, key, userGroup);
|
||
};
|
||
|
||
// 右侧工具类按钮(新年 / 通知 / 主题切换)显隐:跟随 rightActions 配置,默认显示。
|
||
// 认证页只保留主题切换(对未登录用户有意义且不打断认证流程),通知/新年装饰一并隐藏。
|
||
const isActionVisible = (key) => {
|
||
if (isAuthPage && key !== 'theme') return false;
|
||
return headerNavModules?.rightActions?.[key] !== false;
|
||
};
|
||
|
||
// 语言切换按钮(中/英):由管理员在 运营设置→通用设置 控制,默认显示。
|
||
// 与 HeaderActionButtons 同源走公开 /api/status(普通用户可读),status 未加载时
|
||
// 回退 localStorage 缓存;仅显式 false 才隐藏。认证页也展示(对未登录用户有意义)。
|
||
let languageSwitchEnabled = true;
|
||
try {
|
||
const status =
|
||
statusState?.status ||
|
||
(localStorage.getItem('status')
|
||
? JSON.parse(localStorage.getItem('status'))
|
||
: null);
|
||
if (status && status.language_switch_enabled === false) {
|
||
languageSwitchEnabled = false;
|
||
}
|
||
} catch (e) {
|
||
languageSwitchEnabled = true;
|
||
}
|
||
|
||
// 是否存在「主操作区」按钮(新手教程 / 推广 / 充值),用于决定分隔线是否显示
|
||
const showWeyAiDownload =
|
||
isLoggedIn && weyaiDownloadVisible && isButtonVisible('weyaiDownload');
|
||
|
||
const hasPrimaryActions =
|
||
(isLoggedIn && isButtonVisible('tutorial')) ||
|
||
(isLoggedIn && isButtonVisible('pricing')) ||
|
||
showWeyAiDownload ||
|
||
isButtonVisible('recharge');
|
||
|
||
// 是否存在「工具区」按钮(新年 / 通知 / 主题 / 语言)
|
||
const hasUtilityActions =
|
||
isActionVisible('newYear') ||
|
||
isActionVisible('notifications') ||
|
||
isActionVisible('theme') ||
|
||
languageSwitchEnabled;
|
||
|
||
const actionIcon = (Icon) => <Icon size={16} strokeWidth={1.5} aria-hidden />;
|
||
|
||
return (
|
||
<div className='ui-header-actions flex items-center shrink-0'>
|
||
{/* Tutorial Button - 新手教程按钮 (桌面端显示) */}
|
||
{isLoggedIn && isButtonVisible('tutorial') && (
|
||
<Link
|
||
to='/tutorial'
|
||
className='ui-header-action-link ui-header-desktop-action'
|
||
>
|
||
<AntButton icon={actionIcon(BookOpen)}>
|
||
{!isMobile && t('新手教程')}
|
||
</AntButton>
|
||
</Link>
|
||
)}
|
||
|
||
{/* Promotion Button - 推广按钮 (桌面端显示) */}
|
||
{isLoggedIn && isButtonVisible('pricing') && (
|
||
<AntButton
|
||
icon={actionIcon(Share2)}
|
||
className='ui-header-desktop-action'
|
||
onClick={() => setPromotionVisible(true)}
|
||
>
|
||
{!isMobile && t('推广')}
|
||
</AntButton>
|
||
)}
|
||
|
||
{/* WeyAi 桌面端下载 - 有 WeyAi 访问权限且下载页已开放时显示 (桌面端显示) */}
|
||
{showWeyAiDownload && (
|
||
<Tooltip title={t('下载 WeyAi')}>
|
||
<Link
|
||
to='/weyai/download'
|
||
className='ui-header-action-link ui-header-desktop-action'
|
||
aria-label={t('下载 WeyAi 桌面端')}
|
||
>
|
||
<AntButton
|
||
type='text'
|
||
className='ui-header-download'
|
||
icon={actionIcon(Download)}
|
||
>
|
||
{!isMobile && t('下载 WeyAi')}
|
||
</AntButton>
|
||
</Link>
|
||
</Tooltip>
|
||
)}
|
||
|
||
{/* Recharge Button - 充值按钮 */}
|
||
{isButtonVisible('recharge') && !isMiniProgramWebView() && (
|
||
<Link to='/recharge' className='ui-header-action-link'>
|
||
<AntButton
|
||
icon={actionIcon(Wallet)}
|
||
aria-label={isMobile ? t('充值') : undefined}
|
||
>
|
||
{!isMobile && t('充值')}
|
||
</AntButton>
|
||
</Link>
|
||
)}
|
||
|
||
<PromotionPosterModal
|
||
visible={promotionVisible}
|
||
onClose={() => setPromotionVisible(false)}
|
||
/>
|
||
|
||
{/* Divider:原型仅在主操作与工具按钮之间保留一条竖线 */}
|
||
{hasPrimaryActions && hasUtilityActions && (
|
||
<Divider orientation='vertical' className='ui-header-actions-divider' />
|
||
)}
|
||
|
||
{isActionVisible('newYear') && (
|
||
<NewYearButton isNewYear={isNewYear} t={t} />
|
||
)}
|
||
|
||
{isActionVisible('theme') && (
|
||
<ThemeToggle theme={theme} onThemeToggle={onThemeToggle} t={t} />
|
||
)}
|
||
|
||
{/* 系统公告:原型位于主题与语言之间 */}
|
||
{isActionVisible('notifications') && (
|
||
<NotificationButton
|
||
unreadCount={unreadCount}
|
||
onNoticeOpen={onNoticeOpen}
|
||
t={t}
|
||
/>
|
||
)}
|
||
|
||
{languageSwitchEnabled && (
|
||
<LanguageSelector
|
||
currentLang={currentLang}
|
||
onLanguageChange={onLanguageChange}
|
||
t={t}
|
||
/>
|
||
)}
|
||
|
||
<UserArea
|
||
userState={userState}
|
||
isLoading={isLoading}
|
||
isMobile={isMobile}
|
||
headerNavModules={headerNavModules}
|
||
rightActions={headerNavModules?.rightActions}
|
||
sidebarModulesAdmin={sidebarModulesAdmin}
|
||
isOrgUser={isOrgUser}
|
||
logout={logout}
|
||
navigate={navigate}
|
||
t={t}
|
||
/>
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default ActionButtons;
|