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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 && this.data.customerId) {
  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. let url = encodeURI(`ws://` + app.webSocketUrl + `/webSocket/{"userno":${customerId},"messageModule":"010"}`);
  100. //建立连接
  101. wx.connectSocket({
  102. url,//本地
  103. success: function () {
  104. console.log('tabbar-websocket接口调通~还没真正的成功!')
  105. },
  106. fail: function () {
  107. console.log('tabbar-websocket连接失败~')
  108. },
  109. })
  110. wx.onSocketError(res=>{
  111. console.log(`打印异常`)
  112. console.log(res);
  113. })
  114. //连接成功
  115. wx.onSocketOpen(function () {
  116. console.log('tabbar-连接成功,真正的成功', 'onSocketOpen');
  117. // 连接成功后,设置为 true 放置反复连接
  118. app.globalData.isOnSocketOpen = true;
  119. })
  120. // 接收服务器的消息事件
  121. wx.onSocketMessage(function (res) {
  122. // 收到消息让tabbar消息有红点
  123. _this.setListDot(true);
  124. })
  125. // 监听连接关闭
  126. wx.onSocketClose(function () {
  127. console.log('监听 WebSocket 连接关闭事件')
  128. })
  129. },
  130. // 设置tabbar消息红点显示与否
  131. setListDot(isShow) {
  132. let list = this.data.list;
  133. list.forEach(el => {
  134. if (el.text == '消息') {
  135. el.dot = isShow;
  136. }
  137. })
  138. this.setData({
  139. list
  140. })
  141. },
  142. // tab切换
  143. tabChange(e) {
  144. if (e.detail.item.text == "发布需求" || e.detail.item.text == "消息") {
  145. if (!app.globalData.customerId) {
  146. app.goLogin();
  147. this.setData({
  148. currentIndex: this.data.currentIndex
  149. })
  150. return
  151. }
  152. }
  153. const url = e.detail.item.pagePath;
  154. // wx.redirectTo({ url });
  155. wx.reLaunch({
  156. url,
  157. // success: function (res) {
  158. // // 通过eventChannel向被打开页面传送数据
  159. // res.eventChannel.emit('customerid', { customerid })
  160. // }
  161. })
  162. }
  163. }
  164. })