2 Commits d36df64d00 ... 9b963ea038

Author SHA1 Message Date
  double 9b963ea038 optimize: WebSocket连接移入layout组件中 2 years ago
  double b391abeec7 feat: 消息提醒-建立WebSocket连接 2 years ago
4 changed files with 103 additions and 5 deletions
  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 17
     created: function () {
18
-        
18
+
19 19
     },
20 20
     methods: {
21
-        
22 21
     }
23 22
 }
24 23
 </script>

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

@@ -22,7 +22,10 @@
22 22
           {{$route.name}}
23 23
         </div>
24 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 29
           <div class="head-img"><img :src="userInfo.avatar_url"></div>
27 30
           <el-dropdown trigger="click"
28 31
                        placement="top"
@@ -41,11 +44,20 @@
41 44
         <router-view></router-view>
42 45
       </div>
43 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 54
   </div>
45 55
 </template>
46 56
 
47 57
 <script type="text/javascript">
58
+import { mapMutations } from 'vuex'
48 59
 import leftMenu from './leftMenu'
60
+import minePupop from "../../../components/minePupop/index.vue";
49 61
 import api from '@/server/home'
50 62
 
51 63
 
@@ -58,16 +70,47 @@ export default {
58 70
       isback: false,
59 71
       userInfo: {
60 72
         name: '333'
61
-      }
73
+      },
74
+      msgPupopVisible: false,
75
+      timer: null,
76
+      wsInstance: null
62 77
     }
63 78
   },
64 79
   created () {
65 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 108
   components: {
68 109
     leftMenu,
110
+    minePupop
69 111
   },
70 112
   methods: {
113
+    ...mapMutations(['SAVE_COMMON_VALUE']),
71 114
     getUserInfo () {
72 115
       let that = this
73 116
       api.getUserInfo().then(res => {
@@ -78,7 +121,20 @@ export default {
78 121
       })
79 122
     },
80 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 132
       localStorage.removeItem('token');
133
+
134
+      // 关闭WebSocket连接并清除定时器
135
+      this.wsInstance.close()
136
+      clearInterval(this.timer)
137
+      
82 138
       this.$router.replace('/login')
83 139
     },
84 140
     changeMenu (index) {
@@ -89,6 +145,9 @@ export default {
89 145
     },
90 146
     goBack () {
91 147
       this.$router.back()
148
+    },
149
+    onAppMessage () {
150
+      this.msgPupopVisible = true
92 151
     }
93 152
   },
94 153
   computed: {
@@ -181,6 +240,16 @@ export default {
181 240
       .news {
182 241
         width: 24px;
183 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 254
         img {
186 255
           width: 100%;
@@ -217,4 +286,23 @@ export default {
217 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 308
 </style>

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

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

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

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