Files
pj_h5_cloud/src/App.vue
T
2025-04-09 19:14:43 +08:00

220 lines
5.2 KiB
Vue

<script lang="ts">
import isDevEnv from "./sheep/utils/isDevEnv"
// import { onLaunch, onShow, onHide } from "@dcloudio/uni-app"
// onLaunch(() => {})
// onShow(() => {})
// onHide(() => {})
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",
launchParam: ["canteen"],
auth: [],
},
{
pagePath: "pages/tabbar/selfMerchant",
text: "自营商家",
iconPath: "/static/tabbar/selfMerchant.png",
selectedIconPath: "/static/tabbar/selfMerchant-select.png",
launchParam: ["selfMerchant"],
auth: [],
},
{
pagePath: "pages/tabbar/coopMerchant",
text: "合作商家",
iconPath: "/static/tabbar/coopMerchant.png",
selectedIconPath: "/static/tabbar/coopMerchant-select.png",
launchParam: ["selfMerchant"],
auth: ["user"],
},
{
pagePath: "pages/tabbar/order",
text: "订单",
iconPath: "/static/tabbar/order.png",
selectedIconPath: "/static/tabbar/order-select.png",
launchParam: ["canteen", "selfMerchant"],
auth: ["user"],
},
{
pagePath: "pages/tabbar/flowStatistics",
text: "流水统计",
iconPath: "/static/tabbar/order.png",
selectedIconPath: "/static/tabbar/order-select.png",
launchParam: [],
auth: ["merchant"],
},
{
pagePath: "pages/tabbar/individualOrder",
text: "订单",
iconPath: "/static/tabbar/order.png",
selectedIconPath: "/static/tabbar/order-select.png",
launchParam: [],
auth: ["merchant"],
},
{
pagePath: "pages/tabbar/my",
text: "我的",
iconPath: "/static/tabbar/my.png",
selectedIconPath: "/static/tabbar/my-select.png",
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: "" })
// 环境判断
const res = await (window as any).CP2.getAppInfo()
if (res.code == 0) {
// 不是诗画环境
const launchParam = getLaunchParam()
console.log("app launch 启动路径参数", launchParam)
switch (launchParam) {
case "canteen":
uni.switchTab({ url: "/pages/tabbar/canteen" })
break
case "selfMerchant":
uni.switchTab({ url: "/pages/tabbar/selfMerchant" })
break
default:
break
}
} else {
// 是诗画环境
const userRole = uni.getStorageSync("userRole")
if (!userRole) return uni.redirectTo({ url: "/pages/user/userRole" })
if (userRole === "user") return uni.switchTab({ url: "/pages/tabbar/coopMerchant" })
if (userRole === "merchant") return uni.switchTab({ url: "/pages/tabbar/individualOrder" })
}
},
onShow() {},
onHide() {},
methods: {
async _initTabbar() {
if (isDevEnv()) {
tabbar.forEach((item, index) => {
setTabbarItem(index, item, true)
})
return
}
const launchParam = getLaunchParam()
console.log("tabbar launch 启动路径参数", launchParam)
const res = await (window as any).CP2.getAppInfo()
if (res.code == 0) {
// 不是诗画环境
if (launchParam) {
setTabbarByLaunchParam(launchParam)
} else {
uni.hideTabBar()
uni.showToast({ title: "启动参数不合法", icon: "none" })
}
} else {
// 是诗画环境
const userRole = uni.getStorageSync("userRole")
switch (userRole) {
case "user":
setTabbarByUserRole("user")
break
case "merchant":
setTabbarByUserRole("merchant")
break
default:
uni.hideTabBar()
uni.showToast({ title: "用户身份未确认", icon: "none" })
break
}
}
},
},
}
</script>
<style lang="scss">
@import "@/sheep/scss/global.scss";
@import url(@/uni_modules/tm-ui/css/remixicon.min.css);
$theme-green: #06be36;
$theme-blue: #0094ff;
$theme-gray: #999;
.botder_btn {
border: 1px solid #999;
color: #999;
font-weight: 500;
font-size: 28rpx;
border-radius: 10rpx;
padding: 8rpx 15rpx;
}
.prefix_title {
font-size: 32rpx;
font-weight: 500;
color: #333;
&::before {
content: "";
display: inline-block;
width: 8rpx;
height: 24rpx;
background: $theme-blue;
border-radius: 4rpx;
margin-right: 10rpx;
}
}
</style>