nomadro/apps/navmini/weapp/pages/index/index.js
eric d93a355fd5 Migrate navmini/ebook/nomadweb into apps/ as same-origin portal subpaths.
Shield external portal jumps (keep EXTERNAL_* for later), serve /nav from static build, and proxy /book + /community to vertical Next apps with Caddy notes.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 03:30:34 -05:00

334 lines
8.1 KiB
JavaScript

let nav = {
brand: "游民导航",
tagline: "",
footer: "",
brandIcon: "/icons/_brand.webp",
categories: [],
};
try {
nav = require("../../data/links.js");
} catch (e1) {
try {
nav = require("../../data/links.json");
} catch (e2) {
console.error("load links failed", e1, e2);
}
}
function countLinks(cat) {
var n = 0;
var groups = cat.groups || [];
for (var i = 0; i < groups.length; i++) {
n += (groups[i].links || []).length;
}
return n;
}
function buildCategories(categories, activeId) {
var list = categories || [];
var out = [];
for (var i = 0; i < list.length; i++) {
var c = list[i];
out.push({
id: c.id,
title: c.title,
emoji: c.emoji || "",
groups: c.groups || [],
linkCount: countLinks(c),
active: c.id === activeId,
});
}
return out;
}
function readWindowMetrics() {
var statusBarHeight = 20;
var navBarHeight = 44;
try {
var sys =
typeof wx.getWindowInfo === "function"
? wx.getWindowInfo()
: wx.getSystemInfoSync();
statusBarHeight = sys.statusBarHeight || 20;
} catch (e) {
console.warn("getWindowInfo failed", e);
}
try {
if (typeof wx.getMenuButtonBoundingClientRect === "function") {
var menu = wx.getMenuButtonBoundingClientRect();
if (menu && menu.height && menu.top) {
navBarHeight = (menu.top - statusBarHeight) * 2 + menu.height;
}
}
} catch (e) {
console.warn("getMenuButtonBoundingClientRect failed", e);
}
if (!navBarHeight || navBarHeight < 32) navBarHeight = 44;
return {
statusBarHeight: statusBarHeight,
navBarHeight: navBarHeight,
pagePadTop: statusBarHeight + navBarHeight,
};
}
function themeFields(theme) {
var dark = theme === "dark";
return {
theme: theme,
themeClass: dark ? "theme-dark" : "",
themeIcon: dark ? "☀" : "☾",
};
}
Page({
data: {
brand: nav.brand || "游民导航",
tagline: nav.tagline || "",
footer: nav.footer || "",
brandIcon: nav.brandIcon || "/icons/_brand.webp",
categories: [],
activeId: "",
activeTitle: "",
theme: "light",
themeClass: "",
themeIcon: "☾",
drawerOpen: false,
showTop: false,
toast: "",
statusBarHeight: 20,
navBarHeight: 44,
pagePadTop: 64,
sheetOpen: false,
sheetLink: {
icon: "",
title: "",
subtitle: "",
href: "",
},
loadError: "",
},
_jumping: false,
_sectionTops: [],
_toastTimer: null,
onLoad: function () {
var first = (nav.categories && nav.categories[0]) || null;
var activeId = first ? first.id : "";
var metrics = readWindowMetrics();
var app = getApp();
var theme =
(app && app.globalData && app.globalData.theme) || "light";
var patch = Object.assign(
{},
metrics,
themeFields(theme),
{
categories: buildCategories(nav.categories, activeId),
activeId: activeId,
activeTitle: first ? first.title : "",
loadError:
!nav.categories || !nav.categories.length
? "导航数据未加载,请重新编译"
: "",
}
);
this.setData(patch);
this._applyTheme(theme);
},
onReady: function () {
var self = this;
setTimeout(function () {
self._measureSections();
}, 50);
},
onShow: function () {
this._applyTheme(this.data.theme);
},
onPageScroll: function (e) {
var scrollTop = (e && e.scrollTop) || 0;
var showTop = scrollTop > 480;
if (showTop !== this.data.showTop) {
this.setData({ showTop: showTop });
}
if (this._jumping || !this._sectionTops.length) return;
var marker =
scrollTop + this.data.statusBarHeight + this.data.navBarHeight + 24;
var current = this._sectionTops[0].id;
for (var i = 0; i < this._sectionTops.length; i++) {
if (this._sectionTops[i].top <= marker) current = this._sectionTops[i].id;
else break;
}
if (current !== this.data.activeId) {
this._setActive(current);
}
},
_setActive: function (id) {
var cat = null;
var cats = this.data.categories || [];
for (var i = 0; i < cats.length; i++) {
if (cats[i].id === id) {
cat = cats[i];
break;
}
}
this.setData({
activeId: id,
activeTitle: (cat && cat.title) || "",
categories: buildCategories(nav.categories, id),
});
},
_measureSections: function () {
var self = this;
var query = wx.createSelectorQuery();
query.selectAll(".cat-panel").boundingClientRect();
query.selectViewport().scrollOffset();
query.exec(function (res) {
var rects = (res && res[0]) || [];
var scroll = (res && res[1]) || { scrollTop: 0 };
self._sectionTops = [];
for (var i = 0; i < rects.length; i++) {
var r = rects[i];
self._sectionTops.push({
id: String(r.id || "").replace(/^cat-/, ""),
top: (r.top || 0) + (scroll.scrollTop || 0),
});
}
});
},
_applyTheme: function (theme) {
var dark = theme === "dark";
try {
wx.setBackgroundColor({
backgroundColor: dark ? "#0a0f14" : "#e9eef4",
backgroundColorTop: dark ? "#0a0f14" : "#e9eef4",
backgroundColorBottom: dark ? "#0a0f14" : "#e9eef4",
});
} catch (e) {
/* ignore */
}
},
openDrawer: function () {
this.setData({ drawerOpen: true });
},
closeDrawer: function () {
this.setData({ drawerOpen: false });
},
noop: function () {},
toggleTheme: function () {
var theme = this.data.theme === "dark" ? "light" : "dark";
this.setData(themeFields(theme));
try {
wx.setStorageSync("navmini-theme", theme);
} catch (e) {
/* ignore */
}
var app = getApp();
if (app && app.globalData) app.globalData.theme = theme;
this._applyTheme(theme);
},
onSelectCategory: function (e) {
var id = e.currentTarget.dataset.id;
if (!id) return;
this._setActive(id);
this.setData({ drawerOpen: false });
this._jumping = true;
var self = this;
var offset = -(this.data.statusBarHeight + this.data.navBarHeight + 8);
wx.pageScrollTo({
selector: "#cat-" + id,
duration: 320,
offsetTop: offset,
complete: function () {
setTimeout(function () {
self._jumping = false;
self._measureSections();
}, 360);
},
});
},
onTapLink: function (e) {
var id = e.currentTarget.dataset.id;
var catid = e.currentTarget.dataset.catid;
var groupid = e.currentTarget.dataset.groupid;
var link = null;
var cats = nav.categories || [];
for (var i = 0; i < cats.length; i++) {
if (cats[i].id !== catid) continue;
var groups = cats[i].groups || [];
for (var j = 0; j < groups.length; j++) {
if (groups[j].id !== groupid) continue;
var links = groups[j].links || [];
for (var k = 0; k < links.length; k++) {
if (links[k].id === id) {
link = links[k];
break;
}
}
}
}
if (!link || !link.href) {
this._showToast("无效链接");
return;
}
this.setData({
sheetOpen: true,
sheetLink: {
icon: link.icon || "/icons/_brand.webp",
title: link.title || "",
subtitle: link.subtitle || "",
href: link.href || "",
},
});
},
closeSheet: function () {
this.setData({ sheetOpen: false });
},
copySheetUrl: function () {
var link = this.data.sheetLink;
if (!link || !link.href) return;
var self = this;
wx.setClipboardData({
data: link.href,
success: function () {
self._showToast("已复制,请到浏览器打开");
},
fail: function () {
self._showToast("复制失败");
},
});
},
backTop: function () {
wx.pageScrollTo({ scrollTop: 0, duration: 300 });
},
_showToast: function (msg) {
var self = this;
if (this._toastTimer) clearTimeout(this._toastTimer);
this.setData({ toast: msg });
this._toastTimer = setTimeout(function () {
self.setData({ toast: "" });
self._toastTimer = null;
}, 1800);
},
});