diff --git a/src/App.vue b/src/App.vue index 38fb491..696d41d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -7,13 +7,22 @@ import isDevEnv from "./sheep/utils/isDevEnv" // onShow(() => {}) // onHide(() => {}) -const tabbar = [ +interface TabbarItem { + pagePath: string + text: string + iconPath: string + selectedIconPath: string + launchParam: string[] + auth: string[] +} + +const tabbar: TabbarItem[] = [ { pagePath: "pages/tabbar/canteen", text: "食堂", iconPath: "/static/tabbar/canteen.png", selectedIconPath: "/static/tabbar/canteen-select.png", - authPath: ["pages/tabbar/canteen"], + launchParam: ["canteen"], auth: [], }, { @@ -21,7 +30,7 @@ const tabbar = [ text: "自营商家", iconPath: "/static/tabbar/selfMerchant.png", selectedIconPath: "/static/tabbar/selfMerchant-select.png", - authPath: ["pages/tabbar/selfMerchant"], + launchParam: ["selfMerchant"], auth: [], }, { @@ -29,7 +38,7 @@ const tabbar = [ text: "合作商家", iconPath: "/static/tabbar/coopMerchant.png", selectedIconPath: "/static/tabbar/coopMerchant-select.png", - authPath: ["pages/tabbar/selfMerchant"], + launchParam: ["selfMerchant"], auth: ["user"], }, { @@ -37,7 +46,7 @@ const tabbar = [ text: "订单", iconPath: "/static/tabbar/order.png", selectedIconPath: "/static/tabbar/order-select.png", - authPath: ["pages/tabbar/canteen", "pages/tabbar/selfMerchant"], + launchParam: ["canteen", "selfMerchant"], auth: ["user"], }, { @@ -45,7 +54,7 @@ const tabbar = [ text: "流水统计", iconPath: "/static/tabbar/order.png", selectedIconPath: "/static/tabbar/order-select.png", - authPath: [], + launchParam: [], auth: ["merchant"], }, { @@ -53,7 +62,7 @@ const tabbar = [ text: "订单", iconPath: "/static/tabbar/order.png", selectedIconPath: "/static/tabbar/order-select.png", - authPath: [], + launchParam: [], auth: ["merchant"], }, { @@ -61,25 +70,58 @@ const tabbar = [ text: "我的", iconPath: "/static/tabbar/my.png", selectedIconPath: "/static/tabbar/my-select.png", - authPath: ["pages/tabbar/canteen", "pages/tabbar/selfMerchant"], + launchParam: ["canteen", "selfMerchant"], auth: ["user", "merchant"], }, ] +// 获取启动参数 +const getLaunchParam = () => { + return new URL(window.location.href).searchParams.get("launchParam") +} + +// 设置tabbar项目 +const setTabbarItem = (index: number, item: TabbarItem, visible: boolean) => { + uni.setTabBarItem({ + index, + pagePath: item.pagePath, + text: item.text, + iconPath: item.iconPath, + selectedIconPath: item.selectedIconPath, + visible, + }) +} + +// 根据启动参数设置tabbar项目 +const setTabbarByLaunchParam = (launchParam: string) => { + tabbar.forEach((item, index) => { + const visible = item.launchParam.includes(launchParam) + setTabbarItem(index, item, visible) + }) +} + +// 根据用户角色设置tabbar项目 +const setTabbarByUserRole = (role: string) => { + tabbar.forEach((item, index) => { + const visible = item.auth.includes(role) + setTabbarItem(index, item, visible) + }) +} + export default { async onLaunch() { if (isDevEnv()) return uni.setNavigationBarTitle({ title: "" }) - console.log("环境判断") - - // 诗画浦江获取用户信息进行环境判断 + // 环境判断 const res = await (window as any).CP2.getAppInfo() if (res.code == 0) { // 不是诗画环境 - const launchParam = new URL(window.location.href).searchParams.get("launchParam") + const launchParam = getLaunchParam() + console.log("app launch 启动路径参数", launchParam) + switch (launchParam) { case "canteen": uni.switchTab({ url: "/pages/tabbar/canteen" }) @@ -94,20 +136,8 @@ export default { // 是诗画环境 const userRole = uni.getStorageSync("userRole") if (!userRole) return uni.redirectTo({ url: "/pages/user/userRole" }) - if (userRole && userRole == "user") return uni.switchTab({ url: "/pages/tabbar/coopMerchant" }) - if (userRole && userRole == "merchant") return uni.switchTab({ url: "/pages/tabbar/individualOrder" }) - - // const res = await user.isMerchant() - // if (res.code == 0) { - // if (res.data) { - // return uni.redirectTo({ url: "/pages/user/userRole" }) - // } else { - // uni.setStorageSync("userRole", "user") - // uni.switchTab({ url: "/pages/tabbar/coopMerchant" }) - // } - // } else { - // uni.showToast({ title: "角色身份判断失败", icon: "none" }) - // } + if (userRole === "user") return uni.switchTab({ url: "/pages/tabbar/coopMerchant" }) + if (userRole === "merchant") return uni.switchTab({ url: "/pages/tabbar/individualOrder" }) } }, onShow() {}, @@ -115,128 +145,27 @@ export default { methods: { async _initTabbar() { - if (isDevEnv()) { - tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath, auth }, index) => { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: true, - }) - }) - return - } - - const launchParam = new URL(window.location.href).searchParams.get("launchParam") + const launchParam = getLaunchParam() console.log("tabbar launch 启动路径参数", launchParam) - // 诗画浦江获取用户信息进行环境判断 const res = await (window as any).CP2.getAppInfo() if (res.code == 0) { // 不是诗画环境 - switch (launchParam) { - case "canteen": - tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath }, index) => { - if (authPath.includes("pages/tabbar/canteen")) { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: true, - }) - } else { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: false, - }) - } - }) - break - case "selfMerchant": - tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath }, index) => { - if (authPath.includes("pages/tabbar/selfMerchant")) { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: true, - }) - } else { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: false, - }) - } - }) - break - default: - uni.hideTabBar() - uni.showToast({ title: "启动参数不合法", icon: "none" }) - break + if (launchParam) { + setTabbarByLaunchParam(launchParam) + } else { + uni.hideTabBar() + uni.showToast({ title: "启动参数不合法", icon: "none" }) } } else { // 是诗画环境 const userRole = uni.getStorageSync("userRole") switch (userRole) { case "user": - tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath, auth }, index) => { - if (auth.includes("user")) { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: true, - }) - } else { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: false, - }) - } - }) + setTabbarByUserRole("user") break case "merchant": - tabbar.forEach(({ pagePath, text, iconPath, selectedIconPath, authPath, auth }, index) => { - if (auth.includes("merchant")) { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: true, - }) - } else { - uni.setTabBarItem({ - index, - pagePath, - text, - iconPath, - selectedIconPath, - visible: false, - }) - } - }) + setTabbarByUserRole("merchant") break default: uni.hideTabBar()