Initial import of weiyuan frontend

This commit is contained in:
qiubaishen
2026-10-15 16:33:46 +08:00
commit d8f3aee261
774 changed files with 374914 additions and 0 deletions
+679
View File
@@ -0,0 +1,679 @@
/*
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 <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
// WeyAi 桌面端下载页(方案 4.8)。两种挂载方式,同一个组件:
// - /weyai/download 独立公开路由,不需要登录,自带顶部让位与页面背景;
// - /user-center/weyai-download 嵌在用户中心里(embedded),侧栏保留、背景由 shell 提供。
// 结构:hero(版本/动作)→ 为你推荐 → 客户端能做什么 → 全部安装包 → 三步开始。
// 加载失败 / 未开放 / 暂无安装包三种状态都保留 hero 与说明区,只替换安装包区域。
import React, { useContext, useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { Button, Skeleton, Tag } from '@douyinfe/semi-ui';
import {
AlertTriangle,
Cpu,
Download,
ExternalLink,
Globe,
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 PageBackground from '../../components/common/PageBackground';
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';
// 更新源清单里的 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 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 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 androidMeta = androidApk
? [
androidApk.name,
androidApk.size ? formatSize(androidApk.size) : '',
// 和桌面端同版本时不重复写版本号(同桌面端安装包行的规则),免得这一行被截断
mobileRelease.version && mobileRelease.version !== release?.version
? `v${mobileRelease.version}`
: '',
]
.filter(Boolean)
.join(' · ')
: '';
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 = () => {
document
.getElementById(ALL_PACKAGES_ID)
?.scrollIntoView({ behavior: 'smooth', block: 'start' });
};
const download = (url) => window.open(url, '_self');
const fileMeta = (file) =>
[
file.name,
file.size ? formatSize(file.size) : '',
file.version && file.version !== release?.version
? `v${file.version}`
: '',
]
.filter(Boolean)
.join(' · ');
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 size='small'>{file.kind}</Tag>}
{isReco && (
<Tag size='small' color='blue'>
{t('推荐')}
</Tag>
)}
</div>
<span className='wd-file__meta' title={file.name}>
{fileMeta(file)}
</span>
</div>
<Button
icon={<Download size={14} />}
theme={isReco ? 'solid' : 'light'}
type='primary'
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 && (
<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
size='large'
theme='solid'
type='primary'
icon={<Download size={16} />}
onClick={() => download(recommended.url)}
>
{t('下载 WeyAi')}
</Button>
<Button theme='borderless' type='tertiary' onClick={scrollToAll}>
{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
size='large'
theme='solid'
type='primary'
icon={<Download size={16} />}
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 theme='solid' type='primary' onClick={scrollToAll}>
{t('查看全部安装包')}
</Button>
</div>
)}
</div>
</section>
);
};
const renderPackages = () => {
if (!statusLoaded || loading) {
return (
<section className='wd-card wd-skeleton' aria-busy='true'>
<Skeleton
loading
active
placeholder={
<>
<Skeleton.Title style={{ width: 240 }} />
<Skeleton.Paragraph rows={2} style={{ marginTop: 12 }} />
<Skeleton.Button style={{ width: 160, marginTop: 12 }} />
</>
}
/>
</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
theme='solid'
type='primary'
icon={<RefreshCw size={14} />}
onClick={load}
>
{t('重试')}
</Button>
}
/>
);
}
if (files.length === 0) {
return (
<StateCard
icon={<Package size={26} aria-hidden='true' />}
title={t('暂无可下载的安装包')}
desc={t('请稍后再来,或联系管理员')}
/>
);
}
return (
<>
{renderRecommended()}
<section id={ALL_PACKAGES_ID} className='flex flex-col gap-3'>
<div className='wd-section-head'>
<h2>{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'>
<span className='wd-platform__name'>
<PlatformIcon platform={platform} />
{PLATFORM_LABEL[platform]}
</span>
<span className='wd-platform__count'>
{t('{{count}} 个安装包', {
count: groups[platform].length,
})}
</span>
</div>
{groups[platform].map(renderFileRow)}
</section>
))}
{androidApk && (
<section className='wd-card wd-platform'>
<div className='wd-platform__head'>
<span className='wd-platform__name'>
<Smartphone size={18} aria-hidden='true' />
{t('手机端 · Android')}
</span>
<span className='wd-platform__count'>
{t('{{count}} 个安装包', { count: 1 })}
</span>
</div>
<div className='wd-file'>
<div className='wd-file__main'>
<div className='wd-file__title'>
<span>Android</span>
<Tag size='small'>apk</Tag>
</div>
<span className='wd-file__meta' title={androidApk.name}>
{androidMeta}
</span>
</div>
<Button
icon={<Download size={14} />}
theme='light'
type='primary'
aria-label={`${t('下载')} ${androidApk.name}`}
onClick={() => download(androidApk.url)}
>
{t('下载')}
</Button>
</div>
<div className='wd-platform__note'>
{t(
'在手机上扫码连接电脑上的 WeyAi,或用本站账号登录网页版,随时继续对话',
)}
</div>
</section>
)}
</div>
</section>
</>
);
};
const rootClass = `weyai-download ${
embedded
? 'weyai-download--embedded'
: 'weyai-download--standalone mt-[60px]'
}`;
return (
<div className={rootClass}>
{!embedded && <PageBackground grid />}
<div className='weyai-download__inner'>
<header className='wd-hero'>
<div className='min-w-0'>
<div className='wd-eyebrow'>
<MonitorDown size={14} aria-hidden='true' />
WeyAi Desktop
</div>
<h1>{t('下载 WeyAi 桌面端')}</h1>
<p>{t('用你的账号登录,直接使用平台提供的模型和余额')}</p>
<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>
<div className='wd-hero-actions'>
{/* 未登录访客不给网页版按钮:hook 对匿名恒可见,点了会直接打到网关的
/sso/start 再被弹回登录,不如顶栏那样先引导登录 */}
{isLoggedIn && webVisible && webUrl && (
<Button
icon={<Globe size={14} />}
theme='light'
type='primary'
onClick={() => window.open(webUrl, '_blank', 'noopener')}
>
{t('使用网页版')}
</Button>
)}
<Button
icon={<ExternalLink size={14} />}
onClick={() => {
window.location.href = OPEN_APP_URL;
}}
>
{t('已安装?打开 WeyAi')}
</Button>
</div>
</header>
{renderPackages()}
<section className='flex flex-col gap-3'>
<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='flex flex-col gap-3'>
<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;