业务交流通
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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