Layout.vue 6.13 KB
<template>
  <div :class="classObj" class="app-wrapper">
    <div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside"/>
    <sidebar class="sidebar-container"/>
    <div class="main-container">
      <navbar/>
      <app-main/>
    </div>
  </div>
</template>

<script>
import { Navbar, Sidebar, AppMain } from './components'
import ResizeMixin from './mixin/ResizeHandler'

export default {
  name: 'Layout',
  components: {
    Navbar,
    Sidebar,
    AppMain
  },
  mixins: [ResizeMixin],
  computed: {
    sidebar() {
      return this.$store.state.app.sidebar
    },
    device() {
      return this.$store.state.app.device
    },
    classObj() {
      return {
        hideSidebar: !this.sidebar.opened,
        openSidebar: this.sidebar.opened,
        withoutAnimation: this.sidebar.withoutAnimation,
        mobile: this.device === 'mobile'
      }
    }
  },
  methods: {
    handleClickOutside() {
      this.$store.dispatch('closeSideBar', { withoutAnimation: false })
    },
    notificationFn(data){
      const _this = this;

      // var notification = new Notification('有个竞价单超过3小时无人报价', {
      //   body: data.orderSn+' , '+data.shopName + '  '+data.serviceTypes,
      //   icon: 'favicon.ico'
      // });
      // notification.onclick = function () {
      //   console.log('点击');
      //   _this.$router.push({
      //     path: '/order/order',
      //   })
      //   notification.close();
      // };
      // return
      console.log(data,'web data from server')
      if (window.Notification) {
        var ua = navigator.userAgent.toLowerCase();
        if (ua.indexOf('safari') != -1) {
          if (ua.indexOf('chrome') > -1 || ua.indexOf('firefox') > -1) {
            // Chrome
            Notification.requestPermission().then(function (permission) {
              console.log(permission,'Notification');
              if (permission == "granted") {
                  var notification = new Notification('有个竞价单超过'+data.hour+'小时无人报价', {
                    body: data.orderSn+' , '+data.shopName + '  '+data.serviceTypes,
                    icon: 'favicon.ico'
                  });
                  notification.onclick = function () {
                    console.log('点击');
                    _this.$router.push({
                      path: '/order/bidding',
                    })
                    notification.close();
                  };
              } else {
                Notification.requestPermission();
                console.log('没有权限,用户拒绝:Notification');
              }
            });
          } else {
              // Safari
              Notification.requestPermission(function (permission) {
                if (permission == "granted") {
                  var notification = new Notification('有个竞价单超过'+data.hour+'小时无人报价', {
                    body: data.orderSn+' , '+data.shopName + '  '+data.serviceTypes,
                    icon: 'favicon.ico'
                  });
                  notification.onclick = function () {
                    console.log('点击');
                    notification.close();
                  };
                } else {
                  Notification.requestPermission();
                  console.log('没有权限,用户拒绝:Notification');
                }
              })
          }
        }
      } else {
        console.log('不支持Notification');
      }
    },
    initWebSocket(){
      //浏览器消息通知弹框
      // this.notificationFn()
      const that = this;
      if (typeof (WebSocket) == 'undefined') {
        this.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时通知信息,请使用谷歌浏览器!',
          type: 'warning',
          position: 'bottom-right',
          duration: 0
        });
      } else {
        // 实例化socket,这里我把用户名传给了后台,使后台能判断要把消息发给哪个用户,其实也可以后台直接获取用户IP来判断并推送
          const socketUrl = process.env.BASE_WS+'/carservice/usercenter/websocket/'+this.$store.getters.navbarId;
          this.socket = new WebSocket(socketUrl);
          //心跳测试
          const heartCheck = {
            timeout:5000,
            timeObj:null,
            reset:function(){
              clearInterval(this.timeObj);
              return this;
            },
            start:function(){
              this.timeObj = setInterval(function(){
                that.socket.send('1');
              },this.timeout)
            }
          }

          // 监听socket打开
          this.socket.onopen = function() {
            console.log('浏览器WebSocket已打开');
            heartCheck.reset().start();
          };
          // 监听socket消息接收
          this.socket.onmessage = function(msg) {
            // 转换为json对象
            const data = JSON.parse(msg.data);
            if(!data||data.code != 200) return;
            that.notificationFn(data.data)
          };
          // 监听socket错误
          this.socket.onerror = function() {
            that.$notify({
              title: '错误',
              message: '服务器連接错误,无法接收实时通知信息',
              type: 'error',
              position: 'bottom-right',
              duration: 0
            });
          };
          // 监听socket关闭
          this.socket.onclose = function() {
            console.log('WebSocket已关闭');
          }
      }
    },
  },
  created(){
    //启动websocket
    if(!this.$store.getters.isOpenWebsocket&&this.$store.getters.navbarType === 0){
      this.$store.dispatch('SetISWebsocket',true);
      // this.initWebSocket();
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  @import "~@/styles/mixin.scss";
  .app-wrapper {
    @include clearfix;
    position: relative;
    height: 100%;
    width: 100%;
    &.mobile.openSidebar{
      position: fixed;
      top: 0;
    }
  }
  .drawer-bg {
    background: #000;
    opacity: 0.3;
    width: 100%;
    top: 0;
    height: 100%;
    position: absolute;
    z-index: 999;
  }
</style>