业务交流通
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.

tabbar.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // components/tabbar/tabbar.js
  2. const app = getApp()
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. currentIndex: {
  9. type: String,
  10. value: '0',
  11. }
  12. },
  13. /**
  14. * 组件的初始数据
  15. */
  16. data: {
  17. customerId: null,
  18. list: [
  19. {
  20. text: "首页",
  21. pagePath: "/pages/index/index",
  22. iconPath: "/images/tabbar/home-bar.png",
  23. selectedIconPath: "/images/tabbar/home-bar.png",
  24. },
  25. {
  26. text: "发布需求",
  27. pagePath: "/pages/releaseModule/index",
  28. iconPath: "/images/tabbar/release-bar.png",
  29. selectedIconPath: "/images/tabbar/release-bar.png",
  30. },
  31. {
  32. text: "消息",
  33. pagePath: "/pages/msgModule/index",
  34. iconPath: "/images/tabbar/msg-bar.png",
  35. selectedIconPath: "/images/tabbar/msg-bar.png",
  36. dot: false,
  37. },
  38. {
  39. text: "我的",
  40. pagePath: "/pages/myModule/index",
  41. iconPath: "/images/tabbar/my-bar.png",
  42. selectedIconPath: "/images/tabbar/my-bar.png",
  43. },
  44. ]
  45. },
  46. /*
  47. *
  48. * 小程序自定义组件挂载后执行
  49. *
  50. */
  51. ready() {
  52. const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
  53. if (businessCommunicationCustomer) {
  54. this.setData({
  55. customerId: businessCommunicationCustomer.customerId
  56. })
  57. }
  58. // 获取当前路由,如果是消息页的话要把消息的红点取消
  59. this.getCurrentRouter();
  60. // 如果 websocket 没有连接,连接
  61. if (!app.globalData.isOnSocketOpen) {
  62. this.initWebSocket();
  63. }
  64. },
  65. /**
  66. * 组件的方法列表
  67. */
  68. methods: {
  69. // 获取当前路由
  70. getCurrentRouter() {
  71. let pages = getCurrentPages();
  72. let currentPage = null;
  73. if (pages.length) {
  74. currentPage = pages[pages.length - 1];
  75. }
  76. // 如果是消息页,置空红点
  77. if (currentPage.route == "pages/msgModule/index") {
  78. this.setListDot(false);
  79. }
  80. },
  81. // init websocket
  82. initWebSocket() {
  83. let _this = this;
  84. let customerId = this.data.customerId;
  85. //建立连接
  86. wx.connectSocket({
  87. url: `ws://192.168.18.138/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地
  88. success: function () {
  89. console.log('消息页-websocket连接成功~')
  90. },
  91. fail: function () {
  92. console.log('消息页-websocket连接失败~')
  93. },
  94. })
  95. //连接成功
  96. wx.onSocketOpen(function () {
  97. console.log('tabbar-连接成功,真正的成功', 'onSocketOpen');
  98. // 连接成功后,设置为 true 放置反复连接
  99. app.globalData.isOnSocketOpen = true;
  100. })
  101. // 接收服务器的消息事件
  102. wx.onSocketMessage(function (res) {
  103. // 收到消息让tabbar消息有红点
  104. _this.setListDot(true);
  105. })
  106. // 监听连接关闭
  107. wx.onSocketClose(function () {
  108. console.log('监听 WebSocket 连接关闭事件')
  109. })
  110. },
  111. // 设置tabbar消息红点显示与否
  112. setListDot(isShow) {
  113. let list = this.data.list;
  114. list.forEach(el => {
  115. if (el.text == '消息') {
  116. el.dot = isShow;
  117. }
  118. })
  119. this.setData({
  120. list
  121. })
  122. },
  123. // tab切换
  124. tabChange(e) {
  125. console.log(e);
  126. if (e.detail.item.text == "发布需求" || e.detail.item.text == "消息") {
  127. if (!app.globalData.customerId) {
  128. app.goLogin();
  129. this.setData({
  130. currentIndex: this.data.currentIndex
  131. })
  132. return
  133. }
  134. }
  135. const url = e.detail.item.pagePath;
  136. // wx.redirectTo({ url });
  137. wx.navigateTo({
  138. url,
  139. // success: function (res) {
  140. // // 通过eventChannel向被打开页面传送数据
  141. // res.eventChannel.emit('customerid', { customerid })
  142. // }
  143. })
  144. }
  145. }
  146. })