/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { Trans } from 'react-i18next'; import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js'; import { writeClipboardText } from '../../helpers/clipboard'; import React, { useState, useContext, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { Link, useNavigate, useSearchParams } from 'react-router-dom'; import { Typography, Button, Card, Tag, Toast, Tabs, TabPane, } from '@/components/ui'; import { IconCopy, IconTick, IconArrowRight, IconKey, IconCreditCard, IconCode, IconSend, IconSearch, IconClose, IconVideo, IconArticle, } from '@/components/ui/icons'; import { StatusContext } from '../../context/Status'; import { DEFAULT_LOGIN_REDIRECT } from '../../helpers/data'; import { isMiniProgramWebView } from '../../helpers/miniProgram'; import VideoTutorial from './components/VideoTutorial'; import './styles.css'; const { Title, Paragraph, Text } = Typography; const Tutorial = () => { const { t } = useTranslation(); const navigate = useNavigate(); const [searchParams] = useSearchParams(); const [copiedIndex, setCopiedIndex] = useState(null); const [statusState] = useContext(StatusContext); const [activeOS, setActiveOS] = useState('windows'); const [activeTutorialTab, setActiveTutorialTab] = useState('video'); // 默认视频教程 const scrollContainerRef = useRef(null); // 处理关闭教程 const handleClose = () => { // 检查是否有来源参数,如果有则返回来源页面,否则返回默认页面 const fromPage = searchParams.get('from'); if (fromPage) { navigate(`/${fromPage}`); } else { navigate(DEFAULT_LOGIN_REDIRECT); } }; // 获取后台配置的文档地址 const docsLink = statusState?.status?.docs_link || 'https://docs.weytoken.com/'; // API 服务器地址(生产环境) const serverAddress = 'https://api.weytoken.com'; const steps = [ { key: 'create-key', title: t('创建密钥'), icon: , color: 'var(--brand-gradient-via)', }, { key: 'view-models', title: t('查看可用模型'), icon: , color: 'var(--brand-gradient-to)', }, { key: 'recharge', title: t('充值'), icon: , color: 'var(--brand-primary)', }, { key: 'install-client', title: t('安装客户端'), icon: , color: 'var(--brand-gradient-from)', }, { key: 'start-journey', title: t('开启之旅'), icon: , color: 'var(--brand-primary-hover)', }, ]; // 复制代码 const copyCode = async (code, index) => { try { await writeClipboardText(code); setCopiedIndex(index); Toast.success(t('已复制到剪贴板')); setTimeout(() => setCopiedIndex(null), 2000); } catch (err) { Toast.error(t('复制失败')); } }; // 代码块渲染函数:返回 JSX 而非作为内联组件,避免 Tutorial 重渲染时被识别为新的组件类型导致子树重新挂载 const renderCodeBlock = (code, language, index) => (
{language}
        {code}
      
); // 步骤1:创建密钥 - 使用 JSX 变量而非内联组件,避免 Tutorial 重渲染时被识别为新组件类型导致子树重新挂载(连带 scroll 容器重建、滚动位置丢失) const createKeyStep = (
{t('第一步:创建 API 密钥')} {t('API 密钥是调用 AI 服务的凭证,请妥善保管')}
1 {t('点击顶部导航栏的')} 「{t('密钥管理')}」
2 {t('点击「创建密钥」按钮')}
3 {t('填写密钥名称(如:my-first-key)')}
4 {t('选择密钥分组')}
5 {t('点击「提交」完成创建')}
{I18N_TECHNICAL_TEXT.WARNING_ICON}
{t('重要提示')} {t('密钥只会显示一次,请立即复制保存!格式如:')} sk-xxxxxxxxxxxxxxxxxxxxxxxx
); // 步骤2:查看可用模型 const viewModelsStep = (
{t('第二步:查看密钥可用模型')} {t('创建密钥后,可以查看该密钥有权限使用的模型')}
1 ), }} />
2 {t('点击密钥卡片右侧的「查看可用模型」按钮')}
3 {t('在弹出窗口中查看该密钥可以调用的所有模型')}
{I18N_TECHNICAL_TEXT.TIP_ICON}
{t('小提示')} {t('常用模型推荐:')}
claude-3-5-sonnet gpt-4o gemini-pro
); // 步骤3:充值 const rechargeStep = (
{t('第三步:账户充值')} {t('充值后即可开始使用 AI 服务')}
1 ), }} />
2 {t('选择充值金额')}
3 {t('完成支付,余额实时到账')}
{I18N_TECHNICAL_TEXT.MONEY_ICON}
{t('计费说明')} {t( '平台按 Token 消耗计费,不同模型价格不同。可在「模型服务」页面查看详细价格。', )}
); // 步骤4:安装客户端 const installClientStep = (
{t('第四步:安装 Claude Code 客户端')} {t('Claude Code 是 Anthropic 官方的命令行 AI 编程助手')}
{t('安装 Claude Code')} {renderCodeBlock( 'npm install -g @anthropic-ai/claude-code', 'bash', 'install-claude', )} {t('或使用 npx 直接运行:')} npx @anthropic-ai/claude-code {t('配置环境变量')} {I18N_TECHNICAL_TEXT.WINDOWS_PLATFORM} } itemKey='windows' >
{t('在 PowerShell 中执行(永久生效):')} {renderCodeBlock( `# 永久设置用户环境变量 [System.Environment]::SetEnvironmentVariable('ANTHROPIC_BASE_URL', '${serverAddress}', 'User') [System.Environment]::SetEnvironmentVariable('ANTHROPIC_AUTH_TOKEN', 'sk-你的密钥', 'User') # 设置后请重新打开终端窗口`, 'powershell', 'env-windows', )} {t('环境变量将永久保存,重新打开终端后生效')}
{I18N_TECHNICAL_TEXT.MACOS_PLATFORM} } itemKey='macos' >
{t('在终端中执行(永久生效):')} {renderCodeBlock( `# 添加到 ~/.zshrc(永久生效) echo 'export ANTHROPIC_BASE_URL="${serverAddress}"' >> ~/.zshrc echo 'export ANTHROPIC_AUTH_TOKEN="sk-你的密钥"' >> ~/.zshrc source ~/.zshrc`, 'bash', 'env-macos', )} {t('如使用 bash 请将 ~/.zshrc 改为 ~/.bash_profile')}
{I18N_TECHNICAL_TEXT.LINUX_PLATFORM} } itemKey='linux' >
{t('在终端中执行(永久生效):')} {renderCodeBlock( `# 添加到 ~/.bashrc(永久生效) echo 'export ANTHROPIC_BASE_URL="${serverAddress}"' >> ~/.bashrc echo 'export ANTHROPIC_AUTH_TOKEN="sk-你的密钥"' >> ~/.bashrc source ~/.bashrc`, 'bash', 'env-linux', )} {t('如使用 zsh 请将 ~/.bashrc 改为 ~/.zshrc')}
{t('启动使用')} {renderCodeBlock('claude', 'bash', 'start-claude')}
/help {t('查看帮助')}
/config {t('配置设置')}
/clear {t('清空对话')}
/exit {t('退出程序')}
); // 步骤5:开启之旅 const startJourneyStep = (
{t('第五步:开启 AI 之旅')} {t('恭喜!您已完成所有准备工作')}
{I18N_TECHNICAL_TEXT.CELEBRATION_ICON}
{t('准备就绪!')} {t('您已经完成了所有配置,现在可以开始使用 AI 服务了')}
{t('后续探索')}
{I18N_TECHNICAL_TEXT.DIAMOND_ICON}
{t('模型超市')} {t('查看所有可用模型及价格')}
{I18N_TECHNICAL_TEXT.BOOKS_ICON}
{t('使用文档')} {t('查看更多使用指南和 API 文档')}
{I18N_TECHNICAL_TEXT.GAME_ICON}
{t('体验中心')} {t('在线体验 AI 对话功能')}
{I18N_TECHNICAL_TEXT.CHART_ICON}
{t('用户中心')} {t('查看使用统计和调用记录')}
); // 所有步骤数组(直接引用 JSX 变量,保持元素类型稳定,避免重新挂载) const allStepComponents = [ { component: createKeyStep, title: t('创建密钥'), color: 'var(--brand-gradient-via)', }, { component: viewModelsStep, title: t('查看可用模型'), color: 'var(--brand-gradient-to)', }, { component: rechargeStep, title: t('充值'), color: 'var(--brand-primary)', }, { component: installClientStep, title: t('安装客户端'), color: 'var(--brand-gradient-from)', }, { component: startJourneyStep, title: t('开启之旅'), color: 'var(--brand-primary-hover)', }, ]; // WeyAi 小程序里不出现充值步骤(helpers/miniProgram.js)。其余步骤保留原编号, // 与标题里的「第四步」等一致。 const inMiniProgram = isMiniProgramWebView(); const visibleSteps = steps .map((step, index) => ({ ...step, number: index + 1 })) .filter((step) => !(inMiniProgram && step.key === 'recharge')); const visibleStepComponents = allStepComponents .map((step, index) => ({ ...step, number: index + 1 })) .filter((step) => !(inMiniProgram && step.component === rechargeStep)); // 图文教程内容 - 滚动浏览模式(使用 JSX 变量而非内联组件,避免状态更新时重新挂载导致滚动位置重置) const stepsTutorial = (
{/* 顶部快速导航 */} {/* 可滚动的内容区域 */}
{visibleStepComponents.map((step, index) => (
{step.number} / {allStepComponents.length}
{step.component}
))}
{/* 底部完成提示 */}
{I18N_TECHNICAL_TEXT.CELEBRATION_ICON}
{t('恭喜!您已浏览完所有教程内容')}
); return (
{/* 头部 */}
{t('快速上手')}
{t('新手教程')} {t('5 分钟快速上手,开启您的 AI 之旅')}
{/* 教程类型切换 Tab */}
{t('视频教程')} } itemKey='video' >
{t('图文教程')} } itemKey='steps' >
{stepsTutorial}
); }; export default Tutorial;