微信小程序 根据不同角色显示不同的tabBar 菜单

微信小程序 根据不同角色显示不同的 tabBar 菜单 复制可用

第一个部 设置微信小程序app.json 自行更换json 的页面链接(备注:tabBar 里面一定要设置custom为true, )

"pages": [
   
    "pages/index/index",
    "pages/yyzlb/yyzlb",
    "pages/cgyyjl/cgyyjl",
    "pages/editworkday/editworkday",
    "pages/fillin/fillin",
    "pages/record/record",
    "pages/logs/logs"
   
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "Weixin",
    "navigationBarTextStyle": "black"
  },
  "tabBar": {
    "custom": true,
    "color": "#C8C7CC",
    "selectedColor": "#00a1e9",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/index/index",
        "iconPath": "/static/yuyue_1.png",
        "selectedIconPath": "/static/yuyue_1_1.png",
        "text": "在线预约"
      },
      {
        "pagePath": "pages/record/record",
        "iconPath": "/static/yuyue_2_2.png",
        "selectedIconPath": "/static/yuyue_2.png",
        "text": "预约记录"
      },
      {
        "pagePath": "pages/yyzlb/yyzlb",
        "iconPath": "/static/yuyue_4.png",
        "selectedIconPath": "/static/yuyue_4_4.png",
        "text": "预约记录"
      },
      {
        "pagePath": "pages/editworkday/editworkday",
        "iconPath": "/static/yuyue_3.png",
        "selectedIconPath": "/static/yuyue_3_3.png",
        "text": "预约记录"
      },
      {
        "pagePath": "pages/cgyyjl/cgyyjl",
        "iconPath": "/static/yuyue_2_2.png",
        "selectedIconPath": "/static/yuyue_2.png",
        "text": "预约记录"
      }
    ]
  },
  "sitemapLocation": "sitemap.json"
}

第二步先创建一个公共自定义tabBar 文件夹 custom-tab-bar ,文件夹内创建 小程序文件如下图:在这里插入图片描述

第三步 custom-tab-bar 的文件配置

index.js
Component({
  data: {
   
    selected: null,
    color: "#000000",
    roleId: '0',
    selectedColor: "#1396DB",
    allList: [
      {
      list1: [{
        pagePath: "/pages/index/index",
        iconPath: "/static/yuyue_1.png",
        selectedIconPath: "/static/yuyue_1_1.png",
        text: "在线预约"
       
      }, {
        pagePath: "/pages/record/record",
        iconPath: "/static/yuyue_2.png",
        selectedIconPath: "/static/yuyue_2_2.png",
        text: "预约记录"
      }],
      list2: [
        {
          pagePath: "/pages/yyzlb/yyzlb",
          iconPath: "/static/yuyue_4.png",
          selectedIconPath: "/static/yuyue_4_4.png",
          text: "预约总览",
  
        },
        {
        pagePath: "/pages/editworkday/editworkday",
        iconPath: "/static/yuyue_3.png",
        selectedIconPath: "/static/yuyue_3_3.png",
        text: "设置预约",

      }, {
     
        pagePath: "/pages/cgyyjl/cgyyjl",
        iconPath: "/static/yuyue_2.png",
        selectedIconPath: "/static/yuyue_2_2.png",
        text: "预约记录"
      }]
    }],
    list: []
  },
  attached:function() {
   // const roleId = wx.getStorageSync('statu')
     const  roleId= 1  //根据后台授权用户,决定看到的菜单                                                                                                                                                                 
     //1为客户菜单  2为采购菜单
    if (roleId == 1) { 
      this.setData({
        list: this.data.allList[0].list1
      })
    }else if(roleId==2){
      this.setData({
        list: this.data.allList[0].list2
      })
    }
  },
  methods: {
    switchTab:function(e) {
      
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({ url })
     // this.setData({
       // selected: data.index
      //})
    }
  },
})

index.wxml
<cover-view class="tab-bar">
  <cover-view class="tab-bar-border"></cover-view>
  <cover-view wx:for="{{list}}" wx:key="index" class="tab-bar-item" data-path="{{item.pagePath}}" data-index="{{index}}" bindtap="switchTab">
    <cover-image class="cover-image" src="{{selected === index ? item.selectedIconPath : item.iconPath}}"></cover-image>
    <cover-view class="cover-view" style="color: {{selected === index ? selectedColor : color}}">{{item.text}}</cover-view>
  </cover-view>
</cover-view>
index.wxss
.tab-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 48px;
  background: white;
  display: flex;
  padding-bottom: env(safe-area-inset-bottom);
}

.tab-bar-border {
  background-color: rgba(0, 0, 0, 0.33);
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 1px;
  transform: scaleY(0.5);
}

.tab-bar-item {
  flex: 1;
  text-align: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
}

.tab-bar-item .cover-image {
  width: 44rpx;
  height: 44rpx;
}

.tab-bar-item .cover-view {
  margin-top: 8rpx;
  font-size: 24rpx;
}
roleId= 1 时效果如下图

在这里插入图片描述

roleId=2 时效果如下图

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
微信小程序中,可以通过自定义底部导航栏来实现不同角色登录显示不同页面的功能。具体实现步骤如下: 1. 在app.json文件中定义所有页面的路径和底部导航栏的样式,例如: ```json { "pages": [ "pages/index/index", "pages/user/user", "pages/admin/admin" ], "tabBar": { "color": "#999", "selectedColor": "#333", "backgroundColor": "#fff", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/home.png", "selectedIconPath": "images/home_active.png" }, { "pagePath": "pages/user/user", "text": "用户", "iconPath": "images/user.png", "selectedIconPath": "images/user_active.png" } ] } } ``` 2. 在app.js文件中定义全局变量,用于存储当前用户的角色信息,例如: ```javascript App({ globalData: { role: 'user' } }) ``` 3. 在需要根据角色显示不同页面的地方,例如tabBar的list中,使用条件语句判断当前用户的角色信息,从而显示不同的页面,例如: ```json { "tabBar": { "color": "#999", "selectedColor": "#333", "backgroundColor": "#fff", "list": [ { "pagePath": "pages/index/index", "text": "首页", "iconPath": "images/home.png", "selectedIconPath": "images/home_active.png" }, { "pagePath": "pages/user/user", "text": "用户", "iconPath": "images/user.png", "selectedIconPath": "images/user_active.png", "custom": true, "selected": "{{active == 'user'}}", "pagePath": "pages/user/user" }, { "pagePath": "pages/admin/admin", "text": "管理员", "iconPath": "images/admin.png", "selectedIconPath": "images/admin_active.png", "custom": true, "selected": "{{active == 'admin'}}", "pagePath": "pages/admin/admin", "condition": "{{role == 'admin'}}" } ] } } ``` 其中,通过设置custom和selected属性来自定义底部导航栏的样式,通过设置condition属性来判断当前用户的角色信息,从而显示不同的页面。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

功能点分享

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值