Files
monkey-ai-web/src/pages/WeyAiDownload/index.jsx
T

671 lines
23 KiB
React
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// WeyAi 桌面端下载页(方案 4.8)。两种挂载方式,同一个组件:
// - /weyai/download 独立公开路由,不需要登录,自带顶部让位与页面背景;
// - /user-center/weyai-download 嵌在用户中心里(embedded),侧栏保留、背景由 shell 提供。
// 首屏展示版本、入口和推荐安装包,后续按系统列出安装包和使用说明。
import { I18N_TECHNICAL_TEXT } from '../../i18n/technicalText.js';
import {
Download as ActionDownload,
RefreshCw as ActionRefreshCw,
Globe as ActionGlobe,
ExternalLink as ActionExternalLink,
} from '@/components/common/icons/ActionIcons';
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, Skeleton, Tag } from 'antd';
import {
AlertTriangle,
Cpu,
Laptop,
LogIn,
Monitor,
MonitorDown,
Package,
RefreshCw,
Smartphone,
Terminal,
Wallet,
} from 'lucide-react';
import { API } from '../../helpers/api';
import { StatusContext } from '../../context/Status';
import { UserContext } from '../../context/User';
import UserCenterPageHeader from '../../components/common/UserCenterPageHeader';
import { useWeyAiWebEntry } from '../../hooks/common/useWeyAiWebEntry';
import {
PLATFORMS,
detectClient,
formatSize,
groupByPlatform,
normalizeRelease,
pickRecommended,
} from './release';
import './weyai-download.css';
// 不带动作只唤起应用(navigate 动作要求目标路由在桌面端白名单里)
const OPEN_APP_URL = 'weyai://';
const ALL_PACKAGES_ID = 'weyai-download-all';
const PACKAGES_HEADING_ID = 'weyai-download-all-title';
// 更新源清单里的 releaseDate 是 ISO 时间串(2026-09-20T08:00:00.000Z),
// 展示只需要日期。固定用 ISO 日期(UTC):按语种本地化会让泰语走佛历、
// 让 UTC-9 以西的访客看到前一天;解析不了的原样返回,绝不因此把 hero 弄空
function formatReleaseDate(value) {
if (!value) return '';
const date = new Date(value);
if (Number.isNaN(date.getTime())) return value;
return date.toISOString().slice(0, 10);
}
const PLATFORM_LABEL = { windows: 'Windows', mac: 'macOS', linux: 'Linux' };
const PLATFORM_ICON = { windows: Monitor, mac: Laptop, linux: Terminal };
function PlatformIcon({ platform, size = 18 }) {
const Icon = PLATFORM_ICON[platform] || MonitorDown;
return <Icon size={size} aria-hidden='true' />;
}
function FileMeta({ name, size, version, releaseVersion }) {
const details = [
size ? formatSize(size) : '',
version && version !== releaseVersion ? `v${version}` : '',
].filter(Boolean);
return (
<div className='wd-file__meta'>
<span className='wd-file__name'>{name}</span>
{details.length > 0 && <span>{details.join(' · ')}</span>}
</div>
);
}
// 状态卡:加载失败 / 未开放 / 暂无安装包共用一个壳
function StateCard({ icon, title, desc, danger, actions }) {
return (
<section
className={`wd-card wd-state${danger ? ' wd-state--danger' : ''}`}
role={danger ? 'alert' : undefined}
>
<div className='wd-state__icon'>{icon}</div>
<div className='wd-state__title'>{title}</div>
{desc && <div className='wd-state__desc'>{desc}</div>}
{actions && <div className='wd-state__actions'>{actions}</div>}
</section>
);
}
const WeyAiDownload = ({ embedded = false }) => {
const { t } = useTranslation();
const [statusState] = useContext(StatusContext);
const [userState] = useContext(UserContext);
const isLoggedIn = !!userState?.user?.id;
const status = statusState?.status;
const statusLoaded = status !== undefined;
const downloadEnabled =
status?.weyai_download_enabled === true ||
status?.weyai_download_enabled === 'true';
// 网页版入口:与顶栏同一套判定(开关 + 地址 + 开放范围),有权限才给链接
const { visible: webVisible, url: webUrl } = useWeyAiWebEntry();
const [loading, setLoading] = useState(true);
const [loadError, setLoadError] = useState('');
const [release, setRelease] = useState(null);
const [client, setClient] = useState({ os: '', arch: '' });
const packagesRef = useRef(null);
const packagesHeadingRef = useRef(null);
const archLabel = (arch) => {
if (arch === 'arm64') return t('ARM 64 位');
if (arch === 'armv7l') return t('ARM 32 位');
if (arch === 'x64') return t('x86 64 位');
if (arch === 'universal') return t('通用(Universal)');
if (arch === 'ia32') return t('x86 32 位');
// 兜底给出原始架构名,好过让整行标题退化成文件名、与下一行重复
return arch || '';
};
const load = async () => {
setLoading(true);
setLoadError('');
try {
const res = await API.get('/api/weyai/desktop/release', {
skipErrorHandler: true,
disableDuplicate: true,
});
const body = res?.data;
if (body?.success === false) {
setLoadError(body.message || t('获取版本信息失败,请稍后重试'));
return;
}
const data = body && 'data' in body ? body.data : body;
setRelease(normalizeRelease(data));
} catch (error) {
// 本地化串优先:服务端故障时后端恒返回中文「服务器内部错误」,直显会让
// 九个语种的访客都看到中文,而且这句话对访客零信息量。后端文案只留给
// 带具体说明的业务错误(4xx),5xx 一律用本页自己的提示
const httpStatus = error?.response?.status;
const backendMessage = error?.response?.data?.message;
setLoadError(
httpStatus && httpStatus < 500 && backendMessage
? backendMessage
: t('获取版本信息失败,请稍后重试'),
);
} finally {
setLoading(false);
}
};
useEffect(() => {
let cancelled = false;
detectClient().then((result) => {
if (!cancelled) setClient(result);
});
return () => {
cancelled = true;
};
}, []);
useEffect(() => {
if (statusLoaded && downloadEnabled) {
load();
} else if (statusLoaded) {
setLoading(false);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [statusLoaded, downloadEnabled]);
const files = release?.files || [];
const releaseDisabled = release?.enabled === false;
const recommended = pickRecommended(files, client);
const groups = groupByPlatform(files);
const platformsWithFiles = PLATFORMS.filter(
(platform) => groups[platform].length > 0,
);
// 手机端(目前只有 Android):发布过才有,没有就整块不显示
const mobileRelease = release?.mobile || null;
const androidApk = mobileRelease?.android || null;
const showPackages =
statusLoaded &&
loading === false &&
downloadEnabled &&
!releaseDisabled &&
!loadError &&
files.length > 0;
const features = useMemo(
() => [
{
icon: LogIn,
title: t('账号直登'),
desc: t('用本站账号在浏览器里登录一次,客户端自动接入,无需手填密钥'),
},
{
icon: Wallet,
title: t('余额通用'),
desc: t('个人钱包或企业额度直接消费,与网页端同一套账'),
},
{
icon: Cpu,
title: t('模型同步'),
desc: t(
'使用与网页端相同的分组与模型,由平台统一下发,无需在客户端配置',
),
},
{
icon: RefreshCw,
title: t('版本统一发布'),
desc: t('安装包与更新源同一来源,版本低于平台要求时客户端会提示升级'),
},
],
[t],
);
const steps = useMemo(
() => [
{
title: t('下载并安装'),
desc: t('选择与你系统匹配的安装包,安装过程与普通桌面软件一致'),
},
{
title: t('打开 WeyAi,点击登录'),
desc: t('客户端会拉起浏览器,用本站账号完成登录'),
},
{
title: t('选择使用范围'),
desc: t('个人或所在企业,授权后自动返回客户端即可开始对话'),
},
],
[t],
);
const scrollToAll = () => {
packagesHeadingRef.current?.focus({ preventScroll: true });
packagesRef.current?.scrollIntoView({
behavior: window.matchMedia('(prefers-reduced-motion: reduce)').matches
? 'auto'
: 'smooth',
block: 'start',
});
};
const download = (url) => window.open(url, '_self');
const renderFileRow = (file) => {
const isReco = recommended && recommended.url === file.url;
return (
<div
key={file.url}
className={`wd-file${isReco ? ' wd-file--reco' : ''}`}
>
<div className='wd-file__main'>
<div className='wd-file__title'>
<span>{archLabel(file.arch) || file.name}</span>
{file.kind && <Tag>{file.kind}</Tag>}
{isReco && <Tag className='wd-recommended-tag'>{t('推荐')}</Tag>}
</div>
<FileMeta
name={file.name}
size={file.size}
version={file.version}
releaseVersion={release?.version}
/>
</div>
<Button
icon={<ActionDownload size={16} aria-hidden />}
type={isReco ? 'primary' : 'default'}
aria-label={`${t('下载')} ${file.name}`}
onClick={() => download(file.url)}
>
{t('下载')}
</Button>
</div>
);
};
const renderRecommended = () => {
if (recommended) {
return (
<section className='wd-card wd-reco'>
<div className='wd-reco__row'>
<div className='wd-reco__main'>
<div className='wd-os-badge'>
<PlatformIcon platform={recommended.platform} size={26} />
</div>
<div className='min-w-0'>
<div className='wd-eyebrow'>
{t('为你的 {{os}} 推荐', {
os: PLATFORM_LABEL[recommended.platform],
})}
</div>
<h2 className='wd-reco__title'>
{PLATFORM_LABEL[recommended.platform]}
{archLabel(recommended.arch)
? ` · ${archLabel(recommended.arch)}`
: ''}
</h2>
<div className='wd-reco__meta'>
<code>{recommended.name}</code>
{recommended.size > 0 && (
<span>{formatSize(recommended.size)}</span>
)}
{release?.version && <span>v{release.version}</span>}
</div>
{/* 只在"猜了 Apple 芯片包但其实不知道芯片"时提示;推荐的已是
universal 或 x64 包时提示反而误导 */}
{client.os === 'mac' &&
!client.arch &&
recommended.arch === 'arm64' && (
<div className='wd-reco__hint'>
{t(
'未能识别芯片类型:Apple 芯片请用此包,Intel 芯片请在下方选择 x86 64 位版本',
)}
</div>
)}
</div>
</div>
<div className='wd-reco__actions'>
<Button
type='primary'
icon={<ActionDownload size={16} aria-hidden />}
onClick={() => download(recommended.url)}
>
{t('下载 WeyAi')}
</Button>
<Button
type='text'
onClick={scrollToAll}
aria-controls={ALL_PACKAGES_ID}
>
{t('其他平台与架构')}
</Button>
</div>
</div>
</section>
);
}
if (client.os === 'mobile' && client.mobile === 'android' && androidApk) {
return (
<section className='wd-card wd-reco'>
<div className='wd-reco__row'>
<div className='wd-reco__main'>
<div className='wd-os-badge'>
<Smartphone size={26} aria-hidden='true' />
</div>
<div className='min-w-0'>
<div className='wd-eyebrow'>{t('为你的 Android 手机推荐')}</div>
<h2 className='wd-reco__title'>{t('WeyAi 手机端')}</h2>
<div className='wd-reco__meta'>
<code>{androidApk.name}</code>
{androidApk.size > 0 && (
<span>{formatSize(androidApk.size)}</span>
)}
{mobileRelease.version && (
<span>v{mobileRelease.version}</span>
)}
</div>
<div className='wd-reco__hint'>
{t(
'安装时如果提示「未知来源」,请在手机设置里允许当前浏览器安装应用',
)}
</div>
</div>
</div>
<div className='wd-reco__actions'>
<Button
type='primary'
icon={<ActionDownload size={16} aria-hidden />}
onClick={() => download(androidApk.url)}
>
{t('下载 Android 版')}
</Button>
</div>
</div>
</section>
);
}
return (
<section className='wd-card wd-reco'>
<div className='wd-reco__row'>
<div className='wd-reco__main'>
<div className='wd-os-badge'>
<MonitorDown size={26} aria-hidden='true' />
</div>
<div className='min-w-0'>
<h2 className='wd-reco__title'>
{client.os === 'mobile'
? t('请在电脑上下载')
: t('选择你的安装包')}
</h2>
<div className='wd-reco__hint'>
{client.os === 'mobile'
? t(
'WeyAi 桌面端支持 Windows、macOS 和 Linux,请在电脑上打开此页面下载',
)
: t('未能识别你的系统,请在下方选择对应的安装包')}
</div>
{client.os === 'mobile' &&
client.mobile === 'ios' &&
androidApk && (
<div className='wd-reco__hint'>
{t('iOS 版暂未推出,手机端目前只有 Android 版')}
</div>
)}
</div>
</div>
{client.os !== 'mobile' && (
<div className='wd-reco__actions'>
<Button
type='primary'
onClick={scrollToAll}
aria-controls={ALL_PACKAGES_ID}
>
{t('查看全部安装包')}
</Button>
</div>
)}
</div>
</section>
);
};
const renderPackageState = () => {
if (!statusLoaded || loading) {
return (
<section className='wd-card wd-skeleton' aria-busy='true'>
<Skeleton active title={{ width: '40%' }} paragraph={{ rows: 2 }} />
<Skeleton.Button active style={{ width: 160, maxWidth: '100%' }} />
</section>
);
}
if (!downloadEnabled || releaseDisabled) {
return (
<StateCard
icon={<MonitorDown size={26} aria-hidden='true' />}
title={t('桌面端下载暂未开放')}
desc={t('请稍后再来,或联系管理员')}
/>
);
}
if (loadError) {
return (
<StateCard
danger
icon={<AlertTriangle size={26} aria-hidden='true' />}
title={t('获取版本信息失败')}
desc={loadError}
actions={
<Button
type='primary'
icon={<ActionRefreshCw size={16} aria-hidden />}
onClick={load}
>
{t('重试')}
</Button>
}
/>
);
}
return (
<StateCard
icon={<Package size={26} aria-hidden='true' />}
title={t('暂无可下载的安装包')}
desc={t('请稍后再来,或联系管理员')}
/>
);
};
const renderPackages = () => (
<section
ref={packagesRef}
id={ALL_PACKAGES_ID}
className='wd-section'
aria-labelledby={PACKAGES_HEADING_ID}
>
<div className='wd-section-head'>
<h2 ref={packagesHeadingRef} id={PACKAGES_HEADING_ID} tabIndex={-1}>
{t('全部安装包')}
</h2>
<p>
{t('共 {{count}} 个文件', {
count: files.length + (androidApk ? 1 : 0),
})}
{release?.version ? ` · v${release.version}` : ''}
</p>
</div>
<div className='wd-platforms'>
{platformsWithFiles.map((platform) => (
<section key={platform} className='wd-card wd-platform'>
<div className='wd-platform__head'>
<h3 className='wd-platform__name'>
<PlatformIcon platform={platform} />
{PLATFORM_LABEL[platform]}
</h3>
<span className='wd-platform__count'>
{t('{{count}} 个安装包', {
count: groups[platform].length,
})}
</span>
</div>
<div className='wd-platform__files'>
{groups[platform].map(renderFileRow)}
</div>
</section>
))}
{androidApk && (
<section className='wd-card wd-platform'>
<div className='wd-platform__head'>
<h3 className='wd-platform__name'>
<Smartphone size={18} aria-hidden='true' />
{t('手机端 · Android')}
</h3>
<span className='wd-platform__count'>
{t('{{count}} 个安装包', { count: 1 })}
</span>
</div>
<div className='wd-platform__files'>
<div className='wd-file'>
<div className='wd-file__main'>
<div className='wd-file__title'>
<span>{I18N_TECHNICAL_TEXT.ANDROID}</span>
<Tag>apk</Tag>
</div>
<FileMeta
name={androidApk.name}
size={androidApk.size}
version={mobileRelease.version}
releaseVersion={release?.version}
/>
</div>
<Button
icon={<ActionDownload size={16} aria-hidden />}
aria-label={`${t('下载')} ${androidApk.name}`}
onClick={() => download(androidApk.url)}
>
{t('下载')}
</Button>
</div>
</div>
<div className='wd-platform__note'>
{t(
'在手机上扫码连接电脑上的 WeyAi,或用本站账号登录网页版,随时继续对话',
)}
</div>
</section>
)}
</div>
</section>
);
const rootClass = `weyai-download ${
embedded ? 'weyai-download--embedded' : 'weyai-download--standalone'
}`;
const heroActions = (
<div className='wd-hero-actions'>
{isLoggedIn && webVisible && webUrl && (
<Button
icon={<ActionGlobe size={16} aria-hidden />}
onClick={() => window.open(webUrl, '_blank', 'noopener')}
>
{t('使用网页版')}
</Button>
)}
<Button
icon={<ActionExternalLink size={16} aria-hidden />}
onClick={() => {
window.location.href = OPEN_APP_URL;
}}
>
{t('已安装?打开 WeyAi')}
</Button>
</div>
);
return (
<div className={rootClass}>
<div className='weyai-download__inner'>
<div className='wd-intro'>
<div className='wd-hero'>
{!embedded && (
<div className='wd-eyebrow'>
<MonitorDown size={16} aria-hidden='true' />
{I18N_TECHNICAL_TEXT.WEYAI_DESKTOP}
</div>
)}
<UserCenterPageHeader
title={t('下载 WeyAi 桌面端')}
subtitle={t('用你的账号登录,直接使用平台提供的模型和余额')}
breadcrumb={embedded ? undefined : null}
extra={heroActions}
/>
<div className='wd-hero-meta'>
{release?.version && (
<span className='wd-chip'>
{t('最新版本 v{{version}}', { version: release.version })}
</span>
)}
{release?.releaseDate && (
<span className='wd-chip wd-chip--muted'>
{t('发布于 {{date}}', {
date: formatReleaseDate(release.releaseDate),
})}
</span>
)}
{/* 有清单时按实际有安装包的平台展示,别在缺某平台 feed 时还声称支持 */}
<span className='wd-chip wd-chip--muted'>
{[
...(platformsWithFiles.length
? platformsWithFiles
: PLATFORMS
).map((platform) => PLATFORM_LABEL[platform]),
...(androidApk ? ['Android'] : []),
].join(' · ')}
</span>
</div>
</div>
{showPackages ? renderRecommended() : renderPackageState()}
</div>
{showPackages && renderPackages()}
<section className='wd-section'>
<div className='wd-section-head'>
<h2>{t('客户端能做什么')}</h2>
</div>
<div className='wd-features'>
{features.map(({ icon: Icon, title, desc }, index) => (
<div key={index} className='wd-card wd-feature'>
<div className='wd-feature__icon'>
<Icon size={18} aria-hidden='true' />
</div>
<div className='min-w-0'>
<span className='wd-feature__title'>{title}</span>
<span className='wd-feature__desc'>{desc}</span>
</div>
</div>
))}
</div>
</section>
<section className='wd-section'>
<div className='wd-section-head'>
<h2>{t('三步开始使用')}</h2>
<p>{t('登录在浏览器完成,客户端不保存你的密码')}</p>
</div>
<div className='wd-steps'>
{steps.map((step, index) => (
<div key={index} className='wd-card wd-step'>
<span className='wd-step__num'>{index + 1}</span>
<span className='wd-step__title'>{step.title}</span>
<span className='wd-step__desc'>{step.desc}</span>
</div>
))}
</div>
</section>
<p className='wd-footnote'>{t('遇到安装或登录问题,请联系管理员')}</p>
</div>
</div>
);
};
export default WeyAiDownload;