2 次代码提交 d36df64d00 ... 9b963ea038

作者 SHA1 备注 提交日期
  double 9b963ea038 optimize: WebSocket连接移入layout组件中 3 年之前
  double b391abeec7 feat: 消息提醒-建立WebSocket连接 3 年之前
共有 4 个文件被更改,包括 103 次插入5 次删除
  1. 1 2
      src/App.vue
  2. 90 2
      src/components/common/layout/layout.vue
  3. 10 0
      src/pages/login/login.vue
  4. 2 1
      src/store/modules/comVal.js

+ 1 - 2
src/App.vue

@@ -15,10 +15,9 @@ export default {
15
         
15
         
16
     },
16
     },
17
     created: function () {
17
     created: function () {
18
-        
18
+
19
     },
19
     },
20
     methods: {
20
     methods: {
21
-        
22
     }
21
     }
23
 }
22
 }
24
 </script>
23
 </script>

+ 90 - 2
src/components/common/layout/layout.vue

@@ -22,7 +22,10 @@
22
           {{$route.name}}
22
           {{$route.name}}
23
         </div>
23
         </div>
24
         <div class="right">
24
         <div class="right">
25
-          <div class="news"><img src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/pad_clerk/home/news.png"></div>
25
+          <div @click="onAppMessage" class="news">
26
+            <span class="dot"></span>
27
+            <img src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/pad_clerk/home/news.png" />
28
+          </div>
26
           <div class="head-img"><img :src="userInfo.avatar_url"></div>
29
           <div class="head-img"><img :src="userInfo.avatar_url"></div>
27
           <el-dropdown trigger="click"
30
           <el-dropdown trigger="click"
28
                        placement="top"
31
                        placement="top"
@@ -41,11 +44,20 @@
41
         <router-view></router-view>
44
         <router-view></router-view>
42
       </div>
45
       </div>
43
     </div>
46
     </div>
47
+    <minePupop :show="msgPupopVisible">
48
+      <div class="block">
49
+          <div class="delete-pupop" @click="msgPupopVisible=false">
50
+            <img src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/pad_clerk/icon/slices/delete.png" alt />
51
+          </div>
52
+      </div>
53
+    </minePupop>
44
   </div>
54
   </div>
45
 </template>
55
 </template>
46
 
56
 
47
 <script type="text/javascript">
57
 <script type="text/javascript">
58
+import { mapMutations } from 'vuex'
48
 import leftMenu from './leftMenu'
59
 import leftMenu from './leftMenu'
60
+import minePupop from "../../../components/minePupop/index.vue";
49
 import api from '@/server/home'
61
 import api from '@/server/home'
50
 
62
 
51
 
63
 
@@ -58,16 +70,47 @@ export default {
58
       isback: false,
70
       isback: false,
59
       userInfo: {
71
       userInfo: {
60
         name: '333'
72
         name: '333'
61
-      }
73
+      },
74
+      msgPupopVisible: false,
75
+      timer: null,
76
+      wsInstance: null
62
     }
77
     }
63
   },
78
   },
64
   created () {
79
   created () {
65
     this.getUserInfo()
80
     this.getUserInfo()
81
+
82
+      console.log('开始连接...')
83
+      let token = this.$store.state.comVal.token || localStorage.getItem('token')
84
+      //申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
85
+      this.wsInstance = new WebSocket(`wss://ws.ijolijoli.com?access_token=${token}`);
86
+      this.wsInstance.onopen = () => {
87
+        //当WebSocket创建成功时,触发onopen事件
88
+        console.log("连接成功");
89
+        this.timer = setInterval(() => {
90
+          this.wsInstance.send(JSON.stringify({type:'ping'}));
91
+        }, 30000);
92
+      }
93
+      this.wsInstance.onmessage = (e) => {
94
+        //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
95
+        console.log('收到消息',e.data)
96
+        let data = JSON.parse(e.data);
97
+        console.log(data)
98
+      }
99
+      this.wsInstance.onclose = (e) => {
100
+        //当客户端收到服务端发送的关闭连接请求时,触发onclose事件
101
+        console.log("连接已关闭");
102
+      }
103
+      this.wsInstance.onerror = (e) => {
104
+        //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
105
+        console.log(e);
106
+      }
66
   },
107
   },
67
   components: {
108
   components: {
68
     leftMenu,
109
     leftMenu,
110
+    minePupop
69
   },
111
   },
70
   methods: {
112
   methods: {
113
+    ...mapMutations(['SAVE_COMMON_VALUE']),
71
     getUserInfo () {
114
     getUserInfo () {
72
       let that = this
115
       let that = this
73
       api.getUserInfo().then(res => {
116
       api.getUserInfo().then(res => {
@@ -78,7 +121,20 @@ export default {
78
       })
121
       })
79
     },
122
     },
80
     loginOut () {
123
     loginOut () {
124
+      this.SAVE_COMMON_VALUE({
125
+        'key': 'token',
126
+        'value': null
127
+      })
128
+      this.SAVE_COMMON_VALUE({
129
+        'key': 'isLogin',
130
+        'value': false
131
+      })
81
       localStorage.removeItem('token');
132
       localStorage.removeItem('token');
133
+
134
+      // 关闭WebSocket连接并清除定时器
135
+      this.wsInstance.close()
136
+      clearInterval(this.timer)
137
+      
82
       this.$router.replace('/login')
138
       this.$router.replace('/login')
83
     },
139
     },
84
     changeMenu (index) {
140
     changeMenu (index) {
@@ -89,6 +145,9 @@ export default {
89
     },
145
     },
90
     goBack () {
146
     goBack () {
91
       this.$router.back()
147
       this.$router.back()
148
+    },
149
+    onAppMessage () {
150
+      this.msgPupopVisible = true
92
     }
151
     }
93
   },
152
   },
94
   computed: {
153
   computed: {
@@ -181,6 +240,16 @@ export default {
181
       .news {
240
       .news {
182
         width: 24px;
241
         width: 24px;
183
         height: 24px;
242
         height: 24px;
243
+        position: relative;
244
+        .dot {
245
+          width: 8px;
246
+          height: 8px;
247
+          border-radius: 50%;
248
+          background-color: #e54635;
249
+          position: absolute;
250
+          right: 2px;
251
+          top: 2px;
252
+        }
184
 
253
 
185
         img {
254
         img {
186
           width: 100%;
255
           width: 100%;
@@ -217,4 +286,23 @@ export default {
217
     background-color: #f7f8fa;
286
     background-color: #f7f8fa;
218
   }
287
   }
219
 }
288
 }
289
+.block {
290
+  width: 540px;
291
+  height: 550px;
292
+  background: #ffffff;
293
+  border-radius: 8px;
294
+  position: relative;
295
+  .delete-pupop {
296
+    position: absolute;
297
+    width: 32px;
298
+    height: 32px;
299
+    top: 5px;
300
+    right: 5px;
301
+    img {
302
+      width: 100%;
303
+      height: 100%;
304
+      display: block;
305
+    }
306
+  }
307
+}
220
 </style>
308
 </style>

+ 10 - 0
src/pages/login/login.vue

@@ -30,6 +30,7 @@
30
 </template>
30
 </template>
31
 
31
 
32
 <script>
32
 <script>
33
+import { mapMutations } from 'vuex'
33
 import api from '../../server/home'
34
 import api from '../../server/home'
34
 import axios from 'axios';
35
 import axios from 'axios';
35
 // import Cookies from "js-cookie"
36
 // import Cookies from "js-cookie"
@@ -46,6 +47,7 @@ export default {
46
   watch: {},
47
   watch: {},
47
 
48
 
48
   methods: {
49
   methods: {
50
+    ...mapMutations(['SAVE_COMMON_VALUE']),
49
     onLogin () {
51
     onLogin () {
50
       let params = {
52
       let params = {
51
         username: this.account,
53
         username: this.account,
@@ -54,6 +56,14 @@ export default {
54
       api.getToken(params).then(res => {
56
       api.getToken(params).then(res => {
55
         if (res.code == 200) {
57
         if (res.code == 200) {
56
           localStorage.setItem("token", res.data.token)
58
           localStorage.setItem("token", res.data.token)
59
+          this.SAVE_COMMON_VALUE({
60
+            'key': 'token',
61
+            'value': res.data.token
62
+          })
63
+          this.SAVE_COMMON_VALUE({
64
+            'key': 'isLogin',
65
+            'value': true
66
+          })
57
           axios.defaults.headers.token = res.data.token
67
           axios.defaults.headers.token = res.data.token
58
           this.$router.push({
68
           this.$router.push({
59
             path: '/home'
69
             path: '/home'

+ 2 - 1
src/store/modules/comVal.js

@@ -1,6 +1,7 @@
1
 const state = {
1
 const state = {
2
   name: 666,
2
   name: 666,
3
-  token: null
3
+  token: null,
4
+  isLogin: false
4
 }
5
 }
5
 
6
 
6
 const actions = {
7
 const actions = {