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

import { storage } from "@/utils/storage.uts";
import { notifyUserSceneTypeChanged } from "@/utils/userSceneEvents.uts";
import { hideNativeTabBar } from "@/utils/tabBarShell.uts";

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

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

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

export type SceneTypeItem = { describe: string; icon: string; title: string; }

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 getSceneTypeItem(sceneId: number): null | SceneTypeItem {
	if (sceneId == 1) return { title: "轻改", describe: "太阳膜升级转化", icon: "轻" };
	if (sceneId == 0) return { title: "会员权益/升级", describe: "权益延保", icon: "权" };
	if (sceneId == 7) return { title: "二手车", describe: "二手车业务", icon: "二" };
	return null;
}

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 showScenePopup = ref(false);
	const showSetting = ref(false);

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

	const sceneTypeList = computed((): UTSJSONObject[] => {
		const info = source.getUserInfo();
		let openArr: number[] = [1, 0, 7];
		if (info != null) {
			const arr = (info as UTSJSONObject).openSceneArr as null | number[];
			if (arr != null && arr.length > 0) {
				openArr = arr;
			}
		}
		const result: UTSJSONObject[] = [];
		const valid = [1, 0, 7];
		for (const element of openArr) {
			const e = element as number;
			if (valid.indexOf(e) >= 0) {
				const item = getSceneTypeItem(e);
				if (item != null) {
					result.push({ title: item.title, describe: item.describe, icon: item.icon } as UTSJSONObject);
				}
			}
		}
		if (result.length === 0) {
			result.push(
				{ title: "轻改", describe: "太阳膜升级转化", icon: "轻" } as UTSJSONObject,
				{ title: "会员权益/升级", describe: "权益延保", icon: "权" } as UTSJSONObject,
				{ title: "二手车", describe: "二手车业务", icon: "二" } as UTSJSONObject
			);
		}
		return result;
	});

	const sceneTypeIdList = computed((): number[] => {
		const info = source.getUserInfo();
		let openArr: number[] = [1, 0, 7];
		if (info != null) {
			const arr = (info as UTSJSONObject).openSceneArr as null | number[];
			if (arr != null && arr.length > 0) {
				openArr = arr;
			}
		}
		const valid = [1, 0, 7];
		const result: number[] = [];
		for (const element of openArr) {
			const e = element as number;
			if (valid.indexOf(e) >= 0) {
				result.push(e);
			}
		}
		if (result.length === 0) {
			return [1, 0, 7];
		}
		return result;
	});

	const userName = computed((): string => {
		const info = source.getUserInfo();
		if (info == null) {
			return "";
		}
		const obj = info as UTSJSONObject;
		const raw = obj.name ?? obj.staffName ?? obj.merchantName;
		if (raw == null) {
			return "";
		}
		const s = `${raw}`;
		return s !== "" ? s : "";
	});

	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 onScenePopupVisible = (v: boolean): void => {
		showScenePopup.value = v;
	};

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

	const onSceneChange = (): void => {
		showScenePopup.value = true;
	};

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

	const onChooseScene = (index: number): void => {
		const ids = sceneTypeIdList.value;
		if (index < 0 || index >= ids.length) {
			return;
		}
		const sceneType = ids[index];
		const raw = storage.get("userInfo");
		if (raw == null) {
			return;
		}
		const info = raw as UTSJSONObject;
		const current = info.sceneType as null | number;
		if (current != null && current == sceneType) {
			return;
		}
		info.sceneType = sceneType;
		uni.showLoading({ title: "切换中", mask: true });
		storage.set("userInfo", info, 0);
		notifyUserSceneTypeChanged(sceneType);
		uni.switchTab({
			url: "/pages/index/index",
			success: () => {
				hideNativeTabBar();
			},
			complete: () => {
				uni.hideLoading();
			},
			fail: (_err: any) => {
				uni.hideLoading();
				reLaunchPage("pages/index/index");
			}
		});
	};

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

	return {
		showScenePopup,
		showSetting,
		currentTabIndex,
		sceneTypeList,
		sceneTypeIdList,
		userName,
		onTabChange,
		onScenePopupVisible,
		onSettingVisible,
		onSceneChange,
		onSetting,
		onChooseScene,
		jumpPersonCenter
	};
}