qingyiay
2023-04-19 3fd84886a15713ece1ba34954f49fd1e075ef7be
pages/tabbar-page/index-tabbar/index-tabbar.vue
@@ -1,9 +1,21 @@
<template>
   <view>
      <customer-index v-if="roleType == 1" ref="customerIndexRef"></customer-index>
      <freight-forwarder-index v-if="roleType == 2" ref="freightForwarderIndexRef"></freight-forwarder-index>
      <driver-index v-if="roleType == 3" ref="driverIndexRef"></driver-index>
      <view class="search-bar">
         <view class="icon-box" @click="messagePage"><u-icon name="chat" color="#000" size="50"></u-icon></view>
      </view>
      <customer-index v-if="roleType == 1" ref="customerIndexRef" :orderPlanDataStore="orderPlanDataStore"></customer-index>
      <freight-forwarder-index
         v-if="roleType == 2"
         ref="freightForwarderIndexRef"
         :indexHistoryCoalData="indexHistoryCoalData"
         :indexHuoDaiOrderPlanData="indexHuoDaiOrderPlanData"
      ></freight-forwarder-index>
      <driver-index v-if="roleType == 3" ref="driverIndexRef" :indexdriverBillOfLoadingData="indexdriverBillOfLoadingData"></driver-index>
      <tab-bar :current="0"></tab-bar>
      <!-- 推送消息弹窗 -->
      <u-modal :show="messagePushShow" :title="messageList.title" @confirm="messageconfirm">
         <view class="slot-content"><rich-text :nodes="messageList.content"></rich-text></view>
      </u-modal>
   </view>
</template>
@@ -11,25 +23,127 @@
import customerIndex from '@/pages/customer-page/customer-index/customer-index.vue';
import driverIndex from '@/pages/driver-page/driver-index/driver-index.vue';
import freightForwarderIndex from '@/pages/freight-forwarder-page/freightForwarder-index/freightForwarder-index.vue';
import { mapState, mapMutations } from 'vuex';
import { webSocketUrl } from '@/api/request.js';
let socket = null;
export default {
   components: {
      customerIndex,
      driverIndex,
      freightForwarderIndex
   },
   computed: {
      ...mapState(['globalweighHouseCode', 'globalweigh'])
   },
   //首页下拉刷新
   onPullDownRefresh() {
      uni.showLoading({ title: '加载中...' });
      // 获取历史提煤单
      if (this.roleType == 1 || this.roleType == 2) {
         this.$reqGet('getJhOrderPlanDataPage', { current: 1, size: 10 }).then(res => {
            if (res.data.records) {
               this.indexHistoryCoalData = res.data.records;
               uni.hideLoading();
               uni.stopPullDownRefresh();
            } else {
               this.$u.toast('加载失败');
            }
         });
      }
      // 客户获取日计划
      if (this.roleType == 1) {
         this.$reqGet('GetOrderPlan').then(res => {
            if (res.data) {
               this.orderPlanDataStore = res.data;
               this.$u.toast('加载成功');
               uni.hideLoading();
               uni.stopPullDownRefresh();
            } else {
               this.$u.toast('加载失败');
            }
         });
      } else if (this.roleType == 2) {
         this.$reqGet('huoDaiList').then(res => {
            if (res.data) {
               this.indexHuoDaiOrderPlanData = res.data;
               this.$u.toast('加载成功');
               uni.hideLoading();
               uni.stopPullDownRefresh();
            } else {
               this.$u.toast('加载失败');
            }
         });
      } else if (this.roleType == 3) {
         this.$reqGet('qiangDanList').then(res => {
            if (res.code == 0) {
               this.indexdriverBillOfLoadingData = res.data;
               this.$u.toast('加载成功');
               uni.hideLoading();
               uni.stopPullDownRefresh();
            } else {
               this.$u.toast('加载失败');
               uni.hideLoading();
               uni.stopPullDownRefresh();
            }
         });
      }
   },
   onLoad() {
      // 开启双人通话
      wx.setEnable1v1Chat({
         enable: true,
         backgroundType: 1,
         minWindowType: 2,
         success() {
            console.log('开启双人通话成功');
         },
         fail() {
            console.log('开启双人通话失败');
         },
         complete() {
            console.log('开启双人通话成功Complete');
         }
      });
   },
   data() {
      return {
         roleType: null
         roleType: null,
         orderPlanDataStore: [],
         indexHuoDaiOrderPlanData: [],
         indexHistoryCoalData: [],
         indexdriverBillOfLoadingData: {},
         messagePushShow: false,
         messageList: {},
         isconnect: false
      };
   },
   onShow() {
      this.init();
      if (!this.isconnect) {
         this.initWebsocket();
      }
      this.userAuthorization();
   },
   onHide() {
      console.log('页面隐藏');
      if (!this.isconnect) {
         this.initWebsocket();
      }
   },
   onUnload() {
      console.log('页面卸载');
      socket.close();
      clearInterval(this.intervalId);
   },
   methods: {
      ...mapMutations(['pushMessage', 'changeWeigh', 'changeinfraredStatus']),
      messagePage() {
         uni.navigateTo({
            url: '/pages/public-page/message/message'
         });
      },
      init() {
         this.roleType = uni.getStorageSync('roleType');
         console.log(this.roleType, 'indexTabbar');
         switch (this.roleType) {
            case 1:
               console.log('customerIndexRef');
@@ -52,9 +166,125 @@
            default:
               break;
         }
      },
      // 要求用户授权相机/麦克风权限
      userAuthorization() {
         wx.getSetting({
            success(res) {
               // wx.startRecord();
               console.log('success');
               if (!res.authSetting['scope.record'] || !res.authSetting['scope.camera']) {
                  if (!res.authSetting['scope.camera']) {
                     wx.authorize({
                        scope: 'scope.camera',
                        success() {
                           // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
                           console.log('授权成功');
                        },
                        fail() {
                           console.log('授权失败');
                        }
                     });
                  } else if (!res.authSetting['scope.record']) {
                     wx.authorize({
                        scope: 'scope.record',
                        success() {
                           // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
                           console.log('授权成功');
                        },
                        fail: () => {
                           console.log('授权失败');
                        }
                     });
                  }
               }
            },
            fail() {
               console.log('获取失败');
            }
         });
      },
      // 初始化websocket
      initWebsocket() {
         let wsUrl = `${webSocketUrl}?access_token=${uni.getStorageSync('token')}`;
         socket = uni.connectSocket({
            url: wsUrl,
            header: {
               CLIENT_TOC: 'Y'
            },
            complete: res => {
               console.log(res, 'socket结果');
               if (res.errMsg == 'connectSocket:ok') {
                  this.isconnect = true;
               }
            }
         });
         socket.onOpen(() => {
            console.log('onOpen');
            this.intervalId = setInterval(() => {
               socket.send({
                  data: JSON.stringify({ type: 'ping' }),
                  success(e) {
                     console.log(e, '发送心跳成功');
                  }
               });
            }, 30000);
         });
         // 获取服务器传来的数据,做相应处理
         socket.onMessage(res => {
            console.log('message', res);
            if (res.data.startsWith('weigh')) {
               let nowWeighObj = JSON.parse(res.data.slice(7));
               if (this.globalweighHouseCode && nowWeighObj.eqCode == this.globalweighHouseCode) {
                  if (nowWeighObj.eqInfraredStatus) {
                     this.changeinfraredStatus(true);
                     this.changeWeigh(nowWeighObj.weigh);
                  } else {
                     this.changeinfraredStatus(false);
                     this.changeWeigh(nowWeighObj.weigh);
                  }
               }
            } else if (res.data.startsWith('msg')) {
               this.messageList = JSON.parse(res.data.slice(5));
               this.messagePushShow = true;
            } else {
               console.log('websocket还在活着');
            }
         });
         socket.onClose(e => {
            console.log('webSocketClose', e);
            this.$u.toast('连接已断开,请重新进入该页面,重试!!');
            this.isconnect = false;
         });
         socket.onError(err => {
            console.log('socket报错', err);
            this.$u.toast('出现错误,请重新进入该页面,重试!!');
         });
      },
      messageconfirm() {
         this.messagePushShow = false;
      }
   }
};
</script>
<style lang="scss" scoped></style>
<style lang="scss" scoped>
.search-bar {
   height: vww(20);
   width: 96%;
   margin: vww(3) auto;
   position: relative;
   .icon-box {
      width: vww(30);
      height: vww(30);
      background-color: #eee;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      position: absolute;
      right: vww(-3);
   }
}
</style>