Sfoglia il codice sorgente

feat: 消息提醒-建立WebSocket连接

double 2 anni fa
parent
commit
b391abeec7

+ 52 - 3
src/App.vue

@@ -5,20 +5,69 @@
5
 </template>
5
 </template>
6
 
6
 
7
 <script>
7
 <script>
8
+import { mapMutations } from 'vuex'
8
 export default {
9
 export default {
9
     data() {
10
     data() {
10
         return {
11
         return {
11
-            list:[]
12
+            list:[],
13
+            timer: null,
14
+            wsInstance: null
12
         }
15
         }
13
     },
16
     },
17
+    computed: {
18
+        isLogin () {
19
+            return this.$store.state.comVal.isLogin
20
+        } 
21
+    },
14
     mounted: function () {
22
     mounted: function () {
15
         
23
         
16
     },
24
     },
17
     created: function () {
25
     created: function () {
18
-        
26
+        let token = this.$store.state.comVal.token || localStorage.getItem('token')
27
+        if (token) {
28
+            this.SAVE_COMMON_VALUE({
29
+                'key': 'isLogin',
30
+                'value': true
31
+            })
32
+        }
19
     },
33
     },
20
     methods: {
34
     methods: {
21
-        
35
+        ...mapMutations(['SAVE_COMMON_VALUE']),
36
+    },
37
+    watch: {
38
+        isLogin (newVal, oldVal) {
39
+            // console.log('newVal, oldVal: ', newVal, oldVal);
40
+            if (newVal) {
41
+                let token = this.$store.state.comVal.token || localStorage.getItem('token')
42
+                //申请一个WebSocket对象,参数是服务端地址,同http协议使用http://开头一样,WebSocket协议的url使用ws://开头,另外安全的WebSocket协议使用wss://开头
43
+                this.wsInstance = new WebSocket(`wss://ws.ijolijoli.com?access_token=${token}`);
44
+                this.wsInstance.onopen = () => {
45
+                    //当WebSocket创建成功时,触发onopen事件
46
+                    console.log("连接成功");
47
+                    this.timer = setInterval(() => {
48
+                        this.wsInstance.send(JSON.stringify({type:'ping'}));
49
+                    }, 30000);
50
+                }
51
+                this.wsInstance.onmessage = (e) => {
52
+                    //当客户端收到服务端发来的消息时,触发onmessage事件,参数e.data包含server传递过来的数据
53
+                    console.log('收到消息',e.data)
54
+                    let data = JSON.parse(e.data);
55
+                    console.log(data)
56
+                }
57
+                this.wsInstance.onclose = (e) => {
58
+                    //当客户端收到服务端发送的关闭连接请求时,触发onclose事件
59
+                    console.log("连接已关闭");
60
+                }
61
+                this.wsInstance.onerror = (e) => {
62
+                    //如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
63
+                    console.log(e);
64
+                }
65
+            } else {
66
+                // console.log('清除定时器并关闭连接-不再send心跳ping')
67
+                this.wsInstance.close()
68
+                clearInterval(this.timer)
69
+            }
70
+        }
22
     }
71
     }
23
 }
72
 }
24
 </script>
73
 </script>

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

@@ -22,7 +22,7 @@
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"><img src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/pad_clerk/home/news.png"></div>
26
           <div class="head-img"><img :src="userInfo.avatar_url"></div>
26
           <div class="head-img"><img :src="userInfo.avatar_url"></div>
27
           <el-dropdown trigger="click"
27
           <el-dropdown trigger="click"
28
                        placement="top"
28
                        placement="top"
@@ -41,11 +41,20 @@
41
         <router-view></router-view>
41
         <router-view></router-view>
42
       </div>
42
       </div>
43
     </div>
43
     </div>
44
+    <minePupop :show="msgPupopVisible">
45
+      <div class="block">
46
+          <div class="delete-pupop" @click="msgPupopVisible=false">
47
+            <img src="https://we-spa.oss-cn-shenzhen.aliyuncs.com/pad_clerk/icon/slices/delete.png" alt />
48
+          </div>
49
+      </div>
50
+    </minePupop>
44
   </div>
51
   </div>
45
 </template>
52
 </template>
46
 
53
 
47
 <script type="text/javascript">
54
 <script type="text/javascript">
55
+import { mapMutations } from 'vuex'
48
 import leftMenu from './leftMenu'
56
 import leftMenu from './leftMenu'
57
+import minePupop from "../../../components/minePupop/index.vue";
49
 import api from '@/server/home'
58
 import api from '@/server/home'
50
 
59
 
51
 
60
 
@@ -58,7 +67,8 @@ export default {
58
       isback: false,
67
       isback: false,
59
       userInfo: {
68
       userInfo: {
60
         name: '333'
69
         name: '333'
61
-      }
70
+      },
71
+      msgPupopVisible: false
62
     }
72
     }
63
   },
73
   },
64
   created () {
74
   created () {
@@ -66,8 +76,10 @@ export default {
66
   },
76
   },
67
   components: {
77
   components: {
68
     leftMenu,
78
     leftMenu,
79
+    minePupop
69
   },
80
   },
70
   methods: {
81
   methods: {
82
+    ...mapMutations(['SAVE_COMMON_VALUE']),
71
     getUserInfo () {
83
     getUserInfo () {
72
       let that = this
84
       let that = this
73
       api.getUserInfo().then(res => {
85
       api.getUserInfo().then(res => {
@@ -78,7 +90,16 @@ export default {
78
       })
90
       })
79
     },
91
     },
80
     loginOut () {
92
     loginOut () {
93
+      this.SAVE_COMMON_VALUE({
94
+        'key': 'token',
95
+        'value': null
96
+      })
97
+      this.SAVE_COMMON_VALUE({
98
+        'key': 'isLogin',
99
+        'value': false
100
+      })
81
       localStorage.removeItem('token');
101
       localStorage.removeItem('token');
102
+      
82
       this.$router.replace('/login')
103
       this.$router.replace('/login')
83
     },
104
     },
84
     changeMenu (index) {
105
     changeMenu (index) {
@@ -89,6 +110,9 @@ export default {
89
     },
110
     },
90
     goBack () {
111
     goBack () {
91
       this.$router.back()
112
       this.$router.back()
113
+    },
114
+    onAppMessage () {
115
+      this.msgPupopVisible = true
92
     }
116
     }
93
   },
117
   },
94
   computed: {
118
   computed: {
@@ -217,4 +241,23 @@ export default {
217
     background-color: #f7f8fa;
241
     background-color: #f7f8fa;
218
   }
242
   }
219
 }
243
 }
244
+.block {
245
+  width: 540px;
246
+  height: 550px;
247
+  background: #ffffff;
248
+  border-radius: 8px;
249
+  position: relative;
250
+  .delete-pupop {
251
+    position: absolute;
252
+    width: 32px;
253
+    height: 32px;
254
+    top: 5px;
255
+    right: 5px;
256
+    img {
257
+      width: 100%;
258
+      height: 100%;
259
+      display: block;
260
+    }
261
+  }
262
+}
220
 </style>
263
 </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 = {