微信小程序 根据不同用户切换不同TabBar


Posted in Javascript onApril 21, 2022

现有需求:

小程序用户有三种身份(公众、运维人员、领导),根据不同用户身份显示不同的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

(详见微信官方文档)

微信小程序 根据不同用户切换不同TabBar

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.普通用户

微信小程序 根据不同用户切换不同TabBar

2.运维人员

微信小程序 根据不同用户切换不同TabBar

3.领导

微信小程序 根据不同用户切换不同TabBar

可以看到根据用户信息里的roleId成功的切换了不同的tabbar

总结

到此这篇关于微信小程序如何根据不同用户切换不同TabBar的文章就介绍到这了!


Tags in this post...

Javascript 相关文章推荐
tagName的使用,留一笔
Jun 26 Javascript
用一段js程序来实现动画功能
Mar 06 Javascript
JS中自定义定时器让它在某一时刻执行
Sep 02 Javascript
Javascript模块化编程详解
Dec 01 Javascript
node.js中的fs.chmodSync方法使用说明
Dec 18 Javascript
jquery获取select选中值的方法分析
Dec 22 Javascript
JavaScript仿支付宝密码输入框
Dec 29 Javascript
javascript实现获取指定精度的上传文件的大小简单实例
Oct 25 Javascript
微信小程序封装分享与分销功能过程解析
Aug 13 Javascript
Vue修改项目启动端口号方法
Nov 07 Javascript
vue+animation实现翻页动画
Jun 29 Javascript
使用Vue3+Vant组件实现App搜索历史记录功能(示例代码)
Jun 09 Vue.js
vue动态绑定style样式
Apr 20 #Vue.js
JS setTimeout与setInterval的区别
Apr 20 #Javascript
Vue OpenLayer测距功能的实现
vue 给数组添加新对象并赋值
Apr 20 #Vue.js
vue 数字翻牌器动态加载数据
Apr 20 #Vue.js
vue3.0 数字翻牌组件的使用方法详解
Apr 20 #Vue.js
vue封装数字翻牌器
Apr 20 #Vue.js
You might like
php2html php生成静态页函数
2008/12/08 PHP
谈谈新手如何学习PHP 默默经典版本
2009/08/04 PHP
php中常用字符串处理代码片段整理
2011/11/07 PHP
php缩小png图片不损失透明色的解决方法
2013/12/25 PHP
PHP.ini安全配置检测工具pcc简单介绍
2015/07/02 PHP
php图像验证码生成代码
2017/06/08 PHP
Underscore.js常用方法总结
2015/02/28 Javascript
JavaScript SHA512加密算法详细代码
2016/10/06 Javascript
VueJs与ReactJS和AngularJS的异同点
2016/12/12 Javascript
Linux Centos7.2下安装nodejs&amp;npm配置全局路径的教程
2018/05/15 NodeJs
jQuery实现表单动态加减、ajax表单提交功能
2018/06/08 jQuery
echarts实现地图定时切换散点与多图表级联联动详解
2018/08/07 Javascript
深入理解Vue父子组件生命周期执行顺序及钩子函数
2018/08/12 Javascript
Vue.js更改调试地址端口号的实例
2018/09/19 Javascript
javscript 数组扁平化的实现
2020/02/03 Javascript
vue+springboot+element+vue-resource实现文件上传教程
2020/10/21 Javascript
[01:03:38]2014 DOTA2国际邀请赛中国区预选赛5.21 CNB VS CIS
2014/05/22 DOTA
Python实现的概率分布运算操作示例
2017/08/14 Python
Python爬豆瓣电影实例
2018/02/23 Python
在spyder IPython console中,运行代码加入参数的实例
2020/04/20 Python
HTML5 video循环播放多个视频的方法步骤
2020/08/06 HTML / CSS
Guess荷兰官网:美国服饰品牌
2020/01/22 全球购物
Linux如何为某个操作添加别名
2013/03/01 面试题
Linux上比较文件的命令都有哪些
2013/09/28 面试题
电子商务自荐书范文
2014/01/04 职场文书
优秀教师事迹简介
2014/02/02 职场文书
监察建议书范文
2014/03/12 职场文书
2014年群众路线教育实践活动整改措施
2014/09/24 职场文书
一份教室追逐打闹的检讨书
2014/09/27 职场文书
省委召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
精神文明建设先进个人事迹材料
2014/12/24 职场文书
催款通知书范文
2015/04/17 职场文书
Java输出Hello World完美过程解析
2021/06/13 Java/Android
使用 CSS 轻松实现一些高频出现的奇形怪状按钮
2021/12/06 HTML / CSS
MySQL数据库实验之 触发器和存储过程
2022/06/21 MySQL
python可视化分析绘制带趋势线的散点图和边缘直方图
2022/06/25 Python