gaoshp
2024-01-05 b5db49ce1e4c678ce4a7120928811a9621128b8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<template>
  <section class="app-main" ref="container" v-bind:style="appMainsStyleObject">
    <transition name="fade" mode="out-in">
      <!-- <router-view :key="key"></router-view> -->
      <router-view></router-view>
    </transition>
  </section>
</template>
 
<script>
  // import { default as Bus, SCROLL_TOP, ON_VISIBILITY_CHANGE } from '@/bus/EventBus'
  // import { default as Bus, SCROLL_TOP, ON_MESSAGE, ON_VISIBILITY_CHANGE } from '@/bus/EventBus'
  import { default as Bus, SCROLL_TOP, ON_VISIBILITY_CHANGE } from '@/bus/EventBus'
  import { URL_CFG } from '@/api/MdcApi'
  import { getToken } from '@/utils/auth'
  import { mapGetters } from 'vuex'
  export default {
    name: 'AppMain',
    computed: {
      appMainsStyleObject: function() {
        return `height: ${this.$store.getters.mainHeight}px`
      },
      ...mapGetters({
        userInfo: 'userInfo'
      })
      // key() {
      //   return this.$route.name !== undefined ? this.$route.name + +new Date() : this.$route + +new Date()
      // }
    },
    created() {
      Bus.$on(SCROLL_TOP, () => {
        this.$refs.container.scrollTop = 0
      })
      // this.openWebSocket()
      document.addEventListener('click', ev => {
        this.$store.dispatch('GetInfo')
      }, false)
      let state
      let visibilityChange
      if (typeof document.hidden !== 'undefined') {
        visibilityChange = 'visibilitychange'
        state = 'visibilityState'
      } else if (typeof document.mozHidden !== 'undefined') {
        visibilityChange = 'mozvisibilitychange'
        state = 'mozVisibilityState'
      } else if (typeof document.msHidden !== 'undefined') {
        visibilityChange = 'msvisibilitychange'
        state = 'msVisibilityState'
      } else if (typeof document.webkitHidden !== 'undefined') {
        visibilityChange = 'webkitvisibilitychange'
        state = 'webkitVisibilityState'
      }
      // 页面可见性变化
      document.addEventListener(visibilityChange, () => {
        if (document[state] === 'visible') {
          Bus.$emit(ON_VISIBILITY_CHANGE)
          // this.openWebSocket()
        } else {
          if (this.webSocket) {
            // this.webSocket.close()
          }
        }
      }, false)
    },
    data() {
      return {
        webSocket: null
      }
    },
    methods: {
      /**
       *
       * @param msg
       * @param type  (success warning error)
       */
      showToast(msg, type) {
        const opt = { message: msg }
        if (type) {
          opt.type = type
        }
        this.$message(opt)
      },
      openWebSocket() {
        // let webSocket = null
        if ('WebSocket' in window) {
          this.webSocket = new WebSocket(URL_CFG.WEB_SOCKET_URL)
          this.webSocket.onopen = () => {
            // console.log('数据同步已开启')
          }
          window.onbeforeunload = () => {
            if (this.webSocket) {
              this.webSocket.close()
            }
          }
          this.webSocket.onclose = () => {
            // console.log('数据同步已关闭')
          }
          this.webSocket.onmessage = (res) => {
            // console.log(res)
            const data = res.data || '{}'
            // console.log(data)
            if (data.user && data.user === this.userInfo.user) {
              if (getToken().value !== data.token) {
                this.showToast(`您已经被迫下线,请重新登录或联系管理员`, 'error')
                setTimeout(() => {
                  this.$store.dispatch('LogOut').then(() => {
                    location.reload() // 为了重新实例化vue-router对象 避免bug
                  })
                }, 2000)
              } else {
                this.$store.dispatch('GetInfo').then(res => {
                  const currentRouter = this.$router.currentRoute
                  const workshopList = this.$store.getters.workshopList || []
                  const workshopIdArr = workshopList.map(item => item.id)
                  if (workshopIdArr.length === 0 || workshopIdArr.indexOf(currentRouter.params.pid) === -1) {
                    // console.log(`您已经没有权限操作此页面,请重新登录`)
                    this.showToast(`您已经被迫下线,请重新登录或联系管理员`, 'error')
                    setTimeout(() => {
                      this.$store.dispatch('LogOut').then(() => {
                        location.reload() // 为了重新实例化vue-router对象 避免bug
                      })
                    }, 2000)
                  }
                })
              }
              return
            }
            // 广播socket数据
            // Bus.$emit(ON_MESSAGE, data)
          }
        } else {
          // console.log('请使用ie9以上的浏览器访问')
        }
      }
    }
  }
</script>