爱简收旧衣按件回收前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.4 KiB

1 week ago
  1. import AdminTabBar from '../../wxcomponents/admin-tabr/admin-tabar'
  2. import UserTabBar from '../../wxcomponents/user-tabar/user-tabar'
  3. export default {
  4. components: {
  5. AdminTabBar,
  6. UserTabBar
  7. },
  8. data() {
  9. return {
  10. userRole: '',
  11. isTabBarVisible: true,
  12. currentPath: '',
  13. tabBarIndex: 0
  14. }
  15. },
  16. computed: {
  17. isAdmin() {
  18. return this.userRole === 'admin'
  19. },
  20. shouldShowTabBar() {
  21. const tabBarPages = [
  22. '/pages/component/admin_home',
  23. '/pages/component/role_management',
  24. '/pages/component/admin_my'
  25. ]
  26. return tabBarPages.includes(this.currentPath)
  27. }
  28. },
  29. methods: {
  30. async checkUserRole() {
  31. try {
  32. const userInfo = uni.getStorageSync('userInfo')
  33. if (userInfo && userInfo.role) {
  34. this.userRole = userInfo.role
  35. } else {
  36. this.userRole = 'user'
  37. }
  38. } catch (e) {
  39. console.error('获取用户角色失败:', e)
  40. this.userRole = 'user'
  41. }
  42. },
  43. updateCurrentPath() {
  44. const pages = getCurrentPages()
  45. if (pages.length > 0) {
  46. const currentPage = pages[pages.length - 1]
  47. console.log(currentPage,'text');
  48. this.currentPath = '/' + currentPage.route
  49. this.isTabBarVisible = this.shouldShowTabBar
  50. console.log('当前页面路径:', this.currentPath)
  51. }
  52. },
  53. hideTabBar() {
  54. this.isTabBarVisible = false
  55. },
  56. showTabBar() {
  57. this.isTabBarVisible = this.shouldShowTabBar
  58. },
  59. switchTab(index) {
  60. const routes = ['pages/index/index', 'pages/role-management/role-management', 'pages/my/my']
  61. if (!this.isAdmin && index === 1) {
  62. uni.showToast({
  63. title: '无权限访问',
  64. icon: 'none'
  65. })
  66. return
  67. }
  68. this.tabBarIndex = index
  69. uni.switchTab({
  70. url: '/' + routes[index]
  71. })
  72. }
  73. },
  74. onShow() {
  75. this.checkUserRole()
  76. this.updateCurrentPath()
  77. // 获取当前页面路径,设置正确的tabBarIndex
  78. const pages = getCurrentPages()
  79. const currentPage = pages[pages.length - 1]
  80. const route = currentPage.route
  81. const routes = ['pages/index/index', 'pages/role-management/role-management', 'pages/my/my']
  82. this.tabBarIndex = routes.findIndex(r => r === route)
  83. },
  84. onLoad() {
  85. this.checkUserRole()
  86. this.updateCurrentPath()
  87. },
  88. mounted() {
  89. this.checkUserRole()
  90. this.updateCurrentPath()
  91. }
  92. }