import { requireLogin } from "./auth"; type QueryValue = string | number | boolean | undefined | null; function withQuery(path: string, query: Record = {}) { const params = Object.entries(query) .filter( ([, value]) => value !== undefined && value !== null && value !== "", ) .map( ([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`, ) .join("&"); return params ? `${path}?${params}` : path; } export function go(path: string, query?: Record) { uni.navigateTo({ url: withQuery(path, query) }); } export function goRoot(path: string, query?: Record) { uni.reLaunch({ url: withQuery(path, query) }); } export function goBack(fallback = "/pages/home/index") { const pages = getCurrentPages(); if (pages.length > 1) { uni.navigateBack(); return; } goRoot(fallback); } export function goWildArchives() { go("/pages/wild-archives/index"); } export function goPlay() { uni.switchTab({ url: "/pages/play/index" }); } export function goConcierge() { uni.switchTab({ url: "/pages/concierge/index" }); } export function goVehicleDemand(vehicleOptionId?: string) { const path = "/pages/vehicle-demand"; const query = vehicleOptionId ? { vehicleOptionId } : undefined; const redirect = withQuery(path, query); if (requireLogin(redirect)) { go(path, query); } } export function goWildArchiveDetail(archiveId: string) { go("/pages/wild-archives/detail", { id: archiveId }); } export function goTeamBuildingDetail(teamBuildingId: string) { go("/pages/team-buildings/detail", { id: teamBuildingId }); } export function goWanfaRouteDetail(routeId: string) { go("/pages/detail/index", { routeId }); } export function goMine() { if (requireLogin("/pages/mine/index")) { goRoot("/pages/mine/index"); } }