127 lines
5.3 KiB
JavaScript
127 lines
5.3 KiB
JavaScript
// 数据来源:
|
||
// - 默认只用后端接口,接口出错就在页面上直接显示错误,不会换成演示数据;
|
||
// - 用 STATIC_HOSTS 里的域名打开(本地是 http://127.0.0.1:8090),或地址栏写 ?source=mock,用本地演示数据
|
||
// (data/community.json + robots/),保留最早的完整样子作参考。
|
||
import * as api from './api.js';
|
||
import { normalizeRobot } from './registry.js';
|
||
import { urdfResourceUrlCandidates } from './urdf-paths.js';
|
||
import { modelUrlResolver } from './model-assets.js';
|
||
|
||
export const STATIC_HOSTS = ['127.0.0.1'];
|
||
|
||
export function dataMode() {
|
||
if (api.BUILD_ENV === 'dev') return Promise.resolve('api');
|
||
let mock = STATIC_HOSTS.includes(location.hostname);
|
||
try {
|
||
if (new URLSearchParams(location.search).get('source') === 'mock') mock = true;
|
||
sessionStorage.removeItem('vm.source'); // 清掉旧版本记住的切换
|
||
} catch { /* 忽略 */ }
|
||
return Promise.resolve(mock ? 'mock' : 'api');
|
||
}
|
||
|
||
// ---------- 后端 Project → 页面使用的项目结构 ----------
|
||
const PALETTE = ['#c8ff3e', '#5ea8ff', '#ff7a9a', '#ffb347', '#7cf0c8', '#b79cff', '#ffd166', '#6ee7ff'];
|
||
export function userFrom(u) {
|
||
const code = u?.userCode || '';
|
||
let h = 0;
|
||
for (const ch of code || u?.nickname || '?') h = (h * 31 + ch.charCodeAt(0)) >>> 0;
|
||
return { handle: null, code, name: u?.nickname || '已注销用户', color: PALETTE[h % PALETTE.length] };
|
||
}
|
||
|
||
// 类型接口没返回的编码(比如列表里出现了但类型接口漏了)兜底显示中文
|
||
const TYPE_FALLBACK = { GENERAL: '通用', HUMANOID: '人形', QUADRUPED: '四足', WHEELED: '轮式', ROBOTIC_ARM: '机械臂' };
|
||
export async function typeName(code) {
|
||
const types = await api.getProjectTypes().catch(() => []);
|
||
return types.find((t) => t.code === code)?.name || TYPE_FALLBACK[code] || code;
|
||
}
|
||
|
||
export async function toProject(raw) {
|
||
const types = await api.getProjectTypes().catch(() => []);
|
||
return {
|
||
api: true,
|
||
raw,
|
||
id: raw.projectCode,
|
||
code: raw.projectCode,
|
||
slug: raw.name,
|
||
name: raw.name,
|
||
description: raw.description || '',
|
||
projectType: raw.projectType,
|
||
type: types.find((t) => t.code === raw.projectType)?.name || TYPE_FALLBACK[raw.projectType] || raw.projectType,
|
||
archetype: raw.projectType,
|
||
owner: userFrom(raw.owner),
|
||
coverUrl: raw.coverUrl,
|
||
liked: !!raw.liked,
|
||
starsTotal: raw.likeCount ?? 0,
|
||
commentCount: raw.commentCount ?? 0,
|
||
forksTotal: raw.forkCount, // 后端暂未提供,undefined 时页面不显示
|
||
updated: api.utcIso(raw.publishedAt || raw.updatedAt),
|
||
publishedCommitSha: raw.publishedCommitSha,
|
||
urdfMainUrl: raw.urdfMainUrl || null,
|
||
urdfBaseUrl: raw.urdfBaseUrl || null,
|
||
modelGlbUrl: raw.modelGlbUrl || null,
|
||
modelPartsBaseUrl: raw.modelPartsBaseUrl || null,
|
||
modelPartsFiles: raw.modelPartsFiles || [],
|
||
assemblyJsonUrl: raw.assemblyJsonUrl || null,
|
||
walkingResourcesBaseUrl: raw.walkingResourcesBaseUrl || null,
|
||
walkingResourcesFiles: raw.walkingResourcesFiles || [],
|
||
assemblyManifestUrl: raw.assemblyManifestUrl || null,
|
||
walkingManifestUrl: raw.walkingManifestUrl || null,
|
||
sourceProjectCode: raw.sourceProjectCode,
|
||
topics: raw.tags || [],
|
||
license: raw.license || null,
|
||
featured: false,
|
||
robot: null,
|
||
};
|
||
}
|
||
|
||
export async function listProjects(opts, signal) {
|
||
const page = await api.listCommunityProjects(opts, signal);
|
||
return { ...page, items: await Promise.all(page.items.map(toProject)) };
|
||
}
|
||
|
||
export async function getProject(code) {
|
||
return toProject(await api.getCommunityProject(code));
|
||
}
|
||
|
||
// 社区目录固定读当前发布版本,按项目编码缓存;项目详情页一打开就和项目信息同时请求
|
||
const treeCache = new Map();
|
||
export function prefetchRoot(code) {
|
||
if (!treeCache.has(code)) treeCache.set(code, api.getCommunityTree(code, '').catch((err) => { treeCache.delete(code); throw err; }));
|
||
return treeCache.get(code);
|
||
}
|
||
const rootTree = (p) => prefetchRoot(p.code);
|
||
|
||
// ---------- 3D 模型:项目的 urdfMainUrl 字段(URDF 主文件的公开地址),没有就不显示 3D ----------
|
||
// 相对路径按主文件目录解析;package:// 同时兼容嵌套 ROS 包和旧版扁平上传目录。
|
||
export function ensureRobot(p) {
|
||
if (p.robot !== undefined && p.robot !== null) return Promise.resolve(p.robot);
|
||
p.robot = p.urdfMainUrl ? robotFromUrdf(p) : null;
|
||
return Promise.resolve(p.robot);
|
||
}
|
||
|
||
function robotFromUrdf(p) {
|
||
const base = (p.urdfBaseUrl || p.urdfMainUrl.replace(/[^/]*$/, '')).replace(/\/+$/, '');
|
||
const candidates = (url) => urdfResourceUrlCandidates(base, url, p.urdfMainUrl);
|
||
const resolveUrl = modelUrlResolver(base);
|
||
return normalizeRobot({
|
||
id: p.code,
|
||
name: p.name,
|
||
description: p.description,
|
||
urdf: p.urdfMainUrl,
|
||
resolveUrl: (url) => resolveUrl(candidates(url)[0]),
|
||
resolveMeshUrls: candidates,
|
||
packages: (name) => `package://${name}`,
|
||
archetype: 'generic', // 没有关节映射:演示时所有关节在限位内摆动
|
||
source: { project: p.code },
|
||
});
|
||
}
|
||
|
||
// ---------- README ----------
|
||
export async function getReadme(p) {
|
||
const tree = await rootTree(p);
|
||
const entry = tree.entries.find((e) => e.type === 'file' && /^readme(\.md|\.markdown|\.txt)?$/i.test(e.name));
|
||
if (!entry) return null;
|
||
const file = await api.getCommunityFile(p.code, entry.path);
|
||
return file.encoding === 'utf-8' ? file.content : null;
|
||
}
|