业务交流通
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

tabbar.js 5.5KB

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