首頁 > 軟體

微信小程式如何根據不同使用者切換不同TabBar(簡單易懂!)

2022-04-21 13:00:36

現有需求:

小程式使用者有三種身份(公眾、運維人員、領導),根據不同使用者身份顯示不同的tabbar

眾所周知微信小程式全域性檔案app.json裡面的"tabBar"裡面的list只能放置2-5個,要想實現3個tabbar,必須得複用tabbar,三種身份都需要個人中心,剩下的是長列表(兩個),表單,圖表 剛好是5個,廢話少說,上程式碼

程式碼有點長,建議仔細看一下

1全域性.app.json

tabbar裡面的sustom要設定為true

"custom": true,
{
  "pages": [
    xxxxxx:xxxxxx
   
  ],
  "window": {
   xxxxxxxxx
  },
  "tabBar": {
    "custom": true,
    "color": "#7A7E83",
    "selectedColor": "#d81e06",
    "borderStyle": "black",
    "backgroundColor": "#ffffff",
    "list": [
      {
        "pagePath": "pages/Users/myrepaire/myrepaire",
        "text": "我要報修",
        "iconPath": "/images/tabbar/weixiu1.png",
        "selectedIconPath": "/images/tabbar/weixiu2.png"
      },
      {
        "pagePath": "pages/Users/myMalfunction/myMalfunction",
        "text": "我的故障",
        "iconPath": "/images/tabbar/myweixiu1.png",
        "selectedIconPath": "/images/tabbar/myweixiu2.png"
      },
      {
        "pagePath": "pages/logs/logs",
        "text": "個人中心",
        "iconPath": "/images/tabbar/user1.png",
        "selectedIconPath": "/images/tabbar/user2.png"
      },
      {
        "pagePath": "pages/weixiu/myweixiu/myweixiu",
        "text": "我的維修",
        "iconPath": "/images/tabbar/myweixiu1.png",
        "selectedIconPath": "/images/tabbar/myweixiu1.png"
      },
      {
        "pagePath": "pages/charts/charts",
        "text": "故障報表",
        "iconPath": "/images/tabbar/chart1.png",
        "selectedIconPath": "/images/tabbar/chart2.png"
      }
    ]
  },
  "sitemapLocation": "sitemap.json"
}

可以看到全域性app.json裡面放了5個不同的tabbar路徑

2.自定義custom-tab-bar

(詳見微信官方檔案)

index.js

Component({
  data: {
    selected: 0,
    color: "#000000",
    roleId: '',
    selectedColor: "#1396DB",
    allList: [{

      list1: [{
        pagePath: "/pages/Users/myrepaire/myrepaire",
        iconPath: "/images/tabbar/weixiu1.png",
        selectedIconPath: "/images/tabbar/weixiu2.png",
        text: "我要報修"
      }, {
        pagePath: "/pages/Users/myMalfunction/myMalfunction",
        iconPath: "/images/tabbar/myweixiu1.png",
        selectedIconPath: "/images/tabbar/myweixiu2.png",
        text: "我的故障"
      }, {
        pagePath: "/pages/logs/logs",
        text: "個人中心",
        iconPath: "/images/tabbar/user1.png",
        selectedIconPath: "/images/tabbar/user2.png"
      }],



      list2: [{
        pagePath: "/pages/weixiu/myweixiu/myweixiu",
        iconPath: "/images/tabbar/weixiu1.png",
        selectedIconPath: "/images/tabbar/weixiu2.png",
        text: "我要維修"
      }, {
        pagePath: "/pages/Users/myMalfunction/myMalfunction",
        iconPath: "/images/tabbar/myweixiu1.png",
        selectedIconPath: "/images/tabbar/myweixiu2.png",
        text: "我的維修"
      }, {
        pagePath: "/pages/logs/logs",
        text: "個人中心",
        iconPath: "/images/tabbar/user1.png",
        selectedIconPath: "/images/tabbar/user2.png"
      }],


      list3: [{
        pagePath: "/pages/Users/myrepaire/myrepaire",
        iconPath: "/images/tabbar/weixiu1.png",
        selectedIconPath: "/images/tabbar/weixiu2.png",
        text: "我要報修"
      }, {
        pagePath: "/pages/charts/charts",
        iconPath: "/images/tabbar/chart1.png",
        selectedIconPath: "/images/tabbar/chart2.png",
        text: "故障報表"
      }, {
        pagePath: "/pages/logs/logs",
        text: "個人中心",
        iconPath: "/images/tabbar/user1.png",
        selectedIconPath: "/images/tabbar/user2.png"
      }]
    }],
    list: []
  },
  attached() {
    const roleId = wx.getStorageSync('statu')
    if (roleId == 20) {
      this.setData({
        list: this.data.allList[0].list1
      })
    }else if(roleId==5){
      this.setData({
        list: this.data.allList[0].list3
      })
    }else if(roleId==102){
      this.setData({
        list: this.data.allList[0].list2
      })
    }
  },
  methods: {
    switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({ url })
      this.setData({
        selected: data.index
      })
    }
  },
})

分析:

  1. 首先,小程式tabbar只識別list裡面的東西
  2. 先在data中定義一個list和allList陣列,把三重身份使用者的list分別定義為list1,list2,list3,放入allList
  3. const roleId = wx.getStorageSync('statu')
    獲取使用者資訊存到快取中,根據不同和的roleId來判斷是什麼身份,
  4. this.setData({ list: this.data.allList[0].list2 })
    根據身份把allList裡面的子陣列賦值給系統預設識別的`list``
  5. switchTab的作用根據不同的路徑切換tabbar下標
   switchTab(e) {
      const data = e.currentTarget.dataset
      const url = data.path
      wx.switchTab({ url })
      this.setData({
        selected: data.index
      })
    }

index.json

{
  "usingComponents": {}
}

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;
}

最後,在tabbar裡面設定過 pagePath的路徑檔案下的 xxx.js的onshow:function(){}裡面設定

或者說凡是用到tabbar元件的頁面對應的xx.js裡的onshow:function(){}都要按照以下進行設定

不然會出現tabbar顯示與點選不同步的現象

  /**
   * 生命週期函數--監聽頁面顯示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }

  },

//selected: 0就是選中的tabbar下標,根據不同頁面顯示不同tabbar下標

結果展示

1.普通使用者

2.運維人員

3.領導

可以看到根據使用者資訊裡的roleId成功的切換了不同的tabbar

總結

到此這篇關於微信小程式如何根據不同使用者切換不同TabBar的文章就介紹到這了,更多相關小程式使用者切換不同TabBar內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com