tabbarShared.uts 4.67 KB
import { computed, ref } from "vue";

import { hideNativeTabBar } from "@/utils/tabBarShell.uts";
import { navigateToSelectStoreWithSessionStores } from "@/utils/shukeSelectStoreNav.uts";

export type HomeFrameTabbarSource = {
	getTabIndex: () => number
	getUserInfo: () => null | UTSJSONObject
}

/** TabBarShell `provide`:`Ref<UTSJSONObject>`;MainTabShell `inject`,与插槽同源 */
export const SCENE_SHELL_USER_INFO_INJECT_KEY = "homeFrame.sceneShellUserInfo"

/**
 * 横屏侧栏头像文案:与 simgle_app `LeftSide` 使用 `userInfo.userName` 一致;
 * 数科/ROP 返回里常见 `name`、`staffName`、`nickName`、`merchantName` 等作回退。
 */
export function resolveHomeFrameDisplayName(info: null | UTSJSONObject): string {
	if (info == null) {
		return "";
	}
	const obj = info as UTSJSONObject;
	const userName = obj.userName as string | null;
	if (userName != null && userName !== "") {
		return userName;
	}
	const raw = obj.name ?? obj.staffName ?? obj.nickName ?? obj.merchantName;
	if (raw == null) {
		return "";
	}
	const s = `${raw}`;
	return s !== "" ? s : "";
}

export type TabItem = {
	label: string;
	icon: string;
	name: string;
	/** 模板内仅用路径字段,避免 UTS 在 slot 中调用 import 的函数 */
	iconSrcActive: string;
	iconSrcInactive: string;
};

/** 与 pages.json tabBar 一致:首页 | 接待中心 | 我的(选店为普通页,见 shukeSelectStoreNav) */
export const HOME_FRAME_TABS: TabItem[] = [
	{
		label: "首页",
		icon: "home",
		name: "HomeSlot",
		iconSrcActive: "/static/images/tabbar/home-active.png",
		iconSrcInactive: "/static/images/tabbar/home.png"
	},
	{
		label: "接待",
		icon: "record",
		name: "ReceptionCenter",
		iconSrcActive: "/static/images/tabbar/record-active.png",
		iconSrcInactive: "/static/images/tabbar/record.png"
	},
	{
		label: "我的",
		icon: "my",
		name: "MyCenter",
		iconSrcActive: "/static/images/tabbar/study-active.png",
		iconSrcInactive: "/static/images/tabbar/study.png"
	},
];

export function getTabIconSrc(icon: string, active: boolean): string {
	if (icon === "home") {
		return active ? "/static/images/tabbar/home-active.png" : "/static/images/tabbar/home.png";
	}
	if (icon === "record") {
		return active ? "/static/images/tabbar/record-active.png" : "/static/images/tabbar/record.png";
	}
	if (icon === "study") {
		return active ? "/static/images/tabbar/study-active.png" : "/static/images/tabbar/study.png";
	}
	if (icon === "my") {
		return active ? "/static/images/tabbar/study-active.png" : "/static/images/tabbar/study.png";
	}
	return "/static/images/tabbar/home.png";
}

function reLaunchPage(pagesJsonPath: string): void {
	const trimmed = pagesJsonPath.startsWith("/") ? pagesJsonPath.substring(1) : pagesJsonPath;
	const withSlash = "/" + trimmed;
	uni.reLaunch({
		url: withSlash,
		fail: (_err: any) => {
			uni.reLaunch({ url: trimmed });
		}
	});
}

function switchTabPage(pagesJsonPath: string): void {
	const trimmed = pagesJsonPath.startsWith("/") ? pagesJsonPath.substring(1) : pagesJsonPath;
	const url = "/" + trimmed;
	uni.switchTab({
		url,
		success: () => {
			hideNativeTabBar();
		},
		fail: (_err: any) => {
			reLaunchPage(trimmed);
		}
	});
}

export function useHomeFrameTabbar(source: HomeFrameTabbarSource) {
	const showSetting = ref(false);

	const currentTabIndex = computed((): number => {
		return source.getTabIndex();
	});

	const userName = computed((): string => {
		return resolveHomeFrameDisplayName(source.getUserInfo());
	});

	const navigateToTab = (index: number): void => {
		const cur = source.getTabIndex();
		if (index == cur) {
			return;
		}
		if (index == 0) {
			switchTabPage("pages/index/index");
			return;
		}
		if (index == 1) {
			switchTabPage("pages/receptionCenter/center");
			return;
		}
		if (index == 2) {
			switchTabPage("pages/person/myCenter");
			return;
		}
	};

	const onTabChange = (name: string | number): void => {
		const idx = name as number;
		navigateToTab(idx);
	};

	const onSettingVisible = (v: boolean): void => {
		showSetting.value = v;
	};

	const onSetting = (): void => {
		showSetting.value = true;
	};

	const jumpPersonCenter = (): void => {
		uni.navigateTo({ url: "/pages/person/attendance" });
	};

	/** 横屏侧栏底栏「切换」:进选店页(非 tab) */
	const onSwitchStore = async (): Promise<void> => {
		try {
			uni.showLoading({ title: "加载中", mask: true });
			await navigateToSelectStoreWithSessionStores();
		} catch (error) {
			const msg = error != null ? `${error}` : "操作失败";
			uni.showToast({ title: msg, icon: "none" });
		} finally {
			uni.hideLoading();
		}
	};

	return {
		showSetting,
		currentTabIndex,
		userName,
		onTabChange,
		onSettingVisible,
		onSetting,
		jumpPersonCenter,
		onSwitchStore
	};
}