微信小程序 根据不同角色显示不同的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 ,文件夹内创建 小程序文件如下图:微信小程序 根据不同角色显示不同的tabBar 菜单_第1张图片

第三步 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 时效果如下图

微信小程序 根据不同角色显示不同的tabBar 菜单_第2张图片

roleId=2 时效果如下图

在这里插入图片描述

你可能感兴趣的:(微信小程序,notepad++,小程序)