tab-bar.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import eventEmitter from '../../utils/eventEmitter'
  2. import { isPhoneX } from './../../utils/tools'
  3. import $router from './../../utils/routes'
  4. let app = getApp()
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. inactiveColor: {
  11. type: String,
  12. value: '#79868F'
  13. },
  14. activeColor: {
  15. type: String,
  16. value: '#1FE4DC'
  17. }
  18. },
  19. /**
  20. * 组件的初始数据
  21. */
  22. data: {
  23. active: 0,
  24. tabItems: [
  25. {
  26. title: '首页',
  27. url: 'home',
  28. icon: {
  29. normal: '/assets/images/tabs/home.svg',
  30. active: '/assets/images/tabs/home-active.svg'
  31. }
  32. },
  33. // {
  34. // title: '直播',
  35. // url: 'live',
  36. // icon: {
  37. // normal: '/assets/images/tabs/live.svg',
  38. // active: '/assets/images/tabs/home-active.svg'
  39. // }
  40. // },
  41. {
  42. title: '发现',
  43. url: 'find',
  44. icon: {
  45. normal: '/assets/images/tabs/find.svg',
  46. active: '/assets/images/tabs/home-active.svg'
  47. }
  48. },
  49. {
  50. title: '消息',
  51. url: 'chatList',
  52. info: app.globalData.unViewMsg,
  53. icon: {
  54. normal: '/assets/images/tabs/msg.svg',
  55. active: '/assets/images/tabs/msg-active.svg'
  56. }
  57. },
  58. {
  59. title: '我的',
  60. url: 'my',
  61. icon: {
  62. normal: '/assets/images/tabs/my.svg',
  63. active: '/assets/images/tabs/my-active.svg'
  64. }
  65. },
  66. ],
  67. isPhoneX: false
  68. },
  69. attached: function() {
  70. },
  71. pageLifetimes: {
  72. show () {
  73. const currentRoute = getCurrentPages()[0].route
  74. const { tabItems } = this.data
  75. tabItems[3].info = app.globalData.unViewMsg
  76. tabItems.forEach((item, index) => {
  77. if ($router.tabRoutes[item.url] && $router.tabRoutes[item.url].indexOf(currentRoute) !== -1) {
  78. this.setData({
  79. active: index,
  80. tabItems: tabItems,
  81. })
  82. }
  83. })
  84. isPhoneX().then(res => {
  85. this.setData({
  86. isPhoneX: res
  87. })
  88. })
  89. this.init()
  90. },
  91. hide () {
  92. getApp().getIMHandler().removeOnReceiveMessageListener({listener: this.fn})
  93. }
  94. },
  95. /**
  96. * 组件的方法列表
  97. */
  98. methods: {
  99. onChange(e) {
  100. const index = e.detail, { tabItems } = this.data
  101. $router.push(tabItems[index].url)
  102. // wx.switchTab({
  103. // url: tabItems[index].url,
  104. // })
  105. },
  106. init () {
  107. this.fn = (msg) => {
  108. const { tabItems } = this.data
  109. app.globalData.unViewMsg = app.globalData.unViewMsg ? app.globalData.unViewMsg+1 : 1
  110. tabItems[3].info = app.globalData.unViewMsg
  111. this.setData({
  112. tabItems
  113. })
  114. app.getNewMessage(msg)
  115. }
  116. getApp().getIMHandler().setOnReceiveMessageListener({
  117. listener: this.fn
  118. });
  119. }
  120. }
  121. })