shukeSelectStoreNav.uts 2.07 KB
import { apiShukeStoreList } from "@/api/user-api.uts";
import { storage } from "@/utils/storage.uts";
import { hideNativeTabBar } from "@/utils/tabBarShell.uts";

/** 选店页非 tabBar,统一 reLaunch;门店列表只依赖 pendingShukeStores,勿在 URL 上挂整段 JSON */
const SELECT_STORE_PAGE_URL = "/pages/common/selectStore/index";

function reLaunchSelectStoreWithStores(stores: any[]): void {
	// 存储门店列表(storage.set 会自动处理序列化)
	storage.set("pendingShukeStores", stores, 0);
	
	uni.reLaunch({
		url: SELECT_STORE_PAGE_URL,
		success: (): void => {
			hideNativeTabBar();
		},
		fail: (err: any): void => {
			const msg = err != null ? `${err}` : "打开选店失败";
			uni.showToast({ title: msg, icon: "none" });
		}
	});
}

/** 登录成功后进入选店页(从非 tab 栈打开,reLaunch + pending) */
export function switchTabSelectStoreWithStoresPayload(stores: any[]): void {
	reLaunchSelectStoreWithStores(stores);
}

/**
 * 与 simgle_app 侧栏切换门店:会话拉门店列表,`reLaunch` + pending。
 * 确认门店凭据由选店页 `readLoginFormForSelectStore`(loginForm 或 `shukeLastConfirmForm`)提供。
 */
export async function navigateToSelectStoreWithSessionStores(): Promise<void> {
	const spRaw = storage.get("shukeSpCode") as string | null;
	const spCode = spRaw != null ? spRaw : "";
	const storeListRes = await apiShukeStoreList({
		spCode
	} as UTSJSONObject);
	if (storeListRes == null) {
		throw new Error("获取门店列表失败");
	}
	const storeCodeRaw = storeListRes["code"] as number | string | null;
	if (storeCodeRaw != null && `${storeCodeRaw}` === "500") {
		const sm =
			(storeListRes["msg"] as string | null) != null
				? `${storeListRes["msg"]}`
				: "获取门店列表失败";
		throw new Error(sm);
	}
	const storeData = storeListRes["data"] as UTSJSONObject | null;
	const stores = storeData != null ? (storeData["stores"] as any[] | null) : null;
	if (stores == null || stores.length == 0) {
		throw new Error("暂无门店信息,请联系管理员");
	}
	reLaunchSelectStoreWithStores(stores);
}