123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- // components/tabbar/tabbar.js
- const app = getApp()
- const $request = require('../../utils/request.js');
- Component({
- /**
- * 组件的属性列表
- */
- properties: {
- currentIndex: {
- type: String,
- value: '0',
- }
- },
-
- /**
- * 组件的初始数据
- */
- data: {
- customerId: null,
- list: [
- {
- text: "首页",
- pagePath: "/pages/index/index",
- iconPath: "/images/tabbar/home-bar.png",
- selectedIconPath: "/images/tabbar/home-bar-selected.png",
- },
- {
- text: "发布需求",
- pagePath: "/pages/releaseModule/index",
- iconPath: "/images/tabbar/release-bar.png",
- selectedIconPath: "/images/tabbar/release-bar-selected.png",
- },
- {
- text: "消息",
- pagePath: "/pages/msgModule/index",
- iconPath: "/images/tabbar/msg-bar.png",
- selectedIconPath: "/images/tabbar/msg-bar-selected.png",
- dot: false,
- },
- {
- text: "我的",
- pagePath: "/pages/myModule/index",
- iconPath: "/images/tabbar/my-bar.png",
- selectedIconPath: "/images/tabbar/my-bar-selected.png",
- },
- ]
- },
- /*
- *
- * 小程序自定义组件挂载后执行
- *
- */
- ready() {
- const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer') || null;
- if (businessCommunicationCustomer) {
- this.setData({
- customerId: businessCommunicationCustomer.customerId
- })
- }
- // 如果 websocket 没有连接,连接
- if (!app.globalData.isOnSocketOpen) {
- this.initWebSocket();
- }
- // let _this = this;
- let tiems = setInterval(() => {
- // 如果此时openid已被赋值
- if (!!app.globalData.openid) {
- this.getChatRecordTable();
- clearInterval(tiems)
- }
- }, 500);
- },
- /**
- * 组件的方法列表
- */
- methods: {
- // 获取当前路由
- getChatRecordTable() {
- this.setListDot(false);
- $request.get('/businessCommunicationDemand/getChatRecordTable.action').then(res => {
- // console.log(res,'res');
- if(res.status == 0){
- let chatRecordTable = res.data.chatRecordTable;
- let sum = 0;
- chatRecordTable.forEach(el=>{
- sum += el.size;
- })
- if(sum > 0){
- this.setListDot(true);
- }
- }
- }).catch(err=>{
- console.log(err);
- })
- },
- // init websocket
- initWebSocket() {
- let _this = this;
- let customerId = this.data.customerId;
- //建立连接
- wx.connectSocket({
- url: `ws://192.168.18.138/webSocket/{"userno":${customerId},"messageModule":"010"}`,//本地
- success: function () {
- console.log('消息页-websocket连接成功~')
- },
- fail: function () {
- console.log('消息页-websocket连接失败~')
- },
- })
-
- //连接成功
- wx.onSocketOpen(function () {
- console.log('tabbar-连接成功,真正的成功', 'onSocketOpen');
- // 连接成功后,设置为 true 放置反复连接
- app.globalData.isOnSocketOpen = true;
- })
-
- // 接收服务器的消息事件
- wx.onSocketMessage(function (res) {
- // 收到消息让tabbar消息有红点
- _this.setListDot(true);
- })
-
- // 监听连接关闭
- wx.onSocketClose(function () {
- console.log('监听 WebSocket 连接关闭事件')
- })
-
- },
- // 设置tabbar消息红点显示与否
- setListDot(isShow) {
- let list = this.data.list;
- list.forEach(el => {
- if (el.text == '消息') {
- el.dot = isShow;
- }
- })
- this.setData({
- list
- })
- },
- // tab切换
- tabChange(e) {
- console.log(e);
- if (e.detail.item.text == "发布需求" || e.detail.item.text == "消息") {
- if (!app.globalData.customerId) {
- app.goLogin();
- this.setData({
- currentIndex: this.data.currentIndex
- })
- return
- }
- }
-
- const url = e.detail.item.pagePath;
- // wx.redirectTo({ url });
- wx.navigateTo({
- url,
- // success: function (res) {
- // // 通过eventChannel向被打开页面传送数据
- // res.eventChannel.emit('customerid', { customerid })
- // }
- })
- }
- }
- })
|